<?php

class SiteController extends Controller

{

    /**

     * Declares class-based actions.

     */

     

    public function init() {

        parent::init();

        $this->initAjaxCsrfToken();

    }

 

    // this function will work to post csrf token.

    protected function initAjaxCsrfToken() {

 

        Yii::app()->clientScript->registerScript('AjaxCsrfToken', ' $.ajaxSetup({

                         data: {"' . Yii::app()->request->csrfToken . '": "' . Yii::app()->request->csrfToken . '"},

                         cache:false

                    });', CClientScript::POS_HEAD);

    } 

     

    public function actions()

    {

        return array(

            // captcha action renders the CAPTCHA image displayed on the contact page

            'captcha' => array(

                'class' => 'CCaptchaAction',

                'backColor' => 0xFFFFFF,

            ),

            // page action renders "static" pages stored under 'protected/views/site/pages'

            // They can be accessed via: index.php?r=site/page&view=FileName

            'page' => array(

                'class' => 'CViewAction',

            ),

        );

    }

    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('Index', 'ChangePassword', 'UpdatePassword', 'BrowserControl', 'SaveBrowserView', 'hscodeupload', 'ftp', 'Upload', 'Importidocs'),



                'expression' => '$user->isMember()',



            ),



            array('deny', // deny all users



                'users' => array('*'),



            ),



        );



    }



    /**

     * This is the default 'index' action that is invoked

     * when an action is not explicitly requested by users.

     */

    public function actionIndex()

    {
        $_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);
       // $_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_FULL_SPECIAL_CHARS  );

        if(count($_GET) > 0){

             $this->render('index');

            /*if (Yii::app()->user->have_access) {

                $this->redirect('index.php?r=site/index');

            } else {

                $this->redirect('index.php?r=site/index');

                 $this->render('index');

            }*/

        }

        else{

            throw new CHttpException(404, 'Page not found.');

        }



    }

    public function actionChangePassword()

    {

        $_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if(count($_GET) > 0){

            Yii::app()->clientScript->scriptMap = array('*jquery.js' => false, '*jquery.min.js' => false, 'jquery.min.js' => false, '*jquery-ui.js' => false, '*jquery-ui.min.js' => false, 'jquery-2.1.4.js' => false);

            $this->render('changepassword');

        }

        else{

            throw new CHttpException(404, 'Page not found.');

        }

    }

    public function actionUpdatePassword()

    {

            $_POST  = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

             /*Count of POST*/

             if(count($_POST) > 0){

                $curr_pwd = $_POST['user_current_password'];

                $new_pwd = $_POST['user_new_password'];

                $rep_pwd = $_POST['user_reenter_password'];

                $login_user_id = Yii::app()->user->id;

                $arr_user_details = User::model()->findByPk($login_user_id);

                $salt = '$2a$10$1qAz2wSx3eDc4rFv5tGb5t';

                $password = crypt($_POST['user_current_password'], $salt);

        

                if ($arr_user_details->password == $password) {

                    $new_password = crypt($_POST['user_new_password'], $salt);

        

                    $arr_user_details->password = $new_password;

                    $arr_user_details->save(false);

                    $json['status'] = "success";

                } else {

                    $json['status'] = "fail";

                }

                echo CJSON::encode($json);

             }

        	else{

        		$this->render('404');

        	}

    }



    public function actionBrowserControl()

    {

        $_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if(count($_GET) > 0){

            $model = MasterSetting::model()->find();

            if ($model->browser_view == '') {

                $model = new MasterSetting();

            }

            Yii::app()->clientScript->scriptMap = array('*jquery.js' => false, '*jquery.min.js' => false, 'jquery.min.js' => false, '*jquery-ui.js' => false, '*jquery-ui.min.js' => false);

            $this->render('browser_control', array('model' => $model));

        }

        else{

            throw new CHttpException(404, 'Page not found.');

        }

    }

    public function actionSaveBrowserView()

    {

            $_POST  = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

             /*Count of POST*/

            if(count($_POST) > 0){

                $model = new MasterSetting;

                if (isset($_POST['MasterSetting'])) {

                    if ((int) @$_POST['MasterSetting']['id'] > 0) {

                        $model->id = (int) $_POST['MasterSetting']['id'];

                        $model->scenario = 'update';

                        $model = MasterSetting::model()->findByPK($_POST['MasterSetting']['id']);

                    }

        

                    $model->attributes = $_POST['MasterSetting'];

                    if ($model->validate()) {

                        $model->save();

                        $model->refresh();

                        $jsonData['model'] = 'MasterSetting';

                        $jsonData['status'] = 'success';

                        echo CJSON::encode($jsonData);

                    } else {

                        $error = CActiveForm::validate($model, '', false);

                        if ($error != '[]') {

                            echo $error;

                        }

        

                    }

                }

                Yii::app()->end();

             }

        	else{

        		$this->render('404');

        	}

    }

    public function actionError()

    {

        if ($error = Yii::app()->errorHandler->error) {

            // print_r($error);

            if (Yii::app()->request->isAjaxRequest) {

                echo $error['message'];

            } else {

                $this->render('error', $error);

            }



        }

    }



    public function actionHscodeupload()

    {

        $_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if(count($_GET) > 0){

            Yii::app()->clientScript->scriptMap = array('*jquery.js' => false, '*jquery.min.js' => false, 'jquery.min.js' => false, '*jquery-ui.js' => false, '*jquery-ui.min.js' => false);

            $this->render('upload_hsc');

        }

        else{

            throw new CHttpException(404, 'Page not found.');

        }

    }

    public function actionFtp()

    {

        $_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if(count($_GET) > 0){

            Yii::app()->clientScript->scriptMap = array('*jquery.js' => false, '*jquery.min.js' => false, 'jquery.min.js' => false, '*jquery-ui.js' => false, '*jquery-ui.min.js' => false);

            $this->render('server');

        }

        else{

            throw new CHttpException(404, 'Page not found.');

        }

    }



    public function actionImportidocs()

    {

        

         $_POST  = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

         /*Count of POST*/

        if(count($_POST) > 0){



            $doc = $_POST['file_name'];

            $doc = str_replace(',', '', $doc);

            $file = fopen("upload/hscodecsv/$doc", "r");

    

            $x = 0;

            while ($line = fgets($file, 1000)) {

                if ($x > 0) {

                    $hs_code = "";

                    $description = "";

                    $line = explode(",", $line);

                    foreach ($line as $i => $row) {

                        switch ($i) {

                            case 0:

                                $hs_code = trim($row);

                                break;

                            case 1:

                                $description = trim($row);

                                break;

                        }

                    }

                    //SAVE DATA

                    if ($hs_code != '') {

                        $hscodeModel = HSCode::model()->find(array('condition' => 'hs_code="' . $hs_code . '"'));

                        if ($hscodeModel->hs_code == "") {

                            $hscodeModel = new HSCode;

                        }

                        $hscodeModel->hs_code = $hs_code;

                        $hscodeModel->description = $description;

                        $hscodeModel->save(false);

                    }

                }

                $x++;

            }

            $jsonData['status'] = 'success';

            echo CJSON::encode($jsonData);

        }

    	else{

    		$this->render('404');

    	}

    }



}

