<?php
class report_log_events extends c_report
{
 var $title='Журнал событий' ;
 var $allow_to_rol=array() ;
 var $filter=array() ;
 var $view_by_space_filter=1 ;
 var $use_jwplayer=1 ;


 function setting($options=array())
 {   $arr_actions=array() ;
     if (_sizeof($this->filter['actions'])) foreach($this->filter['actions'] as $value=>$use)
     { unset($this->filter['actions'][$value]) ;
       $new_value=str_replace('|','/',$value) ;
       $this->filter['actions'][$new_value]=$use ;
     }

     if (!$this->filter['action']) $arr_actions=execSQL_line('select distinct(obj_name) from '.ENGINE()->table_mo_log.' where obj_name!="" order by obj_name') ;
     $recs_members=ACCOUNTS()->get_list_members() ;
     $arr_groups=execSQL_row('select pkey,obj_name,clss from obj_site_account where clss=206') ;


     $this->form_open() ;
        $this->row_open() ;
            $this->filter_section_select_multiple('actions','Событие:',2,$arr_actions,array('use_value_as_id'=>1)) ;
            $this->filter_section_select_multiple('account_ids','Оператор:',2,$recs_members) ;
            $this->filter_section_select('group_id','Группа:',2,$arr_groups) ;
            $this->filter_section_input_text('ip','IP:',2) ;
            $this->filter_section_input_text('reffer','Объект события (ID):',2) ;
        $this->row_close() ;
        $this->row_open() ;
            $this->filter_section_input_text('data_from','От:',2,array('class'=>'datetimepicker')) ;
            $this->filter_section_input_text('data_to','До:',2,array('class'=>'datetimepicker')) ;
            $this->filter_section_select('order','Сортировка:',2,$this->arr_order,['nospace_element'=>1]) ;
        $this->row_close() ;
        $this->panel_button_apply();
     $this->form_close() ;

  }

 function setting_to_account($options=array())
 {
     //damp_array($this->filter) ;
     $arr_actions=[];
     if (!$this->filter['action']) $arr_actions=execSQL_line('select distinct(obj_name) from '.ENGINE()->table_mo_log.' where obj_name!="" order by obj_name') ;
     $arr_order=array(0=>'Обратная','pkey'=>'Прямая') ;
     $this->form_open() ;
         $this->row_open() ;
//             $this->filter_section_select_multiple('actions','Событие:',2,$arr_actions,array('use_value_as_id'=>1)) ;
             $this->filter_section_input_text('data_from','От:',2,array('class'=>'datetimepicker')) ;
             $this->filter_section_input_text('data_to','До:',2,array('class'=>'datetimepicker')) ;
             $this->filter_section_select('order','Сортировка:',2,$arr_order,['nospace_element'=>1]) ;
         $this->row_close() ;
         $this->panel_button_apply();
     $this->form_close() ;
  }

