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 (!ttAccessAllowed('manage_roles')) {
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, $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->getKey('dropdown.status_active'),INACTIVE=>$i18n->getKey('dropdown.status_inactive'))));
71 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->getKey('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->getKey('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->getKey('button.add')));
79 if ($request->isPost()) {
80 if ($request->getParameter('btn_save')) {
81 // Validate user input.
82 if (!ttValidString($cl_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.thing_name'));
83 if (!ttValidString($cl_description, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.description'));
86 $existing_role = ttRoleHelper::getRoleByName($cl_name);
87 if (!$existing_role || ($cl_role_id == $existing_role['id'])) {
88 // Update role information.
89 if (ttRoleHelper::update(array(
92 'description' => $cl_description,
93 'status' => $cl_status))) {
94 header('Location: roles.php');
97 $err->add($i18n->getKey('error.db'));
99 $err->add($i18n->getKey('error.object_exists'));
102 if ($request->getParameter('btn_delete') && $request->getParameter('assigned_rights')) {
103 $rights = $role['rights'];
104 $to_delete = $request->getParameter('assigned_rights');
105 foreach($to_delete as $index) {
106 $right_to_delete = $assigned_rights[$index];
107 $rights = str_replace($right_to_delete, '', $rights);
108 $rights = str_replace(',,',',', $rights);
110 $rights = trim($rights, ',');
111 if (ttRoleHelper::update(array('id' => $cl_role_id,'rights'=> $rights))) {
112 header('Location: role_edit.php?id='.$role['id']);
115 $err->add($i18n->getKey('error.db'));
117 if ($request->getParameter('btn_add') && $request->getParameter('available_rights')) {
118 $rights = $role['rights'];
119 $to_add = $request->getParameter('available_rights');
120 foreach($to_add as $index) {
121 $right_to_add = $available_rights[$index];
122 // Just in case remove it.
123 $rights = str_replace($right_to_add, '', $rights);
124 $rights = str_replace(',,',',', $rights);
125 // Add the right only if we have it ourselves.
126 if (in_array($right_to_add, $user->rights))
127 $rights .= ','.$right_to_add;
129 $rights = trim($rights, ',');
130 if (ttRoleHelper::update(array('id' => $cl_role_id,'rights'=> $rights))) {
131 header('Location: role_edit.php?id='.$role['id']);
134 $err->add($i18n->getKey('error.db'));
138 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
139 $smarty->assign('title', $i18n->getKey('title.edit_role'));
140 $smarty->assign('content_page_name', 'role_edit.tpl');
141 $smarty->display('index.tpl');