<?php // этот модуль необходимо делать тольео для крупных проектов, для небольшых описание пунктов меню и файлов меню лучше делать в init.php
$__functions['init'][]      ='_accounts_admin_vars' ;
$__functions['boot_admin'][] ='_accounts_admin_boot' ;
//$__functions['alert'][]   ='_accounts_admin_alert' ;
$__functions['install'][]	='_accounts_install_modul' ;



function _accounts_admin_vars() //
{   //-----------------------------------------------------------------------------------------------------------------------------
	// описание переменных модуля
	//-----------------------------------------------------------------------------------------------------------------------------
    $_SESSION['menu_admin_site']['Модули']['account']=array('name'=>'Аккаунты','href'=>'editor_account.php','icon'=>'menu/group.jpg') ;
    //$_SESSION['menu_admin_site']['Модули'][]=array('name'=>'Аккаунты','href'=>'editor_account.php') ;

	//-----------------------------------------------------------------------------------------------------------------------------
	// описание классов модуля
	//-----------------------------------------------------------------------------------------------------------------------------

    // запись в таблице аккаунтов
    $_SESSION['descr_clss'][86]['name']='Аккаунт' ;
    $_SESSION['descr_clss'][86]['parent']=0 ;
    $_SESSION['descr_clss'][86]['fields']=array('login'=>'varchar(255)','password'=>'varchar(255)','email'=>'varchar(255)','act_code'=>'varchar(255)','reffer'=>'any_object','info'=>'serialize') ;
    $_SESSION['descr_clss'][86]['fields']['ext_info']='serialize';
    $_SESSION['descr_clss'][86]['on_change_event']['password']='on_change_event_clss_86_password' ;
    $_SESSION['descr_clss'][86]['child_to']=array(1) ;
    $_SESSION['descr_clss'][86]['parent_to']=array() ;
    $_SESSION['descr_clss'][86]['no_show_in_tree']=1 ; // не показывать объекты класса в дереве

    // Задание
    $_SESSION['descr_clss'][200]['name']='Девайс' ;
    $_SESSION['descr_clss'][200]['parent']=86 ;
    //$_SESSION['descr_clss'][200]['on_change_event']['status']='clss_200_on_change_event_status' ;
    $_SESSION['descr_clss'][200]['fields']['obj_name']=array('type'=>'varchar(100)') ;
    $_SESSION['descr_clss'][200]['fields']['num']=array('type'=>'varchar(100)') ;
    //$_SESSION['descr_clss'][200]['fields']['XMPP_login']=array('type'=>'varchar(32)') ;
    //$_SESSION['descr_clss'][200]['fields']['XMPP_password']=array('type'=>'varchar(32)') ;
    $_SESSION['descr_clss'][200]['fields']['ip']=array('type'=>'varchar(64)') ;
    $_SESSION['descr_clss'][200]['fields']['reboot']=array('type'=>'int(1)') ; // флаг что при следующей загрузке была перегазгужена сессия
    $_SESSION['descr_clss'][200]['no_show_in_tree']=0 ; // не показывать объекты класса в дереве

    //$_SESSION['descr_clss'][200]['child_to']=array(1) ;
    //$_SESSION['descr_clss'][200]['parent_to']=array() ;

    $_SESSION['descr_clss'][200]['list']['field']['pkey']=array('title'=>'id') ;
    $_SESSION['descr_clss'][200]['list']['field']['obj_name']=array('title'=>'Название') ;
    $_SESSION['descr_clss'][200]['list']['field']['ip']=array('title'=>'IP:порт') ;
    //$_SESSION['descr_clss'][200]['list']['field']['XMPP_login']=array('title'=>'Логин') ;
    //$_SESSION['descr_clss'][200]['list']['field']['XMPP_password']=array('title'=>'Пароль') ;
    //$_SESSION['descr_clss'][200]['list']['options']['buttons']=array(array('title'=>'Зарегистрировать новое устройтво','cmd'=>'aspmo.get_panel_register_device','class'=>'v2'),'save','delete','copy') ; // добавлена кнопка с расширенным описанием

    $_SESSION['descr_clss'][200]['details']=$_SESSION['descr_clss'][200]['list'] ;

    $_SESSION['descr_clss'][205]=array() ;
    $_SESSION['descr_clss'][205]['name']='АРМ' ;
    $_SESSION['descr_clss'][205]['parent']=200 ;
    $_SESSION['descr_clss'][205]['child_to']=array(206) ;
    $_SESSION['descr_clss'][205]['options']['create_by_list_default_name']='ip' ;

    $_SESSION['descr_clss'][205]['fields']['stream_name_1']=array('type'=>'varchar(32)') ;
    $_SESSION['descr_clss'][205]['fields']['stream_name_2']=array('type'=>'varchar(32)') ;


    //$_SESSION['descr_clss'][205]['list']['field']['id']=array('title'=>'id') ;
    $_SESSION['descr_clss'][205]['list']['field']['pkey']=array('title'=>'ID') ;
    $_SESSION['descr_clss'][205]['list']['field']['obj_name']=array('title'=>'Название') ;
    $_SESSION['descr_clss'][205]['list']['field']['ip']=array('title'=>'IP') ;
    $_SESSION['descr_clss'][205]['list']['field']['stream_name_1']=array('title'=>'stream 1') ;
    $_SESSION['descr_clss'][205]['list']['field']['stream_name_2']=array('title'=>'stream 2') ;
    
    
    $_SESSION['descr_clss'][205]['list']['options']=array('no_icon_edit'=>1);

    //$_SESSION['descr_clss'][205]['list']=$_SESSION['descr_clss'][200]['list'] ;
    //$_SESSION['descr_clss'][205]['details']=$_SESSION['descr_clss'][200]['list'] ;

    //-----------------------------------------------------------------------------------------------------
    $_SESSION['descr_clss'][206]=array() ;
    $_SESSION['descr_clss'][206]['name']='Рабочая группа' ;
    $_SESSION['descr_clss'][206]['parent']=1 ;

    $_SESSION['descr_clss'][206]['parent_to']=array(206,205,203,210,601,602,603) ;
    $_SESSION['descr_clss'][206]['child_to']=array(1,206) ;
    $_SESSION['descr_clss'][206]['fields']['settings']=array('type'=>'serialize') ;
    $_SESSION['descr_clss'][206]['fields']['med_org_id']=array('type'=>'int(11)','index'=>'med_org_id') ;
    $_SESSION['descr_clss'][206]['fields']['level']=array('type'=>'int(11)','index'=>'level') ;
    $_SESSION['descr_clss'][206]['fields']['parent1']=array('type'=>'int(11)','index'=>'parent1','default'=>0) ;
    $_SESSION['descr_clss'][206]['fields']['parent2']=array('type'=>'int(11)','index'=>'parent2','default'=>0) ;
    $_SESSION['descr_clss'][206]['fields']['parent3']=array('type'=>'int(11)','index'=>'parent3','default'=>0) ;
    $_SESSION['descr_clss'][206]['fields']['parent4']=array('type'=>'int(11)','index'=>'parent4','default'=>0) ;
    $_SESSION['descr_clss'][206]['fields']['parent5']=array('type'=>'int(11)','index'=>'parent5','default'=>0) ;
    $_SESSION['descr_clss'][206]['fields']['parent6']=array('type'=>'int(11)','index'=>'parent6','default'=>0) ;
    $_SESSION['descr_clss'][206]['fields']['parent7']=array('type'=>'int(11)','index'=>'parent7','default'=>0) ;
    $_SESSION['descr_clss'][206]['fields']['parent8']=array('type'=>'int(11)','index'=>'parent8','default'=>0) ;
    $_SESSION['descr_clss'][206]['fields']['parent9']=array('type'=>'int(11)','index'=>'parent9','default'=>0) ;

    $_SESSION['descr_clss'][206]['list']['field']['pkey']=array('title'=>'id') ;
    $_SESSION['descr_clss'][206]['list']['field']['obj_name']=array('title'=>'Название') ;
    //$_SESSION['descr_clss'][206]['list']['field']['ip']=array('title'=>'IP') ;
    //$_SESSION['descr_clss'][206]['list']['options']=array('no_icon_edit'=>1);

    //-----------------------------------------------------------------------------------------------------
    $_SESSION['descr_clss'][210]=array() ;
    $_SESSION['descr_clss'][210]['name']='Аккакунт' ;
   	$_SESSION['descr_clss'][210]['parent']=86 ;
   	$_SESSION['descr_clss'][210]['fields']=array('obj_name'=>'varchar(255)') ;
   	$_SESSION['descr_clss'][210]['fields']['obj_name']='varchar(255)';
   	$_SESSION['descr_clss'][210]['fields']['rol']=array('type'=>'indx_select','array'=>'ARR_roll') ;
    $_SESSION['descr_clss'][210]['fields']['ip']=array('type'=>'varchar(64)') ;
    $_SESSION['descr_clss'][210]['fields']['arm_id']=array('type'=>'int(11)') ;
    $_SESSION['descr_clss'][210]['fields']['group_id']=array('type'=>'int(11)') ;     // id группы, в которой сейчас зарегистрирован персонал (по IP АРМ)
    $_SESSION['descr_clss'][210]['fields']['sess_id']=array('type'=>'varchar(32)') ;
    $_SESSION['descr_clss'][210]['fields']['time_login']=array('type'=>'timedata','data_format'=>'d.m.y G:i','default'=>0) ;
    $_SESSION['descr_clss'][210]['fields']['time_logout']=array('type'=>'timedata','data_format'=>'d.m.y G:i','default'=>0) ;
    $_SESSION['descr_clss'][210]['fields']['smena_in']=array('type'=>'timedata','data_format'=>'d.m.y G:i','default'=>0) ;
    $_SESSION['descr_clss'][210]['fields']['smena_out']=array('type'=>'timedata','data_format'=>'d.m.y G:i','default'=>0) ;
    $_SESSION['descr_clss'][210]['fields']['smena']=array('type'=>'indx_select','array'=>'ARR_smena_status') ; // активная смена
    $_SESSION['descr_clss'][210]['fields']['smena']=array('type'=>'int(11)') ; // активная смена
    //$_SESSION['descr_clss'][210]['fields']['sert_id']=array('type'=>'varchar(128)','default'=>'') ;
    //$_SESSION['descr_clss'][210]['fields']['sert_name']=array('type'=>'varchar(128)') ;
    //$_SESSION['descr_clss'][210]['fields']['sert_ud']=array('type'=>'varchar(128)') ; // удостоверяющий центр
    //$_SESSION['descr_clss'][210]['fields']['sert_from']=array('type'=>'timedata','data_format'=>'d.m.y G:i','default'=>0) ; // срок дейтствия ЭЦП
    //$_SESSION['descr_clss'][210]['fields']['sert_to']=array('type'=>'timedata','data_format'=>'d.m.y G:i','default'=>0) ; // срок дейтствия ЭЦП
    $_SESSION['descr_clss'][210]['fields']['access_pass']=array('type'=>'varchar(32)') ;
    $_SESSION['descr_clss'][210]['fields']['email']=array('type'=>'varchar(128)') ;
    $_SESSION['descr_clss'][210]['fields']['phone']=array('type'=>'varchar(128)') ;
    $_SESSION['descr_clss'][210]['fields']['contact_name']=array('type'=>'varchar(128)') ;
    $_SESSION['descr_clss'][210]['fields']['org_name']=array('type'=>'varchar(128)') ;
    $_SESSION['descr_clss'][210]['fields']['working']=array('type'=>'varchar(128)') ;
    //$_SESSION['descr_clss'][210]['fields']['monitor']=array('type'=>'indx_select','array'=>'ARR_monitor') ;
    $_SESSION['descr_clss'][210]['fields']['UID']=array('type'=>'varchar(40)') ;
    $_SESSION['descr_clss'][210]['fields']['evulate_ECP']=array('type'=>'int(1)','default'=>1) ;
    $_SESSION['descr_clss'][210]['fields']['timezone']=array('type'=>'varchar(50)','default'=>32) ;
    $_SESSION['descr_clss'][210]['fields']['debug_sql']=array('type'=>'int(1)','default'=>0) ;
    $_SESSION['descr_clss'][210]['fields']['_image_name']=array('type'=>'varchar(50)','default'=>'') ;


    $_SESSION['descr_clss'][210]['child_to']=array(206,1) ;
    $_SESSION['descr_clss'][210]['parent_to']=array(3,5) ;
    $_SESSION['descr_clss'][210]['no_show_in_tree']=0 ; // не показывать объекты класса в дереве
    $_SESSION['descr_clss'][210]['icons']=_PATH_TO_BASED_CLSS_IMG.'/98.png' ;
   	$_SESSION['descr_clss'][210]['list']['field']['pkey']			=array('title'=>'ID') ;
   	$_SESSION['descr_clss'][210]['list']['field']['enabled']			=array('title'=>'Сост.') ;
   	$_SESSION['descr_clss'][210]['list']['field']['obj_name']			=array('title'=>'ФИО','td_class'=>'left') ;
   	$_SESSION['descr_clss'][210]['list']['field']['rol']		 	    =array('title'=>'Роль','td_class'=>'left') ;
   	$_SESSION['descr_clss'][210]['list']['field']['login']				=array('title'=>'Логин') ;
    $_SESSION['descr_clss'][210]['list']['field']['password']			=array('title'=>'Пароль') ;
    $_SESSION['descr_clss'][210]['list']['field']['ip']			        =array('title'=>'Текущий сеанс','readonly'=>1) ;
    $_SESSION['descr_clss'][210]['list']['field']['UID']			    =array('title'=>'UID') ;
    $_SESSION['descr_clss'][210]['list']['field']['monitor']			=array('title'=>'Режим монитора') ;
    $_SESSION['descr_clss'][210]['list']['field']['time_login']			=array('title'=>'Вход','readonly'=>1) ;
    $_SESSION['descr_clss'][210]['list']['field']['time_logout']		=array('title'=>'Выход','readonly'=>1) ;
    $_SESSION['descr_clss'][210]['list']['field']['smena_in']			=array('title'=>'Начало смены','readonly'=>1) ;
    $_SESSION['descr_clss'][210]['list']['field']['smena_out']		    =array('title'=>'Окончание смены','readonly'=>1) ;
    $_SESSION['descr_clss'][210]['list']['field']['smena']		        =array('title'=>'Активная смена') ;
    $_SESSION['descr_clss'][210]['list']['field']['group_id']		    =array('title'=>'Рабочая группа') ;

    $_SESSION['descr_clss'][210]['details']=$_SESSION['descr_clss'][210]['list'] ;

    

}

