<?php
// специальный отчет для добавления сотрудников в группы
class report_personals_to_group extends c_report
{
 var $title='Сотрудники' ;
 var $view_by_space_filter=1 ;
 var $allow_org_ids=1 ;
 var $usl_select_org='' ;

 function setting($options=array())
 {   if (!is_array($options['disabled_filter'])) $options['disabled_filter']=array() ;
     $recs_orgs=execSQL('select * from obj_site_personals where parent=1 and clss=207 order by obj_name') ;
     ?><div id="panel_filter">
             <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                 <?if ($this->filter['ids']){?><input type=hidden name=filter[ids] value="(<?echo implode(';',array_keys($this->filter['ids']))?>)"><?}?>
                 <?if ($this->filter['client_ids']){?><input type=hidden name=filter[client_ids] value="(<?echo implode(';',array_keys($this->filter['client_ids']))?>)"><?}?>
                 <?if ($this->filter['client_id']){?><input type=hidden name=filter[client_id] value="<?echo $this->filter['client_id']?>"><?}?>
                  <fieldset>
                      <div class="row" id="person_info">
                           <section class="col col-4">
                               <label class="label">ФИО</label>
                               <label class="input">
                                    <input type="text" name="filter[name]" placeholder="" SetAutoComplete cmd="autocomplette_personal_name" value="<?echo $this->filter['name']?>">
                               </label>
                           </section>
                          <section class="col col-4">
                              <label class="label">Подразделениe:</label>
                              <label class="select">
                                   <select name="filter[dep_ids][]" multiple="multiple">
                                       <?if (_sizeof($recs_orgs)) foreach($recs_orgs as $rec_org)
                                       {?><option value="<?echo $rec_org['pkey']?>" <?if (isset($this->filter['dep_ids'][$rec_org['pkey']])) echo 'selected'?>><?echo $rec_org['obj_name']?></option><?}
                                       ?>
                                   </select>
                              </label>
                          </section>
                          <section class="col col-2">
                              <label class="label">Пропуск</label>
                              <label class="input">
                                  <input type="text" name="filter[propusk]" placeholder="" LANG="LAT" value="<?echo $this->filter['propusk']?>">
                              </label>
                          </section>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }


  function create_usl_select_obj($_params=array(),&$options=array())
  {  //damp_array($this->filter,1,-1) ;
     $usl=array() ;
     $usl[]='clss=211' ;
     if ($this->filter['name'])            $usl[]='obj_name like "'.$this->filter['name'].'%"' ;
     if ($this->filter['dep_ids'])         $usl[]='parent2 in ('.implode(',',array_keys($this->filter['dep_ids'])).')' ;
     if ($this->filter['org_ids'])         $usl[]='parent2 in ('.implode(',',array_keys($this->filter['org_ids'])).')' ;
     if ($this->filter['otdel_ids'])         $usl[]='parent3 in ('.implode(',',array_keys($this->filter['otdel_ids'])).')' ;
     if ($this->filter['ids'])             $usl[]='pkey in ('.implode(',',array_keys($this->filter['ids'])).')' ;
     if ($this->filter['propusk'])         $usl[]='propusk="'.$this->filter['propusk'].'"' ;
     if ($this->filter['working_id'])      $usl[]='working="'.$this->filter['working_id'].'"' ;
     if (_sizeof($this->filter['working_ids'])) $usl[]='working in ('.implode(',',array_keys($this->filter['working_ids'])).')' ;
     if ($this->filter['working'])         $usl[]='working="'.$this->filter['working'].'"' ;
     if ($this->filter['personal_ids'])    $usl[]='pkey in ('.$this->filter['personal_ids'].')' ;
     if ($this->filter['active'])          $usl[]='enabled=1' ;
     $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['name'])                            $info['ФИО']='"'.$this->filter['name'].'"' ;
   if ($this->filter['org_id'])                          $info['организация']=PERSONAL()->get_name_by_id($this->filter['org_id']) ;
   //if ($this->filter['otdel_id'])                        $info['отдел']=PERSONAL()->get_name_by_id($this->filter['otdel_id']) ;
   if ($this->filter['otdel_id'])
   {   $arr=explode(',',$this->filter['otdel_id']) ;
       $arr2=array() ;
       foreach($arr as $otdel_id) $arr2[]=PERSONAL()->get_name_by_id($otdel_id) ;
       $info['отдел']=implode(', ',$arr2);
   }
   if ($this->filter['dep_id'])                          $info['Подразделение']=PERSONAL()->get_full_dep_name_by_dep_id($this->filter['dep_id'])  ;
   if ($this->filter['dep_ids'])                         $info['Подразделениz']=PERSONAL()->get_org_names_by_ids(array_keys($this->filter['dep_ids']))  ;
   if ($this->filter['working'])                         $info['должность']=_IL('IL_works')->recs[$this->filter['working']]['obj_name'];
   if ($this->filter['working_id'])                      $info['должность']=_IL('IL_works')->recs[$this->filter['working_id']]['obj_name'];
   if (_sizeof($this->filter['working_ids']))
   {   $arr=array() ;
       foreach($this->filter['working_ids'] as $work_id=>$flag)  $arr[]=_IL('IL_works')->recs[$work_id]['obj_name'] ;
       $info['должность']=implode(',',$arr) ;
   }
   if ($this->filter['propusk'])                         $info['номер пропуска']='"'.$this->filter['propusk'].'"' ;

   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  '.PERSONAL()->view_personal.'
                        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 obj_name' ;
    //echo $usl ;
 	$list_rec=execSQL('select *
                       from  '.PERSONAL()->view_personal.'
                       where '.$usl.$order.$limit,$options) ;
    if (_sizeof($list_rec)) foreach($list_rec as $id=>$rec) PERSONAL()->prepare_public_info_to_personal($list_rec[$id],$options) ;
    return($list_rec) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;
      $arr_personal_ids=array() ;
    if ($options['cur_group'])
    { $rec_group=PERSONAL()->get_group_by_id($options['cur_group']) ;
      $personal_ids=get_linked_ids_to_rec($rec_group,array('clss'=>211)) ;
      if ($personal_ids) $arr_personal_ids=explode(',',$personal_ids)  ;
    }
    ?><table class="list_personal_table_img basic fz_small full auto"<?echo $id?>>
        <colgroup><col id="c1"><col id="c2"><col id="c3"><col id="c4"></colgroup>
        <tr><th>Выбор</th><th>Пропуск</th><th>Фото</th><th>ФИО</th><th>Дата рождения</th><th>Возраст</th><th>Организация</th><th>Подразделение</th><th>Должность</th><th>Группа сотрудника</th></tr><?
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        { ?><tr class=item>
                <td><?if (!in_array($rec['pkey'],$arr_personal_ids)){?><input type="<?echo ($options['check_type'])? $options['check_type']:'checkbox'?>" name="_check[]" value="<?echo $rec['_reffer']?>"><?} else echo '<span class="red">Сотрудник уже в группе</span>'?></td>
                <td class="id"><?echo $rec['propusk']?><?if ($rec['demo_access']){?><div class="demo_access" title="Сотрудник может проходить неограниченное число МО"></div><?}?></td>
                <td class="img"><a  href="<?echo $rec['__href_window']?>" class="new_window" target="_blank" title="win_second"><img src="<?echo img_clone($rec,'small')?>"></a></td>
                <td class="left"><a href="<?echo $rec['__href_window']?>" class="new_window" target="_blank" title="win_second"><?echo $rec['obj_name']?></a>
                    <br><?echo '<span class="nowrap small">'.$rec['uid'].'</span>'?>
                    <?if (!$rec['enabled'])echo '<br><br><span class="red bold">УВОЛЕН</span> ('.date('d.m.Y H:i',$rec['fired_data']).')'?>
                </td>
                <td class="dr"><?echo $rec['dr']?></td>
                <td class="age"><?echo $rec['age']?></td>
                <td class="left"><?echo $rec['_org_name']?></td>
                <td class="left"><?echo $rec['_otdel_name']?></td>
                <td class="left"><?echo _IL('IL_works')->recs[$rec['working']]['obj_name']?></td>
                <td class="left"><?echo $rec['_groups_names']?></td>
            </tr><?
        }
      ?></table><?
      //damp_array($rec) ;
  }
    
