Modifications for module installer in openemr.
[openemr.git] / interface / modules / zend_modules / public / js / acl / acl.js
blob8bbc63317bb20680a5928bb0f779000ce7fa06be
1 /* +-----------------------------------------------------------------------------+
2  *    OpenEMR - Open Source Electronic Medical Record
3  *    Copyright (C) 2013 Z&H Consultancy Services Private Limited <sam@zhservices.com>
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the GNU Affero General Public License as
7  *    published by the Free Software Foundation, either version 3 of the
8  *    License, or (at your option) any later version.
9  *
10  *    This program is distributed in the hope that it will be useful,
11  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *    GNU Affero General Public License for more details.
14  *
15  *    You should have received a copy of the GNU Affero General Public License
16  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *    @author  Jacob T.Paul <jacob@zhservices.com>
18  *    @author  Basil P T <basil@zhservices.com>
19  * +------------------------------------------------------------------------------+
20  */
22 // Global variables
23 var selected_user = '';
24 var selected_component = '';
25 var allowed_array = new Array();
26 var denied_array = new Array();
29 function createtree() {
30   $("#td_componets,#td_users,#td_allowed_users,#td_denied_users").treeview({
31     animated: "fast",
32     collapsed: true,
33     control: "#control_div"
34   });
37 $(document).mouseup(function(e) {
38   var container = $(".popup_items");
39   if (!container.is(e.target) && container.has(e.target).length === 0) {
40     $(".popup_items").css("display", "none");
41   }
42 });
44 $(window).load(function() {
45   $(".scrollable").customScrollbar();
46 });
48 $(document).one("mouseover", function() {
49   $(".scrollable").customScrollbar("resize");
50   $("#expand_tree").click();
51 });
53 $(document).ready(function() {
54   $(".popup_items").draggable();
55   createtree();
56   $(".draggable2").draggable({
57     containment: "#outer_table",
58     scroll: false,
59     revert: true,
60     drag: function() {
61       selected_user = this.id;
62     },
63   });
64   $(".draggable3").draggable({
65     containment: "#outer_table",
66     scroll: false,
67     revert: true,
68     drag: function() {
69       selected_user = this.id;
70     },
71   });
72   $(".draggable4").draggable({
73     containment: "#outer_table",
74     scroll: false,
75     revert: true,
76     drag: function() {
77       selected_user = this.id;
78     },
79   });
80   $(".delete_droppable").droppable({
81     accept: ".draggable3,.draggable4",
82     activeClass: "deleted",
83     over: function(event, ui) {
84       $(".delete_droppable").addClass("red");
85       setTimeout(function() {
86         $(".delete_droppable").removeClass("red");
87       }, 400);
88     },
89     drop: function(event, ui) {
90                         var user_delete         = false;
91                         var invalid_group = false;
92       var arr_id = selected_user.split("-");
93       if (arr_id[1] == 0) {
94                                 if($("#" + selected_user).css("opacity") == "1"){
95                                         $("#li_" + selected_user).css("display", "none");
96                                         $("#li_" + selected_user).find('li').css("display", "none");
97                                 } else {
98                                         invalid_group = true;
99                                 }
100         
101       } else {
102                                 user_delete = true;
103       }
105       if (selected_user.indexOf("user_group_allowed_") != -1) {
106         if (arr_id[1] == 0 && !user_delete && !invalid_group) {
107           index = allowed_array.indexOf("li_" + selected_user);
108           if (index != -1) allowed_array.splice(index, 1);
109           else {
110             $("#li_" + selected_user).find("li").each(function() {
111               index_1 = allowed_array.indexOf($(this).attr("id"));
112               if (index_1 != -1) allowed_array.splice(index_1, 1);
113             });
114           }
115         } 
118       } else if (selected_user.indexOf("user_group_denied_") != -1) {
119         if (arr_id[1] == 0 && !user_delete && !invalid_group) {
120           index = denied_array.indexOf("li_" + selected_user);
121           if (index != -1) denied_array.splice(index, 1);
122           else {
123             $("#li_" + selected_user).find("li").each(function() {
124               index_1 = denied_array.indexOf($(this).attr("id"));
125               if (index_1 != -1) denied_array.splice(index_1, 1);
126             });
127           }
128         } 
129       }
130                         if(user_delete){
131                                 var resultTranslated = js_xl('User Cannot be Deleted');
132                                 $("#messages").html(resultTranslated.msg);
133                                 setTimeout(function() {
134                                 $("#messages").html("");
135       }, 3000);
136                         } else if(invalid_group) {
137                                 var resultTranslated = js_xl('Please Select an Active Group');
138                                 $("#messages").html(resultTranslated.msg);
139                                 setTimeout(function() {
140                                         $("#messages").html("");
141                                 }, 3000);
142                         } else {
143                                 saveAcl();
144                         }
145       
146     },
147   });
148   $(".droppableAllowed").droppable({
149     accept: ".draggable2,.draggable4",
150     drop: function() {
151       var dragged_from_denied = false;
152       if (selected_user.indexOf("user_group_denied_") != -1) {
153         dragged_from_denied = true;
154         new_id = selected_user.replace("user_group_denied_", "user_group_allowed_");
155         denied_id = selected_user;
156         var arr_id = selected_user.split("-");
157         if (arr_id[1] != 0) {
158           var li_cout = 0;
159           $($($("#li_" + selected_user).parent().get(0)).parent().get(0)).find("li").each(function() {
160             if ($(this).css("display") == "list-item") li_cout++;
161           });
162           $("#li_" + selected_user).css("display", "none");
163         }
164       } else {
165         new_id = selected_user.replace("user_group_", "user_group_allowed_");
166         denied_id = selected_user.replace("user_group_", "user_group_denied_");
167       }
168                         target_visibility = $("#li_" + new_id).css("display");
169       $("#li_" + new_id).css("display", "");
170       var arr_id = selected_user.split("-");
171       if (arr_id[1] == 0) {
172         // Add to array -- allowed
173         if (dragged_from_denied) {
174                                         source_opacity = $("#"+selected_user).css("opacity");
175                                         target_opacity = $("#" + new_id).css("opacity");
176                                         new_opacity              = (target_opacity == "1" && target_visibility != "none") ? "1" : source_opacity;
177           $("#" + new_id).css("opacity",new_opacity);
178           index = denied_array.indexOf("li_" + selected_user);
179           if (index != -1) {
180             if (allowed_array.indexOf(new_id) == -1) allowed_array.push("li_" + new_id);
181             $("#li_" + new_id).find("li").each(function() {
182               child_id = $(this).attr("id");
183               index = allowed_array.indexOf(child_id);
184               if (index != -1) allowed_array.splice(index, 1);
185             });
186           } else {
187             if (allowed_array.indexOf(new_id) == -1) {
188               $("#li_" + selected_user).find("li").each(function() {
189                 if ($(this).css("display") == "list-item") {
190                   id = $(this).attr("id").replace("user_group_denied_", "user_group_allowed_");
191                   if (allowed_array.indexOf(id) == -1) {
192                     allowed_array.push(id);
193                   }
194                 }
195               });
196             }
197           }
198         } else {
199           if (allowed_array.indexOf(new_id) == -1) allowed_array.push("li_" + new_id);
200           $("#li_" + new_id).find("li").each(function() {
201             child_id = $(this).attr("id");
202             index = allowed_array.indexOf(child_id);
203             if (index != -1) allowed_array.splice(index, 1);
204           });
205         }
207         // Remove From Denied Array
208         index = denied_array.indexOf("li_" + denied_id);
209         if (index != -1) denied_array.splice(index, 1);
210         parent_id = $($($("#li_" + denied_id).parent().get(0)).parent().get(0)).attr("id");
211         $("#li_" + denied_id).find("li").each(function() {
212           child_id = $(this).attr("id");
213           index = denied_array.indexOf(child_id);
214           if (index != -1) denied_array.splice(index, 1);
215         });
217         if (dragged_from_denied) {
218           index = denied_array.indexOf("li_" + selected_user);
219           if (index != -1) $("#li_" + new_id).find('li').css("display", "");
220           else {
221             $("#li_" + selected_user).find("li").each(function() {
222               if ($(this).css("display") == "list-item") {
223                 id = $(this).attr("id").replace("user_group_denied_", "user_group_allowed_");
224                 $("#" + id).css("display", "");
225                 $("#" + id).find("div:eq(1)").css("opacity", "1");
226               }
227             });
228           }
229         } else {
230           $("#li_" + new_id).find('li').css("display", "");
231           $("#li_" + new_id).find("div:eq(1)").css("opacity", "1");
232         }
233         $("#li_" + denied_id).find('li').css("display", "none");
234         $("#li_" + denied_id).css("display", "none");
236       } else {
237         // Add to array -- allowed
238         parent_id = $($($("#li_" + new_id).parent().get(0)).parent().get(0)).attr("id");
239         if (allowed_array.indexOf("li_" + new_id) == -1 && allowed_array.indexOf(parent_id) == -1) allowed_array.push("li_" + new_id);
241         $($($("#li_" + new_id).parent().get(0)).parent().get(0)).css("display", "");
242         if (new_id.substr(-2) != "-0" && (allowed_array.indexOf(parent_id) == -1 || !dragged_from_denied)) {
243           $($($("#li_" + new_id).parent().get(0)).parent().get(0)).find("div:eq(1)").css("opacity", "0.5");
244         }
245         var li_cout = 0;
246         $("#li_" + denied_id).css("display", "none");
247         $($($("#li_" + denied_id).parent().get(0)).parent().get(0)).find("li").each(function() {
248           if ($(this).css("display") == "list-item") li_cout++;
249         });
250                                 if (li_cout == 0 && $("#"+parent_id.replace("li_","").replace("allowed","denied")).css("opacity") != "1") {
251           $($($("#li_" + denied_id).parent().get(0)).parent().get(0)).css("display", "none");
252         }
254         index = denied_array.indexOf("li_" + denied_id);
255         if (index != -1) denied_array.splice(index, 1);
256       }
257       saveAcl();
258     }
259   });
260   $(".droppableDenied").droppable({
261     accept: ".draggable2,.draggable3",
262     drop: function() {
263       var dragged_from_allowed = false;
264       if (selected_user.indexOf("user_group_allowed_") != -1) {
265         dragged_from_allowed = true;
266         new_id = selected_user.replace("user_group_allowed_", "user_group_denied_");
267         denied_id = selected_user;
268         var arr_id = selected_user.split("-");
269         if (arr_id[1] != 0) {
270           var li_cout = 0;
271           $($($("#li_" + selected_user).parent().get(0)).parent().get(0)).find("li").each(function() {
272             if ($(this).css("display") == "list-item") li_cout++;
273           });
274           $("#li_" + selected_user).css("display", "none");
275         }
276       } else {
277         new_id = selected_user.replace("user_group_", "user_group_denied_");
278         denied_id = selected_user.replace("user_group_", "user_group_allowed_");
279       }
280                         
281                         target_visibility = $("#li_" + new_id).css("display");
282       $("#li_" + new_id).css("display", "");
283       var arr_id = selected_user.split("-");
284       if (arr_id[1] == 0) {
285                                 source_opacity = $("#"+selected_user).css("opacity");
286                                 target_opacity = $("#" + new_id).css("opacity");
287                                 new_opacity              = (target_opacity == "1" && target_visibility != "none") ? "1" : source_opacity;
288                                 $("#" + new_id).css("opacity",new_opacity);
289         // Add to array -- denied
290         if (dragged_from_allowed) {
291           index = allowed_array.indexOf("li_" + selected_user);
292           if (index != -1) {
293             if (denied_array.indexOf(new_id) == -1) denied_array.push("li_" + new_id);
294             $("#li_" + new_id).find("li").each(function() {
295               child_id = $(this).attr("id");
296               index = denied_array.indexOf(child_id);
297               if (index != -1) denied_array.splice(index, 1);
298             });
299           } else {
300             if (denied_array.indexOf(new_id) == -1) {
301               $("#li_" + selected_user).find("li").each(function() {
302                 if ($(this).css("display") == "list-item") {
303                   id = $(this).attr("id").replace("user_group_allowed_", "user_group_denied_");
304                   if (denied_array.indexOf(id) == -1) {
305                     denied_array.push(id);
306                   }
307                 }
308               });
309             }
310           }
311         } else {
312           if (denied_array.indexOf(new_id) == -1) denied_array.push("li_" + new_id);
313           $("#li_" + new_id).find("li").each(function() {
314             child_id = $(this).attr("id");
315             index = denied_array.indexOf(child_id);
316             if (index != -1) denied_array.splice(index, 1);
317           });
318         }
320         // Remove From Allowed Array
321         index = allowed_array.indexOf("li_" + denied_id);
322         if (index != -1) allowed_array.splice(index, 1);
323         parent_id = $($($("#li_" + denied_id).parent().get(0)).parent().get(0)).attr("id");
324         $("#li_" + denied_id).find("li").each(function() {
325           child_id = $(this).attr("id");
326           index = allowed_array.indexOf(child_id);
327           if (index != -1) allowed_array.splice(index, 1);
328         });
329         if (dragged_from_allowed) {
330           index = allowed_array.indexOf("li_" + selected_user);
331           if (index != -1) $("#li_" + new_id).find('li').css("display", "");
332           else {
333             $("#li_" + selected_user).find("li").each(function() {
334               if ($(this).css("display") == "list-item") {
335                 id = $(this).attr("id").replace("user_group_allowed_", "user_group_denied_");
336                 $("#" + id).css("display", "");
337                 $("#" + id).find("div:eq(1)").css("opacity", "1");
338               }
339             });
340           }
341         } else {
342           $("#li_" + new_id).find('li').css("display", "");
343           $("#li_" + new_id).find("div:eq(1)").css("opacity", "1");
344         }
345         $("#li_" + denied_id).find('li').css("display", "none");
346         $("#li_" + denied_id).css("display", "none");
348       } else {
350         // Add to array -- denied
351         parent_id = $($($("#li_" + new_id).parent().get(0)).parent().get(0)).attr("id");
352         if (denied_array.indexOf("li_" + new_id) == -1 && denied_array.indexOf(parent_id) == -1) denied_array.push("li_" + new_id);
354         $($($("#li_" + new_id).parent().get(0)).parent().get(0)).css("display", "");
355         if (new_id.substr(-2) != "-0" && (denied_array.indexOf(parent_id) == -1 || !dragged_from_allowed)) {
356           $($($("#li_" + new_id).parent().get(0)).parent().get(0)).find("div:eq(1)").css("opacity", "0.5");
357         }
358         $("#li_" + denied_id).css("display", "none");
359         var li_cout = 0;
361         $($($("#li_" + denied_id).parent().get(0)).parent().get(0)).find("li").each(function() {
362           if ($(this).css("display") == "list-item") li_cout++;
363         });
364         if (li_cout == 0 && $("#"+parent_id.replace("li_","").replace("denied","allowed")).css("opacity") != "1") {
365           $($($("#li_" + denied_id).parent().get(0)).parent().get(0)).css("display", "none");
366         }
368         index = allowed_array.indexOf("li_" + denied_id);
369         if (index != -1) allowed_array.splice(index, 1);
370       }
371       saveAcl();
372     }
373   });
374   $(".module_check").click(function() {
375     var clicked_id = $(this).attr("id");
376     var clicked_arr = clicked_id.split("_");
377     if ($(this).is(":checked"))
378       $(".group_" + clicked_arr[1]).attr("checked", true);
379     else
380       $(".group_" + clicked_arr[1]).removeAttr("checked");
381     saveGroupAcl();
382   });
383   $(".component_check").click(function() {
384     saveGroupAcl();
385   });
388 function selectThis(id) {
389   $(".selected_componet").removeClass("selected_componet");
390   $("#" + id).addClass("selected_componet");
391   selected_component = id;
394 function saveAcl() {
395   if (selected_component == '') {
396     var resultTranslated = js_xl('Select a Component');
397     alert(resultTranslated.msg);
398     return;
399   }
400   $(".scrollable").customScrollbar("resize");
401   var selected_module = selected_component;
402   var allowed_users = JSON.stringify(allowed_array);
403   var denied_users = JSON.stringify(denied_array);
404   $.ajax({
405     type: "POST",
406     url: ajax_path,
407     dataType: "html",
408     data: {
409       ajax_mode: 'save_acl',
410       selected_module: selected_module,
411       allowed_users: allowed_users,
412       denied_users: denied_users
413     },
414     async: false,
415     success: function(thedata) {
416       var resultTranslated = js_xl('ACL Updated Successfully');
417       $("#messages").html(resultTranslated.msg);
418       setTimeout(function() {
419         $("#messages").html("");
420       }, 3000);
421     },
422   });
425 function rebuild() {
426   denied_array = [];
427   allowed_array = [];
428   $.ajax({
429     type: "POST",
430     url: ajax_path,
431     dataType: "html",
432     data: {
433       ajax_mode: 'rebuild',
434       selected_module: selected_component,
435     },
436     async: false,
437     success: function(thedata) {
438       $(".class_li").css("display", "none");
439       obj = JSON.parse(thedata);
440       // Allowed Groups
442       for (var index in obj['group_allowed']) {
443         $("#li_user_group_allowed_" + obj['group_allowed'][index] + "-0").css("display", "");
444         $("#li_user_group_allowed_" + obj['group_allowed'][index] + "-0").find("li").css("display", "");
445         allowed_array.push("li_user_group_allowed_" + obj['group_allowed'][index] + "-0");
446       }
448       //Denied Groups
449       for (var index in obj['group_denied']) {
450         $("#li_user_group_denied_" + obj['group_denied'][index] + "-0").css("display", "");
451         $("#li_user_group_denied_" + obj['group_denied'][index] + "-0").find("li").css("display", "");
452         denied_array.push("li_user_group_denied_" + obj['group_denied'][index] + "-0");
453       }
455       // Allowed users
456       for (var index in obj['user_allowed']) {
457         if ($("#li_user_group_allowed_" + index + "-0").css("display") == "none") {
458           $("#li_user_group_allowed_" + index + "-0").css("display", "");
459           $("#li_user_group_allowed_" + index + "-0").find("div:eq(1)").css("opacity", "0.5");
460         }
461         for (var k in obj['user_allowed'][index]) {
462           $("#li_user_group_allowed_" + index + "-" + obj['user_allowed'][index][k]).css("display", "");
463           $("#li_user_group_denied_" + index + "-" + obj['user_allowed'][index][k]).css("display", "none");
464           allowed_array.push("li_user_group_allowed_" + index + "-" + obj['user_allowed'][index][k]);
465         }
466       }
467       //Denied users
468       for (var index in obj['user_denied']) {
469         if ($("#li_user_group_denied_" + index + "-0").css("display") == "none") {
470           $("#li_user_group_denied_" + index + "-0").find("div:eq(1)").css("opacity", "0.5");
471           $("#li_user_group_denied_" + index + "-0").css("display", "");
472         }
473         for (var k in obj['user_denied'][index]) {
474           $("#li_user_group_denied_" + index + "-" + obj['user_denied'][index][k]).css("display", "");
475           $("#li_user_group_allowed_" + index + "-" + obj['user_denied'][index][k]).css("display", "none");
476           denied_array.push("li_user_group_denied_" + index + "-" + obj['user_denied'][index][k]);
477         }
478       }
480     }
481   });
484 function saveGroupAcl() {
485   var ACL_DATA = {};
486   ACL_DATA['allowed'] = {};
487   ACL_DATA['denied'] = {};
488   var i = -1;
489   $("#table_acl").find("input:checkbox").each(function() {
490     var id = $(this).attr("id");
491     id_arr = id.split("_");
493     if ($(this).is(":checked")) {
494       i++;
495       if (!ACL_DATA['allowed'][id_arr[0]]) ACL_DATA['allowed'][id_arr[0]] = {};
496       ACL_DATA['allowed'][id_arr[0]][i] = id_arr[1];
497     } else {
498       i++;
499       if (!ACL_DATA['denied'][id_arr[0]]) ACL_DATA['denied'][id_arr[0]] = {};
500       ACL_DATA['denied'][id_arr[0]][i] = id_arr[1];
501     }
502   });
503   $.ajax({
504     type: "POST",
505     url: ajax_path,
506     dataType: "html",
507     data: {
508       ajax_mode: 'save_acl_advanced',
509       acl_data: JSON.stringify(ACL_DATA),
510       module_id: module_id
511     },
512     async: false,
513     success: function(thedata) {
514       var resultTranslated = js_xl('ACL Updated Successfully');
515       $("#messages_div").html(resultTranslated.msg);
516       setTimeout(function() {
517         $("#messages_div").html("");
518       }, 3000);
519     },
520   })
523 function addNewItem(section) {
524   $(".popup_items").css('display', 'none');
525   $("#add_new_" + section).slideDown();
528 function getSectionById(mod_id) {
529   $("#add_component_section_identifier").val("");
530   $("#add_component_section_name").val("");
531   if (mod_id == "") {
532     $("#add_component_section_id").html("");
533     return;
534   }
536   $.ajax({
537     type: "POST",
538     url: ajax_path,
539     dataType: "html",
540     data: {
541       ajax_mode: 'get_sections_by_module',
542       module_id: mod_id,
543     },
544     async: false,
545     success: function(thedata) {
546       obj = JSON.parse(thedata);
547       out = "<option value=''></option>";
548       for (var index in obj) {
549         out += "<option value='" + index + "'>" + obj[index] + "</option>";
550       }
551       $("#add_component_section_id").html(out);
552     },
553   });
556 function addSectionSave() {
557   mod_id = $("#add_component_mod_id").val();
558   parent_id = $("#add_component_section_id").val();
559   section_identifier = $("#add_component_section_identifier").val();
560   section_name = $("#add_component_section_name").val();
561   if ($.trim(section_identifier) == '' || $.trim(section_name) == '') {
562     var resultTranslated = js_xl('Section ID and Name Cannot be Empty');
563     alert(resultTranslated.msg);
564     return;
565   }
566   $.ajax({
567     type: "POST",
568     url: ajax_path,
569     dataType: "html",
570     data: {
571       ajax_mode: 'save_sections_by_module',
572       mod_id: mod_id,
573       parent_id: parent_id,
574       section_identifier: section_identifier,
575       section_name: section_name,
576     },
577     async: false,
578     success: function(thedata) {
579       var resultTranslated = js_xl('Section saved successfully');
580       $("#add_component_section_message").html(resultTranslated.msg);
581       setTimeout(function() {
582         $("#add_component_section_message").html("");
583       }, 3000);
584     },
585   });