<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class VGSListviewColors_Layout_Model extends Vtiger_Base_Model {


    public static function getInstance() {
		return new self();
    }

    public static function getInstanceById($id) {
            $db = PearDatabase::getInstance();
            $model = VGSListviewColors_Layout_Model::getInstance();

            $result = $db->pquery("SELECT * FROM vtiger_vgslistviewcolors WHERE vgslistviewcolorsid = ?", array($id));
            $num_rows = $db->num_rows($result);
            if ($num_rows > 0) {
                    $values = $db->query_result_rowdata($result, 0);
                    $model->setData($values);
            }
            return $model;
    }

    public static function getInstanceByArray($row) {
            $model = VGSListviewColors_Layout_Model::getInstance();
            return $model->setData($row);
    }

    /**
     * Function to get the record id
     * @return <Number>
     */
    public function getId() {
            return $this->get('vgslistviewcolorsid');
    }

    /**
     * Function to get the selected module id
     * @return <Number>
     */
    public function getModuleId() {
            return $this->get('moduleid');
    }

    /**
     * Function to get the selected module name
     * @return <String>
     */
    public function getModuleName() {
            return getTabname($this->getModuleId());
    }

    /**
     * Function to get the description
     * @return <String>
     */
    function getDescription() {
            return $this->get('description');
    }

    /**
     * Function to get the description
     * @return <Array>
     */
    function getConditions() {
            return Zend_Json::decode(html_entity_decode($this->get('conditions'),ENT_COMPAT));
    }
    
    /**
     * Function to get the description
     * @return <Array>
     */
    function getActions() {
            return Zend_Json::decode(html_entity_decode($this->get('actions'),ENT_COMPAT));
    }

    /**
     * Function to get the description
     * @return <String>
     */
    function getConditionsToDisplay() {
            Zend_Json::decode(html_entity_decode($this->get('conditions'),ENT_COMPAT));
    }
    
    /**
     * Functions transforms workflow filter to advanced filter
     * @return <Array>
     */
    function transformToAdvancedFilterCondition() {
        $conditions = $this->getConditions();
        $transformedConditions = array();

        if(!empty($conditions)) {
            foreach($conditions as $index => $info) {
                $columnName = $info['fieldname'];
                $value = $info['value'];
                // To convert date value from yyyy-mm-dd format to user format
                $valueArray = explode(',', $value);
                $isDateValue = false;
                for($i = 0; $i < count($valueArray); $i++) {
                    if(Vtiger_Functions::isDateValue($valueArray[$i])) {
                        $isDateValue = true;
                        $valueArray[$i] = DateTimeField::convertToUserFormat($valueArray[$i]);
                    }
                }
                if($isDateValue) {
                    $value = implode(',', $valueArray);
                }
                // End
                if($columnName == 'filelocationtype')
                    $value = ($value == 'I') ? vtranslate('LBL_INTERNAL','Documents') : vtranslate('LBL_EXTERNAL','Documents');
                elseif ($columnName == 'folderid') {
                    $folderInstance = Documents_Folder_Model::getInstanceById($value);
                    $value = $folderInstance->getName();
                }
                if(!($info['groupid'])) {
                    $firstGroup[] = array('columnname' => $columnName, 'comparator' => $info['operation'], 'value' => $value,
                            'column_condition' => $info['joincondition'], 'valuetype' => $info['valuetype'], 'groupid' => $info['groupid']);
                } else {
                    $secondGroup[] = array('columnname' => $columnName, 'comparator' => $info['operation'], 'value' => $value,
                            'column_condition' => $info['joincondition'], 'valuetype' => $info['valuetype'], 'groupid' => $info['groupid']);
                }
            }
        }
        $transformedConditions[1] = array('columns'=>$firstGroup);
        $transformedConditions[2] = array('columns'=>$secondGroup);
        return $transformedConditions;
    }
    
    static function getWorkFlowFilterFromAdvanceFilter($conditions) {
        $wfCondition = array();

        if(!empty($conditions)) {
            foreach($conditions as $index => $condition) {
                $columns = $condition['columns'];
                if($index == '1' && empty($columns)) {
                    $wfCondition[] = array('fieldname'=>'', 'operation'=>'', 'value'=>'', 'valuetype'=>'',
                        'joincondition'=>'', 'groupid'=>'0');
                }
                if(!empty($columns) && is_array($columns)) {
                    foreach($columns as $column) {
                        $wfCondition[] = array('fieldname'=>$column['columnname'], 'operation'=>$column['comparator'],
                            'value'=>$column['value'], 'valuetype'=>$column['valuetype'], 'joincondition'=>$column['column_condition'],
                            'groupjoin'=>$condition['condition'], 'groupid'=>$column['groupid']);
                    }
                }
            }
        }
        return $wfCondition;
    }
}