<?php

class InfoSourcesController extends BaseController {

	protected $rules		 = array(
			'source' => 'required',
	);
	protected $messages	 = array(
			'required' => 'Неообходимо ввести название источника информации',
	);
	private $project		 = null;

	/**
	 * Constructor
	 */
	public function __construct() {
		$id			 = Request::segment(2);
		$project = Project::find($id);
		if ($project == null || !is_numeric($id)) {
			return \App::abort(404);
		}
		else {
			$this->project = $project;
		}
		// Establish Filters
		if (Sentry::check()) {
			$user = User::find(Sentry::getUser()->id);
			if (!$user->hasAccess('admin')) {
				if (!$user->hasAnyRole(array(Role::ROLE_PROJECTS_EDITOR, Role::ROLE_PRODUCTS_EDITOR))) {
					$this->beforeFilter('hasRole:' . Role::ROLE_PROJECTS_EDITOR);
					$this->beforeFilter('hasRole:' . Role::ROLE_PRODUCTS_EDITOR);
				}
			}
		}
		else {
			$this->beforeFilter('auth');
		}
	}

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index() {
		return View::make('info_sources.index')->with('project', $this->project);
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create() {
		return View::make('info_sources.create')->with('project', $this->project);
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store() {
		$input			 = Input::all();
		$validation	 = Validator::make($input, $this->rules, $this->messages);

		if ($validation->fails()) {
			return Redirect::action('InfoSourcesController@create', array($this->project->id))
											->withInput()
											->withErrors($validation->errors());
		}
		else {
			$input	     = Input::all();
			$infoSource  = new InfoSource (array(
					'source'   => e($input['source']),
					'comments' => e($input['comments'])
			));
			$infoSource	 = $this->project->infoSources()->save($infoSource);
			return Redirect::action('InfoSourcesController@index', array($this->project->id));
		}
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($projectId, $id) {
		$infoSource = InfoSource::find($id);
		if ($infoSource == null || !is_numeric($id) || $infoSource->project_id != $projectId) {
			return \App::abort(404);
		}

		return View::make('info_sources.edit')
										->with('infoSource', $infoSource)
										->with('project', $this->project);
	}

	// получение комментариев к источнику информации
	public function getComments() {
		$input	    = Input::all();	
		$infoSource = InfoSource::find($input['sourceValue']);
		
		if ($infoSource == null || !is_numeric($input['sourceValue'])) {
			$response = array(
				'status'  => 'fail',
				'message' => 'Источник клиентов не найден'		
		);
		  return Response::json($response);
		}
		
		$response = array(
				'status'  => 'success',
				'message'	=> $infoSource->comments		
		);
		return Response::json($response);
	}
	
	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($projectId, $id) {
		$input			 = Input::all();
		$validation	 = Validator::make($input, $this->rules, $this->messages);

		if ($validation->fails()) {
			return Redirect::action('InfoSourcesController@edit', array($projectId, $id))
											->withInput()
											->withErrors($validation->errors());
		}
		else {
			$infoSource = InfoSource::find($id);
			if ($infoSource == null || !is_numeric($id) || $infoSource->project_id != $projectId) {
				return \App::abort(404);
			}

			$input				        = Input::all();
			$infoSource->source	  = e($input['source']);
			$infoSource->comments	= e($input['comments']);
			$infoSource->save();
			return Redirect::action('InfoSourcesController@index', array($projectId));
		}
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($projectId, $id) {
		InfoSource::destroy($id);
		return Redirect::action('InfoSourcesController@index', array($projectId));
	}

}
