<?php

class ProjectsController extends BaseController {

	protected $rules		 = array(
			'name' => 'required|max:50',
	);
	protected $messages	 = array(
			'required' => 'Неообходимо ввести название проекта'
	);

	/**
	 * Constructor
	 */
	public function __construct() {
		// 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() {
		$projects = null;
		if (User::find(Sentry::getUser()->id)->hasAccess('admin')) {
			$projects = Project::orderBy('name', 'ASC')->get();
		}
		elseif (User::find(Sentry::getUser()->id)->hasAccess('moderator') || User::find(Sentry::getUser()->id)->hasAnyRole(array(Role::ROLE_PROJECTS_EDITOR, Role::ROLE_PRODUCTS_EDITOR))) {
			$projects = Project::orderBy('name', 'ASC')->get();
		}
		else {
			$projects = Project::where('user_id', Sentry::getUser()->id)->orderBy('name', 'ASC')->get();
		}
		return View::make('projects.index')->with('projects', $projects);
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create() {
		return View::make('projects.create')->with('users', User::myUsers());
	}

	/**
	 * 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('ProjectsController@create')
											->withInput()
											->withErrors($validation->errors());
		}
		else {
			$input	 = Input::all();
			$project = Project::create(array(
									'name'		 => e($input['name']),
									'user_id'	 => Sentry::getUser()->id
			));
			if (isset($input['projectUsers'])) {
				$project->users()->attach(array_keys($input['projectUsers']));
			}
			return Redirect::to('/projects');
		}
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id) {
		$project = Project::find($id);
		if ($project == null || !is_numeric($id) || !User::find(Sentry::getUser()->id)->hasAccess('moderator') && !User::find(Sentry::getUser()->id)->hasRole(Role::ROLE_PROJECTS_EDITOR)) {
			return \App::abort(404);
		}

		$currentUsers	 = $project->users->toArray();
		$projectUsers	 = array();
		foreach ($currentUsers as $user) {
			array_push($projectUsers, $user['id']);
		}
		return View::make('projects.edit')
										->with('users', User::myUsers())
										->with('project', $project)
										->with('projectUsers', $projectUsers);
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id) {
		$input			 = Input::all();
		$validation	 = Validator::make($input, $this->rules, $this->messages);

		if ($validation->fails()) {
			return Redirect::action('ProjectsController@edit', $id)
											->withInput()
											->withErrors($validation->errors());
		}
		else {
			$project = Project::find($id);
			if ($project == null || !is_numeric($id) || !User::find(Sentry::getUser()->id)->hasAccess('moderator') && !User::find(Sentry::getUser()->id)->hasRole(Role::ROLE_PROJECTS_EDITOR)) {
				return \App::abort(404);
			}

			$project->name	 = e($input['name']);
			$project->usecrm = (isset($input['usecrm']) ? 1 : 0);
			$project->save();
			if (isset($input['projectUsers'])) {
				$project->users()->sync(array_keys($input['projectUsers']));
			}
			else {
				$project->users()->detach();
			}
			return Redirect::to('/projects');
		}
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id) {
		$project = Project::find($id);
		if ($project == null || !is_numeric($id) || !User::find(Sentry::getUser()->id)->hasAccess('Moderators') && !User::find(Sentry::getUser()->id)->hasRole(Role::ROLE_PROJECTS_EDITOR)) {
			return \App::abort(404);
		}
		Project::destroy($id);
		return Redirect::to('/projects');
	}

}