function _accounts_admin_boot($options)
{
    create_system_modul_obj('accounts',$options) ;

}


function _accounts_install_modul($DOT_root)
{
    SETUP()->reg_report('Служебные отчеты','aspmo_setting/report_permissions.php') ;         //
    SETUP()->reg_report('Служебные отчеты','aspmo_setting/report_members.php') ;

    // аккаунты ЭСМО
    $pattern=array() ;
    $pattern['table_title']		        = 	'Аккаунты' ;
    $pattern['use_clss']		        = 	'1,203,205,206,210' ;
    $pattern['def_recs'][]		        =	array ('parent'=>0,	'clss'=>1,	'enabled'=>1,	'indx'=>0,	'obj_name'=>'ЭСМО') ;
    $pattern['table_name']				= 	'obj_site_account' ;
    $pattern['table_clss']				= 	101 ;
    $pattern['table_parent']			= 	$DOT_root;
    $id_DOT_account=create_table_by_pattern($pattern) ;

    // аккаунты ЛК - фото
    $pattern=array() ;
    $pattern['table_title']				= 	'Фото' ;
    $pattern['use_clss']				= 	'3' ;
    $pattern['table_name']				= 	'obj_site_account_image' ;
    $pattern['table_clss']				= 	101 ;
    $pattern['table_parent']			= 	$id_DOT_account;
    $pattern['dir_to_image']			= 	'public/account/';
    $id_img=create_table_by_pattern($pattern) ;

    SETUP()->check_obj_table_image_dir($id_img) ;  // проверяем папки изображений
	SETUP()->check_obj_table_file_dir($id_img) ;   // проверяем папки файлов


    // аккаунты ЛК - файлы
    $pattern=array() ;
    $pattern['table_title']				= 	'Файлы' ;
    $pattern['use_clss']				= 	'5' ;
    $pattern['table_name']				= 	'obj_site_account_files' ;
    $pattern['table_clss']				= 	101 ;
    $pattern['table_parent']			= 	$id_DOT_account;
    $pattern['dir_to_image']			= 	'/public/account/';
    $id_img=create_table_by_pattern($pattern) ;

	SETUP()->check_obj_table_image_dir($id_img) ;  // проверяем папки изображений
	SETUP()->check_obj_table_file_dir($id_img) ;   // проверяем папки файлов


    $id_sect=SETUP_add_section_to_site_setting('Авторизация') ;
    SETUP_add_rec_to_site_setting($id_sect,"Разрешить простые пароли",1,'LS_use_simple_pass',array('el'=>'checkbox')) ;
    SETUP_add_rec_to_site_setting($id_sect,"Авторизация по паролю",1,'LS_auth_by_pass',array('el'=>'checkbox')) ;
    //SETUP_add_rec_to_site_setting($id_sect,"Авторизация по Рутокен ЭЦП",0,'LS_auth_by_token') ;
    SETUP_add_rec_to_site_setting($id_sect,"Сохранять в логе пароль при неудачной авторизации",1,'LS_account_save_open_pass',array('el'=>'checkbox')) ;
    SETUP_add_rec_to_site_setting($id_sect,"Число попыток авторизации",3,'LS_account_autorize_limit',array('el'=>'')) ;
    SETUP_add_rec_to_site_setting($id_sect,"Время попыток авторизации",30,'LS_account_autorize_time',array('human'=>'min')) ;
    SETUP_add_rec_to_site_setting($id_sect,"Разрешить выбор группы фельдшеру",0,'LS_select_group_to_account',array('el'=>'checkbox','descr'=>'При авторизации фельдшеру, находящего в корневой группе, будет предложено выбрать одну из вложенных групп')) ;
	SETUP_add_rec_to_site_setting($id_sect,"Наименования групп ЭСМО",'Группа','LS_person_group_title') ;  // группа или АТП

    $sql='create view view_account as
                select  t1.*,
                        t3.file_name as _signature_file_name,
                        t4.file_name as _stamp_file_name,
                        t5.pkey as _arm_id,
                        t5.obj_name as _arm_name
                from obj_site_account t1
                left join obj_site_account_files t2 on t2.parent=t1.pkey and t2.obj_name="root_sertificat"
                left join obj_site_account_image t3 on t3.parent=t1.pkey and t3.alt="signature"
                left join obj_site_account_image t4 on t4.parent=t1.pkey and t4.alt="stamp"
                left join obj_site_account t5 on t5.ip=t1.ip and t5.clss=205
                ' ;
	create_viewer('view_account',$sql) ;

 $sql='select  t1.pkey,t1.clss,t1.enabled,t1.indx,t1.obj_name,t1.parent,t1.c_data,t1.r_data,t1.tkey,t1.settings,t1.timezone,t1.last_mo_event,t1.zone_id,t1.level,t1.parent1,t1.parent2,t1.parent3,t1.parent4,t1.parent5,t1.parent6,t1.parent7,t1.parent8,t1.parent9,
       t2.obj_name as viewer_name,t2.ext_info as filter_set
       from obj_site_account t1
       left join personals_filter t2 on t2.reffer=concat(t1.pkey,".",t1.tkey)
       where t1.clss=206
      ' ;
	create_viewer('view_workgroups',$sql) ;

	// испрользуется для обращений типа _IL('ARR_accounts')->recs
    $sql='create view view_ARR_accounts as
                         select  t1.pkey as _id,t1.*
                         from view_account t1 
                         where t1.clss=210 and t1.enabled=1 
                         order by t1.id,t1.pkey
                         ' ;
	create_viewer('view_ARR_accounts',$sql) ;

    if ($GLOBALS['install_options']['viewers'])
    { ACCOUNTS()->update_workgroups_viewers() ;
      TREE('obj_site_account')->update_levels_parents() ;
    }


}

function get_pass_hash($login,$pass)
{
   return(hash('sha256',$login.$pass)) ;
}





?>
