<?php

/**
  model class name  = Blacklist
  form-id  == blacklist
 */
class DefaultController 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',
                    "create",
                    "update",
                    "delete"
                ),
                'expression' => AccessManager::checkSessionPermission($this->module->name),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }
    
    public function actionIndex()
    {
        $model = new ScriptExtension();

        $this->render("index", array("model" => $model));
    }

    public function actionCreate()
    {
        $model = new ScriptExtension();
        $this->performAjaxValidation($model);

        if (isset($_POST["ScriptExtension"])) {

            $model->attributes = $_POST["ScriptExtension"];
            if (!$model->save()) {
                dump($model->errors);
                die();
            }
        }
        $this->renderPartial("_form", array("model" => $model), false, true);
    }

    public function actionUpdate($id)
    {
        $model = $this->loadModel($id);

        if (isset($_POST["ScriptExtension"])) {
            $model->attributes = $_POST["ScriptExtension"];
            $model->save();
        }
        $this->render("update", array("model" => $model));
    }

    public function actionDelete($id)
    {
        if (Yii::app()->request->isPostRequest) {
            $this->loadModel($id)->delete();

            if (!isset($_GET['ajax'])) {
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
            }
        } else {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function loadModel($id)
    {

        $model = ScriptExtension::model()->findByPk($id);
        if ($model === null) {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'script-extension-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
    
    public function getCustomAutoCompleteWordList()
    {
        $filename = __DIR__."/../components/wordList.ini";
        $words = parse_ini_file($filename,true);
        $jsWords = "[";
        foreach ($words as $word => $params) {
            $jsWords.= json_encode($params).",";
        }
        $jsWords .= "]";
        return $jsWords;
    }
}