  // шаблон - вывод ссылками без разделитея
  function print_template_HTML_small($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;
      $arr_personal_ids=array() ;
    if ($options['cur_group'])
    { $rec_group=PERSONAL()->get_group_by_id($options['cur_group']) ;
      $personal_ids=get_linked_ids_to_rec($rec_group,array('clss'=>211)) ;
      if ($personal_ids) $arr_personal_ids=explode(',',$personal_ids)  ;
    }
    ?><table class="list_personal_table_img basic fz_small full auto"<?echo $id?>>
        <colgroup><col id="c1"><col id="c2"><col id="c3"><col id="c4"></colgroup>
        <tr><th>Выбор</th><th>ФИО</th><th>Организация, подразделение, должность</th><th>Группа сотрудника</th></tr><?
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        { ?><tr class=item>
                <td><?if (!in_array($rec['pkey'],$arr_personal_ids)){?><input type="<?echo ($options['check_type'])? $options['check_type']:'checkbox'?>" name="_check[]" value="<?echo $rec['_reffer']?>"><?} else echo '<span class="red">Сотрудник уже в группе</span>'?></td>
                <td class="left"><a href="<?echo $rec['__href_window']?>" class="new_window" target="_blank" title="win_second"><?echo $rec['obj_name']?></a>
                    <br><?echo $rec['dr']?>
                    <br><?echo $rec['age'].' '.get_case_by_count_1_2_5($rec['age'],'год,года,лет')?>
                    <br><?echo '<span class="nowrap small">'.$rec['uid'].'</span>'?>
                    <?if (!$rec['enabled'])echo '<br><br><span class="red bold">УВОЛЕН</span> ('.date('d.m.Y H:i',$rec['fired_data']).')'?>
                </td>
                <td class="left"><?echo $rec['_org_name']?><br><?echo $rec['_otdel_name']?><br><?echo _IL('IL_works')->recs[$rec['working']]['obj_name']?></td>
                <td class="left"><?echo $rec['_groups_names']?></td>
            </tr><?
        }
      ?></table><?
      //damp_array($rec) ;
  }

    function print_template_CSV($list_recs,$options=array())
    { $i=$options['from'] ;
       if (_sizeof($list_recs)) foreach($list_recs as $rec)
       {   $data=array() ;
      	   $data['№']=$i+1 ;
           $data['Пропуск']=$rec['propusk']  ;
           //$data['Табельный номер']=$rec['tabnumber']  ;
      	   $data['ФИО']=$rec['obj_name']  ;
           $data['UID']=$rec['uid']  ;
           $data['Дата рождения']=$rec['dr']  ;
           $data['Возраст']=$rec['age']  ;
      	   $data['Организация']=$rec['_org_name'] ;
      	   $data['Подразделение']=$rec['_otdel_name'] ;
           $data['Должность']=$rec['_work_name'] ;
           $data['Группа']=$rec['_groups_names'] ;
           // вносим данные в csv
           $this->put_data_to_CSV($data,$i) ;
      	   $i++ ;
  	}

   }

}

?>
