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('ttRoleHelper');
 
  34 if (!ttAccessAllowed('manage_roles')) {
 
  35   header('Location: access_denied.php');
 
  38 $cl_role_id = (int)$request->getParameter('id');
 
  39 $role = ttRoleHelper::get($cl_role_id);
 
  41   header('Location: access_denied.php');
 
  44 // End of access checks.
 
  46 $assigned_rights = explode(',', $role['rights']);
 
  47 $available_rights = array_diff($user->rights, $assigned_rights);
 
  49 if ($request->isPost()) {
 
  50   $cl_name = trim($request->getParameter('name'));
 
  51   $cl_description = trim($request->getParameter('description'));
 
  52   $cl_rank = $request->getParameter('rank');
 
  53   $cl_status = $request->getParameter('status');
 
  55   $cl_name = $role['name'];
 
  56   $cl_description = $role['description'];
 
  57   $cl_rank = $role['rank'];
 
  58   $cl_status = $role['status'];
 
  61 $form = new Form('roleForm');
 
  62 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_role_id));
 
  63 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 250px;','value'=>$cl_name));
 
  64 $form->addInput(array('type'=>'textarea','name'=>'description','style'=>'width: 250px; height: 40px;','value'=>$cl_description));
 
  65 for ($i = 0; $i < $user->rank; $i++) {
 
  68 $form->addInput(array('type'=>'combobox','name'=>'rank','data'=>$rank_data,'value'=>$cl_rank));
 
  69 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
 
  70   'data'=>array(ACTIVE=>$i18n->get('dropdown.status_active'),INACTIVE=>$i18n->get('dropdown.status_inactive'))));
 
  71 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
 
  73 // Multiple select controls for assigned and available rights.
 
  74 $form->addInput(array('type'=>'combobox','name'=>'assigned_rights','style'=>'width: 250px;','multiple'=>true,'data'=>$assigned_rights));
 
  75 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('button.delete')));
 
  76 $form->addInput(array('type'=>'combobox','name'=>'available_rights','style'=>'width: 250px;','multiple'=>true,'data'=>$available_rights));
 
  77 $form->addInput(array('type'=>'submit','name'=>'btn_add','value'=>$i18n->get('button.add')));
 
  79 if ($request->isPost()) {
 
  80     if ($request->getParameter('btn_save')) {
 
  81     // Validate user input.
 
  82     if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.thing_name'));
 
  83     if (!ttValidString($cl_description, true)) $err->add($i18n->get('error.field'), $i18n->get('label.description'));
 
  84     if ($cl_rank >= $user->rank || $cl_rank < 0) $err->add($i18n->get('error.field'), $i18n->get('form.roles.rank'));
 
  87       $existing_role = ttRoleHelper::getRoleByName($cl_name);
 
  88       if (!$existing_role || ($cl_role_id == $existing_role['id'])) {
 
  89         // Update role information.
 
  90         if (ttRoleHelper::update(array(
 
  94           'description' => $cl_description,
 
  95           'status' => $cl_status))) {
 
  96           header('Location: roles.php');
 
  99           $err->add($i18n->get('error.db'));
 
 101         $err->add($i18n->get('error.object_exists'));
 
 104   if ($request->getParameter('btn_delete') && $request->getParameter('assigned_rights')) {
 
 105      $rights = $role['rights'];
 
 106      $to_delete = $request->getParameter('assigned_rights');
 
 107      foreach($to_delete as $index) {
 
 108        $right_to_delete = $assigned_rights[$index];
 
 109        $rights = str_replace($right_to_delete, '', $rights);
 
 110        $rights = str_replace(',,',',', $rights);
 
 112      $rights = trim($rights, ',');
 
 113      if (ttRoleHelper::update(array('id' => $cl_role_id,'rights'=> $rights))) {
 
 114        header('Location: role_edit.php?id='.$role['id']);
 
 117        $err->add($i18n->get('error.db'));
 
 119   if ($request->getParameter('btn_add') && $request->getParameter('available_rights')) {
 
 120      $rights = $role['rights'];
 
 121      $to_add = $request->getParameter('available_rights');
 
 122      foreach($to_add as $index) {
 
 123        $right_to_add = $available_rights[$index];
 
 124        // Just in case remove it.
 
 125        $rights = str_replace($right_to_add, '', $rights);
 
 126        $rights = str_replace(',,',',', $rights);
 
 127        // Add the right only if we have it ourselves.
 
 128        if (in_array($right_to_add, $user->rights))
 
 129          $rights .= ','.$right_to_add;
 
 131      $rights = trim($rights, ',');
 
 132      if (ttRoleHelper::update(array('id' => $cl_role_id,'rights'=> $rights))) {
 
 133        header('Location: role_edit.php?id='.$role['id']);
 
 136        $err->add($i18n->get('error.db'));
 
 140 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 141 $smarty->assign('title', $i18n->get('title.edit_role'));
 
 142 $smarty->assign('content_page_name', 'role_edit.tpl');
 
 143 $smarty->display('index.tpl');