<?php
include_once(_DIR_EXT.'/HTML/c_page_JSON.php');
class c_services_personals_tree extends c_page_JSON
{
    function get_structure()
    {   $id=($_GET['id']=='#')? 1:(int)$_GET['id'];

        $usl=($_GET['id']=='#' and $_GET['org_id'])? ' and pkey='.$_GET['org_id']:'' ;

        $rec_section=PERSONAL()->get_org_by_id($id) ;
        $recs_sections=execSQL('select pkey,tkey,obj_name,parent,level,clss,
                                  (select count(t2.pkey) from view_deps t2 where t2.parent=t1.pkey and t2.clss in (1,207,208)  and enabled=1) as cnt_subsections, 
                                  (select count(t2.pkey) from view_personals_active t2 where t2.parent'.($rec_section['level']+1).'=t1.pkey and t2.clss=211) as cnt_personals 
                                  from view_deps t1 
                                  where parent='.$id.' and enabled=1 '.$usl.' order by obj_name');

        $res=array();
        if(_sizeof($recs_sections)) foreach($recs_sections as $rec)
        {
            $has_children=($rec['cnt_subsections'])? true:false;
            $parent_uid=($rec['parent']==1)? '#':$rec['parent'];
            $type='clss_'.$rec['clss'];
            $uid=$rec['pkey'];
            $name=($rec['cnt_personals'])? $rec['obj_name'].' <span class=cnt>['.$rec['cnt_personals'].']</span>':$rec['obj_name'] ;
            $res[]=array('id'=>$uid,
                         'reffer'=>$rec['_reffer'],
                         'text'=>$name,
                         'parent'=>$parent_uid,
                         'type'=>$type,
                         'children'=>$has_children,
                         'state'=>array('opened'=>false,'selected'=>false));
        }

        return ($res);
    }

}
