<?php
class report_log_sql_errors extends c_report
{
 var $title='Журнал ошибок SQL' ;
 var $allow_to_rol=array() ;
 var $filter=array() ;
 var $view_by_space_filter=1 ;
 var $use_jwplayer=1 ;

 function setting($options=array())
 {
     $members=ACCOUNTS()->get_list_members() ;
     //damp_array($this->filter) ;
     ?><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">
                          <section class="col col-2">
                               <label class="label">Событие:</label>
                               <label class="select">
                                  <? $recs=execSQL_line('select distinct(obj_name) from '.ENGINE()->table_mo_log.' where obj_name!="" order by obj_name') ;?>
                                     <select name="filter[actions][]" multiple="multiple"><option value="0">Все события</option>
                                        <? if (_sizeof($recs)) foreach($recs as $value) {?><option value="<?echo $value?>" <?if (isset($this->filter['actions'][$value])) echo 'selected'?>><?echo $value?></option><?} ?>
                                    </select>
                               </label>
                            </section>
                          <section class="col col-2">
                              <label class="label">Оператор:</label>
                              <label class="select">
                                  <select id="account_ids" name="filter[account_ids][]"  multiple="multiple"><option value="0">Все</option>
                                      <?if (_sizeof($members))foreach($members as $rec_member)
                                        {?><option value="<?echo $rec_member['pkey']?>" <?if (isset($this->filter['account_ids'][$rec_member['pkey']])) echo 'selected'?>><?=$rec_member['obj_name']?></option><?}
                                      ?>
                                  </select>
                              </label>
                          </section>
                          <section class="col col-2">
                              <label class="label">IP:</label>
                              <label class="input">
                                  <input type="text" name="filter[ip]"  value="<? echo $this->filter['ip']?>">
                              </label>
                           </section>


                          <section class="col col-2">
                              <label class="label">Дата от</label>
                              <label class="input">
                                  <i class="icon-append fa fa-calendar"></i>
                                  <input type="text" name="filter[data_from]" class="datetimepicker" value="<?if ($this->filter['data_from']) echo $this->filter['data_from']?>">
                              </label>
                            </section>
                            <section class="col col-2">
                              <label class="label">Дата до</label>
                              <label class="input">
                                  <i class="icon-append fa fa-calendar"></i>
                                  <input type="text" name="filter[data_to]" class="datetimepicker" value="<?if ($this->filter['data_to']) echo $this->filter['data_to']?>">
                              </label>
                          </section>
                          <section class="col col-2">
                               <label class="label">Сортировка:</label>
                               <label class="select">
                                  <select name="filter[order]" id="order">
                                      <option value="0" <?if (!$this->filter['order']) echo 'selected'?>>Обратная</option>
                                      <option value="1" <?if ($this->filter['order']==1) echo 'selected'?>>Прямая</option>
                                  </select>
                               </label>
                            </section>
                        </div>
                  </fieldset>
                 <div id="panel_buttons"><input type="submit" class="button v2" cmd="set_filter" value="OK"></div>
              </form>
       </div>
     <?
  }


  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'])              $usl[]='obj_name like "'.$this->filter['action'].'"' ;
     if (_sizeof($this->filter['actions']))
     {  $arr=array() ;
        foreach($this->filter['actions'] as $action=>$temp) $arr[]='"'.$action.'"' ;
        $usl[]='obj_name in ('.implode(',',$arr).')' ;
     }

     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="'.$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  '.TM_LOG_SQL_ERRORS.' 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  '.TM_LOG_SQL_ERRORS.' where '.$usl.$order.$limit,$options) ;
    return($list_rec) ;
  }


  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?>>
        <thead>
            <tr><th>ID</th><th>IP</th><th>Дата, время</th><th>Оператор</th><th>Код ошибки</th><th>Описание</th><th>URL страницы</th><th>trace</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']='' ;}

            ?><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['typ'] ?></td>
              <td class="left"><? echo base64_decode($rec['obj_name']) ;?></td>
              <td class="left"><? echo $rec['url'] ;?></td>
              <td class="comment left"><? if ($rec['trace']) echo '<a target=_blank href="'.$rec['trace'].'">'.$rec['trace'].'</a>'?></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) ;

  }





}

?>