  function create_usl_select_obj($_params,&$options)
  {  $usl=array() ;
     //$usl[]='clss=251' ;

     if ($this->filter['from_id'])             $usl[]='pkey>='.$this->filter['from_id'] ;
     if ($this->filter['to_id'])               $usl[]='pkey<='.$this->filter['to_id'] ;
     if ($this->filter['action']=='exec_crontab')  $usl[]='obj_name like "'.$this->filter['action'].'"' ;
     elseif ($this->filter['action'])              $usl[]='obj_name like "'.$this->filter['action'].'"' ;
     elseif (_sizeof($this->filter['actions']))
     {  $arr=array() ;
        foreach($this->filter['actions'] as $action=>$temp) $arr[]='"'.$action.'"' ;
        $usl[]='obj_name in ('.implode(',',$arr).')' ;
     }
     else                                          $usl[]='obj_name!="exec_crontab"' ;

     if ($this->filter['account_id'])              $usl[]='account_id="'.$this->filter['account_id'].'"' ;
     if (_sizeof($this->filter['account_ids']))     $usl[]='account_id in ('.implode(',',array_keys($this->filter['account_ids'])).')' ;


     if ($this->filter['user_id'])              $usl[]='user_id="'.$this->filter['user_id'].'"' ;
     if ($this->filter['mo_id'])              $usl[]='mo_id="'.$this->filter['mo_id'].'"' ;
     if ($this->filter['personal_id'])          $usl[]='personal_id="'.$this->filter['personal_id'].'"' ;
     if ($this->filter['terminal_id'])          $usl[]='terminal_id="'.$this->filter['terminal_id'].'"' ;

     if ($this->filter['reffer'])              $usl[]='(reffer1 like "'.$this->filter['reffer'].'%" or'.
                                                      ' reffer2 like "'.$this->filter['reffer'].'%" or'.
                                                      ' reffer3 like "'.$this->filter['reffer'].'%" or'.
                                                      ' reffer4 like "'.$this->filter['reffer'].'%" or'.
                                                      ' reffer5 like "'.$this->filter['reffer'].'%")' ;

     if ($this->filter['ip'])          $usl[]='ip="'.$this->filter['ip'].'"' ;

     // время передаваемое в текстовом формате
     if ($this->filter['data_from'])           $usl[]='c_data>='.strtotime($this->filter['data_from']) ;
     if ($this->filter['data_to'])             $usl[]='c_data<='.strtotime($this->filter['data_to']) ;
     // текст внутри результата
     if ($this->filter['text'])              $usl[]='comment like "%'.$this->filter['text'].'%"' ;

     $usl_res=(_sizeof($usl))? implode(' and ',$usl):1 ;
     //damp_array($usl,1,-1) ;
     return($usl_res) ;
  }

 function panel_info_filter()
 { $_str=array() ; $info=array() ; $title='' ;
   if ($this->filter['action'])                            $info['Операция']='"'.$this->filter['action'].'"' ;
   if (_sizeof($this->filter['actions']))
   {  $arr=array() ;
      foreach($this->filter['actions'] as $action=>$temp) $arr[]='"'.$action.'"' ;
      $info['Операции']=implode(',',$arr) ;
   }

   if ($this->filter['account_id'])              $info['Оператор']=ACCOUNTS()->get_member_name_by_id($this->filter['account_id']) ;
   if (_sizeof($this->filter['account_ids']))
   {   $arr=array() ;
       foreach($this->filter['account_ids'] as $id=>$temp) $arr[]=ACCOUNTS()->get_member_name_by_id($id) ;
       $info['Операторы']=implode(',',$arr) ;
   }

   if ($this->filter['data_from'])                         $info['c']=$this->filter['data_from'] ;
   if ($this->filter['data_to'])                           $info['по']=$this->filter['data_to'] ;
   if ($this->filter['text'])                              $info['текст']=$this->filter['text'] ;
   if ($this->filter['ip'])                                $info['IP']=$this->filter['ip'] ;
   if (_sizeof($info)) foreach($info as $title=>$value) $_str[]=$title.': <strong>'.$value.'</strong>' ;
   if (_sizeof($_str)) $title='<p class=center>'.implode(' ',$_str).'</p>' ;
   return $title ;
 }


  function get_cnt_items($usl,$options=array())
  { $cnt=execSQL_value('select count(pkey) from  '.ENGINE()->table_mo_log.' where '.$usl,$options) ;
    return($cnt) ;
  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    $order=($this->filter['order'])? ' order by '.$this->filter['order']:' order by pkey desc' ;
      $options['debug']=0 ;
    $list_rec=execSQL('select * from  '.ENGINE()->table_mo_log.' where '.$usl.$order.$limit,$options) ;
    return($list_rec) ;
  }


