<?php
include_once(_DIR_TO_CLASS.'/c_page.php') ;
class c_page_cab_report extends c_page
{
 var $title='Раздел "Отчеты"' ;
 public $report_obj ;
 public $use_datepicker=1 ;
 public $use_jwplayer=0 ;
 public $use_fusioncharts=0 ;
 public $use_report='' ;
 public $filter=array() ;
 public $view_h1_title=true ;
 public $error=array() ;
 public $report_editing=false ;
 public $no_h2_title=false ;
 public $filter_status=0 ;

 public $err=array() ;
 public $arr_allow_reports=array() ;

 function set_head_tags()
 {
     parent::set_head_tags(); // TODO: Change the autogenerated stub

 }

 function select_obj_info()
 { if (_CUR_REPORT and _CUR_REPORT!=_CUR_FILTER and !$this->use_report) $this->use_report=_CUR_REPORT ;
   if ($this->use_report) $this->read_report() ;
   if (!$this->use_report or !_sizeof($_SESSION['arr_allow_reports'])) $this->read_all_reports() ;
 }

 function read_report()
 {    $rec_report=REPORTS()->reports_list[$this->use_report] ;
      // временно для совместимости со старыми URL
      if (!_sizeof($rec_report)) $rec_report=REPORTS()->reports_list[str_replace('report_','',$this->use_report)] ;
      if (_sizeof($rec_report))
      { $script_dir=$rec_report['script_dir'] ;
        $class_name=$rec_report['class_name'] ;
           $script_exists=0 ;
           if (file_exists(_DIR_EXT.'/'.$script_dir)) { include_once(_DIR_EXT.'/'.$script_dir) ; $script_exists=1 ; }
           if (file_exists(_DIR_EXT_LOCAL.'/'.$script_dir)) { include_once(_DIR_EXT_LOCAL.'/'.$script_dir) ; $script_exists=1 ; }
           if ($script_exists)
            { if (class_exists($class_name))
           { $this->report_obj=new $class_name() ;
             if (is_object($this->report_obj))
             { $this->h1=($this->h1)? $this->h1:$this->report_obj->title ;
               $this->view_h1_title=$this->report_obj->view_h1_title ;
               $this->report_obj->editing=$this->report_editing ;
               // переносим опции отчета в страницу
               $this->use_jwplayer=$this->report_obj->use_jwplayer ;
               $this->use_datepicker=$this->report_obj->use_datepicker ;
               $this->use_fusioncharts=$this->report_obj->use_fusioncharts ;
               // сохраняем на странице опции показа текущего отчета
               $this->filter_status=$_SESSION['filter_status'][$this->use_report] ;
               // запоминаем название текущей группы отчетов
               $cur_group_name=REPORTS()->reports_list[_CUR_REPORT]['section_title'] ;
               // подсвечиваем в меню пунк меню с вкладкой группы отчетов
               $this->selected_item_by_url='/cab/reports/[group='.$cur_group_name.']/' ;


             } else $this->error=array('code'=>'file_not_found','text'=>'Не удалось создать объект класса для отчета "'.$this->use_report.'": <strong>'.$class_name.'</strong>') ;
             //$allow_to_rol=$report_obj->allow_to_rol ;
           } else $this->error=array('code'=>'file_not_found','text'=>'Не найден класс отчета "'.$this->use_report.'": <strong>'.$class_name.'</strong> в скрипте <strong>'.hide_server_dir(_DIR_EXT.'/'.$script_dir).'</strong>') ;
         } else $this->error=array('code'=>'file_not_found','text'=>'Не найден скрипт отчета "'.$this->use_report.'": <strong>'.hide_server_dir(_DIR_EXT.'/'.$script_dir).'</strong>') ;
      }
      else $this->error=array('code'=>'report_not_register','text'=>'Отчет "'.$this->use_report.'" не зарегистрирован в REPORTS()->reports_list') ;
 }

 function read_all_reports()
 {  // damp_array(ENGINE()->reports_sections,1,-1) ;
    $allow_items=execSQL_row('select obj_name,rol_id from obj_site_permit where rol_id="'.MEMBER()->info['rol'].'"') ;
    //damp_array(REPORTS()->reports_list) ;

    // готовим список доступных отчетов
     $_SESSION['arr_allow_reports']=array() ;
    if (_sizeof($_SESSION['indx_report_section'])) foreach($_SESSION['indx_report_section'] as $indx=>$section_title) $_SESSION['arr_allow_reports'][$section_title]=array() ;

    if (_sizeof(REPORTS()->reports_list)) foreach(REPORTS()->reports_list as $name=>$rec_report)
      {  $script_dir=$rec_report['script_dir'] ;
         $class_name=$rec_report['class_name'] ;
         if (isset($allow_items[$class_name]) or MEMBER()->rol==1) // если доступ к отчету разрешен для данной роли или админмистратор
            {  $script_exists=0 ;
               if (file_exists(_DIR_EXT.'/'.$script_dir)) { include_once(_DIR_EXT.'/'.$script_dir) ; $script_exists=1 ; }
               if (file_exists(_DIR_EXT_LOCAL.'/'.$script_dir)) { include_once(_DIR_EXT_LOCAL.'/'.$script_dir) ; $script_exists=1 ; }
               if ($script_exists)
                 { if (class_exists($class_name))
                { $report_obj=new $class_name() ; //echo 'Создаем отчет класса '.$class_name.'<br>' ;
                  $title=$report_obj->title ;
                  $section_title=(isset($rec_report['section_title']))? $rec_report['section_title']:$_SESSION[$rec_report['system']]->system_title ;
                  $indx_report_section=$_SESSION['indx_report_section'][$section_title] ;
                  if ($report_obj->show_on_page_peports) $_SESSION['arr_allow_reports'][$section_title][$name]=$title ;
                } else $this->err[]=$script_dir.': <span class=red> класс '.$class_name.' - не найден</span>' ;
              } else $this->err[]=$script_dir.': <span class=red>скрипт не найден</span>' ;
      }
 }
 }

