<?php
/**
 * IMPORTANT NOTE
 * The code of this system is closed and a private proprietary of Flex Solutions.
 * A written authorization/license from Flex Solutions is required before using or modifying this code.
 * Email: sales@flexsolution.biz, Phone: +962795342585, Website: http://www.flexsolutions.biz
 */
 
/**
 * Setting
 *
 * Yii controller to control Setting actions
 *
 * @version 1.0
 *
 * @author Amaney
 * @package application.controllers
 */
class SettingController extends Controller {

    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters() {
	return array(
	    'accessControl', // perform access control for CRUD operations
	);
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
	return array(
	    array('allow', // allow all users to perform 'index' and 'view' actions
		'actions' => array('index'),
		'roles' => array(User::SYSTEM_ADMIN_ROLE),
	    ),
	    array('deny', // deny all users
		'users' => array('*'),
	    ),
	);
    }

    /**
     * Lists all settings.
     */
    public function actionIndex() {
	$settings_model = null;

	if ($_POST) { // {begin} if the form is posted
	    if (isset($_POST['settings'])) { // {begin} the  settings are posted
		$settings_model = new Setting();
		$settings_model->attributes = $_POST['Setting'];
		// set the secanrio of the $settings_model as "settings" since we don't want to validate the codes related fields, for more info, check the validation rules
		$settings_model->setScenario("settings");
		if ($settings_model->validate()) { //{begin} validate the general settings model
		    // save the general settings to the DB
		    $setting_ldap_server = Setting::model()->findByAttributes(array('name' => 'first_ldap_server'));
		    $setting_ldap_server->value = $settings_model->first_ldap_server;
		    $setting_ldap_server->save();

		    $setting_second_ldap_server = Setting::model()->findByAttributes(array('name' => 'second_ldap_server'));
		    $setting_second_ldap_server->value = $settings_model->second_ldap_server;
		    $setting_second_ldap_server->save();

		    $setting_authentication_ldap_server = Setting::model()->findByAttributes(array('name' => 'first_authentication_ldap_server'));
		    $setting_authentication_ldap_server->value = $settings_model->first_authentication_ldap_server;
		    $setting_authentication_ldap_server->save();

		    $setting_second_authentication_ldap_server = Setting::model()->findByAttributes(array('name' => 'second_authentication_ldap_server'));
		    $setting_second_authentication_ldap_server->value = $settings_model->second_authentication_ldap_server;
		    $setting_second_authentication_ldap_server->save();

		    $setting_ldap_port = Setting::model()->findByAttributes(array('name' => 'ldap_port'));
		    $setting_ldap_port->value = $settings_model->ldap_port;
		    $setting_ldap_port->save();

		    $setting_base_dn = Setting::model()->findByAttributes(array('name' => 'base_dn'));
		    $setting_base_dn->value = $settings_model->base_dn;
		    $setting_base_dn->save();

		    $setting_backup_file_path = Setting::model()->findByAttributes(array('name' => 'backup_file_path'));
		    (substr($settings_model->backup_file_path, -1) === '\\') ? $setting_backup_file_path->value = $settings_model->backup_file_path : $setting_backup_file_path->value = $settings_model->backup_file_path . '\\';
		    $setting_backup_file_path->save();

		    $setting_max_document_size = Setting::model()->findByAttributes(array('name' => 'max_document_size'));
		    $setting_max_document_size->value = $settings_model->max_document_size;
		    $setting_max_document_size->save();

		    $setting_tid_daily_fine = Setting::model()->findByAttributes(array('name' => 'tid_daily_fine'));
		    $old_fine = $setting_tid_daily_fine->value;
		    $setting_tid_daily_fine->value = $settings_model->tid_daily_fine;
		    $setting_tid_daily_fine->save();
		    if ($old_fine != $settings_model->tid_daily_fine) {
			Tid::UpdateAllTIDStatuses($error_msg);
			Asset::UpdateAllAssetStatuses($error_msg, $expired_vehicle, $expired_radioactives);
		    }


		    $setting_grace_period = Setting::model()->findByAttributes(array('name' => 'grace_period'));
		    $old_grace = $setting_grace_period->value;
		    $setting_grace_period->value = $settings_model->grace_period;
		    $setting_grace_period->save();
		    if ($old_grace != $settings_model->grace_period) {
			Tid::UpdateAllTIDStatuses($error_msg);
			Asset::UpdateAllAssetStatuses($error_msg, $expired_vehicle, $expired_radioactives);
		    }

		    $setting_documents_path = Setting::model()->findByAttributes(array('name' => 'documents_path'));
		    $setting_documents_path->value = $settings_model->documents_path;
		    $setting_documents_path->save();

		    $setting_vehicle_type_of_asset = Setting::model()->findByAttributes(array('name' => 'vehicle_type_of_asset'));
		    if ($setting_vehicle_type_of_asset->value != $settings_model->vehicle_type_of_asset && Asset::isThereVehicleAssets($setting_vehicle_type_of_asset->value))
			$settings_model->addError('vehicle_type_of_asset', 'You cannot change vehicle type of asset');
		    else {
			$setting_vehicle_type_of_asset->value = $settings_model->vehicle_type_of_asset;
			$setting_vehicle_type_of_asset->save();
		    }

		    $setting_radio_active_type_of_asset = Setting::model()->findByAttributes(array('name' => 'radio_active_type_of_asset'));
		    if ($setting_radio_active_type_of_asset->value != $settings_model->radio_active_type_of_asset && Asset::isThereRadioactiveAssets($setting_radio_active_type_of_asset->value))
			$settings_model->addError('radio_active_type_of_asset', 'You cannot change radioactive type of asset');
		    else {
			$setting_radio_active_type_of_asset->value = $settings_model->radio_active_type_of_asset;
			$setting_radio_active_type_of_asset->save();
		    }


		    $setting_dashboard_update = Setting::model()->findByAttributes(array('name' => 'dashboard_update'));
		    $setting_dashboard_update->value = $settings_model->dashboard_update;
		    $setting_dashboard_update->save();

		    $setting_under_transfer_renewal_stage = Setting::model()->findByAttributes(array('name' => 'under_transfer_renewal_stage'));
		    $setting_under_transfer_renewal_stage->value = $settings_model->under_transfer_renewal_stage;
		    $setting_under_transfer_renewal_stage->save();



		    if (!$settings_model->hasErrors())
			Yii::app()->user->setFlash('success', Yii::t('default', 'The Setting Values have been updated successfully'));
		    else
			Yii::app()->user->setFlash('error', CHtml::errorSummary($settings_model));
		} // {end} validate the settings model
		else { // validation of settings failed
		    Yii::app()->user->setFlash('error', CHtml::errorSummary($settings_model));
		}
		// render the page
		$settings_array = Setting::getSettingsArray();
		$this->render('index', array('settings_array' => $settings_array, 'settings_model' => $settings_model, 'ajaxValue' => ''));
	    } // {end} the settings are posted
	} // {end} if the form is posted
	else { // the form is not posted, i.e. first time
	    $settings_model = new Setting();
	    $settings_array = Setting::getSettingsArray();
	    $this->render('index', array('settings_array' => $settings_array, 'settings_model' => $settings_model, 'ajaxValue' => ''));
	}
    }

}