 function print_template_HTML2($list_recs,$options)
 {  ?><table class="basic fz_small <?if (!$this->filter['no_full_width']) echo 'full'?> auto">
       <tr>
           <th>ID</th>
           <th>IP</th>
           <th>Событие</th>
           <th>Время</th>
           <th>Зона доступа</th>
           <th>Турникет</th>
           <th>Направление</th>
           <th>Результат</th>
           <th>Ф.И.О.</th>
           <th>Пропуск</th>
       </tr>
     <? //$i=($options['first_obj'])? $options['first_obj']:1;
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        { ?><tr>
                <td><?echo $rec['pkey']?></td>
                <td><?echo $rec['ip']?></td>
                <td><?echo $_SESSION['ARR_evt_type'][$rec['evt_type']]?></td>
                <td><?echo date('d.m.Y H:i:s',$rec['c_data'])?>
                     <?if ($rec['_turn_stream_name']){?>&nbsp;<span class="v2" cmd="tracking/get_video_to_pass" stream="<?echo $rec['_turn_stream_name']?>" evt_id="<?echo $rec['pkey']?>"><i class="fa fa-video-camera"></i></span><?}?>
                </td>
                <td class="left"><?echo SKD()->get_full_zone_path($rec['zone_id'])?></td>
                <td class="left"><?echo $_SESSION['ARR_device_name_small'][$rec['_device_clss']].'' ;
                                  if ($rec['_device_id']) echo $rec['_device_id'] ; else echo $rec['_device_turn_id'];
                    ?></td>
                <td><?echo (!$rec['access_type'])? 'Вход':'Выход'?></td>
                <td><?echo ($rec['result'])? '<span class="green">OK</span>':'<span class="red">'.$rec['obj_name'].'</span>'?></td>
                <td class="left"><a href="/cab/bp/person/<?echo $rec['person_id']?>/" class="new_window" title="win_second"><?echo $rec['_person_fio']?></a></td>
                <td class="left"><?if ($rec['pass_id']) { echo $_SESSION['ARR_pass_type_small'][$rec['pass_type']]?> № <?echo $rec['pass_id']?><?}
                                   if ($rec['pass_image']) {?><i class="fa fa-file-image-o fz_18 v2" cmd="aspmo_bp/get_gate_pass_image" event_id="<?echo $rec['pkey']?>"></i><?}

                    ?></td>
            </tr>
       <?}
     ?>
     </table><?

     $this->panel_info_select($options) ;

 }

  function print_template_HTML($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;
     $recs_account=execSQL('select pkey,obj_name,rol from obj_site_account') ;
     ?><table class="basic fz_small <?if (!$this->filter['no_full_width']) echo 'full'?> auto list_logs_events" <?echo $id?>>
        <colgroup><col id=c1><col id=c2><col id=c3><col id=c3><col id=c4><col id=c5><col id=c6><col id=c7></colgroup>
        <thead>
            <tr><th>ID</th><th>IP</th><th>Дата, время</th><th>Оператор</th><th>Действие</th><th>Объекты события</th><th>Результат</th><th>Данные операции</th><th>Лог запроса</th><th>Время<br>выполения</th></tr>
        </thead>
        <tbody>

        <?
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        {   if (isset($rec['user_id']) and isset($recs_account[$rec['user_id']]) and !$rec['member_id'])
                { $rec['member_id']=$rec['user_id'] ; $rec['user_id']='' ;

                }
            $recs_reffers=array() ; $str=array() ;
            if (isset($rec['reffer1'])) $recs_reffers[$rec['reffer1']]=get_obj_info($rec['reffer1']) ;
            if (isset($rec['reffer2'])) $recs_reffers[$rec['reffer2']]=get_obj_info($rec['reffer2']) ;
            if (isset($rec['reffer3'])) $recs_reffers[$rec['reffer3']]=get_obj_info($rec['reffer3']) ;
            if (isset($rec['reffer4'])) $recs_reffers[$rec['reffer4']]=get_obj_info($rec['reffer4']) ;
            if (isset($rec['reffer5'])) $recs_reffers[$rec['reffer5']]=get_obj_info($rec['reffer5']) ;
            if (isset($rec['personal_id']) and function_exists('PERSONAL'))
            {   $rec_personal=PERSONAL()->get_personal_by_id($rec['personal_id']) ; ;
                $recs_reffers[$rec_personal['_reffer']]=$rec_personal ;
            }
            if (_sizeof($recs_reffers)) foreach($recs_reffers as $rec_reffer)
              {  $class_name=$_SESSION['descr_clss'][$rec_reffer['clss']]['name'] ;
                 $obj_name=$rec_reffer['obj_name'] ;
                 $obj_ID=$rec_reffer['pkey'] ;
                 //$str[]=$class_name.' '.$obj_name.', ID'.$obj_ID.', ['.$rec_reffer['_reffer'].']' ;
                 $str[]=$class_name.' '.$obj_name.', ID '.$obj_ID.'' ;
              }

            ?><tr>
              <td class=id><? echo $rec['pkey'] ;?></td>
              <td class=id><? echo $rec['IP'] ;?></td>
              <td class=data><? echo date('d.m.Y H:i:s',$rec['c_data']) ;?></td>
              <td class=name><? echo $recs_account[$rec['account_id']]['obj_name'].'<br>'.$_SESSION['ARR_roll'][$rec['rol']]['obj_name'] ?></td>
              <td class=name><? echo $rec['obj_name'] ?></td>
              <td class="left"><? if (_sizeof($str)) echo implode('<br>',$str) ;?></td>
              <td class="comment left"><? echo preg_replace('/\<script\>(.+?)\<\/script\>/is','',$rec['comment'])
                  ?></td>
              <td class=data><? if ($rec['data']) damp_array(unserialize($rec['data']),1,-1) ?></td>
            <td><? if ($rec['request_uid']) echo '<a href="/logs/page_trace_'.$rec['request_uid'].'.html" target="_blank">Лог</a>'?></td>
              <td class=time><? if ($rec['time_exec']) echo round($rec['time_exec'],5) ?></td>
           </tr><?
        }
    ?></tbody></table>

    <? $this->panel_info_select($options) ;

  }