 function get_cur_page_meta_tags()
 {
   if (is_object($this->report_obj)) $this->h1=$this->report_obj->title ;
 }

 function second_menu_config()
 { $this->second_menu['/cab/reports/']=array('title'=>'Все отчеты','class'=>'fa fa-file-text-o') ;
   if (_sizeof($_SESSION['arr_allow_reports'])>1) foreach($_SESSION['arr_allow_reports'] as $system_name=>$arr_reports)
        if (_sizeof($arr_reports)) $this->second_menu['/cab/reports/[group='.$system_name.']/']=array('title'=>$system_name,'class'=>'fa fa-file-text-o') ;

   //if (is_object($this->report_obj)) $this->second_menu[$this->report_obj->url_report]=array('title'=>$this->report_obj->title,'class'=>'fa fa-file-text-o') ;  ;
 }


 function block_main()
 { if ($this->error['code']) {?><div class="alert"><? echo $this->error['text']?></div><?}
   elseif ($this->report_obj) $this->panel_report_view() ;
   else if (!$this->use_report)  $this->panel_report_select() ;
 }

 function panel_block_right()
 { ?><div id="button_hidden_right_panel" <?if ($this->filter_status==2 or $this->filter_status==0) echo 'class=hidden'?> report_code="<?echo $this->use_report?>"><i class="fa fa-caret-right"></i></div>
     <div id="button_show_right_panel" <?if ($this->filter_status==1 or $this->filter_status==0) echo 'class=hidden'?> report_code="<?echo $this->use_report?>"><i class="fa fa-caret-left"></i></div>
     <div id="button_close_right_panel" <?if ($this->filter_status==2 or $this->filter_status==0) echo 'class=hidden'?> report_code="<?echo $this->use_report?>"><i class="fa fa-search-minus"></i></div>
     <div id=block_right <?if ($this->filter_status!=1) echo 'class=hidden'?>><?
         if ($this->check_access() and !$this->access_denited) $this->block_right();
   ?></div><?
 }

 function block_right()
 { if (method_exists($this->report_obj,'setting_details')) $this->report_obj->setting_details() ;
 }

 function report_sender(){
     $url = explode('/', _CUR_REPORT_DIR);
     $rec = execSQL_van('select pkey,obj_name from '.SEND_REPORT()->main_table.' where class_name="'.$url[count($url)-2].'"');
     if ((int) $rec['pkey'] && isset($this->report_obj->not_send_report) == false){
         ?>
         <a href="/cab/send_report/report/<? echo $rec['pkey']; ?>/" title="Перейти к формированию отправки&#10;для отчета <? echo $rec['obj_name'];?>"
            target="_blank" style="font-size:16px; color:#56b6e1;">
             <i class="fa fa-paper-plane"></i>
         </a>
         <?
     }
 }

 function panel_report_view()
 { ?><div id="panel_report_view">
     <? if (function_exists('SEND_REPORT')) $this->report_sender(); ?>
       <div class="inner <?if ($this->filter_status==1 or $this->filter_status==2)  echo 'hidden'?>">
            <?if (method_exists($this->report_obj,'setting_details')){?><div id="button_open_right_panel" report_code="<?echo $this->use_report?>"><i class="fa fa-search-plus"></i></div><?}?>
            <?$this->report_obj->setting() ;?>
       </div><?
     if (_sizeof($this->filter) or $this->report_obj->view_by_space_filter) $this->report_obj->view() ;
     else
     {  ?><h2 class="center"><?echo  $this->report_obj->title?></h2><?
        ?><div class="alert" style="text-align:center;">Не заданы параметры отчета</div><?
     }
   ?></div><?
 }

 function panel_report_select()
 {  if (_sizeof($this->err)) echo '<br><h3>Ошибки при чтении списка отчетов:</h3>'.implode('<br>',$this->err) ;
    //damp_array($_SESSION['arr_allow_reports']) ;
    ?><div id="list_reports_themes"><?
    if (_sizeof($_SESSION['arr_allow_reports'])) foreach($_SESSION['arr_allow_reports'] as $system_name=>$arr_reports) if (!$this->filter['group'] or $this->filter['group']==$system_name)
    {
      if (_sizeof($arr_reports))
    { if (!$this->no_h2_title){?><h2>Отчеты: <?echo $system_name?></h2><?}
        ?><div class="list_reports_icons"><?
        foreach($arr_reports as $name=>$title)
        { ?><div class="item"><a href="/cab/reports/<?echo $name?>/"><img src="/<?echo _CLASS_?>/images/report.png"></a><div class="title"><a href="/cab/reports/<?echo $name?>/"><?echo $title?></a></div></div><?}
        ?><div class="clear"></div></div><?
      }
    }
    ?></div><?

    ?><style type="text/css">
        div#list_reports_themes{position:relative;}
        div#list_reports_themes h2{margin:10px 0 10px 0;}
        div.list_reports_icons{position:relative;margin-top:15px;}
        div.list_reports_icons div.item{width:150px;height:200px;float:left;margin:0 15px 15px 0;text-align:center;}
        div.list_reports_icons div.item img{width:100px;}
        div.list_reports_icons div.item div.title{text-align:center;margin-top:5px;}
        div.list_reports_icons div.item div.title a{text-decoration:none;}
        div.list_reports_icons div.item:hover div.title a{text-decoration:underline;}

      </style><?

 }


}
?>
