<?php namespace App\Modules\Backend\Configurations\Controllers;

use Auth, Input, Redirect, View, DB, Datatables, Validator, Innfile, Hash, Alert, Acl;
use App\Modules\Backend\Configurations\Models\Configuration;

class ConfigurationController extends \BaseController {

	/**
	 * Constructor
	 */
	public function __construct() {
        View::share('active_module', 'configurations');
    }



    /**
     * Mostramos las configuraciones generales para editarse.
     */
    public function getGeneral()
    {
    	Acl::canOrFail('configurations', 'update');
    	$config = new Configuration();
    	$config->loadAllAttributesAs();
		return View::make('configurations::general')
				->with('title', trans('configurations::label.general_configurations'))
				->with('config', $config)
				->with("formOptions", array("role" 	=> "form", 
					"id" 		=> "frmPrincipal", 
					"class" 	=> "form-horizontal",
					"method" 	=> "PUT",
					"url" 		=> url("admin/configurations/general")));
    }


    /**
     * Guarda los cambios de las configuraciones generales.
     */
    public function putGeneral()
    {
    	Acl::canOrFail('configurations', 'update');
    	$data = Input::all();
    	
    	$config = new Configuration();
    	$config->loadAllAttributesAs();
    	$config->fill($data);
	    if($config->saveConfigs())
    	{
    		Alert::message(trans('dashboard::message.success'), 'success');
    		return Redirect::to('admin/configurations/general');
    	}
    	else {
    		Alert::message(trans('dashboard::message.error'), 'danger');
    		return Redirect::to('admin/configurations/general')
    			->withInput();
    	}
    }


    /**
     * Mostramos las configuraciones de la página de error para editarse.
     */
    public function getErrorPage()
    {
    	Acl::canOrFail('configurations', 'update');
    	$config = new Configuration();
    	$config->loadAllAttributesAs();
		return View::make('configurations::error_page')
				->with('title', trans('configurations::label.error_page_configurations'))
				->with('config', $config)
				->with("formOptions", array("role" 	=> "form", 
					"id" 		=> "frmPrincipal", 
					"class" 	=> "form-horizontal",
					"method" 	=> "PUT",
					"url" 		=> url("admin/configurations/error-page")));
    }


    /**
     * Guarda los cambios de las configuraciones de la página de error.
     */
    public function putErrorPage()
    {
    	Acl::canOrFail('configurations', 'update');
    	$data = Input::all();
    	
    	$config = new Configuration();
    	$config->loadAllAttributesAs();
    	$config->fill($data);

        if(!Input::has('_default_error_page'))
            $config->setConfig('default_error_page', 0);


	    if($config->saveConfigs())
    	{
    		Alert::message(trans('dashboard::message.success'), 'success');
    		return Redirect::to('admin/configurations/error-page');
    	}
    	else {
    		Alert::message(trans('dashboard::message.error'), 'danger');
    		return Redirect::to('admin/configurations/error-page')
    			->withInput();
    	}
    }
}