Fixed creating and editing teams by admin - broken during roles revamp.
[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   // Validate user input.
85   if (!ttValidString($cl_team_name, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.team_name'));
86   if (!ttValidString($cl_manager_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_name'));
87   if (!ttValidString($cl_manager_login)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_login'));
88   if (!$auth->isPasswordExternal()) {
89     if (!ttValidString($cl_password1)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
90     if (!ttValidString($cl_password2)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
91     if ($cl_password1 !== $cl_password2)
92       $err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
93   }
94   if (!ttValidEmail($cl_manager_email, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
95
96   if ($err->no()) {
97     if (!ttUserHelper::getUserByLogin($cl_manager_login)) {
98       // Create a new team.
99       if (!defined('CURRENCY_DEFAULT')) define('CURRENCY_DEFAULT', '$');
100       $team_id = ttTeamHelper::insert(array('name'=>$cl_team_name,'currency'=>CURRENCY_DEFAULT,'lang'=>$cl_lang));
101       if ($team_id) {
102         if (!ttRoleHelper::createPredefinedRoles($team_id, $cl_lang))
103           $err->add($i18n->getKey('error.db'));
104
105         $role_id = ttRoleHelper::getTopManagerRoleID();
106
107         // Team created, now create a team manager.
108         $user_id = ttUserHelper::insert(array(
109           'team_id' => $team_id,
110           'role_id' => $role_id,
111           'name' => $cl_manager_name,
112           'login' => $cl_manager_login,
113           'password' => $cl_password1,
114           'email' => $cl_manager_email));
115       }
116       if ($team_id && $user_id) {
117         header('Location: admin_teams.php');
118       } else
119         $err->add($i18n->getKey('error.db'));
120     } else
121       $err->add($i18n->getKey('error.user_exists'));
122   }
123 } // isPost
124
125 $smarty->assign('auth_external', $auth->isPasswordExternal());
126 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
127 $smarty->assign('onload', 'onLoad="document.teamForm.team.focus()"');
128 $smarty->assign('content_page_name', 'admin_team_add.tpl');
129 $smarty->assign('title', $i18n->getKey('title.create_team'));
130 $smarty->display('index.tpl');