Adjusted plugins.php page to support subgroups.
[timetracker.git] / plugins.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
32 // Access checks.
33 if (!ttAccessAllowed('manage_features')) {
34   header('Location: access_denied.php');
35   exit();
36 }
37 if ($request->isPost() && $request->getParameter('group_changed') && !$user->isGroupValid($request->getParameter('group'))) {
38   header('Location: access_denied.php'); // Wrong group id in post.
39   exit();
40 }
41 // End of access checks.
42
43 // TODO: re-structure the file, perhaps by separating post and get processing into whole different sections.
44 if ($request->isPost() && $request->getParameter('group_changed')) {
45   $group_id = $request->getParameter('group');
46   $user->setOnBehalfGroup($group_id);
47 } else {
48   $group_id = $user->getGroup();
49 }
50
51 if ($request->isPost()) {
52   // Plugin checkboxes.
53   $cl_charts = $request->getParameter('charts');
54   $cl_clients = $request->getParameter('clients');
55   $cl_client_required = $request->getParameter('client_required');
56   $cl_invoices = $request->getParameter('invoices');
57   $cl_paid_status = $request->getParameter('paid_status');
58   $cl_custom_fields = $request->getParameter('custom_fields');
59   $cl_expenses = $request->getParameter('expenses');
60   $cl_tax_expenses = $request->getParameter('tax_expenses');
61   $cl_notifications = $request->getParameter('notifications');
62   $cl_locking = $request->getParameter('locking');
63   $cl_quotas = $request->getParameter('quotas');
64   $cl_week_view = $request->getParameter('week_view');
65   $cl_work_units = $request->getParameter('work_units');
66 } else {
67   // Which plugins do we have enabled?
68   $plugins = explode(',', $user->getPlugins());
69   $cl_charts = in_array('ch', $plugins);
70   $cl_clients = in_array('cl', $plugins);
71   $cl_client_required = in_array('cm', $plugins);
72   $cl_invoices = in_array('iv', $plugins);
73   $cl_paid_status = in_array('ps', $plugins);
74   $cl_custom_fields = in_array('cf', $plugins);
75   $cl_expenses = in_array('ex', $plugins);
76   $cl_tax_expenses = in_array('et', $plugins);
77   $cl_notifications = in_array('no', $plugins);
78   $cl_locking = in_array('lk', $plugins);
79   $cl_quotas = in_array('mq', $plugins);
80   $cl_week_view = in_array('wv', $plugins);
81   $cl_work_units = in_array('wu', $plugins);
82 }
83
84 $form = new Form('pluginsForm');
85
86 if ($user->can('manage_subgroups')) {
87   $groups = $user->getGroupsForDropdown();
88   if (count($groups) > 1) {
89     $form->addInput(array('type'=>'combobox',
90       'onchange'=>'document.pluginsForm.group_changed.value=1;document.pluginsForm.submit();',
91       'name'=>'group',
92       'style'=>'width: 250px;',
93       'value'=>$group_id,
94       'data'=>$groups,
95       'datakeys'=>array('id','name')));
96     $form->addInput(array('type'=>'hidden','name'=>'group_changed'));
97     $smarty->assign('group_dropdown', 1);
98   }
99 }
100
101 // Plugin checkboxes.
102 $form->addInput(array('type'=>'checkbox','name'=>'charts','value'=>$cl_charts));
103 $form->addInput(array('type'=>'checkbox','name'=>'clients','value'=>$cl_clients,'onchange'=>'handlePluginCheckboxes()'));
104 $form->addInput(array('type'=>'checkbox','name'=>'client_required','value'=>$cl_client_required));
105 $form->addInput(array('type'=>'checkbox','name'=>'invoices','value'=>$cl_invoices));
106 $form->addInput(array('type'=>'checkbox','name'=>'paid_status','value'=>$cl_paid_status));
107 $form->addInput(array('type'=>'checkbox','name'=>'custom_fields','value'=>$cl_custom_fields,'onchange'=>'handlePluginCheckboxes()'));
108 $form->addInput(array('type'=>'checkbox','name'=>'expenses','value'=>$cl_expenses,'onchange'=>'handlePluginCheckboxes()'));
109 $form->addInput(array('type'=>'checkbox','name'=>'tax_expenses','value'=>$cl_tax_expenses));
110 $form->addInput(array('type'=>'checkbox','name'=>'notifications','value'=>$cl_notifications,'onchange'=>'handlePluginCheckboxes()'));
111 $form->addInput(array('type'=>'checkbox','name'=>'locking','value'=>$cl_locking,'onchange'=>'handlePluginCheckboxes()'));
112 $form->addInput(array('type'=>'checkbox','name'=>'quotas','value'=>$cl_quotas,'onchange'=>'handlePluginCheckboxes()'));
113 $form->addInput(array('type'=>'checkbox','name'=>'week_view','value'=>$cl_week_view,'onchange'=>'handlePluginCheckboxes()'));
114 $form->addInput(array('type'=>'checkbox','name'=>'work_units','value'=>$cl_work_units,'onchange'=>'handlePluginCheckboxes()'));
115
116 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
117
118 $form->setValueByElement('group_changed','');
119
120 if ($request->isPost()) {
121   if ($request->getParameter('group_changed')) {
122     // User changed the group in dropdown.
123     // Redirect to self.
124     header('Location: plugins.php');
125     exit();
126   }
127
128   // Validate user input. Do we have to?
129
130   if ($err->no()) {
131     // Prepare plugins string.
132     if ($cl_charts)
133       $plugins .= ',ch';
134     if ($cl_clients)
135       $plugins .= ',cl';
136     if ($cl_client_required)
137       $plugins .= ',cm';
138     if ($cl_invoices)
139       $plugins .= ',iv';
140     if ($cl_paid_status)
141       $plugins .= ',ps';
142     if ($cl_custom_fields)
143       $plugins .= ',cf';
144     if ($cl_expenses)
145       $plugins .= ',ex';
146     if ($cl_tax_expenses)
147       $plugins .= ',et';
148     if ($cl_notifications)
149       $plugins .= ',no';
150     if ($cl_locking)
151       $plugins .= ',lk';
152     if ($cl_quotas)
153       $plugins .= ',mq';
154     if ($cl_week_view)
155       $plugins .= ',wv';
156     if ($cl_work_units)
157       $plugins .= ',wu';
158
159     // Recycle week view plugin options as they are not configured on this page.
160     $existing_plugins = explode(',', $user->getPlugins());
161     if (in_array('wvn', $existing_plugins))
162       $plugins .= ',wvn';
163     if (in_array('wvl', $existing_plugins))
164       $plugins .= ',wvl';
165     if (in_array('wvns', $existing_plugins))
166       $plugins .= ',wvns';
167
168     $plugins = trim($plugins, ',');
169
170     if ($user->updateGroup(array(
171       'plugins' => $plugins))) {
172       header('Location: success.php');
173       exit();
174     } else
175       $err->add($i18n->get('error.db'));
176   }
177 } // isPost
178
179 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
180 $smarty->assign('onload', 'onLoad="handlePluginCheckboxes();"');
181 $smarty->assign('title', $i18n->get('title.plugins'));
182 $smarty->assign('content_page_name', 'plugins.tpl');
183 $smarty->display('index.tpl');