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('ttTeamHelper');
 
  32 import('ttUserHelper');
 
  34 import('form.TableColumn');
 
  37 if (!ttAccessCheck(right_manage_team)) {
 
  38   header('Location: access_denied.php');
 
  42 // Use the "limit" plugin if we have one. Ignore include errors.
 
  43 // The "limit" plugin is not required for normal operation of the Time Tracker.
 
  44 @include('plugins/limit/user_add.php');
 
  46 if ($user->isPluginEnabled('cl'))
 
  47   $clients = ttTeamHelper::getActiveClients($user->team_id);
 
  49 $assigned_projects = array();
 
  50 if ($request->isPost()) {
 
  51   $cl_name = trim($request->getParameter('name'));
 
  52   $cl_login = trim($request->getParameter('login'));
 
  53   if (!$auth->isPasswordExternal()) {
 
  54     $cl_password1 = $request->getParameter('pas1');
 
  55     $cl_password2 = $request->getParameter('pas2');
 
  57   $cl_email = trim($request->getParameter('email'));
 
  58   $cl_role = $request->getParameter('role');
 
  59   if (!$cl_role) $cl_role = ROLE_USER;
 
  60   $cl_client_id = $request->getParameter('client');
 
  61   $cl_rate = $request->getParameter('rate');
 
  62   $cl_projects = $request->getParameter('projects');
 
  63   if (is_array($cl_projects)) {
 
  64     foreach ($cl_projects as $p) {
 
  65       if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
 
  66         $project_with_rate = array();
 
  67         $project_with_rate['id'] = $p;
 
  68         $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
 
  69         $assigned_projects[] = $project_with_rate;
 
  71         $err->add($i18n->getKey('error.field'), 'rate_'.$p);
 
  76 $form = new Form('userForm');
 
  77 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','value'=>$cl_name));
 
  78 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','value'=>$cl_login));
 
  79 if (!$auth->isPasswordExternal()) {
 
  80   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
 
  81   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
 
  83 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
 
  85 $roles[ROLE_USER] = $i18n->getKey('label.user');
 
  86 $roles[ROLE_COMANAGER] = $i18n->getKey('form.users.comanager');
 
  87 if ($user->isPluginEnabled('cl'))
 
  88   $roles[ROLE_CLIENT] = $i18n->getKey('label.client');
 
  89 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role,'data'=>$roles));
 
  90 if ($user->isPluginEnabled('cl'))
 
  91   $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
 
  93 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
 
  95 $projects = ttTeamHelper::getActiveProjects($user->team_id);
 
  97 // Define classes for the projects table.
 
  98 class NameCellRenderer extends DefaultCellRenderer {
 
  99   function render(&$table, $value, $row, $column, $selected = false) {
 
 100     $this->setOptions(array('width'=>200,'valign'=>'top'));
 
 101     $this->setValue('<label for = "'.$table->getName().'_'.$row.'">'.htmlspecialchars($value).'</label>');
 
 102     return $this->toString();
 
 105 class RateCellRenderer extends DefaultCellRenderer {
 
 106   function render(&$table, $value, $row, $column, $selected = false) {
 
 107     global $assigned_projects;
 
 108     $field = new FloatField('rate_'.$table->getValueAtName($row, 'id'));
 
 109     $field->setFormName($table->getFormName());
 
 110     $field->localize($GLOBALS['I18N']);
 
 112     $field->setFormat('.2');
 
 113     foreach ($assigned_projects as $p) {
 
 114       if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
 
 116     $this->setValue($field->getHtml());
 
 117     return $this->toString();
 
 120 // Create projects table.
 
 121 $table = new Table('projects');
 
 122 $table->setIAScript('setDefaultRate');
 
 123 $table->setTableOptions(array('width'=>'100%','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
 
 124 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
 
 125 $table->setData($projects);
 
 126 $table->setKeyField('id');
 
 127 $table->setValue($cl_projects);
 
 128 $table->addColumn(new TableColumn('name', $i18n->getKey('label.project'), new NameCellRenderer()));
 
 129 $table->addColumn(new TableColumn('p_rate', $i18n->getKey('form.users.rate'), new RateCellRenderer()));
 
 130 $form->addInputElement($table);
 
 132 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.submit')));
 
 134 if ($request->isPost()) {
 
 135   // Validate user input.
 
 136   if (!ttValidString($cl_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.person_name'));
 
 137   if (!ttValidString($cl_login)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.login'));
 
 138   if (!$auth->isPasswordExternal()) {
 
 139     if (!ttValidString($cl_password1)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
 
 140     if (!ttValidString($cl_password2)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
 
 141     if ($cl_password1 !== $cl_password2)
 
 142       $err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
 
 144   if (!ttValidEmail($cl_email, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
 
 145   if (!ttValidFloat($cl_rate, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('form.users.default_rate'));
 
 148     if (!ttUserHelper::getUserByLogin($cl_login)) {
 
 151         'login' => $cl_login,
 
 152         'password' => $cl_password1,
 
 154         'team_id' => $user->team_id,
 
 156         'client_id' => $cl_client_id,
 
 157         'projects' => $assigned_projects,
 
 158         'email' => $cl_email);
 
 159       if (ttUserHelper::insert($fields)) {
 
 160         header('Location: users.php');
 
 163         $err->add($i18n->getKey('error.db'));
 
 165       $err->add($i18n->getKey('error.user_exists'));
 
 169 $smarty->assign('auth_external', $auth->isPasswordExternal());
 
 170 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 171 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
 
 172 $smarty->assign('title', $i18n->getKey('title.add_user'));
 
 173 $smarty->assign('content_page_name', 'mobile/user_add.tpl');
 
 174 $smarty->display('mobile/index.tpl');