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'); // TODO: remove this?
 
  32 import('ttTaskHelper'); // TODO: remove this?
 
  33 import('ttRoleHelper');
 
  36 if (!ttAccessCheck(right_manage_team)) {
 
  37   header('Location: access_denied.php');
 
  40 $cl_role_id = (int)$request->getParameter('id');
 
  41 $role = ttRoleHelper::get($cl_role_id);
 
  43   header('Location: access_denied.php');
 
  46 $assigned_rights = explode(',', $role['rights']);
 
  47 $available_rights = array_diff($user->rights_array, $assigned_rights);
 
  49 if ($request->isPost()) {
 
  50   $cl_name = trim($request->getParameter('name'));
 
  51   $cl_description = trim($request->getParameter('description'));
 
  52   $cl_status = $request->getParameter('status');
 
  54   $cl_name = $role['name'];
 
  55   $cl_description = $role['description'];
 
  56   $cl_status = $role['status'];
 
  59 $form = new Form('roleForm');
 
  60 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_role_id));
 
  61 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 250px;','value'=>$cl_name));
 
  62 $form->addInput(array('type'=>'textarea','name'=>'description','style'=>'width: 250px; height: 40px;','value'=>$cl_description));
 
  64 // Multiple select controls for assigned and available rights.
 
  65 $form->addInput(array('type'=>'combobox','name'=>'assigned_rights','style'=>'width: 250px;','multiple'=>true,'data'=>$assigned_rights));
 
  66 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->getKey('button.delete')));
 
  67 $form->addInput(array('type'=>'combobox','name'=>'available_rights','style'=>'width: 250px;','multiple'=>true,'data'=>$available_rights));
 
  68 $form->addInput(array('type'=>'submit','name'=>'btn_add','value'=>$i18n->getKey('button.add')));
 
  71 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
 
  72   'data'=>array(ACTIVE=>$i18n->getKey('dropdown.status_active'),INACTIVE=>$i18n->getKey('dropdown.status_inactive'))));
 
  73 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->getKey('button.save')));
 
  75 if ($request->isPost()) {
 
  76     if ($request->getParameter('btn_save')) {
 
  77     // Validate user input.
 
  78     if (!ttValidString($cl_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.thing_name'));
 
  79     if (!ttValidString($cl_description, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.description'));
 
  82       $existing_role = ttRoleHelper::getRoleByName($cl_name);
 
  83       if (!$existing_role || ($cl_role_id == $existing_role['id'])) {
 
  84         // Update role information.
 
  85         if (ttRoleHelper::update(array(
 
  88           'description' => $cl_description,
 
  89           'status' => $cl_status))) {
 
  90           header('Location: roles.php');
 
  93           $err->add($i18n->getKey('error.db'));
 
  95         $err->add($i18n->getKey('error.object_exists'));
 
  98   if ($request->getParameter('btn_delete') && $request->getParameter('assigned_rights')) {
 
  99      $rights = $role['rights'];
 
 100      $to_delete = $request->getParameter('assigned_rights');
 
 101      foreach($to_delete as $index) {
 
 102        $right_to_delete = $assigned_rights[$index];
 
 103        $rights = str_replace($right_to_delete, '', $rights);
 
 104        $rights = str_replace(',,',',', $rights);
 
 106      $rights = trim($rights, ',');
 
 107      if (ttRoleHelper::update(array('id' => $cl_role_id,'rights'=> $rights))) {
 
 108        header('Location: role_edit.php?id='.$role['id']);
 
 111        $err->add($i18n->getKey('error.db'));
 
 113   if ($request->getParameter('btn_add') && $request->getParameter('available_rights')) {
 
 114      $rights = $role['rights'];
 
 115      $to_add = $request->getParameter('available_rights');
 
 116      foreach($to_add as $index) {
 
 117        $right_to_add = $available_rights[$index];
 
 118        // Just in case remove it.
 
 119        $rights = str_replace($right_to_add, '', $rights);
 
 120        $rights = str_replace(',,',',', $rights);
 
 121        // Add the right only if we have it ourselves.
 
 122        if (in_array($right_to_add, $user->rights_array))
 
 123          $rights .= ','.$right_to_add;
 
 125      $rights = trim($rights, ',');
 
 126      if (ttRoleHelper::update(array('id' => $cl_role_id,'rights'=> $rights))) {
 
 127        header('Location: role_edit.php?id='.$role['id']);
 
 130        $err->add($i18n->getKey('error.db'));
 
 134 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 135 $smarty->assign('title', $i18n->getKey('title.edit_role'));
 
 136 $smarty->assign('content_page_name', 'role_edit.tpl');
 
 137 $smarty->display('index.tpl');