<?php
 class MastersettingsController extends Controller {
    


    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('login', 'error', 'logout'),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('Create','SaveMasterSettings'),
                'expression' => '$user->isMember()',
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }
   

    public function actionCreate() {
         if(count($_GET) > 0){
            $model = new MasSettings();
            $model = $model->find(array('condition'=>'1=1'));
            if (!((int)$model->id > 0)) {
                $model = new MasSettings();
            }
           $this->render('create', array('model' => $model));  
         }
        else{
              throw new CHttpException(404, 'Page not found.');
        }
    }

   public function actionSaveMasterSettings(){
        
        $_POST  = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
        if(count($_POST) > 0){
            $model = new MasSettings();$other=array();
            if (isset($_POST['MasSettings'])) {
                if ((int) @$_POST['MasSettings']['id'] > 0) {
                    $id = (int) @$_POST['MasSettings']['id'];
                    $model = $model->findByPk($id);
                }else{
                    $model->created_by =Yii::app()->user->id;
                    $model->created_date_time =date('Y-m-d H:i:s');
                }
                $model->updated_by =Yii::app()->user->id;
                $model->updated_date_time =date('Y-m-d H:i:s');
                $model->attributes = $_POST['MasSettings'];
                $valid= $model->validate();
                if ($valid) {
                    $model->save();
                    $jsonData['status'] = 'success';
                    $jsonData['frm'] = $_POST['MasSettings'];
                    $jsonData['id'] = $model->id;
                    echo CJSON::encode($jsonData);
                } else {
                    $error1 = json_decode(CActiveForm::validate($model, '', false), true);
                    $error = CJSON::encode(array_merge_recursive($error1,$other));
                    if ($error != '[]')
                        echo $error;
                }
            }
        }
        else{
              throw new CHttpException(404, 'Page not found.');
        }
        
    }
}