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