Finished refactoring ttAdmin class.
[timetracker.git] / admin_options.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 check.
35 if (!ttAccessAllowed('administer_site')) {
36   header('Location: access_denied.php');
37   exit();
38 }
39
40 if ($request->isPost()) {
41   $cl_name = trim($request->getParameter('name'));
42   $cl_login = trim($request->getParameter('login'));
43   if (!$auth->isPasswordExternal()) {
44     $cl_password1 = $request->getParameter('password1');
45     $cl_password2 = $request->getParameter('password2');
46   }
47   $cl_email = trim($request->getParameter('email'));
48 } else {
49   $cl_name = $user->name;
50   $cl_login = $user->login;
51   $cl_email = $user->email;
52 }
53
54 $form = new Form('optionsForm');
55 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','value'=>$cl_name));
56 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','value'=>$cl_login));
57 if (!$auth->isPasswordExternal()) {
58   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password1','value'=>$cl_password1));
59   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password2','value'=>$cl_password2));
60 }
61 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
62 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
63
64 if ($request->isPost()) {
65   // Validate user input.
66   if (!ttValidString($cl_name))
67     $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
68   if (!ttValidString($cl_login))
69     $err->add($i18n->get('error.field'), $i18n->get('label.login'));
70   // If we change login, it must be unique.
71   if ($cl_login != $user->login && ttUserHelper::getUserByLogin($cl_login))
72     $err->add($i18n->get('error.user_exists'));
73   if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
74       if (!ttValidString($cl_password1))
75         $err->add($i18n->get('error.field'), $i18n->get('label.password'));
76       if (!ttValidString($cl_password2))
77         $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
78       if ($cl_password1 !== $cl_password2)
79         $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
80     }
81   if (!ttValidEmail($cl_email, true))
82     $err->add($i18n->get('error.field'), $i18n->get('label.email'));
83
84   if ($err->no() && ttAdmin::updateSelf(array('name' => $cl_name,
85     'login' => $cl_login,
86     'password1' => $cl_password1,
87     'password2' => $cl_password2,
88     'email' => $cl_email))) {
89     header('Location: admin_groups.php');
90     exit();
91   }
92 } // isPost
93
94 $smarty->assign('auth_external', $auth->isPasswordExternal());
95 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
96 $smarty->assign('title', $i18n->get('title.options'));
97 $smarty->assign('content_page_name', 'admin_options.tpl');
98 $smarty->display('index.tpl');