<?php
include_once("c_setting.php");
class c_page_cab_setting_member extends c_page_cab_setting
{
  public $h1='Карточка персонала ЭСМО' ;
  public $rec_member ;
  public $rec_account ;

 function select_obj_info()
 { $cur_id=(_CUR_ID)? _CUR_ID:MEMBER()->id ;
   $this->rec_member=ACCOUNTS()->get_member_by_id($cur_id) ;
   $this->rec_account=$this->rec_member ;
   if (!$this->rec_member['pkey']) $this->result=404 ;
   else if (MEMBER()->id==$cur_id) $this->allow_access_to_member=MEMBER()->id  ; // разрешаем доступ к странице для текущего пользователя
 }

 function menu_config()
 { if (_CUR_ROOT_DIR_NAME=='cab') parent::menu_config() ;
   elseif ($this->rec_member['pkey'])
   { $this->menu[$this->rec_member['__href_window']]=array('title'=>'Карточка персонала "'.$this->rec_member['obj_name'].'"','class'=>'fa fa-user-md') ;
     $this->menu[$this->rec_member['__href_window'].'sign/']=array('title'=>'Подпись') ;
     $this->menu[$this->rec_member['__href_window'].'pass/']=array('title'=>'Смена пароля','class'=>'fa fa-user-md') ;
     $this->menu[$this->rec_member['__href_window'].'sert/']=array('title'=>'Сертификат','class'=>'fa fa-user-md') ;
     $this->menu[$this->rec_member['__href_window'].'personals/']=array('title'=>'Сотрудники аккаунта') ;
     $this->menu[$this->rec_member['__href_window'].'personals_set/']=array('title'=>'Фильтр сотрудников') ;
     $this->menu[$this->rec_member['__href_window'].'auth/']=array('title'=>'История авторизаций','class'=>'fa fa-user-md') ;
     $this->menu[$this->rec_member['__href_window'].'change/']=array('title'=>'История изменений','class'=>'fa fa-user-md') ;
     $this->menu[$this->rec_member['__href_window'].'db/']=array('title'=>'Дамп записи','class'=>'fa fa-user-md') ;
   }
 }

 function second_menu_config()
 { if ($this->rec_member['pkey'] and _CUR_ROOT_DIR_NAME=='cab')
   { $this->second_menu[$this->rec_member['__href']]=array('title'=>'Карточка персонала "'.$this->rec_member['obj_name'].'"','class'=>'fa fa-user-md') ;
     $this->second_menu[$this->rec_member['__href'].'sign/']=array('title'=>'Подпись') ;
     $this->second_menu[$this->rec_member['__href'].'pass/']=array('title'=>'Смена пароля','class'=>'fa fa-user-md') ;
     $this->second_menu[$this->rec_member['__href'].'sert/']=array('title'=>'Сертификат','class'=>'fa fa-user-md') ;
     $this->second_menu[$this->rec_member['__href'].'personals/']=array('title'=>'Сотрудники аккаунта','_class'=>'fa fa-user-md') ;
     $this->second_menu[$this->rec_member['__href'].'personals_set/']=array('title'=>'Фильтр сотрудников') ;
     $this->second_menu[$this->rec_member['__href'].'auth/']=array('title'=>'История авторизаций','class'=>'fa fa-user-md') ;
     $this->second_menu[$this->rec_member['__href'].'change/']=array('title'=>'История изменений','class'=>'fa fa-user-md') ;
     $this->second_menu[$this->rec_member['__href'].'db/']=array('title'=>'Дамп записи','class'=>'fa fa-user-md') ;
   }
 }

 function block_main()
 { $this->page_title() ;
   $this->panel_member_edit() ;
 }

