Refactored admin_group_editp.php.
[timetracker.git] / admin_group_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('ttAdmin');
33
34 // Access checks.
35 if (!ttAccessAllowed('administer_site')) {
36   header('Location: access_denied.php');
37   exit();
38 }
39 $group_id = (int)$request->getParameter('id');
40 $group_details = ttAdmin::getGroupDetails($group_id);
41 if (!($group_id && $group_details)) {
42   header('Location: access_denied.php');
43   exit();
44 }
45 // End of access checks.
46
47 if ($request->isPost()) {
48   $cl_group_name = trim($request->getParameter('group_name'));
49   $cl_manager_name = trim($request->getParameter('manager_name'));
50   $cl_manager_login = trim($request->getParameter('manager_login'));
51   if (!$auth->isPasswordExternal()) {
52     $cl_password1 = $request->getParameter('password1');
53     $cl_password2 = $request->getParameter('password2');
54   }
55   $cl_manager_email = trim($request->getParameter('manager_email'));
56 } else {
57   $cl_group_name = $group_details['group_name'];
58   $cl_manager_name = $group_details['manager_name'];
59   $cl_manager_login = $group_details['manager_login'];
60   if (!$auth->isPasswordExternal()) {
61     $cl_password1 = $cl_password2 = '';
62   }
63   $cl_manager_email = $group_details['manager_email'];
64 }
65
66 $form = new Form('groupForm');
67 $form->addInput(array('type'=>'text','maxlength'=>'80','name'=>'group_name','value'=>$cl_group_name));
68 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_name','value'=>$cl_manager_name));
69 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_login','value'=>$cl_manager_login));
70 if (!$auth->isPasswordExternal()) {
71   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password1','value'=>$cl_password1));
72   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password2','value'=>$cl_password2));
73 }
74 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_email','value'=>$cl_manager_email));
75 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$group_id));
76 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
77 $form->addInput(array('type'=>'submit','name'=>'btn_cancel','value'=>$i18n->get('button.cancel')));
78
79 if ($request->isPost()) {
80   if ($request->getParameter('btn_save')) {
81
82     // Validate user input.
83     if (!ttValidString($cl_group_name))
84       $err->add($i18n->get('error.field'), $i18n->get('label.group_name'));
85     if (!ttValidString($cl_manager_name))
86       $err->add($i18n->get('error.field'), $i18n->get('label.manager_name'));
87     if (!ttValidString($cl_manager_login))
88       $err->add($i18n->get('error.field'), $i18n->get('label.manager_login'));
89     // If we change login, it must be unique.
90     if ($cl_manager_login != $group_details['manager_login']) {
91       if (ttUserHelper::getUserByLogin($cl_manager_login)) {
92         $err->add($i18n->get('error.user_exists'));
93       }
94     }
95     if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
96       if (!ttValidString($cl_password1))
97         $err->add($i18n->get('error.field'), $i18n->get('label.password'));
98       if (!ttValidString($cl_password2))
99         $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
100       if ($cl_password1 !== $cl_password2)
101         $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
102     }
103     if (!ttValidEmail($cl_manager_email, true))
104       $err->add($i18n->get('error.field'), $i18n->get('label.email'));
105
106     if ($err->no()) {
107       if (ttAdmin::updateGroup(array('group_id' => $group_id,
108         'old_group_name' => $group_details['group_name'],
109         'new_group_name' => $cl_group_name,
110         'user_id' => $group_details['manager_id'],
111         'user_name' => $cl_manager_name,
112         'old_login' => $group_details['manager_login'],
113         'new_login' => $cl_manager_login,
114         'password1' => $cl_password1,
115         'password2' => $cl_password2,
116         'email' => $cl_manager_email))) {
117         header('Location: admin_groups.php');
118         exit();
119       } else
120         $err->add($i18n->get('error.db'));
121     }
122   }
123
124   if ($request->getParameter('btn_cancel')) {
125     header('Location: admin_groups.php');
126     exit();
127   }
128 } // isPost
129
130 $smarty->assign('auth_external', $auth->isPasswordExternal());
131 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
132 $smarty->assign('onload', 'onLoad="document.groupForm.manager_name.focus()"');
133 $smarty->assign('title', $i18n->get('title.edit_group'));
134 $smarty->assign('content_page_name', 'admin_group_edit.tpl');
135 $smarty->display('index.tpl');