<?php
include_once('c_personals_org_card.php');
class c_page_cab_personals_org_card_db extends c_page_cab_personals_org_card
{
    public $h1='Карточка организации - исходные данные';
    var $use_textarea_autosize=1;

    function block_main()
    {   $this->h1.=' - исходные данные' ;
        $this->page_title();
        $this->panel_section();
    }

    function panel_section()
    {
        damp_array($this->rec_section,1,-1);
        // информация по фильтрам персонала
        //PERSONALS_FILTER()->show_personal_filter_db_info($this->rec_event['_reffer']) ;

        ?><button class="v1" cmd="check_structure">Проверить структуру разделов</button><?
        ?><button class="v1" cmd="disable_space_otdels">Выключить пустые подразделения</button><?
    }

    function CMD_check_structure()
    {   echo 'Проверяем структуру позразделов<br>' ;
        TREE('obj_site_personals')->update_levels_parents('');
        echo 'Проверка структуру выполнена<br>' ;
    }

    function CMD_disable_space_otdels()
    {   echo 'Отключаем пустые подразделения<br>' ;
        $recs=execSQL('select t1.pkey,t1.enabled,t1.clss,t1.obj_name,
                        (select count(t2.pkey) from obj_site_personals t2 where t2.parent1=t1.pkey and t2.enabled=1) as cnt_parent1,
                        (select count(t2.pkey) from obj_site_personals t2 where t2.parent2=t1.pkey and t2.enabled=1) as cnt_parent2,
                        (select count(t2.pkey) from obj_site_personals t2 where t2.parent3=t1.pkey and t2.enabled=1) as cnt_parent3,
                        (select count(t2.pkey) from obj_site_personals t2 where t2.parent4=t1.pkey and t2.enabled=1) as cnt_parent4,
                        (select count(t2.pkey) from obj_site_personals t2 where t2.parent5=t1.pkey and t2.enabled=1) as cnt_parent5,
                        (select count(t2.pkey) from obj_site_personals t2 where t2.parent6=t1.pkey and t2.enabled=1) as cnt_parent6
                        from obj_site_personals t1
                        where t1.clss in (1,207,208) and enabled=1
                        having cnt_parent1=0 and cnt_parent2=0 and cnt_parent3=0 and cnt_parent4=0 and cnt_parent5=0 and cnt_parent6=0 
                        order by t1.pkey
                        ',1)  ;

        if (_sizeof($recs)) execSQL_update('update obj_site_personals set enabled=0 where pkey in ('.implode(',',array_keys($recs)).') and clss in (1,207,208)',array('debug'=>2)) ;


        //
    }

}

?>