  function panel_member_edit()
    {  ACCOUNTS()->update_member_info(MEMBER()) ;
        $rec=$this->rec_member ;//  damp_array($this->rec_member,1,-1) ; //damp_array($_SESSION['member']) ;
        $arr_groups=ACCOUNTS()->get_arr_tree_workgroups() ;
       ?><div id="panel_member_edit"><form enctype="multipart/form-data">
        <table class="info">
         <?if (MEMBER()->id!=$rec['pkey']){?><tr><td>Состояние:</td><td><input type="hidden" name=member_edit[enabled] value="0"> <input type=checkbox name=member_edit[enabled] value="1" <?if ($this->rec_member['enabled']) echo 'checked'?>></td></tr><?}?>
         <tr><td>Краткое ФИО:</td><td><input data-required type="text" name="member_edit[obj_name]" class="big" value="<?echo htmlspecialchars($rec['obj_name'])?>"></td></tr>
         <tr><td>Полное ФИО:</td><td><input data-required type="text" name="member_edit[contact_name]" class="big" style="width:1000px;" value="<?echo htmlspecialchars($rec['contact_name'])?>"></td></tr>
         <tr><td>Роль:</td><td><?if (MEMBER()->id!=$rec['pkey']){?>
                                   <select  name=member_edit[rol] data-required data-select><option value=""></option>
                                      <?if (_sizeof($_SESSION['ARR_roll'])) foreach($_SESSION['ARR_roll'] as $id=>$rec_rol){?><option value="<?echo $id?>" <?if ($id==$rec['rol']) echo ' selected'?>><?echo $rec_rol['obj_name']?></option><?}?>
                                   </select><?}else echo $rec['_rol_name']?></td></tr>
         <tr><td>Группа:</td><td><?if (MEMBER()->id!=$rec['pkey'] or MEMBER()->rol==1){?>
                                   <select  name=member_edit[parent] data-required data-select><option value=""></option>
                                      <?if (_sizeof($arr_groups)) foreach($arr_groups as $id=>$name){?><option value="<?echo $id?>" <?if ($id==$rec['parent']) echo ' selected'?>><?echo $name?></option><?}?>
                                   </select><?}else echo $rec['_group_name']?></td></tr>
         <tr><td>Организация:</td><td><input type="text" name="member_edit[org_name]" class="big" value="<?echo htmlspecialchars($rec['org_name'])?>"></td></tr>
         <tr><td>Должность:</td><td><input type="text" name="member_edit[working]" class="big" value="<?echo htmlspecialchars($rec['working'])?>"></td></tr>
        <??> <tr><td>Язык интерфейса:</td><td>
              <?
                $recs_locals=execSQL('select * from obj_site_locals where clss in (1) and enabled=1 and parent>0') ;
                 $cur_local_id=$this->rec_member['_ext_info']['local'] ;
                 ?><select name="member_edit[ext_info][local]"><option value="0">Русский</option><?
                 if (_sizeof($recs_locals)) foreach($recs_locals as $rec_local)
                 {  ?><option value="<?echo $rec_local['pkey']?>" <?if($cur_local_id==$rec_local['pkey']) echo 'selected'?>><?echo $rec_local['obj_name']?></option><?
                 }
                 ?></select><?
              ?>
             </td></tr><??>
         <tr><td>Контактный email:</td><td><input type="text" name="member_edit[email]" class="big" value="<?echo htmlspecialchars($rec['email'])?>"></td></tr>
         <tr><td>Контактный телефон:</td><td><input type="text" name="member_edit[phone]" class="big" value="<?echo htmlspecialchars($rec['phone'])?>"></td></tr>
         <tr><td>Табельный номер (UID):</td><td><input type="text" name="member_edit[UID]" class="big" value="<?echo htmlspecialchars($rec['UID'])?>"></td></tr>
         <tr><td>Логин:</td><td><input data-required class=login type=text name=member_edit[login] value="<?echo addslashes($rec['login'])?>"></td></tr>
         <tr><td>Пароль:</td><td>********</td></tr>
         <tr><td>СУПЕРЧИТАТЕЛЬ:</td>
             <td>
               <?if (MEMBER()->rol==1 and $rec['pkey']!=MEMBER()->id){?><input type="hidden" name=member_edit[ext_info][superreader] value="0"> <input type=checkbox name=member_edit[ext_info][superreader] value="1" <?echo ($rec['_ext_info']['superreader'])? 'checked':''?>><?}
                else echo ($rec['_ext_info']['superreader'])? 'ДА':'-' ;
              ?>
           </td></tr>
         <?if ($rec['rol']==5){?><tr><td>Серия путевого листа:</td><td><input class=login type=text name=member_edit[ext_info][seria_PL] value="<?echo addslashes($rec['_ext_info']['seria_PL'])?>"></td></tr><?}?>
         <?if ($rec['rol']==7){?><tr><td>Серия счетов:</td><td><input data-required class=login type=text name=member_edit[ext_info][seria_INV] value="<?echo addslashes($rec['_ext_info']['seria_INV'])?>"></td></tr><?}?>
         <?if ($rec['rol']==7){?><tr><td>Серия актов:</td><td><input data-required class=login type=text name=member_edit[ext_info][seria_AKT] value="<?echo addslashes($rec['_ext_info']['seria_AKT'])?>"></td></tr><?}?>
         <tr><td>Подпись сотрудника:</td>
             <td><img src="<?echo img_clone($this->rec_member['signature'],'small')?>"><br>
                 <input name="upload_sugnature[]" type="file" size="50"><br>Формат GIF, прозрачный фон, рекомендованный размер 300x150px</td>
            </tr>

         <tr><td></td><td><input type="submit" class=v1 cmd="save_member" value="Сохранить" validate="form"></td></tr>
       </table>
       </form></div>
       <div class="alert">Внимание! При изменении роли или языка интерфейса сотруднику необходимо перелогиниться для принятия изменений</div>
        <?
      //damp_array(MEMBER());
    }

