some portal work
[openemr.git] / library / options_listadd.inc
blob99055f108f5dae1e3d4d8b3d657cd17e8c8de8dc
1 <?php
2 /**
3  * This include is just a HTML and JS *fragment* it CANNOT stand alone
4  *
5  * This file is used to include the capability to have a pop-up DIV in which
6  * a user can add a new option to a list box. Primarily this is for the
7  * demographics, history, and referral screens. This code could be used elsewhere
8  * as long as it had the required supporting HTML and JS
9  *
10  * REQUIRED to make work:
11  *  - jQuery
12  *  - form element with the class '.addtolist'
13  *  - be used in a file that already includes globals.php
14  *
15  * @package   OpenEMR
16  * @link      http://www.open-emr.org
17  * @author    Brady Miller <brady.g.miller@gmail.com>
18  * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
19  * @license   https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
20  */
22 use OpenEMR\Common\Csrf\CsrfUtils;
26 <!-- hidden DIV used for creating new list items -->
27 <div id="newlistitem" class="body_top" style="border: 2px outset #666; display:none; position:absolute; padding:5px; ">
28  <input type="hidden" name="newlistitem_listid" id="newlistitem_listid" value="">
29  <input type="hidden" name="newlistitem_fieldid" id="newlistitem_fieldid" value="">
30  <div id="specificForm">
31  </div>
32  <div style = 'text-align:center;'>
33   <div style='margin:0.5em 0 0 0;'>
34    <input type='button' name='newlistitem_submit' id='newlistitem_submit' value='<?php echo xla('Add'); ?>'>
35    <input type='button' name='newlistitem_cancel' id='newlistitem_cancel' value='<?php echo xla('Cancel'); ?>'>
36   </div>
37  </div>
38 </div>
41 <script language="javascript">
43     // collect the custom state widget flag
44     var stateCustomFlag = <?php echo json_encode($GLOBALS['state_custom_addlist_widget']); ?>;
46     // generic form for input box
47     var generic = "<input type='textbox' name='newlistitem_value' id='newlistitem_value' size='20' maxlength='50'>"
49     // state form for input box
50     var state   = "\
51 <ul style='list-style-type:none; margin:0; padding:0;'>\
52  <li style='font-weight:bold; font-size:120%; text-align:center;'>\
53     <?php echo xlt('Enter new State'); ?></li>\
54  <li style='margin:0.5em 0 0 0; padding:0;'>\
55     <?php echo xlt('Full Name'); ?>:\
56   <input type='textbox' name='newlistitem_value' id='newlistitem_value' size='20' maxlength='50'><li>\
57  <li style='margin:0.5em 0 0 0; padding:0;'>\
58     <?php echo xlt('Abbreviation'); ?>:\
59   <input type='textbox' name='newlistitem_abbr' id='newlistitem_abbr' size='10' maxlength='50'><li>\
60 </ul>\
63 // jQuery makes life easier (sometimes)
65 $(document).ready(function(){
67     /********************************************************/
68     /************ List-box modification functions ***********/
69     /********************************************************/
71     $("#newlistitem").on("keypress", function(evt) { if (evt.keyCode == 13) { SaveNewListItem(this, evt); return false; }});
72     $(".addtolist").on("click", function(evt) { AddToList(this, evt); });
73     $("#newlistitem_submit").on("click", function(evt) { SaveNewListItem(this, evt); });
74     $("#newlistitem_cancel").on("click", function(evt) { CancelAddToList(this, evt); });
76     // display the 'new list item' DIV at the mouse position
77     var AddToList = function(btnObj, e) {
78         // capture the ID of the list and specific field being modified from the object's ID
79         $('#newlistitem_listid').val($(btnObj).attr("id").replace(/^addtolistid_/g, ""));
80         $('#newlistitem_fieldid').val($(btnObj).attr("fieldid"));
82         // REMOVE previous form
83     $("#specificForm").empty();
85     // INSERT the selected form
86     if (($("#newlistitem_listid").val() == "state") && (stateCustomFlag)) {
87      // insert state form and clear values
88          $("#specificForm").append(state);
89      $('#newlistitem_value').val("");
90      $('#newlistitem_abbr').val("");
91     }
92     else {
93      // insert generic form and clear values
94      $("#specificForm").append(generic);
95      $('#newlistitem_value').val("");
96         }
98         // make the item visible before setting its x,y location
99         $('#newlistitem').css('display', 'inline');
100         //getting height and width of the message box
101         var height = $('#newlistitem').height();
102         var width = $('#newlistitem').width();
103         //calculating offset for displaying popup message
104         leftVal=e.pageX-(width/2)+"px";
105         topVal=e.pageY-(height/2)+"px";
106         //show the DIV and set cursor focus
107         $('#newlistitem').css({left:leftVal,top:topVal}).show();
108         $('#newlistitem_value').focus();
109     };
111     // hide the add-to-list DIV and clear its textbox
112     var CancelAddToList = function(btnObj, e) {
113         $('#newlistitem').hide();
114     }
116     // save the new list item to the given list
117     var SaveNewListItem = function(btnObj, e) {
118     // VALIDATE the selected form
119         //  Don't allow a number as first character
120         //  Don't allow illegal characters (' and " for now) - still developing
121     //   First, validate fields common to all forms
122         if ($("#newlistitem_value").val().match(/^\d/)) {
123             alert(<?php echo xlj('List items can not start with a number.'); ?>);
124             $("#newlistitem_value").focus();
125             return false;
126         }
127         if ($("#newlistitem_value").val().match(/[\'\"]/)) {
128             alert(<?php echo xlj('List items contains illegal character(s).'); ?>);
129             $("#newlistitem_value").focus();
130             return false;
131         }
132     //  Second, validate form specific fields
133         if (($("#newlistitem_listid").val() == "state") && (stateCustomFlag)) {
134             // state forms specific validation
135             if ($("#newlistitem_abbr").val().match(/^\d/)) {
136                 alert(<?php echo xlj('List items can not start with a number.'); ?>);
137                 $("#newlistitem_abbr").focus();
138                 return false;
139             }
140             if ($("#newlistitem_abbr").val().match(/[\'\"]/)) {
141                 alert(<?php echo xlj('List items contains illegal character(s).'); ?>);
142                 $("#newlistitem_abbr").focus();
143                 return false;
144             }
145         }
147         // PROCESS the selected form
148         if (($("#newlistitem_listid").val() == "state") && (stateCustomFlag)) {
149         // process state form
150         //  (note we have collected a title and abbreviation)
151         var listid = $("#newlistitem_listid").val();
152         var newitem = $('#newlistitem_value').val();
153         var newitem_abbr = $('#newlistitem_abbr').val();
155     }
156     else {
157         // process generic form
158         //  (note we have only collected the title, so will make
159         //   abbreviation same as title)
160         var listid = $("#newlistitem_listid").val();
161         var newitem = $('#newlistitem_value').val();
162         var newitem_abbr = $('#newlistitem_value').val();
163     }
165         // make the AJAX call to save the new value to the specified list
166         // upon returning successfully, refresh the list box and select
167         // the new list item
168         $.getJSON("<?php echo $GLOBALS['webroot']; ?>/library/ajax/addlistitem.php",
169                     {csrf_token_form: <?php echo js_escape(CsrfUtils::collectCsrfToken()); ?>,
170                     listid: listid,
171              newitem: newitem,
172              newitem_abbr: newitem_abbr},
173                     function(jsondata, txtresponse) {
174                  if( jsondata.error == '' ){
175               //process each widget with the same list
176             $("select.addtolistclass_"+listid).each(function(){
177                 var pointer = $(this);
178                 var currentselected = pointer.val();
179                 var listboxfield = pointer.attr("id");
180                 var listbox = document.getElementById(listboxfield);
181                             while (listbox.options.length > 0) { listbox.options[0] = null; }
182                             $.each(jsondata.options, function () {
183                     if (listboxfield == $('#newlistitem_fieldid').val()) {
184                     // build select for the chosen widget field
185                                     listbox.options[listbox.options.length] = new Option(this.title, this.id);
186                                     if (this.id == newitem_abbr) {
187                         pointer.val(this.id);
188                                     }
189                 }
190                 else {
191                     // build select for the other widgets using the same list
192                     listbox.options[listbox.options.length] = new Option(this.title, this.id);
193                     if (this.id == currentselected) {
194                                         pointer.val(currentselected);
195                     }
196                 }
197                             });
198             });
200                         // now hide the DIV
201                         $('#newlistitem').hide();
202              }else{
204                  alert( jsondata.error );
205              }
206                     }
207                 );
209     }  // end SaveNewListItem
211 }); // end jQuery .ready
212 </script>