<?php
class report_permissions extends c_report
{
    var $title='Отчет по правам доступа' ;
    var $view_by_space_filter=1 ;
    var $allow_export_to_csv=1 ;

    function setting($options=array())
    {
        ?><div id="panel_filter">
        <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
            <fieldset>
                <div class="row" id="person_info">
                    <section class="col col-2">
                        <label class="label">Роль</label>
                        <label class="select">
                            <select id="account_ids" name="filter[rol_ids][]"  multiple="multiple"><option value="0">Все</option>
                                <?if (_sizeof($_SESSION['ARR_roll']))foreach($_SESSION['ARR_roll'] as $rol_id=>$rol_rec) if ($rol_id!=1)
                                {?><option value="<?echo $rol_id?>" <?if (isset($this->filter['rol_ids'][$rol_id])) echo 'selected'?>><?echo $rol_rec['obj_name']?></option><?}
                                ?>
                            </select>
                        </label>
                    </section>
                </div>
            </fieldset>
            <?$this->panel_buttons();?>
        </form>
        </div><br>
        <?
    }

    function get_cnt_items($usl,$options=array())
    {
        return(0) ;
    }

    function prepare_data()
    {

        ob_start() ;
        SETUP()->exec_moduls_setup('reg_ext','Регистрация ext-директорий') ;
        SETUP()->save_reg_ext() ;
        $text_setup=ob_get_clean() ;

        $list_ext=ENGINE()->get_list_active_ext() ;

        $result=[];

        $recs=execSQL('select pkey as id,obj_name,rol_id from obj_site_permit where clss=216') ;
        $recs2=group_by_field('obj_name',$recs,'rol_id') ;

        $list=$this->get_all_files(_DIR_TO_CLASS.'/ext/',array('reg_pattern'=>'/^c|(report)_(.+?).php/is','allow_ext'=>ENGINE()->get_list_active_ext())) ;
        if (_CLASS_LOCAL_)
        { $list_files_local_ext=$this->get_all_files(_DIR_TO_CLASS_LOCAL.'/ext/',array('reg_pattern'=>'/^c|(report)_(.+?).php/is','allow_ext'=>ENGINE()->get_list_active_ext())) ;
            $list=array_merge($list,$list_files_local_ext) ;
        }

        if (_sizeof($this->filter['rol_ids'][1])) unset($this->filter['rol_ids'][1]);

        if (!_sizeof($this->filter['rol_ids']) or _sizeof($this->filter['rol_ids'][0])) {
            $col_num=_sizeof($_SESSION['ARR_roll']);
            $rol_ids=array_keys($_SESSION['ARR_roll']);
            unset($rol_ids[0]); $col_num--;
        } else {
            $col_num=_sizeof($this->filter['rol_ids']);
            $rol_ids=array_keys($this->filter['rol_ids']);
        }
        if (_sizeof($list)) foreach ($list as $fname) {
            $script_name=basename($fname) ;
            $no_view=0 ;
            if ($script_name!='c_ajax.php'  and $script_name!='c_ajax_json.php'  and strpos($fname,'~')===false) {
                $text=file_get_contents($fname) ;
                if (preg_match_all('/class (c_page_(.+?)) extends c_page/is',$text,$math) or preg_match_all('/class (report_(.+?)) extends /is',$text,$math)  /*and !preg_match('/function check_access/is',$text,$math2)*/) {
                    $arr=explode('/',hide_server_dir($fname)) ;
                    $class_name=$math[1][0] ;
                    if (!class_exists($class_name)) { $ff=1 ; ob_start() ; include_once($fname) ; ob_end_clean() ; }
                    if (class_exists($class_name)) {
                        $page=new $class_name() ;
                        $parent_class_name=get_parent_class($page) ;
                        if ($parent_class_name=='c_page_TXT') $no_view=1 ;
                        if ($page->no_view_in_setting_right) $no_view=1 ;
                        if (!$no_view){
                            $section_name=$list_ext[$arr[3]] ;
                            $data=[] ;

                            $show_name=str_replace(_DIR_TO_CLASS.'/','',$fname) ;
                            $page_title=($page->h1)? $page->h1:$page->title ;
                            if ($parent_class_name=='c_report') $page_title='Отчет "'.$page_title.'"' ;

                            $data['page_title']=$page_title;
                            $data['show_name']=$show_name;
                            $data['class_name']=$class_name;
                            if (_sizeof($rol_ids)) foreach ($rol_ids as $rol_id) {
                                if (isset($recs2[$class_name][$rol_id]) or $page->allow_access_to_rol==$rol_id) $data['rol'][$rol_id]=1;
                                else $data['rol'][$rol_id]=0;
                            }
                            $result['data'][$section_name][]=$data;
                        }
                    }
                }
            }
        }
        $result['rol_ids']=$rol_ids;
        return $result;
    }

