<?php
class report_media_person_details extends c_report
{
 var $title='Ведомость доведения медиаконтента до персонала' ;
 var $view_by_space_filter=1 ;
 var $event_id=0 ;
 var $personal_id=0 ;
 var $mo_id=0 ;
 var $lession_id=0 ;
 var $rec_lession=array() ;
 var $rec_event=array() ;
 var $event_ext=array() ;
 var $rec_personal;
 //var $allow_export_to_csv=1 ;
 //var $report_script=_DIR_EXT.'/events_media/report_media_person_details.php' ;
 var $arr_view_mode=array(1=>'Тексты',2=>'Фото',3=>'Видео',4=>'Все');

 function init($options=array())
 { if (!function_exists('EVENTS')) return ;
   $this->rec_personal=PERSONAL()->get_personal_by_id($this->personal_id)  ;
 }
 function setting($options=array())
 {    $arr_groups=ACCOUNTS()->get_arr_groups_to_cur_account() ;
      //echo _CUR_REPORT_DIR ;
      ?><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">
                          <? //$this->filter_section_select_multiple('theme_ids','Темы собрания:',2,$arr_themes) ;
                             $this->filter_section_input_text('name','ФИО:',2,array('auto_complete'=>"autocomplette_personal_name")) ;
                             $this->filter_section_select('group_id','Место доведения:',2,$arr_groups) ;
                             $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('view_mode','Показывать контент:',2,$this->arr_view_mode) ;
                          ?>
                      </div>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

function setting_to_personal($options=array())
 {
     // так как название темы может быть свободным, не можем использовать ID для идентификации темы
     $arr_themes=execSQL_row('select distinct(obj_name) as theme_name,obj_name,theme_id from obj_site_lession_themes where parent in (select distinct(parent) from obj_site_lession_persons where personal_id='.$this->personal_id.') order by theme_name') ;
     $arr_groups=execSQL_row('select distinct(t1.group_id) as group_id,t2.obj_name as group_name 
                              from obj_site_lession t1 
                              left join obj_site_account t2 on t2.pkey=t1.group_id
                              where t1.pkey in (select distinct(parent) from obj_site_lession_persons where personal_id='.$this->personal_id.') 
                              order by group_name') ;

      ?><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">
                          <? $this->filter_section_select_multiple('events_ids','Мероприятия:',2,$arr_themes) ;
                             $this->filter_section_select('group_id','Место доведения:',2,$arr_groups) ;
                             $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('view_mode','Показывать контент:',2,$this->arr_view_mode) ;
                          ?>
                        </div>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

  function create_usl_select_obj($_params,&$options)
  {  //damp_array($this->filter) ;
     $usl=array() ;
     //$usl[]='t1.status=4' ;
     //$usl[]='t3.ext="events_media/i_event_media"' ;
     if ($this->personal_id) $usl[]='t1.personal_id='.$this->personal_id ;
     if ($this->event_id) $usl[]='t1.event_id='.$this->event_id ;
     if ($this->mo_id) $usl[]='t1.mo_id='.$this->mo_id ;

     if ($this->filter['group_id'])
      {   $arr1=ACCOUNTS()->tree[$this->filter['group_id']]->get_arr_child() ;
          $usl[]='t2.group_id in ('.implode(',',$arr1).')'  ;
      }

     if ($this->filter['name'])        $usl[]='t6.obj_name like "%'.$this->filter['name'].'%"' ;
     if ($this->filter['data_from'])        $usl[]='t1.c_data>='.strtotime($this->filter['data_from']) ;
     if ($this->filter['data_to'])          $usl[]='t1.c_data<='.strtotime($this->filter['data_to']) ;

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

    function panel_info_filter()
     { $_str=array() ; $info=array() ; $title='' ;
       if ($this->filter['group_id'])        $info['Группа']=ACCOUNTS()->get_member_name_by_id($this->filter['group_id']) ;
       //if ($this->filter['personal_id'])     $info['Сотрудник']=PERSONAL()->get_name_by_id($this->filter['personal_id']) ;
       if ($this->filter['name'])              $info['ФИО']='"'.htmlspecialchars_decode($this->filter['name']).'"' ;
       //if ($this->filter['type'])              $info['Тип мероприятия']='"'.$this->arr_types[$this->filter['type']].'"' ;
       //if ($this->filter['status'])            $info['Cтатус']='"'.$_SESSION['ARR_events_status'][$this->filter['status']].'"' ;
       //if ($this->status)                      $info['Cтатус']='"'.$_SESSION['ARR_events_status'][$this->status].'"' ;
       if ($this->filter['data_from'])         $info['дата от']='"'.$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(t1.pkey)
                        from  media_content_view t1
                        left join obj_site_events_schedule t2 on t2.pkey=t1.event_id
                        left join media_content t3 on t3.pkey=t1.content_id 
                        left join view_mo_session t4 on t4.pkey=t1.mo_id 
                        where '.$usl.'
                        ',$options) ;
    return($cnt) ;

  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    $options['debug']=0 ;
    $order=($options['order'])? ' order by '.$options['order']:' order by t1.pkey desc' ;
 	$list_recs=execSQL('select t1.*,t2.obj_name as event_name,t2.status as event_status,
                        t3.manual,t3.file_name,t3.type,t3.size,t3.width,t3.height,t3.len,t3.moment,
                        t4.group_id,t4.group_name,t4.terminal_id,t4.personal_name
                        from  media_content_view t1
                        left join obj_site_events_schedule t2 on t2.pkey=t1.event_id
                        left join media_content t3 on t3.pkey=t1.content_id 
                        left join view_mo_session t4 on t4.pkey=t1.mo_id 
                         
                        where '.$usl.$order.$limit,$options) ;
    return($list_recs) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { //$lession_ids=array() ; $arr_themes=array() ; $arr_themes_grp=array() ;
    //if (_sizeof($list_recs)) foreach($list_recs as $rec) $lession_ids[]=$rec['parent'] ;
    //if (_sizeof($lession_ids)) $arr_themes=execSQL('select pkey,parent,obj_name from obj_site_lession_themes where parent in ('.implode(',',$lession_ids).')') ;
    //if (_sizeof($arr_themes)) foreach($arr_themes as $rec) $arr_themes_grp[$rec['parent']][]=$rec['obj_name'] ;
    $id=($options['id'])? 'id='.$options['id']:'' ;

    //$recs=execSQL('select * from media_content_view',2) ;
    //print_2x_arr($list_recs) ;
    $i=1 ;
    $arr_event_obj=array() ;
    ?><table class="basic fz_small full auto"<?echo $id?>>
        <tr>
<th>ID</th>
<th>ID участия</th><th>ID мероприятия</th><th>Дата/время доведения</th><?
        if (!$this->personal_id) {?><th>Сотрудник</th><?}
        ?><th>Группа</th><th>Терминал</th><th>Момент доведения</th><th>Время доведения</th>
        <? if (!$this->event_id) {?><th>Мероприятие</th><?}
        ?><th>Тип контента</th><th>Контент</th></tr><?
          if (_sizeof($list_recs)) foreach($list_recs as $rec)
          {   if (!isset($arr_event_obj[$rec['event_id']])) $arr_event_obj[$rec['event_id']]=EVENTS()->get_event_obj_by_id($rec['event_id']) ;
              $obj_event=$arr_event_obj[$rec['event_id']] ;

              if (!$rec['length'] and strpos($rec['type'],'image')!==false) $rec['length']=$rec['len'] ;
              else if (!$rec['length'] and $rec['type']=='text') $rec['length']=$rec['len'] ;
              else if (!$rec['length']) $rec['length']=0 ;

              ?><tr>
<td><? echo $rec['pkey']?></td>                 
                  <td><a href="/window/events/event_pass/<?echo $rec['pass_id']?>/" target=_blank class=new_window><?echo $rec['pass_id']?></a></td>
                  <td><?echo $rec['event_id']?></td>
                  <td class="center"><?echo date('d.m.Y H:i',$rec['c_data'])?></td>
                  <? if (!$this->personal_id) {?><td><?echo $rec['personal_name']?></td><?}?>
                  <td class="center"><?echo $rec['group_name']?></td>
                  <td class="center"><?echo 'Терминал: '.$rec['terminal_id'];?></td>
                  <td class="center"><?echo $obj_event->arr_moment[$rec['moment']]?></td>
                  <td class="center"><?echo $rec['length'].' из '.$rec['len'].' сек.'?></td>
                  <? if (!$this->event_id) {?><td class="center"><?echo $rec['event_name']?></td><?}?>
                  <td class="center"><?echo $rec['type']?></td>
                  <td class="center">
                   <?
                      if ($rec['type']=='text') echo ($this->filter['view_mode']==1 or $this->filter['view_mode']==4)? $rec['manual']:$rec['obj_name'];
                      if (strpos($rec['type'],'image')!==false){ if ($this->filter['view_mode']==2 or $this->filter['view_mode']==4) {?><img src="<?echo $rec['file_name']?>" class="zoom" height="150"><?} else echo basename($rec['file_name']); }
                      if (strpos($rec['type'],'video')!==false){ echo ($this->filter['view_mode']==3 or $this->filter['view_mode']==4)? $this->player_code_avi($rec['file_name']):basename($rec['file_name']) ; } ;
                   ?></td>
              </tr><?
              $i++;
          }
      ?></table><?

  }



    function player_code_avi($video_url,$options=array())
           {  $video_width=($options['video_width'])? $options['video_width']:400 ;
              $video_height=($options['video_height'])? $options['video_height']:300 ;
              //damp_array($options,1,-1) ;
               ?><br>
               <video controls width="<?echo $video_width?>" height="<?echo $video_height?>" <?if ($options['video_rotare']) echo 'style="transform:rotate('.$options['video_rotare'].'deg);"'?>>
                 <source src="<?echo $video_url?>">
               </video>
               <div id="panel_filter" class="button_to_upload_video"><form id="sky-form" class="sky-form">
                         <!--<a class="button" href="/services/get_video/?url=<? echo $video_url?>">Скачать</a>-->
                      </form></div>
                      <style type="text/css">
                          div#panel_filter.button_to_upload_video{text-align:center;}
                          div#panel_filter.button_to_upload_video p.info{margin:50px 100px 50px 100px;font-size:16px;}
                          div#panel_filter.button_to_upload_video a.button{float:none;margin:30px 100px 30px 100px;}
                      </style>
               <?
       }

}

?>
