<?php

/**
 * SQL
 * CREATE  TABLE "main"."permissions" ("id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , "groupId" INTEGER NOT NULL , "type" VARCHAR(20) NOT NULL , "itemId" INTEGER NOT NULL )
 *
 * This is the model class for table "permissions".
 *
 * The followings are the available columns in table 'permissions':
 * @property integer $id
 */
class WebGuiPermissions extends PhonalisaRecord
{

    public $id;
    #	public $groupName;
    public $groupId;
    public $permissions;

    public static function myAttributes()
    {

        return array(
            'id' => 'id',
            #       		'groupName'=>'groupName',
            'groupId' => 'groupId',
            'permissions' => 'permissions',
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Permissions the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'adminWebGuiPermissions';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('groupId, permissions', 'required'), #groupName
            array('groupName, permissions', 'safe'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => __('ID'),
            'groupName' => __('WEBGUIPERMISSIONS_MODEL_NAME'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        $criteria->compare('id', $this->id);
        //	$criteria->compare('name',$this->name);
        return new CPhonalisaDataProvider($this, array('criteria' => $criteria));
    }

    public function getModuleColumn()
    {
        $column = array(
            'type' => 'raw',
            'header' => __('WEBGUIPERMISSIONS_MODEL_SECTION_NAME'),
            'headerHtmlOptions' => array('data-translation-id' => 'WEBGUIPERMISSIONS_MODEL_SECTION_NAME'),
            'value' => array($this, 'renderSectionModules')
        );
        return $column;
    }

    public function renderSectionModules($data, $row)
    {
        $icon = ModuleManager::getIconForModule($data['name'], 'large');

        if ($data['type'] == 'section') {
            return '<b>' . $data['name'] . '</b>';
        } else {
            return '<div class="webGuiPermission">'.$icon.'<span>' . __($data['title']) . '</span></div>';
        }
    }

    public function renderGroups($data, $row, $group)
    {
        if ($data['type'] == 'section') {
            return;
        }

        $group = Groups::model()->findByPk($group->name);
        $model = WebGuiPermissions::model()->findByAttributes(array('groupId' => $group->id));

        $checked = '';
        if (!empty($model)) {
            $permissions = json_decode($model->permissions);
            if (isset($permissions->{$data['name']})) {
                $checked = true;
            } else {
                $checked = '';
            }
        }

        $disabled = '';
        $hiddenValue = '';
        //FIXME rename AddressbookGeneral to adminAddressbooks
        if ($data['name'] == 'addressbookGeneral' && $group->type == 'admins' || strstr($data['name'], 'admin') && $group->type == 'admins') {
            $checked = true;
            $hiddenValue = '1';
            $disabled = 'true';
        }
        if ($data['name'] == 'homeUCItem') {
            $checked = true;
            $hiddenValue = '1';
            $disabled = 'true';
        }
        $checkBox = CHtml::hiddenField('WebGuiPermissions[' . $group->id . '][' . $data['name'] . ']', $hiddenValue);
        $checkBox .= CHtml::checkBox(
                        'WebGuiPermissions[' . $group->id . '][' . $data['name'] . ']', $checked, array(
                    'disabled' => $disabled,
                        )
        );
        return $checkBox;
    }
}
