Renamed errors to err to keep things shorter
[timetracker.git] / admin_team_edit.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('ttTeamHelper');
33
34 // Access check.
35 if (!ttAccessCheck(right_administer_site)) {
36   header('Location: access_denied.php');
37   exit();
38 }
39
40 $team_id = $request->getParameter('id');
41 $team_details = ttTeamHelper::getTeamDetails($team_id); 
42
43 if ($request->isPost()) {
44   $cl_team_name = trim($request->getParameter('team_name'));
45   $cl_manager_name = trim($request->getParameter('manager_name'));
46   $cl_manager_login = trim($request->getParameter('manager_login'));
47   if (!$auth->isPasswordExternal()) {
48     $cl_password1 = $request->getParameter('password1');
49     $cl_password2 = $request->getParameter('password2');
50   }
51   $cl_manager_email = trim($request->getParameter('manager_email'));
52 } else {
53   $cl_team_name = $team_details['team_name'];
54   $cl_manager_name = $team_details['manager_name'];
55   $cl_manager_login = $team_details['manager_login'];
56   if (!$auth->isPasswordExternal()) {
57     $cl_password1 = $cl_password2 = '';
58   }
59   $cl_manager_email = $team_details['manager_email'];
60 }
61
62 $form = new Form('teamForm');
63 $form->addInput(array('type'=>'text','maxlength'=>'80','name'=>'team_name','value'=>$cl_team_name));
64 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_name','value'=>$cl_manager_name));
65 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_login','value'=>$cl_manager_login));
66 if (!$auth->isPasswordExternal()) {
67   $form->addInput(array('type'=>'text','maxlength'=>'30','name'=>'password1','aspassword'=>true,'value'=>$cl_password1));
68   $form->addInput(array('type'=>'text','maxlength'=>'30','name'=>'password2','aspassword'=>true,'value'=>$cl_password2));
69 }
70 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_email','value'=>$cl_manager_email));
71 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$team_id));
72 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->getKey('button.save')));
73 $form->addInput(array('type'=>'submit','name'=>'btn_cancel','value'=>$i18n->getKey('button.cancel')));
74
75 if ($request->isPost()) {
76   if ($request->getParameter('btn_save')) {
77     // Validate user input.
78     if (!ttValidString($cl_team_name, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.team_name'));
79     if (!ttValidString($cl_manager_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_name'));
80     if (!ttValidString($cl_manager_login)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_login'));
81     if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
82       if (!ttValidString($cl_password1)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
83       if (!ttValidString($cl_password2)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
84       if ($cl_password1 !== $cl_password2)
85         $err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
86     }
87     if (!ttValidEmail($cl_manager_email, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
88
89     // New login must be unique.
90     if ($cl_manager_login != $team_details['manager_login'])
91       if (ttUserHelper::getUserByLogin($cl_manager_login)) $err->add($i18n->getKey('error.user_exists'));
92
93     if ($err->no()) {
94       $update_result = ttTeamHelper::update($team_id, array('name'=>$cl_team_name));
95       if ($update_result) {
96         $update_result = ttUserHelper::update($team_details['manager_id'], array(
97           'name' => $cl_manager_name,
98           'login' => $cl_manager_login,
99           'password' => $cl_password1,
100           'email' => $cl_manager_email,
101           'status' => ACTIVE));
102       }
103       if ($update_result) {
104         header('Location: admin_teams.php');
105         exit();
106       } else
107         $err->add($i18n->getKey('error.db'));
108     }
109   }
110
111   if ($request->getParameter('btn_cancel')) {
112     header('Location: admin_teams.php');
113     exit();
114   }
115 } // POST
116
117 $smarty->assign('auth_external', $auth->isPasswordExternal());
118 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
119 $smarty->assign('onload', 'onLoad="document.teamForm.manager_name.focus()"');
120 $smarty->assign('title', $i18n->getKey('title.edit_team'));
121 $smarty->assign('content_page_name', 'admin_team_edit.tpl');
122 $smarty->display('index.tpl');