<?php
    // Check Rights
    if(!$parser->getUserId()) die($parser->gt('not_perms'));
    $perm = $parser->checkPerms('0');
    if(!$perm['r']) die($parser->gt('not_perms'));
    if(!isset($workframe)) die('');

    include_once(SYS_BASE_PATH.'main/classes/wcatalog.class.php');
    class wcat extends wCatalog
    {
        public $perm = array('c' => 0, 'r' => 0, 'u' => 0, 'd' => 0);
        public function _titleframe()
        {
            global $parser;
            //$this->title = $parser->gt('test_uni', __FILE__);
            $this->title = $parser->utils->getFieldFromSql('SELECT name_'.$parser->lang_suff." FROM sys_objtypes WHERE objtype='test_uni'", '');
            $this->gridframeurl = $parser->makeUrl('fcc_test_uni_list');

            $pars = array();
            $pars['onchange'] = 'reloadgridm()';
            $pars['name'] = 'filter_period';
            $pars['val'] = 1;
            $this->addCell($parser->utils->periodMenu($pars));


            $pars['name'] = 'filter_boolfield';
            $pars['onchange'] = 'reloadgridm()';
            $pars['val'] = 1;
            $pars['options'] = array($parser->gt('all'), $parser->gt('mys'), $parser->gt('yesitis'), $parser->gt('noitisnot'));
            $this->addCell($parser->utils->dropdownMenu($pars));


            $this->popup_script = true;
            $pars = array();
            $pars['name'] = 'filter_color';
            $pars['emptyval'] = 0;
            $pars['tree_btn'] = 0;
            $pars['emptyrefval'] = $parser->gt('all',__FILE__);
            $pars['onchange'] = 'reloadgridm()';
            //$pars['val'] = 'sys_colors_yellow';
            //$pars['refval'] = 'Yellow';
            $pars['objtype'] = 'sys_interface_colors';
            $this->addCell($parser->utils->finder($pars));

/*
            $this->max_right_buttons = 3;
            $this->addRightRadioButton($parser->gt('alltypes',__FILE__), '', true, 0);
            $sql = 'SELECT objid, name_'.$parser->lang_suff." FROM sys_objects WHERE objtype='sys_organizers_catnotes' ORDER BY name_".$parser->lang_suff;
            $rs = $parser->conn->SelectLimit($sql, 100);
            while($row = $rs->FetchRow())
                $this->addRightRadioButton($row[1], '', false, $row[0]);
*/
        }


        public function _treeframe()
        {
            global $parser;

            include_once(SYS_BASE_PATH.'main/classes/wajaxtree.class.php');
            $tree = new wAjaxTree();
            $tree->caption = $parser->gt('');

            $tree->onopendialog = 'hide_list';
            $tree->onclosedialog = 'show_list';

            $tree->q->select('objid');
            $tree->q->lselect('name');
            $tree->q->select('isfolder');
            $tree->q->select('parent');
            $tree->q->select("'' AS prim");
            $tree->q->select('0 AS treetype');
            $tree->q->from('sys_objects');
            $tree->q->where("objid<>'0'");
            $tree->q->where("objtype='sys_interface_colors'");
            $tree->q->where('parent=:parent:');
            $tree->q->lorderby('name');

            $tree->leaf_func = 'leafclick';
            $tree->folder_func = 'leafclick';
            $tree->unselect = true;
            $tree->show();
        }

        public function _gridframe()
        {
            global $parser;
            //$active_tab = (isset($_REQUEST['active_tab'])) ? $_REQUEST['active_tab'] : '';
            $filter_color = (isset($_REQUEST['filter_color'])) ? $_REQUEST['filter_color'] : '';
            $filter_boolfield = (isset($_REQUEST['filter_boolfield'])) ? $_REQUEST['filter_boolfield'] : '';
            $filter_period = (isset($_REQUEST['filter_period'])) ? $_REQUEST['filter_period'] : '';
            $cat_id = (isset($_REQUEST['cat_id'])) ? $_REQUEST['cat_id'] : '';
            include_once(SYS_BASE_PATH.'main/classes/wajaxgrid.class.php');
            $w = new wAjaxGrid();
            $w->edit_table_alias = 'o';

            include_once(SYS_BASE_PATH.'ext/wtests/classes/test_uni.class.php');
            $w->f = new test_uni();
            if($cat_id) $w->caption = $parser->utils->getFieldFromSql("SELECT name_ru FROM sys_objects WHERE objid='".$cat_id."'", '');
                else $w->caption = $parser->utils->getFieldFromSql('SELECT name_'.$parser->lang_suff." FROM sys_objtypes WHERE objtype='test_uni'", '');

            $w->q->select('o.id',  $parser->gt('id'));
            $w->q->select('o.datefield', $parser->gt('createdon'));
		    $w->addHidden('filter_period', $filter_period);
            if($filter_period)
            {
                 $per = $parser->utils->getPeriod($filter_period, true);
                 $w->q->where("o.datefield ".$per);
                 $w->f->extra_deleteAll_sql .= "AND (datefield ".$per.")";
            }
            $w->q->select('o.name', $parser->gt('name'), array('edit' => $this->perm['u']));
            $w->rowclass_col = $w->q->select('o.color', $parser->gt('color'), array('hide' => 1));
		    $w->addHidden('filter_boolfield', $filter_boolfield);
            $w->q->select('o.boolfield', $parser->gt('boolfield'));
            switch($filter_boolfield)
            {
                case 1:	 $w->q->where("o.boolfield=1");
                         $w->f->extra_deleteAll_sql .= "AND boolfield=1 ";
                break;
                case 2:	 $w->q->where("o.boolfield=0");
                         $w->f->extra_deleteAll_sql .= "AND boolfield=0 ";
                break;
            }
		    $w->addHidden('filter_color', $filter_color);
            if($filter_color)
            {
                $mfilter_color = $parser->utils->addQuots($filter_color);
                $w->q->where("o.color IN(".$mfilter_color.")");
                $w->f->extra_deleteAll_sql .= "AND color IN(".$mfilter_color.") ";
                $w->f->setFieldVal('color', $filter_color);
            }
            $w->q->from('test_uni o');
            $w->q->where("o.id<>'0'");
            if($cat_id) $w->q->where("o.color='$cat_id'");
            $w->q->orderby('o.name');

            $w->addSearchField('o.name', $parser->gt('name'), 'C');

            $w->editing = $this->perm['u'];
            $w->adding = $this->perm['c'];
            $w->copying = $this->perm['c'];
            $w->deleting = $this->perm['d'];
            $w->multiselect = $this->perm['d'];

    
            $w->cardurl = $parser->makeUrl('test_uni_card');
    
            $w->addHidden('cat_id', $cat_id);

            $w->show();
        }
    }

    $cat = new wcat();
    $cat->workframe = $workframe;
    $cat->perm = $perm;

    $cat->show();

?>