Modifications for module installer in openemr.
[openemr.git] / interface / modules / zend_modules / module / Acl / src / Acl / Controller / AclController.php
blobaaa1a23eeac1cf59e210f3bed3105c64884a8d07
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'];
63 $index = new ViewModel(array(
64 'user_group_main' => $user_group_main,
65 'user_group_allowed' => $user_group_allowed,
66 'user_group_denied' => $user_group_denied,
67 'sections' => $sections,
68 'component_id' => "0-".$module_id,
69 'module_id' => $module_id,
70 'listenerObject' => $this->listenerObject,
71 'active_modules' => $array_active_modules,
72 ));
73 return $index;
76 public function acltabAction()
78 $module_id = $this->params()->fromQuery('module_id');
79 $this->layout('layout/layout_tabs');
80 $index = new ViewModel(array(
81 'mod_id' => $module_id,
82 ));
83 return $index;
86 public function aclAction()
88 $module_id = $this->params()->fromQuery('module_id');
89 $data = $this->getAclTable()->getGroups();
91 $user_groups = array();
92 foreach($data as $row){
93 $user_groups[$row['id']] = $row['name'];
96 $data = $this->getAclTable()->aclSections($module_id);
97 $module_data = array();
98 $module_data['module_components'] = array();
99 foreach($data as $row){
100 if($row['parent_section'] == 0){
101 $module_data['module_name'] = array(
102 'id' => $row['section_id'],
103 'name' => $row['section_name']
105 }else{
106 $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']]) $saved_ACL[$row['section_id']] = array();
115 array_push($saved_ACL[$row['section_id']],$row['group_id']);
118 $acl_view = new ViewModel(
119 array(
120 'user_groups' => $user_groups,
121 'listenerObject' => $this->listenerObject,
122 'module_data' => $module_data,
123 'module_id' => $module_id,
124 'acl_data' => $saved_ACL
127 return $acl_view;
131 public function ajaxAction()
133 $ajax_mode = $this->getRequest()->getPost('ajax_mode', null);
134 if($ajax_mode == "save_acl"){
135 $selected_componet = $this->getRequest()->getPost('selected_module', null);
136 $selected_componet_arr = explode("-",$selected_componet);
137 if($selected_componet_arr[0] == 0) $selected_componet_arr[0] = $selected_componet_arr[1];
139 $allowed_users = json_decode($this->getRequest()->getPost('allowed_users', null));
140 $denied_users = json_decode($this->getRequest()->getPost('denied_users', null));
142 $allowed_users = array_unique($allowed_users);
143 $denied_users = array_unique($denied_users);
145 // Delete Saved ACL Data
146 $data = $this->getAclTable()->deleteGroupACL($selected_componet_arr[0],$selected_componet_arr[1]);
147 $data = $this->getAclTable()->deleteUserACL($selected_componet_arr[0],$selected_componet_arr[1]);
149 // Allowed
150 foreach($allowed_users as $allowed_user){
151 $id = str_replace("li_user_group_allowed_","",$allowed_user);
152 $arr_id = explode("-",$id);
154 if($arr_id[1] == 0){
155 $data = $this->getAclTable()->insertGroupACL($selected_componet_arr[0],$arr_id[0],$selected_componet_arr[1],1);
156 }else{
157 $data = $this->getAclTable()->insertUserACL($selected_componet_arr[0],$arr_id[1],$selected_componet_arr[1],1);
161 // Denied
162 foreach($denied_users as $denied_user){
163 $id = str_replace("li_user_group_denied_","",$denied_user);
164 $arr_id = explode("-",$id);
166 if($arr_id[1] == 0){
167 $data = $this->getAclTable()->insertGroupACL($selected_componet_arr[0],$arr_id[0],$selected_componet_arr[1],0);
168 }else{
169 $data = $this->getAclTable()->insertuserACL($selected_componet_arr[0],$arr_id[1],$selected_componet_arr[1],0);
172 }elseif($ajax_mode == "rebuild"){
173 $selected_componet = $_REQUEST['selected_module'];
174 $selected_componet_arr = explode("-",$selected_componet);
175 if($selected_componet_arr[0] == 0) $selected_componet_arr[0] = $selected_componet_arr[1];
177 $array_users_allowed = array();
178 $array_users_denied = array();
179 $array_groups_allowed = array();
180 $array_groups_denied = array();
182 $res_users = $this->getAclTable()->getAclDataUsers($selected_componet_arr[1]);
183 foreach($res_users as $row){
184 if($row['allowed'] == 1){
185 if(!$array_users_allowed[$row['group_id']]) $array_users_allowed[$row['group_id']] = array();
186 array_push($array_users_allowed[$row['group_id']],$row['user_id']);
187 }else{
188 if(!$array_users_denied[$row['group_id']]) $array_users_denied[$row['group_id']] = array();
189 array_push($array_users_denied[$row['group_id']],$row['user_id']);
192 $res_group = $this->getAclTable()->getAclDataGroups($selected_componet_arr[1]);
193 foreach($res_group as $row){
194 if($row['allowed'] == 1){
195 array_push($array_groups_allowed,$row['group_id']);
196 }else{
197 array_push($array_groups_denied,$row['group_id']);
201 $arr_return = array();
202 $arr_return['group_allowed'] = $array_groups_allowed;
203 $arr_return['group_denied'] = $array_groups_denied;
204 $arr_return['user_allowed'] = $array_users_allowed;
205 $arr_return['user_denied'] = $array_users_denied;
206 echo json_encode($arr_return);
207 }elseif($ajax_mode == "save_acl_advanced"){
208 $ACL_DATA = json_decode($this->getRequest()->getPost('acl_data', null),true);
209 $module_id = $this->getRequest()->getPost('module_id', null);
210 $this->getAclTable()->deleteModuleGroupACL($module_id);
212 foreach($ACL_DATA['allowed'] as $section_id => $sections){
213 foreach($sections as $group_id){
214 $this->getAclTable()->deleteUserACL($module_id,$section_id);
215 $this->getAclTable()->insertGroupACL($module_id,$group_id,$section_id,1);
219 foreach($ACL_DATA['denied'] as $section_id => $sections){
220 foreach($sections as $group_id){
221 $this->getAclTable()->deleteUserACL($module_id,$section_id);
222 $this->getAclTable()->insertGroupACL($module_id,$group_id,$section_id,0);
225 }elseif($ajax_mode == "get_sections_by_module"){
226 $module_id = $this->getRequest()->getPost('module_id', null);
227 $result = $this->getAclTable()->getModuleSections($module_id);
229 $array_sections = array();
230 foreach($result as $row){
231 $array_sections[$row['section_id']] = $row['section_name'];
233 echo json_encode($array_sections);
234 }elseif($ajax_mode == "save_sections_by_module"){
235 $module_id = $this->getRequest()->getPost('mod_id', null);
236 $parent_id = $this->getRequest()->getPost('parent_id', null);
237 $section_identifier = $this->getRequest()->getPost('section_identifier', null);
238 $section_name = $this->getRequest()->getPost('section_name', null);
240 if(!$parent_id) $parent_id = $module_id;
241 $current_section_id = $this->getAclTable()->getSectionsInsertId();
242 $this->getAclTable()->saveACLSections($module_id,$parent_id,$section_identifier,$section_name,$current_section_id);
244 exit();
250 * Function to Print Componets Tree Structure
251 * @param String $currentParent Root Node of Tree
252 * @param String $currLevel Current Depth of Tree
253 * @param String $prevLevel Prev Depth of Tree
256 private function createTreeView($array, $currentParent, $currLevel = 0, $prevLevel = -1)
258 /** Html Escape Function */
259 $viewHelperManager = $this->getServiceLocator()->get('ViewHelperManager');
260 $escapeHtml = $viewHelperManager->get('escapeHtml');
262 foreach($array as $categoryId => $category) {
263 if($category['name']=='') continue;
264 if ($currentParent == $category['parent_id']) {
265 if ($currLevel > $prevLevel) echo " <ul> ";
266 if ($currLevel == $prevLevel) echo " </li> ";
267 $class="";
268 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>";
269 if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }
270 $currLevel++;
271 $this->createTreeView ($array, $categoryId, $currLevel, $prevLevel);
272 $currLevel--;
275 if ($currLevel == $prevLevel) echo "</li></ul> ";
280 * Function to Print User group Tree Structure
281 * @param String $id String to Prepend with <li> Id
282 * @param String $visibility <li> Visibility
283 * @param String $dragabble Class to Make <li> Title Draggable
284 * @param String $li_class <li> Class Name
287 private function createUserGroups($id="user_group_",$visibility="",$dragabble="draggable",$li_class="")
289 /** Html Escape Function */
290 $viewHelperManager = $this->getServiceLocator()->get('ViewHelperManager');
291 $escapeHtml = $viewHelperManager->get('escapeHtml');
293 $output_string = "";
294 $res_users = $this->getAclTable()->aclUserGroupMapping();
296 $tempList = array();
297 foreach($res_users as $row){
298 $tempList[$row['group_id']]['group_name'] = $row['group_name'];
299 $tempList[$row['group_id']]['group_id'] = $row['group_id'];
300 $tempList[$row['group_id']]['items'][] = $row;
303 $output_string .='<ul>';
304 foreach ($tempList as $groupID => $tempListRow) {
305 $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>';
306 if(!empty($tempListRow['items'])) {
307 $output_string .='<ul>';
308 foreach ($tempListRow['items'] as $key => $itemRow){
309 $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>';
311 $output_string .='</ul>';
313 $output_string .='</li>';
315 $output_string .='</ul>';
316 return $output_string;
320 * Table Gateway
322 * @return type
324 public function getAclTable()
326 if (!$this->aclTable) {
327 $sm = $this->getServiceLocator();
328 $this->aclTable = $sm->get('Acl\Model\AclTable');
330 return $this->aclTable;