More work in progress on roles. Implemented status edit for roles.
[timetracker.git] / role_edit.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('initialize.php');
30 import('form.Form');
31 import('ttTeamHelper'); // TODO: remove this?
32 import('ttTaskHelper'); // TODO: remove this?
33 import('ttRoleHelper');
34
35 // Access check.
36 if (!ttAccessCheck(right_manage_team)) {
37   header('Location: access_denied.php');
38   exit();
39 }
40
41 $cl_role_id = (int)$request->getParameter('id');
42
43 if ($request->isPost()) {
44   $cl_name = trim($request->getParameter('name'));
45   $cl_description = trim($request->getParameter('description'));
46   $cl_status = $request->getParameter('status');
47 } else {
48   $role = ttRoleHelper::get($cl_role_id);
49   $cl_name = $role['name'];
50   $cl_description = $role['description'];
51   $cl_status = $role['status'];
52 }
53
54 $form = new Form('roleForm');
55 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_role_id));
56 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 250px;','value'=>$cl_name));
57 $form->addInput(array('type'=>'textarea','name'=>'description','style'=>'width: 250px; height: 40px;','value'=>$cl_description));
58 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
59   'data'=>array(ACTIVE=>$i18n->getKey('dropdown.status_active'),INACTIVE=>$i18n->getKey('dropdown.status_inactive'))));
60 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->getKey('button.save')));
61
62 if ($request->isPost()) {
63   // Validate user input.
64   if (!ttValidString($cl_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.thing_name'));
65   if (!ttValidString($cl_description, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.description'));
66
67   if ($err->no()) {
68     $existing_role = ttRoleHelper::getRoleByName($cl_name);
69     if (!$existing_role || ($cl_role_id == $existing_role['id'])) {
70       // Update role information.
71       if (ttRoleHelper::update(array(
72         'id' => $cl_role_id,
73         'name' => $cl_name,
74         'description' => $cl_description,
75         'status' => $cl_status))) {
76         header('Location: roles.php');
77         exit();
78       } else
79         $err->add($i18n->getKey('error.db'));
80     } else
81       $err->add($i18n->getKey('error.object_exists'));
82   }
83 } // isPost
84
85 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
86 $smarty->assign('title', $i18n->getKey('title.edit_role'));
87 $smarty->assign('content_page_name', 'role_edit.tpl');
88 $smarty->display('index.tpl');