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.
 
  11 // | There are only two ways to violate the license:
 
  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).
 
  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).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 require_once('../initialize.php');
 
  31 import('ttProjectHelper');
 
  32 import('ttTeamHelper');
 
  33 import('ttGroupHelper');
 
  34 import('ttUserHelper');
 
  36 import('form.TableColumn');
 
  39 if (!ttAccessAllowed('manage_users')) {
 
  40   header('Location: access_denied.php');
 
  43 $user_id = (int)$request->getParameter('id');
 
  44 $user_details = $user->getUserDetails($user_id);
 
  46   header('Location: access_denied.php');
 
  49 // End of access checks.
 
  51 $show_quota = $user->isPluginEnabled('mq');
 
  52 if ($user->isPluginEnabled('cl'))
 
  53   $clients = ttGroupHelper::getActiveClients();
 
  55 $show_projects = MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode();
 
  57   $projects = ttGroupHelper::getActiveProjects();
 
  58   if (count($projects) == 0) $show_projects = false;
 
  60 $assigned_projects = array();
 
  62 if ($request->isPost()) {
 
  63   $cl_name = trim($request->getParameter('name'));
 
  64   $cl_login = trim($request->getParameter('login'));
 
  65   if (!$auth->isPasswordExternal()) {
 
  66     $cl_password1 = $request->getParameter('pas1');
 
  67     $cl_password2 = $request->getParameter('pas2');
 
  69   $cl_email = trim($request->getParameter('email'));
 
  70   $cl_role_id = $request->getParameter('role');
 
  71   $cl_client_id = $request->getParameter('client');
 
  72   $cl_status = $request->getParameter('status');
 
  73   $cl_rate = $request->getParameter('rate');
 
  74   $cl_quota_percent = $request->getParameter('quota_percent');
 
  75   $cl_projects = $request->getParameter('projects');
 
  76   if (is_array($cl_projects)) {
 
  77     foreach ($cl_projects as $p) {
 
  78       if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
 
  79         $project_with_rate = array();
 
  80         $project_with_rate['id'] = $p;
 
  81         $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
 
  82         $assigned_projects[] = $project_with_rate;
 
  84         $err->add($i18n->get('error.field'), 'rate_'.$p);
 
  88   $cl_name = $user_details['name'];
 
  89   $cl_login = $user_details['login'];
 
  90   $cl_email = $user_details['email'];
 
  91   $cl_rate = str_replace('.', $user->getDecimalMark(), $user_details['rate']);
 
  92   $cl_quota_percent = str_replace('.', $user->getDecimalMark(), $user_details['quota_percent']);
 
  93   $cl_role_id = $user_details['role_id'];
 
  94   $cl_client_id = $user_details['client_id'];
 
  95   $cl_status = $user_details['status'];
 
  96   $cl_projects = array();
 
  97   $assigned_projects = ttProjectHelper::getAssignedProjects($user_id);
 
  98   foreach($assigned_projects as $p) {
 
  99     $cl_projects[] = $p['id'];
 
 103 $form = new Form('userForm');
 
 104 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','value'=>$cl_name));
 
 105 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','value'=>$cl_login));
 
 106 if (!$auth->isPasswordExternal()) {
 
 107   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
 
 108   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
 
 110 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
 
 112 $active_roles = ttTeamHelper::getActiveRolesForUser();
 
 113 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles,'datakeys'=>array('id', 'name')));
 
 114 if ($user->isPluginEnabled('cl'))
 
 115   $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 117 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
 
 118   'data'=>array(ACTIVE=>$i18n->get('dropdown.status_active'),INACTIVE=>$i18n->get('dropdown.status_inactive'))));
 
 119 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
 
 121   $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'quota_percent','format'=>'.2','value'=>$cl_quota_percent));
 
 123 // Define classes for the projects table.
 
 124 class NameCellRenderer extends DefaultCellRenderer {
 
 125   function render(&$table, $value, $row, $column, $selected = false) {
 
 126     $this->setOptions(array('width'=>200,'valign'=>'top'));
 
 127     $this->setValue('<label for = "'.$table->getName().'_'.$row.'">'.htmlspecialchars($value).'</label>');
 
 128     return $this->toString();
 
 131 class RateCellRenderer extends DefaultCellRenderer {
 
 132   function render(&$table, $value, $row, $column, $selected = false) {
 
 133     global $assigned_projects;
 
 134     $field = new FloatField('rate_'.$table->getValueAtName($row,'id'));
 
 135     $field->setFormName($table->getFormName());
 
 137     $field->setFormat('.2');
 
 138     foreach ($assigned_projects as $p) {
 
 139       if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
 
 141     $this->setValue($field->getHtml());
 
 142     return $this->toString();
 
 145 // Create projects table.
 
 146 $table = new Table('projects');
 
 147 $table->setIAScript('setRate');
 
 148 $table->setTableOptions(array('width'=>'250','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
 
 149 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
 
 150 $table->setData($projects);
 
 151 $table->setKeyField('id');
 
 152 $table->setValue($cl_projects);
 
 153 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
 
 154 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
 
 155 $form->addInputElement($table);
 
 157 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$user_id));
 
 158 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.save')));
 
 159 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
 
 161 if ($request->isPost()) {
 
 162   if ($request->getParameter('btn_submit')) {
 
 163     // Validate user input.
 
 164     if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
 
 165     if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
 
 166     if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
 
 167       if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
 
 168       if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
 
 169       if ($cl_password1 !== $cl_password2)
 
 170         $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
 
 172     if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
 
 173     if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
 
 174     if (!ttValidFloat($cl_quota_percent, true)) $err->add($i18n->get('error.field'), $i18n->get('label.quota'));
 
 177       $existing_user = ttUserHelper::getUserByLogin($cl_login);
 
 178       if (!$existing_user || ($user_id == $existing_user['id'])) {
 
 182           'login' => $cl_login,
 
 183           'password' => $cl_password1,
 
 184           'email' => $cl_email,
 
 185           'status' => $cl_status,
 
 187           'quota_percent' => $cl_quota_percent,
 
 188           'projects' => $assigned_projects);
 
 189         if (in_array('manage_users', $user->rights)) {
 
 190           $fields['role_id'] = $cl_role_id;
 
 191           $fields['client_id'] = $cl_client_id;
 
 194         if (ttUserHelper::update($user_id, $fields)) {
 
 196           // If our own login changed, set new one in cookie to remember it.
 
 197           if (($user_id == $user->id) && ($user->login != $cl_login)) {
 
 198             setcookie('tt_login', $cl_login, time() + COOKIE_EXPIRE, '/');
 
 201           // In case the name of the "on behalf" user has changed - set it in session.
 
 202           if (($user->behalf_id == $user_id) && ($user->behalf_name != $cl_name)) {
 
 203             $_SESSION['behalf_name'] = $cl_name;
 
 206           // If we deactivated our own account, do housekeeping and logout.
 
 207           if ($user->id == $user_id && !is_null($cl_status) && $cl_status == INACTIVE) {
 
 208             // Remove tt_login cookie that stores login name.
 
 209             unset($_COOKIE['tt_login']);
 
 210             setcookie('tt_login', NULL, -1);
 
 213             header('Location: login.php');
 
 217           header('Location: users.php');
 
 221           $err->add($i18n->get('error.db'));
 
 223         $err->add($i18n->get('error.user_exists'));
 
 227   if ($request->getParameter('btn_delete')) {
 
 228     header("Location: user_delete.php?id=$user_id");
 
 233 $rates = ttProjectHelper::getRates($user_id);
 
 234 $smarty->assign('rates', $rates);
 
 236 $smarty->assign('auth_external', $auth->isPasswordExternal());
 
 237 $smarty->assign('active_roles', $active_roles);
 
 238 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 239 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
 
 240 $smarty->assign('show_quota', $show_quota);
 
 241 $smarty->assign('show_projects', $show_projects);
 
 242 $smarty->assign('user_id', $user_id);
 
 243 $smarty->assign('title', $i18n->get('title.edit_user'));
 
 244 $smarty->assign('content_page_name', 'mobile/user_edit.tpl');
 
 245 $smarty->display('mobile/index.tpl');