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');
 
  36 import('ttRoleHelper');
 
  39 if (!ttAccessAllowed('manage_users')) {
 
  40   header('Location: access_denied.php');
 
  43 // End of access checks.
 
  45 // Use the "limit" plugin if we have one. Ignore include errors.
 
  46 // The "limit" plugin is not required for normal operation of Time Tracker.
 
  47 @include('plugins/limit/user_add.php');
 
  49 $show_quota = $user->isPluginEnabled('mq');
 
  50 if ($user->isPluginEnabled('cl'))
 
  51   $clients = ttGroupHelper::getActiveClients();
 
  53 $assigned_projects = array();
 
  54 if ($request->isPost()) {
 
  55   $cl_name = trim($request->getParameter('name'));
 
  56   $cl_login = trim($request->getParameter('login'));
 
  57   if (!$auth->isPasswordExternal()) {
 
  58     $cl_password1 = $request->getParameter('pas1');
 
  59     $cl_password2 = $request->getParameter('pas2');
 
  61   $cl_email = trim($request->getParameter('email'));
 
  62   $cl_role_id = $request->getParameter('role');
 
  63   $cl_client_id = $request->getParameter('client');
 
  64   $cl_rate = $request->getParameter('rate');
 
  65   $cl_quota_percent = $request->getParameter('quota_percent');
 
  66   $cl_projects = $request->getParameter('projects');
 
  67   if (is_array($cl_projects)) {
 
  68     foreach ($cl_projects as $p) {
 
  69       if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
 
  70         $project_with_rate = array();
 
  71         $project_with_rate['id'] = $p;
 
  72         $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
 
  73         $assigned_projects[] = $project_with_rate;
 
  75         $err->add($i18n->get('error.field'), 'rate_'.$p);
 
  80 $form = new Form('userForm');
 
  81 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 300px;','value'=>$cl_name));
 
  82 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','style'=>'width: 300px;','value'=>$cl_login));
 
  83 if (!$auth->isPasswordExternal()) {
 
  84   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
 
  85   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
 
  87 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
 
  89 $active_roles = ttTeamHelper::getActiveRolesForUser();
 
  90 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles,'datakeys'=>array('id', 'name')));
 
  91 if ($user->isPluginEnabled('cl'))
 
  92   $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
  94 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
 
  96   $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'quota_percent','format'=>'.2','value'=>$cl_quota_percent));
 
  98 $show_projects = MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode();
 
 100   $projects = ttGroupHelper::getActiveProjects();
 
 101   if (count($projects) == 0) $show_projects = false;
 
 104 // Define classes for the projects table.
 
 105 class NameCellRenderer extends DefaultCellRenderer {
 
 106   function render(&$table, $value, $row, $column, $selected = false) {
 
 107     $this->setOptions(array('width'=>200));
 
 108     $this->setValue('<label for = "'.$table->name.'_'.$row.'">'.htmlspecialchars($value).'</label>');
 
 109     return $this->toString();
 
 112 class RateCellRenderer extends DefaultCellRenderer {
 
 113   function render(&$table, $value, $row, $column, $selected = false) {
 
 114     global $assigned_projects;
 
 115     $field = new FloatField('rate_'.$table->getValueAtName($row, 'id'));
 
 116     $field->setFormName($table->getFormName());
 
 118     $field->setFormat('.2');
 
 119     foreach ($assigned_projects as $p) {
 
 120       if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
 
 122     $this->setValue($field->getHtml());
 
 123     return $this->toString();
 
 126 // Create projects table.
 
 127 $table = new Table('projects');
 
 128 $table->setIAScript('setDefaultRate');
 
 129 $table->setTableOptions(array('width'=>'100%','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
 
 130 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
 
 131 $table->setData($projects);
 
 132 $table->setKeyField('id');
 
 133 $table->setValue($cl_projects);
 
 134 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
 
 135 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
 
 136 $form->addInputElement($table);
 
 138 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
 
 140 if ($request->isPost()) {
 
 141   // Validate user input.
 
 142   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
 
 143   if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
 
 144   if (!$auth->isPasswordExternal()) {
 
 145     if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
 
 146     if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
 
 147     if ($cl_password1 !== $cl_password2)
 
 148       $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
 
 150   if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
 
 151   // Require selection of a client for a client role.
 
 152   if ($user->isPluginEnabled('cl') && ttRoleHelper::isClientRole($cl_role_id) && !$cl_client_id) $err->add($i18n->get('error.client'));
 
 153   if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
 
 154   if (!ttValidFloat($cl_quota_percent, true)) $err->add($i18n->get('error.field'), $i18n->get('label.quota'));
 
 155   if (!ttUserHelper::canAdd()) $err->add($i18n->get('error.user_count'));
 
 158     if (!ttUserHelper::getUserByLogin($cl_login)) {
 
 161         'login' => $cl_login,
 
 162         'password' => $cl_password1,
 
 164         'quota_percent' => $cl_quota_percent,
 
 165         'group_id' => $user->getGroup(),
 
 166         'org_id' => $user->org_id,
 
 167         'role_id' => $cl_role_id,
 
 168         'client_id' => $cl_client_id,
 
 169         'projects' => $assigned_projects,
 
 170         'email' => $cl_email);
 
 171       $user_id = ttUserHelper::insert($fields);
 
 173         if (!$user->exists()) {
 
 174           // We added a user to an empty subgroup. Set new user as on behalf user.
 
 175           // Needed for user-based things to work (such as notifications config).
 
 176           $user->setOnBehalfUser($user_id);
 
 178         header('Location: users.php');
 
 181         $err->add($i18n->get('error.db'));
 
 183       $err->add($i18n->get('error.user_exists'));
 
 187 $smarty->assign('auth_external', $auth->isPasswordExternal());
 
 188 $smarty->assign('active_roles', $active_roles);
 
 189 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 190 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
 
 191 $smarty->assign('show_quota', $show_quota);
 
 192 $smarty->assign('show_projects', $show_projects);
 
 193 $smarty->assign('title', $i18n->get('title.add_user'));
 
 194 $smarty->assign('content_page_name', 'user_add.tpl');
 
 195 $smarty->display('index.tpl');