<?php

class SitesController extends BaseController {

	protected $rules		 = array(
			'url' => 'required',
	);
	protected $messages	 = array(
			'required' => 'Неообходимо ввести адрес сайта',
			'url'			 => 'Неверный формат адреса сайта'
	);
	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')) {
				$this->beforeFilter('hasRole:' . Role::ROLE_PROJECTS_EDITOR);
			}
		}
		else {
			$this->beforeFilter('auth');
		}
	}

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

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create() {
		return View::make('sites.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('SitesController@create', array($this->project->id))
											->withInput()
											->withErrors($validation->errors());
		}
		else {
			$input				 = Input::all();
			$input['url']	 = str_replace("http://", "", $input['url']);
			$input['url']	 = str_replace("//", "", $input['url']);
			$site					 = new Site(array(
					'url' => e($input['url'])
			));
			$site					 = $this->project->sites()->save($site);
			return Redirect::action('SitesController@index', array($this->project->id));
		}
	}

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

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

	/**
	 * 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('SitesController@edit', array($projectId, $id))
											->withInput()
											->withErrors($validation->errors());
		}
		else {
			$site = Site::find($id);
			if ($site == null || !is_numeric($id) || $site->project_id != $projectId) {
				return \App::abort(404);
			}

			$input		 = Input::all();
			$input['url']	 = str_replace("http://", "", $input['url']);
			$input['url']	 = str_replace("//", "", $input['url']);
			$site->url = e($input['url']);
			$site->save();
			return Redirect::action('SitesController@index', array($projectId));
		}
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($projectId, $id) {
		$site = Site::find($id);
		if ($site == null || !is_numeric($id) || $site->project_id != $projectId) {
			return \App::abort(404);
		}
		Site::destroy($id);
		return Redirect::action('SitesController@index', array($projectId));
	}

}