  function CMD_save_member()
  { //damp_array($_POST['member_edit']) ;
    $res=ACCOUNTS()->member_save($this->rec_member['pkey'],$_POST['member_edit'],array('save_log'=>1)) ;

    if (_sizeof($_FILES))
     { // загрузка печати
       $recs_files=conver_FILES_to_recs($_FILES['upload_file']) ;  //damp_array($recs_files) ;
       if (_sizeof($recs_files[0]))
         { if ($recs_files[0]['type']=='image/gif')
           { ?><h2>Загружаем изображение из файла "<?echo $recs_files[0]['name']?>"</h2><?
             include_once(_DIR_TO_ENGINE.'/admin/i_admin_system.php') ;
             execSQL_update('delete from obj_site_account_image where parent='.$this->rec_member['pkey'].' and alt="stamp"') ;
             $res_upload=obj_upload_image($this->rec_member['_reffer'],$recs_files[0],array('view_upload'=>1,'img_alt'=>'stamp')) ;
             //damp_array($res_upload) ;
             $res_upload=array('type'=>'success','no_default_panel'=>1) ;
             $res='ok' ;
           }
           else $res_upload=array('type'=>'error','code'=>'failed_type','text'=>'Неверный тип файла. Загрузите файл в формате GIF.') ;
         }

       // загрузка подписи
       $recs_files=conver_FILES_to_recs($_FILES['upload_sugnature']) ;  //damp_array($recs_files) ;
       if (_sizeof($recs_files[0]))
         { if ($recs_files[0]['type']=='image/gif')
           { ?><h2>Загружаем изображение из файла "<?echo $recs_files[0]['name']?>"</h2><?
             include_once(_DIR_TO_ENGINE.'/admin/i_admin_system.php') ;
             execSQL_update('delete from obj_site_account_image where parent='.$this->rec_member['pkey'].' and alt="signature"') ;
             $res_upload=obj_upload_image($this->rec_member['_reffer'],$recs_files[0],array('view_upload'=>1,'img_alt'=>'signature')) ;
             //damp_array($res_upload) ;
             $res_upload=array('type'=>'success','no_default_panel'=>1) ;
             $res='ok' ;
           }
           else $res_upload=array('type'=>'error','code'=>'failed_type','text'=>'Неверный тип файла. Загрузите файл в формате GIF.') ;
         }

       // загрузка сертификата
         $recs_files=conver_FILES_to_recs($_FILES['upload_CRT']) ; // damp_array($recs_files) ;
         if (_sizeof($recs_files[0]))
           { if ($recs_files[0]['type']=='application/x-x509-ca-cert')
             { ?><h2>Загружаем сертификат из файла "<?echo $recs_files[0]['name']?>"</h2><?
               include_once(_DIR_TO_ENGINE.'/admin/i_admin_system.php') ;
               execSQL_update('delete from obj_site_account_files where parent='.$this->rec_member['pkey'].' and obj_name="root_sertificat"') ;
               obj_upload_file($this->rec_member['_reffer'],$recs_files[0],array('view_upload'=>1,'new_obj_rec'=>array('obj_name'=>'root_sertificat'))) ;
               //damp_array($res_upload) ;
               $res_upload=array('type'=>'success','no_default_panel'=>1) ;
               $res='ok' ;
             }
             else $res_upload=array('type'=>'error','code'=>'failed_type','text'=>'Выбранный Вами файл не является сертификатом') ;
           }



       //else $res_upload=array('type'=>'error','code'=>'not_uploaded_file','text'=>'Не был выбран файл') ;
       if ($res_upload['type']=='error') echo '<div class="alert">'.$res_upload['text'].'</div>' ;
     }
     //else $res_upload=array('type'=>'error','code'=>'not_uploaded_file','text'=>'Не был выбран файл') ;

    //damp_array($_FILES) ;
    if (!$res) echo '<div class="red">Изменений не обнаружено</div>' ;
    else { echo '<div class="green">Данные сохранены</div>' ;
           $this->rec_member=ACCOUNTS()->get_member_by_id($this->rec_member['pkey']) ;
         }
  }


}



?>
