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->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'));
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(
93 'description' => $cl_description,
94 'status' => $cl_status))) {
95 header('Location: roles.php');
98 $err->add($i18n->get('error.db'));
100 $err->add($i18n->get('error.object_exists'));
103 if ($request->getParameter('btn_delete') && $request->getParameter('assigned_rights')) {
104 $rights = $role['rights'];
105 $to_delete = $request->getParameter('assigned_rights');
106 foreach($to_delete as $index) {
107 $right_to_delete = $assigned_rights[$index];
108 $rights = str_replace($right_to_delete, '', $rights);
109 $rights = str_replace(',,',',', $rights);
111 $rights = trim($rights, ',');
112 if (ttRoleHelper::update(array('id' => $cl_role_id,'rights'=> $rights))) {
113 header('Location: role_edit.php?id='.$role['id']);
116 $err->add($i18n->get('error.db'));
118 if ($request->getParameter('btn_add') && $request->getParameter('available_rights')) {
119 $rights = $role['rights'];
120 $to_add = $request->getParameter('available_rights');
121 foreach($to_add as $index) {
122 $right_to_add = $available_rights[$index];
123 // Just in case remove it.
124 $rights = str_replace($right_to_add, '', $rights);
125 $rights = str_replace(',,',',', $rights);
126 // Add the right only if we have it ourselves.
127 if (in_array($right_to_add, $user->rights))
128 $rights .= ','.$right_to_add;
130 $rights = trim($rights, ',');
131 if (ttRoleHelper::update(array('id' => $cl_role_id,'rights'=> $rights))) {
132 header('Location: role_edit.php?id='.$role['id']);
135 $err->add($i18n->get('error.db'));
139 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
140 $smarty->assign('title', $i18n->get('title.edit_role'));
141 $smarty->assign('content_page_name', 'role_edit.tpl');
142 $smarty->display('index.tpl');