  function print_template_HTML_small($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;
     $recs_account=execSQL('select pkey,obj_name,rol from obj_site_account') ;
     ?><table class="basic fz_small full auto" <?echo $id?>>
        <thead>
            <tr><th>ID</th><th>IP</th><th>Дата, время</th><th>Оператор</th><th>Действие</th><th>Объекты события</th><th>Результат</th></tr>
        </thead>
        <tbody>

        <?
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        {   if (isset($rec['user_id']) and isset($recs_account[$rec['user_id']]) and !$rec['member_id'])
                { $rec['member_id']=$rec['user_id'] ; $rec['user_id']='' ;

                }
            $recs_reffers=array() ; $str=array() ;
            if (isset($rec['reffer1'])) $recs_reffers[]=get_obj_info($rec['reffer1']) ;
            if (isset($rec['reffer2'])) $recs_reffers[]=get_obj_info($rec['reffer2']) ;
            if (isset($rec['reffer3'])) $recs_reffers[]=get_obj_info($rec['reffer3']) ;
            if (isset($rec['reffer4'])) $recs_reffers[]=get_obj_info($rec['reffer4']) ;
            if (isset($rec['reffer5'])) $recs_reffers[]=get_obj_info($rec['reffer5']) ;
            if (_sizeof($recs_reffers)) foreach($recs_reffers as $rec_reffer)
              {  $class_name=$_SESSION['descr_clss'][$rec_reffer['clss']]['name'] ;
                 $obj_name=$rec_reffer['obj_name'] ;
                 $obj_ID=$rec_reffer['pkey'] ;
                 $str[]=$class_name.' '.$obj_name.', ID '.$obj_ID.'' ;
              }
            ?><tr>
              <td class=id><? echo $rec['pkey'] ;?></td>
              <td class=id><? echo $rec['IP'] ;?></td>
              <td class=data><? echo date('d.m.Y H:i:s',$rec['c_data']) ;?></td>
              <td class=name><? echo $recs_account[$rec['account_id']]['obj_name'].'<br>'.$_SESSION['ARR_roll'][$rec['rol']]['obj_name'] ?></td>
              <td class=name><? echo $rec['obj_name'] ?></td>
              <td class="left"><? if (_sizeof($str)) echo implode('<br>',$str) ;?></td>
              <td class="comment left"><? echo $rec['comment'] ?></td>
           </tr><?
        }
    ?></tbody></table>

    <? $this->panel_info_select($options) ;

  }





}

?>
