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');
47 $assigned_rights = explode(',', $role['rights']);
48 $available_rights = array_diff($user->rights, $assigned_rights);
50 if ($request->isPost()) {
51 $cl_name = trim($request->getParameter('name'));
52 $cl_description = trim($request->getParameter('description'));
53 $cl_rank = $request->getParameter('rank');
54 $cl_status = $request->getParameter('status');
56 $cl_name = $role['name'];
57 $cl_description = $role['description'];
58 $cl_rank = $role['rank'];
59 $cl_status = $role['status'];
62 $form = new Form('roleForm');
63 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_role_id));
64 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 250px;','value'=>$cl_name));
65 $form->addInput(array('type'=>'textarea','name'=>'description','style'=>'width: 250px; height: 40px;','value'=>$cl_description));
66 for ($i = 0; $i < $user->rank; $i++) {
69 $form->addInput(array('type'=>'combobox','name'=>'rank','data'=>$rank_data,'value'=>$cl_rank));
70 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
71 'data'=>array(ACTIVE=>$i18n->get('dropdown.status_active'),INACTIVE=>$i18n->get('dropdown.status_inactive'))));
72 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
74 // Multiple select controls for assigned and available rights.
75 $form->addInput(array('type'=>'combobox','name'=>'assigned_rights','style'=>'width: 250px;','multiple'=>true,'data'=>$assigned_rights));
76 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('button.delete')));
77 $form->addInput(array('type'=>'combobox','name'=>'available_rights','style'=>'width: 250px;','multiple'=>true,'data'=>$available_rights));
78 $form->addInput(array('type'=>'submit','name'=>'btn_add','value'=>$i18n->get('button.add')));
80 if ($request->isPost()) {
81 if ($request->getParameter('btn_save')) {
82 // Validate user input.
83 if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.thing_name'));
84 if (!ttValidString($cl_description, true)) $err->add($i18n->get('error.field'), $i18n->get('label.description'));
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');