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('ttGroupHelper');
 
  33 import('ttUserHelper');
 
  35 import('form.TableColumn');
 
  38 if (!ttAccessAllowed('manage_users')) {
 
  39   header('Location: access_denied.php');
 
  42 // End of access checks.
 
  44 // Use the "limit" plugin if we have one. Ignore include errors.
 
  45 // The "limit" plugin is not required for normal operation of Time Tracker.
 
  46 @include('../plugins/limit/user_add.php');
 
  48 $show_quota = $user->isPluginEnabled('mq');
 
  49 if ($user->isPluginEnabled('cl'))
 
  50   $clients = ttGroupHelper::getActiveClients();
 
  52 $assigned_projects = array();
 
  53 if ($request->isPost()) {
 
  54   $cl_name = trim($request->getParameter('name'));
 
  55   $cl_login = trim($request->getParameter('login'));
 
  56   if (!$auth->isPasswordExternal()) {
 
  57     $cl_password1 = $request->getParameter('pas1');
 
  58     $cl_password2 = $request->getParameter('pas2');
 
  60   $cl_email = trim($request->getParameter('email'));
 
  61   $cl_role_id = $request->getParameter('role');
 
  62   $cl_client_id = $request->getParameter('client');
 
  63   $cl_rate = $request->getParameter('rate');
 
  64   $cl_quota_percent = $request->getParameter('quota_percent');
 
  65   $cl_projects = $request->getParameter('projects');
 
  66   if (is_array($cl_projects)) {
 
  67     foreach ($cl_projects as $p) {
 
  68       if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
 
  69         $project_with_rate = array();
 
  70         $project_with_rate['id'] = $p;
 
  71         $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
 
  72         $assigned_projects[] = $project_with_rate;
 
  74         $err->add($i18n->get('error.field'), 'rate_'.$p);
 
  79 $form = new Form('userForm');
 
  80 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','value'=>$cl_name));
 
  81 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','value'=>$cl_login));
 
  82 if (!$auth->isPasswordExternal()) {
 
  83   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
 
  84   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
 
  86 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
 
  88 $active_roles = ttTeamHelper::getActiveRolesForUser();
 
  89 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles,'datakeys'=>array('id', 'name')));
 
  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->get('dropdown.select'))));
 
  93 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
 
  95   $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'quota_percent','format'=>'.2','value'=>$cl_quota_percent));
 
  97 $show_projects = MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode();
 
  99   $projects = ttGroupHelper::getActiveProjects();
 
 100   if (count($projects) == 0) $show_projects = false;
 
 103 // Define classes for the projects table.
 
 104 class NameCellRenderer extends DefaultCellRenderer {
 
 105   function render(&$table, $value, $row, $column, $selected = false) {
 
 106     $this->setOptions(array('width'=>200,'valign'=>'top'));
 
 107     $this->setValue('<label for = "'.$table->getName().'_'.$row.'">'.htmlspecialchars($value).'</label>');
 
 108     return $this->toString();
 
 111 class RateCellRenderer extends DefaultCellRenderer {
 
 112   function render(&$table, $value, $row, $column, $selected = false) {
 
 113     global $assigned_projects;
 
 114     $field = new FloatField('rate_'.$table->getValueAtName($row, 'id'));
 
 115     $field->setFormName($table->getFormName());
 
 117     $field->setFormat('.2');
 
 118     foreach ($assigned_projects as $p) {
 
 119       if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
 
 121     $this->setValue($field->getHtml());
 
 122     return $this->toString();
 
 125 // Create projects table.
 
 126 $table = new Table('projects');
 
 127 $table->setIAScript('setDefaultRate');
 
 128 $table->setTableOptions(array('width'=>'250','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
 
 129 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
 
 130 $table->setData($projects);
 
 131 $table->setKeyField('id');
 
 132 $table->setValue($cl_projects);
 
 133 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
 
 134 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
 
 135 $form->addInputElement($table);
 
 137 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
 
 139 if ($request->isPost()) {
 
 140   // Validate user input.
 
 141   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
 
 142   if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
 
 143   if (!$auth->isPasswordExternal()) {
 
 144     if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
 
 145     if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
 
 146     if ($cl_password1 !== $cl_password2)
 
 147       $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
 
 149   if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
 
 150   if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
 
 151   if (!ttValidFloat($cl_quota_percent, true)) $err->add($i18n->get('error.field'), $i18n->get('label.quota'));
 
 152   if (!ttUserHelper::canAdd()) $err->add($i18n->get('error.user_count'));
 
 155     if (!ttUserHelper::getUserByLogin($cl_login)) {
 
 158         'login' => $cl_login,
 
 159         'password' => $cl_password1,
 
 161         'quota_percent' => $cl_quota_percent,
 
 162         'group_id' => $user->getGroup(),
 
 163         'org_id' => $user->org_id,
 
 164         'role_id' => $cl_role_id,
 
 165         'client_id' => $cl_client_id,
 
 166         'projects' => $assigned_projects,
 
 167         'email' => $cl_email);
 
 168       if (ttUserHelper::insert($fields)) {
 
 169         header('Location: users.php');
 
 172         $err->add($i18n->get('error.db'));
 
 174       $err->add($i18n->get('error.user_exists'));
 
 178 $smarty->assign('auth_external', $auth->isPasswordExternal());
 
 179 $smarty->assign('active_roles', $active_roles);
 
 180 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 181 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
 
 182 $smarty->assign('show_quota', $show_quota);
 
 183 $smarty->assign('show_projects', $show_projects);
 
 184 $smarty->assign('title', $i18n->get('title.add_user'));
 
 185 $smarty->assign('content_page_name', 'mobile/user_add.tpl');
 
 186 $smarty->display('mobile/index.tpl');