<?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
 */

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('System Admin','Super Admin'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    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' => 'authentication_ldap_server'));
                    $setting_authentication_ldap_server->value = $settings_model->authentication_ldap_server;
                    $setting_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_non_ldap_persons_number_prefix = Setting::model()->findByAttributes(array('name' => 'non_ldap_persons_number_prefix'));
                    $setting_non_ldap_persons_number_prefix->value = $settings_model->non_ldap_persons_number_prefix;
                    $setting_non_ldap_persons_number_prefix->save();

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

                    Yii::app()->user->setFlash('success', Yii::t('default', 'The Setting Values have been updated successfully'));
                } // {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' => ''));
        }
    }

    }