Highway to PSR2
[openemr.git] / interface / modules / zend_modules / module / Acl / src / Acl / Controller / AclController.php
blob7fbe837e1cb41c245247f3da9d417b38cd4d9a19
1 <?php
2 /* +-----------------------------------------------------------------------------+
3 * OpenEMR - Open Source Electronic Medical Record
4 * Copyright (C) 2013 Z&H Consultancy Services Private Limited <sam@zhservices.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * @author Jacob T.Paul <jacob@zhservices.com>
19 * @author Basil PT <basil@zhservices.com>
21 * +------------------------------------------------------------------------------+
24 namespace Acl\Controller;
26 use Zend\Mvc\Controller\AbstractActionController;
27 use Zend\View\Model\ViewModel;
28 use Application\Listener\Listener;
30 class AclController extends AbstractActionController
32 protected $aclTable;
33 protected $listenerObject;
35 public function __construct()
37 $this->listenerObject = new Listener;
40 public function indexAction()
42 $module_id = $this->params()->fromQuery('module_id');
43 $result = $this->getAclTable()->aclSections($module_id);
45 $arrayCategories = array();
46 foreach ($result as $row) {
47 $arrayCategories[$row['section_id']] = array("parent_id" => $row['parent_section'], "name" =>
48 $row['section_name'],"id" => $row['section_id']);
51 ob_start();
52 $this->createTreeView($arrayCategories, 0);
53 $sections = ob_get_clean();
55 $user_group_main = $this->createUserGroups("user_group_", "", "draggable2");
56 $user_group_allowed = $this->createUserGroups("user_group_allowed_", "display:none;", "draggable3", "class='class_li'");
57 $user_group_denied = $this->createUserGroups("user_group_denied_", "display:none;", "draggable4", "class='class_li'");
59 $result = $this->getAclTable()->getActiveModules();
60 foreach ($result as $row) {
61 $array_active_modules[$row['mod_id']] = $row['mod_name'];
64 $index = new ViewModel(array(
65 'user_group_main' => $user_group_main,
66 'user_group_allowed' => $user_group_allowed,
67 'user_group_denied' => $user_group_denied,
68 'sections' => $sections,
69 'component_id' => "0-".$module_id,
70 'module_id' => $module_id,
71 'listenerObject' => $this->listenerObject,
72 'active_modules' => $array_active_modules,
73 ));
74 return $index;
77 public function acltabAction()
79 $module_id = $this->params()->fromQuery('module_id');
80 $this->layout('layout/layout_tabs');
81 $index = new ViewModel(array(
82 'mod_id' => $module_id,
83 ));
84 return $index;
87 public function aclAction()
89 $module_id = $this->params()->fromQuery('module_id');
90 $data = $this->getAclTable()->getGroups();
92 $user_groups = array();
93 foreach ($data as $row) {
94 $user_groups[$row['id']] = $row['name'];
97 $data = $this->getAclTable()->aclSections($module_id);
98 $module_data = array();
99 $module_data['module_components'] = array();
100 foreach ($data as $row) {
101 if ($row['parent_section'] == 0) {
102 $module_data['module_name'] = array(
103 'id' => $row['section_id'],
104 'name' => $row['section_name']
106 } else {
107 $module_data['module_components'][$row['section_id']] = $row['section_name'];
111 $data = $this->getAclTable()->getGroupAcl($module_id);
112 $saved_ACL = array();
113 foreach ($data as $row) {
114 if (!$saved_ACL[$row['section_id']]) {
115 $saved_ACL[$row['section_id']] = array();
118 array_push($saved_ACL[$row['section_id']], $row['group_id']);
121 $acl_view = new ViewModel(
122 array(
123 'user_groups' => $user_groups,
124 'listenerObject' => $this->listenerObject,
125 'module_data' => $module_data,
126 'module_id' => $module_id,
127 'acl_data' => $saved_ACL
130 return $acl_view;
133 public function ajaxAction()
135 $ajax_mode = $this->getRequest()->getPost('ajax_mode', null);
136 if ($ajax_mode == "save_acl") {
137 $selected_componet = $this->getRequest()->getPost('selected_module', null);
138 $selected_componet_arr = explode("-", $selected_componet);
139 if ($selected_componet_arr[0] == 0) {
140 $selected_componet_arr[0] = $selected_componet_arr[1];
143 $allowed_users = json_decode($this->getRequest()->getPost('allowed_users', null));
144 $denied_users = json_decode($this->getRequest()->getPost('denied_users', null));
146 $allowed_users = array_unique($allowed_users);
147 $denied_users = array_unique($denied_users);
149 // Delete Saved ACL Data
150 $data = $this->getAclTable()->deleteGroupACL($selected_componet_arr[0], $selected_componet_arr[1]);
151 $data = $this->getAclTable()->deleteUserACL($selected_componet_arr[0], $selected_componet_arr[1]);
153 // Allowed
154 foreach ($allowed_users as $allowed_user) {
155 $id = str_replace("li_user_group_allowed_", "", $allowed_user);
156 $arr_id = explode("-", $id);
158 if ($arr_id[1] == 0) {
159 $data = $this->getAclTable()->insertGroupACL($selected_componet_arr[0], $arr_id[0], $selected_componet_arr[1], 1);
160 } else {
161 $data = $this->getAclTable()->insertUserACL($selected_componet_arr[0], $arr_id[1], $selected_componet_arr[1], 1);
165 // Denied
166 foreach ($denied_users as $denied_user) {
167 $id = str_replace("li_user_group_denied_", "", $denied_user);
168 $arr_id = explode("-", $id);
170 if ($arr_id[1] == 0) {
171 $data = $this->getAclTable()->insertGroupACL($selected_componet_arr[0], $arr_id[0], $selected_componet_arr[1], 0);
172 } else {
173 $data = $this->getAclTable()->insertuserACL($selected_componet_arr[0], $arr_id[1], $selected_componet_arr[1], 0);
176 } elseif ($ajax_mode == "rebuild") {
177 $selected_componet = $_REQUEST['selected_module'];
178 $selected_componet_arr = explode("-", $selected_componet);
179 if ($selected_componet_arr[0] == 0) {
180 $selected_componet_arr[0] = $selected_componet_arr[1];
183 $array_users_allowed = array();
184 $array_users_denied = array();
185 $array_groups_allowed = array();
186 $array_groups_denied = array();
188 $res_users = $this->getAclTable()->getAclDataUsers($selected_componet_arr[1]);
189 foreach ($res_users as $row) {
190 if ($row['allowed'] == 1) {
191 if (!$array_users_allowed[$row['group_id']]) {
192 $array_users_allowed[$row['group_id']] = array();
195 array_push($array_users_allowed[$row['group_id']], $row['user_id']);
196 } else {
197 if (!$array_users_denied[$row['group_id']]) {
198 $array_users_denied[$row['group_id']] = array();
201 array_push($array_users_denied[$row['group_id']], $row['user_id']);
205 $res_group = $this->getAclTable()->getAclDataGroups($selected_componet_arr[1]);
206 foreach ($res_group as $row) {
207 if ($row['allowed'] == 1) {
208 array_push($array_groups_allowed, $row['group_id']);
209 } else {
210 array_push($array_groups_denied, $row['group_id']);
214 $arr_return = array();
215 $arr_return['group_allowed'] = $array_groups_allowed;
216 $arr_return['group_denied'] = $array_groups_denied;
217 $arr_return['user_allowed'] = $array_users_allowed;
218 $arr_return['user_denied'] = $array_users_denied;
219 echo json_encode($arr_return);
220 } elseif ($ajax_mode == "save_acl_advanced") {
221 $ACL_DATA = json_decode($this->getRequest()->getPost('acl_data', null), true);
222 $module_id = $this->getRequest()->getPost('module_id', null);
223 $this->getAclTable()->deleteModuleGroupACL($module_id);
225 foreach ($ACL_DATA['allowed'] as $section_id => $sections) {
226 foreach ($sections as $group_id) {
227 $this->getAclTable()->deleteUserACL($module_id, $section_id);
228 $this->getAclTable()->insertGroupACL($module_id, $group_id, $section_id, 1);
232 foreach ($ACL_DATA['denied'] as $section_id => $sections) {
233 foreach ($sections as $group_id) {
234 $this->getAclTable()->deleteUserACL($module_id, $section_id);
235 $this->getAclTable()->insertGroupACL($module_id, $group_id, $section_id, 0);
238 } elseif ($ajax_mode == "get_sections_by_module") {
239 $module_id = $this->getRequest()->getPost('module_id', null);
240 $result = $this->getAclTable()->getModuleSections($module_id);
242 $array_sections = array();
243 foreach ($result as $row) {
244 $array_sections[$row['section_id']] = $row['section_name'];
247 echo json_encode($array_sections);
248 } elseif ($ajax_mode == "save_sections_by_module") {
249 $module_id = $this->getRequest()->getPost('mod_id', null);
250 $parent_id = $this->getRequest()->getPost('parent_id', null);
251 $section_identifier = $this->getRequest()->getPost('section_identifier', null);
252 $section_name = $this->getRequest()->getPost('section_name', null);
254 if (!$parent_id) {
255 $parent_id = $module_id;
258 $current_section_id = $this->getAclTable()->getSectionsInsertId();
259 $this->getAclTable()->saveACLSections($module_id, $parent_id, $section_identifier, $section_name, $current_section_id);
262 exit();
268 * Function to Print Componets Tree Structure
269 * @param String $currentParent Root Node of Tree
270 * @param String $currLevel Current Depth of Tree
271 * @param String $prevLevel Prev Depth of Tree
274 private function createTreeView($array, $currentParent, $currLevel = 0, $prevLevel = -1)
276 /** Html Escape Function */
277 $viewHelperManager = $this->getServiceLocator()->get('ViewHelperManager');
278 $escapeHtml = $viewHelperManager->get('escapeHtml');
280 foreach ($array as $categoryId => $category) {
281 if ($category['name']=='') {
282 continue;
285 if ($currentParent == $category['parent_id']) {
286 if ($currLevel > $prevLevel) {
287 echo " <ul> ";
290 if ($currLevel == $prevLevel) {
291 echo " </li> ";
294 $class="";
295 echo '<li id="'.$category['parent_id']."-".$category['id'].'" value="'.$escapeHtml($category['name']).'" '.$escapeHtml($class).' ><div onclick="selectThis(\''.$escapeHtml($category['parent_id']).'-'.$escapeHtml($category['id']).'\');rebuild();" class="list">'.$escapeHtml($category['name'])."</div>";
296 if ($currLevel > $prevLevel) {
297 $prevLevel = $currLevel;
300 $currLevel++;
301 $this->createTreeView($array, $categoryId, $currLevel, $prevLevel);
302 $currLevel--;
306 if ($currLevel == $prevLevel) {
307 echo "</li></ul> ";
313 * Function to Print User group Tree Structure
314 * @param String $id String to Prepend with <li> Id
315 * @param String $visibility <li> Visibility
316 * @param String $dragabble Class to Make <li> Title Draggable
317 * @param String $li_class <li> Class Name
320 private function createUserGroups($id = "user_group_", $visibility = "", $dragabble = "draggable", $li_class = "")
322 /** Html Escape Function */
323 $viewHelperManager = $this->getServiceLocator()->get('ViewHelperManager');
324 $escapeHtml = $viewHelperManager->get('escapeHtml');
326 $output_string = "";
327 $res_users = $this->getAclTable()->aclUserGroupMapping();
329 $tempList = array();
330 foreach ($res_users as $row) {
331 $tempList[$row['group_id']]['group_name'] = $row['group_name'];
332 $tempList[$row['group_id']]['group_id'] = $row['group_id'];
333 $tempList[$row['group_id']]['items'][] = $row;
336 $output_string .='<ul>';
337 foreach ($tempList as $groupID => $tempListRow) {
338 $output_string .='<li '.$li_class.' id="li_'.$id.$tempListRow['group_id'].'-0" style="'.$visibility.'"><div class="'.$escapeHtml($dragabble).'" id="'.$id.$tempListRow['group_id'].'-0" >' . $escapeHtml($tempListRow['group_name']).'</div>';
339 if (!empty($tempListRow['items'])) {
340 $output_string .='<ul>';
341 foreach ($tempListRow['items'] as $key => $itemRow) {
342 $output_string .='<li '.$li_class.' id="li_'.$id.$itemRow['group_id'].'-'.$itemRow['user_id'].'" style="'.$visibility.'"><div class="'.$escapeHtml($dragabble).'" id="'.$id.$itemRow['group_id'].'-'.$itemRow['user_id'].'">' . $escapeHtml($itemRow['display_name']) . '</div></li>';
345 $output_string .='</ul>';
348 $output_string .='</li>';
351 $output_string .='</ul>';
352 return $output_string;
356 * Table Gateway
358 * @return type
360 public function getAclTable()
362 if (!$this->aclTable) {
363 $sm = $this->getServiceLocator();
364 $this->aclTable = $sm->get('Acl\Model\AclTable');
367 return $this->aclTable;