Adjusted header.tpl for Work plugin.
[timetracker.git] / profile_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('ttRoleHelper');
33
34 // Access checks.
35 if (!ttAccessAllowed('manage_own_settings')) {
36   header('Location: access_denied.php');
37   exit();
38 }
39 if (!$user->exists()) {
40   header('Location: access_denied.php');  // No users in subgroup.
41   exit();
42 }
43 // End of access checks.
44
45 $can_manage_account = $user->behalfGroup ? $user->can('manage_subgroups') : $user->can('manage_own_account');
46 if ($user->behalf_id) $user_details = $user->getUserDetails($user->behalf_id);
47 $current_login = $user->behalf_id ? $user_details['login'] : $user->login;
48
49 if ($request->isPost()) {
50   $cl_name = trim($request->getParameter('name'));
51   $cl_login = trim($request->getParameter('login'));
52   if (!$auth->isPasswordExternal()) {
53     $cl_password1 = $request->getParameter('password1');
54     $cl_password2 = $request->getParameter('password2');
55   }
56   $cl_email = trim($request->getParameter('email'));
57 } else {
58   if ($user->behalf_id) {
59     $cl_name = $user_details['name'];
60     $cl_login = $user_details['login'];
61     $cl_email = $user_details['email'];
62   } else {
63     $cl_name = $user->name;
64     $cl_login = $user->login;
65     $cl_email = $user->email;
66   }
67 }
68
69 $form = new Form('profileForm');
70 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','value'=>$cl_name,'enable'=>$can_manage_account));
71 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','value'=>$cl_login,'enable'=>$can_manage_account));
72 if (!$auth->isPasswordExternal()) {
73   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password1','value'=>$cl_password1));
74   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password2','value'=>$cl_password2));
75 }
76 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email,'enable'=>$can_manage_account));
77 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
78
79 if ($request->isPost()) {
80   // Validate user input.
81   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
82   if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
83
84   // New login must be unique.
85   if ($cl_login != $current_login && ttUserHelper::getUserByLogin($cl_login))
86     $err->add($i18n->get('error.user_exists'));
87
88   if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
89     if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
90     if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
91     if ($cl_password1 !== $cl_password2)
92       $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
93   }
94   if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
95   // Finished validating user input.
96
97   if ($err->no()) {
98     $fields = $can_manage_account ?
99       array('name'=>$cl_name,'login'=>$cl_login,'password'=>$cl_password1,'email'=>$cl_email) :
100       array('password'=>$cl_password1);
101     $update_result = ttUserHelper::update($user->getUser(), $fields);
102     if ($update_result) {
103       header('Location: time.php');
104       exit();
105     } else
106       $err->add($i18n->get('error.db'));
107   }
108 } // isPost
109
110 $smarty->assign('auth_external', $auth->isPasswordExternal());
111 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
112 $smarty->assign('title', $i18n->get('title.profile'));
113 $smarty->assign('content_page_name', 'profile_edit.tpl');
114 $smarty->display('index.tpl');