<?php
class report_personal_history extends c_report
{
 var $title='История изменений по сотруднику' ;
 var $allow_to_rol=array() ;
 var $filter=array() ;
 var $view_by_space_filter=1 ;
 var $personal_id=0 ;

 function setting($options=array())
 {   $arr_actions=execSQL_line('select distinct(obj_name) from log_personal_changes where obj_name!="" order by obj_name') ;
     $arr_accounts=execSQL_row('select distinct(t1.account_id),t2.obj_name from log_personal_changes t1 left join obj_site_account t2 on t2.pkey=t1.account_id') ;
     $arr_fileds=execSQL_line('select distinct(fname) from log_personal_changes') ;


     $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,$arr_accounts) ;
            $this->filter_section_select_multiple('fname','Поле:',2,$arr_fileds,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,$this->arr_order) ;
        $this->row_close() ;
        $this->panel_button_apply();
     $this->form_close() ;

  }

  function create_usl_select_obj($_params,&$options)
  {  $usl=array() ;
     $usl[]='personal_id='.$this->personal_id ;

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

     if (_sizeof($this->filter['fname']))
     {  $arr=array() ;
        foreach($this->filter['fname'] as $fname=>$temp) $arr[]='"'.$fname.'"' ;
        $usl[]='fname in ('.implode(',',$arr).')' ;
     }

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

     // время передаваемое в текстовом формате
     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']) ;

     $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 (_sizeof($this->filter['actions']))
   {  $arr=array() ;
      foreach($this->filter['actions'] as $action=>$temp) $arr[]='"'.$action.'"' ;
      $info['Операции']=implode(',',$arr) ;
   }

   if (_sizeof($this->filter['fname']))
   {  $arr=array() ;
      foreach($this->filter['fname'] as $fname=>$temp) $arr[]='"'.$fname.'"' ;
      $info['Поля']=implode(',',$arr) ;
   }

   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 (_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())
  { // $options['debug']=2 ;
      $cnt=execSQL_value('select count(pkey) from  log_personal_changes 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']=2 ;
    $list_rec=execSQL('select * from  log_personal_changes where '.$usl.$order.$limit,$options) ;
    return($list_rec) ;
  }


  var $arr_work_types=array(1=>'Основая должность',2=>'Совмещение должность') ;

  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 full auto list_logs_events" <?echo $id?>>
        <thead>
            <tr><th>ID</th><th>Дата, время</th><th>Оператор</th><th>Тип должности</th><th>Имя поля</th><th>Старое значение</th><th>Новое значение</th><th>Источник</th></tr>
        </thead>
        <tbody>

        <?
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        {   ?><tr>
              <td class=id><? echo $rec['pkey'] ;?></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 $this->arr_work_types[$rec['worker_type']]?></td>
              <td class=name><? echo $rec['fname'] ?></td>
              <td class=name><? echo $rec['old_value'] ?></td>
              <td class=name><? echo $rec['new_value'] ?></td>
              <td class=name><? echo $rec['source'] ?></td>
           </tr><?
        }
    ?></tbody></table>

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

  }







}

?>