    function HTML_report_body($options=array())
    {
        $list_recs=$this->prepare_data();

        $rol_cnt=_sizeof($list_recs['rol_ids']);

        if (_sizeof($list_recs['data'])) {
            ?>
            <table class="basic full auto">
            <?
            foreach ($list_recs['data'] as $section_name=>$section_recs) {
                ?>
                <tr>
                    <th class="left" colspan="<?echo (1+$rol_cnt)?>"><h2><?echo $section_name?></h2></th>
                </tr>
                <tr>
                    <th>Страница</th>
                    <?
                    if (_sizeof($list_recs['rol_ids'])) foreach ($list_recs['rol_ids'] as $rol_id) {
                        ?>
                        <th><?echo $_SESSION['ARR_roll'][$rol_id]['obj_name']?></th>
                        <?
                    }
                    ?>
                </tr>
                <?
                if (_sizeof($section_recs)) foreach ($section_recs as $section_rec) {
                    ?>
                    <tr>
                        <td class="left"><div class="comment"><?echo $section_rec['page_title'].'<br>'.$section_rec['show_name'].'<br>'.$section_rec['class_name']?></div></td>
                        <?
                        if (_sizeof($list_recs['rol_ids'])) foreach ($list_recs['rol_ids'] as $rol_id) {
                            $permission_checked=0;
                            if ($section_rec['rol'][$rol_id]) $permission_checked=1;
                            ?>
                            <td <?if ($permission_checked) echo 'class="permission_checked"' ?>><?if ($permission_checked) echo '+'; ?></td>
                            <?
                        }
                        ?>
                    </tr>
                    <?
                }
            }
            ?>
            </table>
            <style type="text/css">
                td.permission_checked{background-color: #aacc99;}
            </style>
            <?
        }
    }

    function get_all_files($dir,$options=array())
    {$list_files=get_files_list($dir,$options,$options['file_pattern']) ; if (!is_array($list_files)) $list_files=array() ;
        $sub_dir_list=get_dir_list($dir) ;
        if (_sizeof($sub_dir_list)) foreach($sub_dir_list as $sub_dir_name)
        {   $ext_name=str_replace(array($dir,'/'),'',$sub_dir_name) ; //echo $sub_dir_name.' => '.$ext_name.'<br>' ;
            if (isset($options['allow_ext'][$ext_name]))
            { if (!_sizeof($options['no_include_dirs']) or (_sizeof($options['no_include_dirs']) and !in_array($sub_dir_name,$options['no_include_dirs'])))
            { if ($options['include_dir']) $list_files[]=$sub_dir_name ;
                $sub_dir_file_list=get_all_files($sub_dir_name,$options) ; //damp_array($sub_dir_file_list,1,-1) ;
                if (_sizeof($sub_dir_file_list))  $list_files=array_merge($list_files,$sub_dir_file_list) ;
            }
            }
        }
        return($list_files) ;
    }


    function CSV_report_body($options=array())
    {

        $i=$options['from'] ;

        $list_recs=$this->prepare_data();

        if (_sizeof($list_recs['data'])) foreach ($list_recs['data'] as $section_name=>$section_recs){
            if (_sizeof($section_recs)) foreach ($section_recs as $section_rec) {
                $data=[];
                $data['Раздел']=$section_name;
                $data['Страница']=$section_rec['page_title'];
                $data['Путь']=$section_rec['show_name'];
                $data['Класс']=$section_rec['class_name'];
                if (_sizeof($list_recs['rol_ids'])) foreach ($list_recs['rol_ids'] as $rol_id) {
                    $data[$_SESSION['ARR_roll'][$rol_id]['obj_name']]=$section_rec['rol'][$rol_id]?'+':'';
                }
                $this->put_data_to_CSV($data,$i) ;
                $i++ ;
            }
        }

    }



}
