Update for tt_users created field.
[timetracker.git] / admin_team_add.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('initialize.php');
30 import('form.Form');
31 import('ttUserHelper');
32 import('ttRoleHelper');
33
34 // Access check.
35 if (!ttAccessAllowed('administer_site')) {
36   header('Location: access_denied.php');
37   exit();
38 }
39
40 if ($request->isPost()) {
41   $cl_team_name = trim($request->getParameter('team_name'));
42   $cl_lang = $request->getParameter('lang');
43   $cl_manager_name = trim($request->getParameter('manager_name'));
44   $cl_manager_login = trim($request->getParameter('manager_login'));
45   if (!$auth->isPasswordExternal()) {
46     $cl_password1 = $request->getParameter('password1');
47     $cl_password2 = $request->getParameter('password2');
48   }
49   $cl_manager_email = trim($request->getParameter('manager_email'));
50 } else
51   $cl_lang = $i18n->lang; // Browser setting from initialize.php.
52
53 $form = new Form('teamForm');
54 $form->addInput(array('type'=>'text','maxlength'=>'200','name'=>'team_name','value'=>$cl_team_name));
55
56 // Prepare an array of available languages.
57 $lang_files = I18n::getLangFileList();
58 foreach ($lang_files as $lfile) {
59   $content = file(RESOURCE_DIR."/".$lfile);
60   $lname = '';
61   foreach ($content as $line) {
62     if (strstr($line, 'i18n_language')) {
63       $a = explode('=', $line);
64       $lname = trim(str_replace(';','',str_replace("'","",$a[1])));
65       break;
66     }
67   }
68   unset($content);
69   $longname_lang[] = array('id'=>I18n::getLangFromFilename($lfile),'name'=>$lname);
70 }
71 $longname_lang = mu_sort($longname_lang, 'name');
72 $form->addInput(array('type'=>'combobox','name'=>'lang','style'=>'width: 200px','data'=>$longname_lang,'datakeys'=>array('id','name'),'value'=>$cl_lang));
73
74 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_name','value'=>$cl_manager_name));
75 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_login','value'=>$cl_manager_login));
76 if (!$auth->isPasswordExternal()) {
77   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password1','value'=>$cl_password1));
78   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password2','value'=>$cl_password2));
79 }
80 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_email','value'=>$cl_manager_email));
81 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.submit')));
82
83 if ($request->isPost()) {
84   /*
85    * Note: creating a group by admin is pretty much the same as self-registration,
86    * except that created_by gields for group and user must be set to admin account.
87    * Therefore, we'll reuse ttRegistrator instance to create a group here
88    * and override created_by fields using ttRegistrator::setCreatedBy() function.
89    */
90
91   // Create fields array for ttRegistrator instance.
92   if (!defined('CURRENCY_DEFAULT')) define('CURRENCY_DEFAULT', '$');
93   $fields = array(
94     'user_name' => $cl_manager_name,
95     'login' => $cl_manager_login,
96     'password1' => $cl_password1,
97     'password2' => $cl_password2,
98     'email' => $cl_manager_email,
99     'group_name' => $cl_team_name,
100     'currency' => CURRENCY_DEFAULT,
101     'lang' => $cl_lang);
102
103   // Create an instance of ttRegistrator class.
104   import('ttRegistrator');
105   $registrator = new ttRegistrator($fields, $err);
106   $registrator->register();
107   $registrator->setCreatedBy($user->id); // Override created_by to admin account.
108   if ($err->no()) {
109     header('Location: admin_teams.php');
110     exit();
111   }
112 } // isPost
113
114 $smarty->assign('auth_external', $auth->isPasswordExternal());
115 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
116 $smarty->assign('onload', 'onLoad="document.teamForm.team.focus()"');
117 $smarty->assign('content_page_name', 'admin_team_add.tpl');
118 $smarty->assign('title', $i18n->getKey('title.create_team'));
119 $smarty->display('index.tpl');