<?php
include_once(_DIR_EXT.'/HTML/c_page_JSON.php');
class c_services_poll_tree extends c_page_JSON
{
    public $tree_root=0 ; // с какого раздела будет раскрываться дерево каталога, 0 - будет показан корневой элемент, 1 - будут показываны сразу разделы второго уровня

    function block_main()
    { if($_GET['id']=='#') { $id=$this->tree_root ; $tkey=$_GET['tkey'] ; }
				else list($id,$tkey)=explode('_',$_GET['id']);
				if (!$tkey) $tkey=_DOT('poll_themes')->pkey ;
				echo $tkey ;
				echo _DOT($tkey)->table_name ;


       if (_DOT($tkey)->table_name=='poll_themes')
        $recs_sections=execSQL('select pkey,obj_name,parent,level,clss,tkey,
                                  (select count(t2.pkey) from poll_themes t2 where t2.parent=t1.pkey and t2.clss in (1,572,481)) as cnt_subsections, 
                                  (select count(t3.pkey) from poll_questions t3 where t3.parent=t1.pkey) as cnt_questions 
                                  from poll_themes t1 
                                  where parent='.$id);

			 if (_DOT($tkey)->table_name=='poll_themes')
        $recs_questions=execSQL('select pkey,obj_name,parent,clss,tkey
                          from poll_questions 
                          where parent='.$id);

/*			 if (_DOT($tkey)->table_name=='poll_themes' or _DOT($tkey)->table_name=='poll_questions')
        $recs_questions=execSQL('select pkey,obj_name,parent,clss,tkey
                          from poll_questions
                          where parent='.$id);
*/
			  if (_DOT($tkey)->table_name=='poll_questions')
        $recs_answers=execSQL('select pkey,parent,obj_name,question_id,clss,tkey from poll_answers where question_id='.$id);

        $res=array();
        if(_sizeof($recs_sections)) foreach($recs_sections as $rec)
        {
            $has_children=($rec['cnt_subsections'] or $rec['cnt_questions'])? true:false;
            $perent_uid=($rec['parent']==$this->tree_root)? '#':$rec['parent'].'_'.$rec['tkey'];
            $type='clss_'.$rec['clss'];
            $uid=$rec['pkey'].'_'.$rec['tkey'];
            $res[]=array('id'=>$uid,
                         'reffer'=>$rec['_reffer'],
                         'text'=>$rec['obj_name'],
                         'parent'=>$perent_uid,
                         'type'=>$type,
                         'children'=>$has_children,
												 '$tkey'=>$tkey,
												 'table_name'=>_DOT($tkey)->table_name,
                         'state'=>array('opened'=>false,'selected'=>false));
        }
        if(_sizeof($recs_questions)) foreach($recs_questions as $rec)
        {
            $perent_uid=($rec['parent']==$this->tree_root)? '#':$rec['parent'].'_'._DOT('poll_themes')->pkey;
            $type='clss_'.$rec['clss'];
            $uid=$rec['pkey'].'_'.$rec['tkey'];
            $res[]=array('id'=>$uid,
                         'reffer'=>$rec['_reffer'],
                         'text'=>$rec['obj_name'],
                         'parent'=>$perent_uid,
                         'type'=>$type,
                         'children'=>true,
												 'table_name'=>_DOT($tkey)->table_name,
                         'state'=>array('opened'=>false,'selected'=>false));
        }
        if(_sizeof($recs_answers)) foreach($recs_answers as $rec)
        {
            $perent_uid=$rec['question_id'].'_'._DOT('poll_questions')->pkey;
            $type='clss_'.$rec['clss'];
            $uid=$rec['pkey'].'_'.$rec['tkey'];
            $res[]=array('id'=>$uid,
                         'reffer'=>$rec['_reffer'],
                         'text'=>$rec['obj_name'],
                         'parent'=>$perent_uid,
                         'type'=>$type,
                         'children'=>false,
                         'table_name'=>_DOT($tkey)->table_name,
                         'state'=>array('opened'=>false,'selected'=>false));
        }
        return ($res);
    }

}
