<?php
include_once("c_setting_members.php");

class c_page_cab_setting_right extends c_page_cab_setting_members
{
  public $h1='Права доступа персонала' ;
  //  function check_access() {return 1;}
 function block_main()
 { $this->page_title() ;

   $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') ;
   //  damp_array($recs2) ;

   ACCOUNTS()->update_member_info($_SESSION['member']) ;

   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() ;

   $list=$this->get_all_files(_DIR_TO_CLASS.'/ext/',array('reg_pattern'=>'/^c|(report)_(.+?).php/is','allow_ext'=>$list_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'=>$list_ext)) ;
     $list=array_merge($list,$list_files_local_ext) ;
   }

   //damp_array($_SESSION['ARR_roll'],1,-1) ;
   //damp_array(ENGINE()->list_active_ext,1,-1) ;
   //damp_array($list,1,-1) ;

     ?><div id="panel_fast_search" style="margin:10px 0;"><strong>Быстрый поиск по</strong>: <input type="text" id="fast_search_url" style="width:200px;font-size:14px;"> </div>

     <table class="setting_right"><?

       $prev_section_name='' ;
       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)
         {   if ($_GET['debug']) echo 'file_name=<strong>'.$fname.'</strong>' ;
             $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)) ;//print_r($arr) ; exit ;
               $class_name=$math[1][0] ;  $ff=0 ;  if ($_GET['debug']) echo ' class_name=<strong>'.$class_name.'</strong>' ;
               if (!class_exists($class_name)) { $ff=1 ; ob_start() ; include_once($fname) ; ob_end_clean() ; }
               else if ($_GET['debug'])  echo '&nbsp;&nbsp;&nbsp;<span style="color:red;">!!! class "<strong>'.$class_name.'</strong>" уже объявен</span>' ;
               if (class_exists($class_name))
               {   $page=new $class_name() ;
                   $parent_class_name=get_parent_class($page) ;
                   $no_view_info=array() ;
                   if ($parent_class_name=='c_page_TXT') { $no_view=1 ; $no_view_info[]='parent_class_name=c_page_TXT' ; }
                   if ($page->no_view_in_setting_right) { $no_view=1 ;  $no_view_info[]='class options no_view_in_setting_right' ; }
                   //if (!isset($list_ext[$arr[3]])) { $no_view=1 ; $no_view_info[]='ext not registrired' ; }
                   if (!$no_view)
                   {   $section_name=$list_ext[$arr[3]] ;
                       $dir_name=str_replace('aspmo_','',$arr[3]) ;
                       if ($prev_section_name!=$section_name)
                      {?><tr class=title dir="<?echo $dir_name?>"><td colspan="<?echo _sizeof($_SESSION['ARR_roll'])+1?>"><h2><?echo $section_name?></h2></td></tr>
                         <tr class="head_item <?echo $dir_name?> hidden" page="<?echo $class_name?>"><th>Страница кабинета</th><?
                          if (_sizeof($_SESSION['ARR_roll'])) foreach($_SESSION['ARR_roll'] as $id=>$rol_info) if ($id!=1){?><th><?echo $rol_info['obj_name']?></th><?}?></tr><?
                       }
                       $prev_section_name=$section_name ;
                       $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.'"' ;
                       ?><tr class="page_item <?echo $dir_name?> hidden"  page="<?echo $class_name?>"><td><?echo '<strong>'.$page_title.'</strong><br><div class="comment">'.$show_name.'<br>'.$class_name.'</div>'?></td><?
                       if (_sizeof($_SESSION['ARR_roll'])) foreach($_SESSION['ARR_roll'] as $id=>$rol_info) if ($id!=1)
                       { ?><td><input type="checkbox" class="v2" cmd="aspmo_setting/change_right_member" <?if (isset($recs2[$class_name][$id]) or $page->allow_access_to_rol==$id) echo 'checked' ?> <?if ($page->allow_access_to_rol==$id) echo ' disabled ' ?> class_name="<?echo $class_name?>" rol_id="<?echo $id?>"</td><?
                          // добавляем разрешение автоматически, если его еще нет
                          if (!isset($recs2[$class_name][$id]) and $page->allow_access_to_rol==$id) ACCOUNTS()->allow_right_rol($id,$class_name) ;

                       }
                       ?></tr><?
                   } else if ($_GET['debug']) echo '&nbsp;&nbsp;<span class="green">Страница не будет показана:</span> '.implode(', ',$no_view_info).' ' ;
               }
               if ($_GET['debug']) echo '<br>' ;

             }
             else if ($_GET['debug']) echo  '<span style="color:red;"> not match c_page_*** or report_***'.'</span><br>' ;
         }
       }
       ?></table><script type="text/javascript">
     $j(document).ready(function()
     {
         $j('tr.title').on('click',function()
         { var class_name=$j(this).attr('dir') ;
           $j('tr.'+class_name).toggleClass('hidden') ;
           //$j(this).next('tr.head_item').toggleClass('hidden') ;
         })    ;
          var text='' ;
          $j('#fast_search_url').keyup(
              function(e)
              {   text=$j(this).val() ;
                  if (text!="")
                  { $j('.setting_right tr.head_item').hide().addClass('hidden') ;
                    $j('.setting_right tr.page_item').hide().addClass('hidden') ;
                    $j('.setting_right tr.page_item').each(function(el)
                    {   var class_page=$j(this).attr('page') ;
                        var title_page=$j(this).find('strong').text() ;
                        console.log(title_page) ;
                        var re = new RegExp(text, 'g');
                        var re_title = new RegExp(text, 'gi');
                        var res_by_class=class_page.match(re);
                        var res_by_title=title_page.match(re_title);
                        //if (res!=null && res.length)   console.log(class_page) ;
                        if (res_by_class!=null && res_by_class.length)   $j(this).removeClass('hidden').show() ; //console.log(class_page) ;
                        if (res_by_title!=null && res_by_title.length)   $j(this).removeClass('hidden').show() ; //console.log(class_page) ;
                    }) ;
                    // скрываем title в котором все элементы hidden
                    $j('.setting_right tr.title').each(function(el)
                    {  var dir=$j(this).attr('dir') ;
                       var count_all=$j('.setting_right tr.page_item.'+dir).length ;
                       var count_hidden=$j('.setting_right tr.page_item.hidden.'+dir).length ;
                       //console.log(dir+' / '+count_all+' / '+count_hidden) ;
                       if (count_all==count_hidden)  $j(this).hide() ;
                       else $j(this).next('tr.head_item').removeClass('hidden').show() ;
                    });
                  }
                  else
                  { $j('.setting_right tr.title').show() ;
                    $j('.setting_right tr.page_item').addClass('hidden') ;
                  }

              }) ;
     }) ;
        </script>
     <?

     echo $text_setup ;
 }

/**
 * function get_all_files
 * @param string $dir абсолютный пусть к директории
 * @param array $options опции
 * @return array $list_files список всех файлов директории
 */

 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) ;
 }



}



?>
