fix: Update patient_tracker.php (#6595)
[openemr.git] / library / custom_template / personalize.php
blob55e10f058e74edb0701846a3c7a5d713f913c682
1 <?php
3 /**
4 * personalize.php
6 * @package OpenEMR
7 * @link https://www.open-emr.org
8 * @author Eldho Chacko <eldho@zhservices.com>
9 * @author Jacob T Paul <jacob@zhservices.com>
10 * @author Brady Miller <brady.g.miller@gmail.com>
11 * @copyright Copyright (c) 2011 Z&H Consultancy Services Private Limited <sam@zhservices.com>
12 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 require_once("../../interface/globals.php");
18 use OpenEMR\Common\Acl\AclMain;
19 use OpenEMR\Core\Header;
21 $list_id = $_REQUEST['list_id'] ? $_REQUEST['list_id'] : $_REQUEST['filter_context'];
23 function Delete_Rows($id)
25 sqlStatement("DELETE FROM template_users WHERE tu_template_id=? AND tu_user_id=?", array($id, $_SESSION['authUserID']));
28 function Insert_Rows($id, $order = "")
30 sqlStatement("REPLACE INTO template_users (tu_template_id,tu_user_id,tu_template_order) VALUES (?,?,?)", array($id, $_SESSION['authUserID'], $order));
33 if (isset($_REQUEST['submitform']) && $_REQUEST['submitform'] == 'save') {
34 $topersonalized = $_REQUEST['topersonalized'];
35 $personalized = $_REQUEST['personalized'];
36 foreach ($topersonalized as $key => $value) {
37 $arr = explode("|", $value);
38 $res = sqlStatement("SELECT * FROM template_users WHERE tu_template_id=? AND tu_user_id=?", array($arr[0], $_SESSION['authUserID']));
39 if (sqlNumRows($res)) {
40 Delete_Rows($arr[0]);
41 $qry = sqlStatement("SELECT * FROM customlists WHERE cl_list_id=? AND cl_deleted=0", array($arr[0]));
42 while ($row = sqlFetchArray($qry)) {
43 Delete_Rows($row['cl_list_slno']);
48 //Add new Categories
49 foreach ($personalized as $key => $value) {
50 $arr = explode("|", $value);
51 if ($arr[1]) {
52 $res = sqlStatement("SELECT * FROM template_users WHERE tu_template_id=? AND tu_user_id=?", array($arr[0], $_SESSION['authUserID']));
53 Insert_Rows($arr[0]);
54 $qry = sqlStatement("SELECT * FROM customlists WHERE cl_list_id=? AND cl_deleted=0", array($arr[0]));
55 while ($row = sqlFetchArray($qry)) {
56 $qryTU = sqlStatement("SELECT * FROM template_users WHERE tu_template_id=? AND tu_user_id=?", array($row['cl_list_slno'], $arr[1]));
57 while ($rowTU = sqlFetchArray($qryTU)) {
58 Insert_Rows($rowTU['tu_template_id'], $rowTU['tu_template_order']);
61 } else {
62 Insert_Rows($arr[0]);
63 $qry = sqlStatement("SELECT * FROM customlists WHERE cl_list_id=? AND cl_deleted=0", array($arr[0]));
64 while ($row = sqlFetchArray($qry)) {
65 Insert_Rows($row['cl_list_slno'], $row['cl_order']);
71 <html>
72 <head>
74 <?php Header::setupHeader(['common', 'opener']); ?>
76 <script>
78 function refreshme() {
79 top.restoreSession();
80 document.location.reload();
83 $(function () {
85 tabbify();
87 $(".iframe_small").on('click', function (e) {
88 e.preventDefault();
89 e.stopPropagation();
90 dlgopen('', '', 400, 250, '', '', {
91 buttons: [
92 {text: '<?php echo xla('Close'); ?>', close: true, style: 'secondary btn-sm'}
94 onClosed: 'refreshme',
95 type: 'iframe',
96 url: $(this).attr('href')
97 });
98 });
100 $(".iframe_medium").on('click', function (e) {
101 e.preventDefault();
102 e.stopPropagation();
103 dlgopen('', '', 450, 300, '', '', {
104 buttons: [
105 {text: '<?php echo xla('Close'); ?>', close: true, style: 'secondary btn-sm'}
107 onClosed: 'refreshme',
108 type: 'iframe',
109 url: $(this).attr('href')
113 $(".iframe_abvmedium").on('click', function (e) {
114 e.preventDefault();
115 e.stopPropagation();
116 dlgopen('', '', 700, 550, '', '', {
117 buttons: [
118 {text: '<?php echo xla('Close'); ?>', close: true, style: 'secondary btn-sm'}
120 onClosed: 'refreshme',
121 type: 'iframe',
122 url: $(this).attr('href')
128 function check_user_category(form, selectFrom, selectedList) {
129 top.restoreSession();
130 var total_selected = form.elements[selectedList].length - 1;
131 var msg = '';
132 for (total_selected; total_selected >= 0; total_selected--) {
133 if (form.elements[selectedList].options[total_selected].selected) {
134 if (document.getElementById('filter_users').value) {
135 $.ajax({
136 type: "POST",
137 url: "ajax_code.php",
138 dataType: "html",
139 data: {
140 item: form.elements[selectedList].options[total_selected].value,
141 list_id: document.getElementById('filter_users').value,
142 source: "check_item"
144 async: false,
145 success: function (thedata) {
146 if (thedata == 'OK') {
147 total_clients = form.elements[selectFrom].length;
148 opt = new Option(form.elements[selectedList].options[total_selected].text, form.elements[selectedList].options[total_selected].value);
149 form.elements[selectFrom].options[total_clients] = opt;
150 form.elements[selectedList].options[total_selected] = null;
152 else {
153 msg += form.elements[selectedList].options[total_selected].text + "\n";
156 error: function () {
157 alert("fail");
161 else {
162 total_clients = form.elements[selectFrom].length;
163 opt = new Option(form.elements[selectedList].options[total_selected].text, form.elements[selectedList].options[total_selected].value);
164 form.elements[selectFrom].options[total_clients] = opt;
165 form.elements[selectedList].options[total_selected] = null;
169 jsub_sortNow(form.elements[selectFrom]);
170 if (msg != '') {
171 if (confirm("<?php echo addslashes(xl('The following categories will be removed from your category List'));?> \n" + msg + "\n <?php echo addslashes(xl('Do you want to continue?'));?>")) {
172 remove_selected(form, selectedList);
175 return;
178 function remove_selected(form, selectedList) {
179 top.restoreSession();
180 var total_selected = form.elements[selectedList].length - 1;
181 for (total_selected; total_selected >= 0; total_selected--) {
182 if (form.elements[selectedList].options[total_selected].selected) {
183 form.elements[selectedList].options[total_selected] = null;
186 jsub_sortNow(form.elements[selectFrom]);
187 return;
190 function all_selected(selectedList) {
191 top.restoreSession();
192 var total_selected = document.getElementById(selectedList).length - 1;
193 for (total_selected; total_selected >= 0; total_selected--) {
194 document.getElementById(selectedList).options[total_selected].selected = true;
198 function all_deselected(selectedList) {
199 top.restoreSession();
200 var total_selected = document.getElementById(selectedList).length - 1;
201 for (total_selected; total_selected >= 0; total_selected--) {
202 document.getElementById(selectedList).options[total_selected].selected = false;
206 function jsub_selected(form, selectFrom, selectedList) {
207 top.restoreSession();
208 var total_selected = form.elements[selectedList].length - 1;
209 for (total_selected; total_selected >= 0; total_selected--) {
210 if (form.elements[selectedList].options[total_selected].selected) {
211 total_clients = form.elements[selectFrom].length;
212 opt = new Option(form.elements[selectedList].options[total_selected].text, form.elements[selectedList].options[total_selected].value);
213 form.elements[selectFrom].options[total_clients] = opt;
214 form.elements[selectedList].options[total_selected] = null;
217 jsub_sortNow(form.elements[selectFrom]);
218 return;
221 function display_category_item(form, selectedList) {
222 top.restoreSession();
223 var len = 0;
224 var selectedval = '';
225 var total_selected = form.elements[selectedList].length - 1;
226 for (total_selected; total_selected >= 0; total_selected--) {
227 if (form.elements[selectedList].options[total_selected].selected) {
228 selectedval = form.elements[selectedList].options[total_selected].value;
229 len++;
232 if (len > 1) {
233 document.getElementById('itemdiv').style.display = 'none';
235 else if (len == 1) {
236 document.getElementById('itemdiv').style.display = '';
237 $.ajax({
238 type: "POST",
239 url: "ajax_code.php",
240 dataType: "html",
241 data: {
242 list_id: selectedval,
243 source: "item_show"
245 async: false,
246 success: function (thedata) {
247 document.getElementById('itemdiv').innerHTML = thedata;
249 error: function () {
250 alert("fail");
253 return;
257 function jsub_sortNow(obj) {
258 top.restoreSession();
259 var len = obj.length - 1;
260 var text = new Array();
261 var values = new Array();
262 var sortarr = new Array();
263 for (var i = len; i >= 0; i--) {
264 text[i] = obj.options[i].text;
265 values[i] = obj.options[i].value;
266 sortarr[i] = obj.options[i].text;
268 sortarr.sort();
269 obj.length = 0;
270 for (i = 0; i <= len; i++) {
271 for (j = 0; j <= len; j++) {
272 if (sortarr[i] == text[j]) {
273 break;
276 opt = new Option(text[j], values[j]);
277 obj.options[i] = opt;
281 function personalize_save() {
282 top.restoreSession();
283 document.getElementById('submitform').value = 'save';
284 all_selected('topersonalized');
285 all_selected('personalized');
286 document.myform.submit();
288 </script>
289 </head>
290 <body class="body_top">
291 <form name="myform" method="post" onsubmit="top.restoreSession();">
292 <div class="container-fluid">
293 <h3><?php echo htmlspecialchars(xl('Filter'), ENT_QUOTES); ?></h3>
294 <div class="row">
295 <label class="col-form-label col-sm-1"><?php echo htmlspecialchars(xl('Context'), ENT_QUOTES); ?></label>
296 <div class="col-sm-5">
297 <select name='filter_context' class="form-control" id='filter_context' onchange='javascript:document.myform.submit();'>
298 <option value=''><?php echo htmlspecialchars(xl('Select a Context'), ENT_QUOTES); ?></option>
299 <?php
300 $context_sql = "SELECT * FROM customlists WHERE cl_list_type=2 AND cl_deleted=0";
301 $context_res = sqlStatement($context_sql);
302 while ($context_row = sqlFetchArray($context_res)) {
303 echo "<option value='" . htmlspecialchars($context_row['cl_list_slno'], ENT_QUOTES) . "' ";
304 echo ($_REQUEST['filter_context'] == $context_row['cl_list_slno']) ? 'selected' : '';
305 echo ">" . htmlspecialchars($context_row['cl_list_item_long'], ENT_QUOTES) . "</option>";
308 </select>
309 </div>
310 <label class="col-form-label col-sm-1"><?php echo htmlspecialchars(xl('Users'), ENT_QUOTES); ?></label>
311 <div class="col-sm-5">
312 <select name='filter_users' class="form-control" id='filter_users' onchange='javascript:document.myform.submit();'>
313 <option value=''><?php echo htmlspecialchars(xl('Select a User'), ENT_QUOTES); ?></option>
314 <?php
315 $user_sql = "SELECT DISTINCT(tu.tu_user_id),u.fname,u.lname FROM template_users AS tu LEFT OUTER JOIN users AS u ON tu.tu_user_id=u.id WHERE tu.tu_user_id!=?";
316 $user_res = sqlStatement($user_sql, array($_SESSION['authUserID']));
317 while ($user_row = sqlFetchArray($user_res)) {
318 echo "<option value='" . htmlspecialchars($user_row['tu_user_id'], ENT_QUOTES) . "' ";
319 echo ($_REQUEST['filter_users'] == $user_row['tu_user_id']) ? 'selected' : '';
320 echo ">" . htmlspecialchars($user_row['fname'] . " " . $user_row['lname'], ENT_QUOTES) . "</option>";
323 </select>
324 </div>
325 <div class="col-12 my-2 text-center">
326 <a href="#" class="btn btn-primary" onclick="top.restoreSession();personalize_save()"><?php echo htmlspecialchars(xl('Save'), ENT_QUOTES); ?></a>
327 <?php
328 if (AclMain::aclCheckCore('nationnotes', 'nn_configure')) {
330 <a href="delete_category.php" id="share_link" class="iframe_medium btn btn-primary" onclick="top.restoreSession();"><?php echo htmlspecialchars(xl('Delete Category'), ENT_QUOTES); ?></a>
331 <?php
334 <?php
335 if (AclMain::aclCheckCore('nationnotes', 'nn_configure')) {
337 <a href="add_template.php?list_id=<?php echo attr($_REQUEST['list_id']); ?>" onclick="top.restoreSession();" class="iframe_small btn btn-primary" title="<?php echo htmlspecialchars(xl('Add Category'), ENT_QUOTES); ?>"><?php echo htmlspecialchars(xl('Add Category'), ENT_QUOTES); ?></a>
338 <?php
341 <?php
342 if (AclMain::aclCheckCore('nationnotes', 'nn_configure')) {
344 <a href="add_context.php" class="iframe_medium btn btn-primary" onclick="top.restoreSession();" title="<?php echo htmlspecialchars(xl('Add Context'), ENT_QUOTES); ?>"><?php echo htmlspecialchars(xl('Add Context'), ENT_QUOTES); ?></a>
345 <?php
348 </div>
349 <div class="col-sm-5 text">
350 <?php echo htmlspecialchars(xl('Available categories'), ENT_QUOTES); ?>
351 </div>
352 <div class="col-sm-2">
353 &nbsp;
354 </div>
355 <div class="col-sm-5 text">
356 <?php $user = sqlQuery("SELECT * FROM users WHERE id=?", array($_SESSION['authUserID'])); ?>
357 <?php echo htmlspecialchars(xl('Categories for') . " " . $user['fname'] . " " . $user['lname'], ENT_QUOTES); ?>
358 </div>
359 <div class="col-sm-5">
360 <select multiple name="topersonalized[]" class="form-control" id="topersonalized" size="6" onchange="display_category_item(document.myform,'topersonalized');">
361 <?php
362 $where = '';
363 $join = '';
364 $arval = array($_SESSION['authUserID']);
365 $arval1 = array($_REQUEST['filter_users'], $_SESSION['authUserID']);
366 if ($_REQUEST['filter_context']) {
367 $where .= " AND cl_list_id=?";
368 array_push($arval, $_REQUEST['filter_context']);
369 array_push($arval1, $_REQUEST['filter_context']);
371 $sql = "SELECT * FROM template_users AS tu LEFT OUTER JOIN customlists AS c ON tu.tu_template_id=c.cl_list_slno
372 WHERE cl_list_type=3 AND cl_deleted=0 AND tu.tu_template_id NOT IN (SELECT tu_template_id FROM template_users AS tuser WHERE
373 tu_user_id=?) " .
374 $where .
375 " ORDER BY cl_list_id,tu_user_id,cl_list_item_long";
376 $resTemplates = sqlStatement($sql, $arval);
377 if ($_REQUEST['filter_users']) {
378 $sql = " SELECT * FROM template_users AS tu LEFT OUTER JOIN customlists AS c ON tu.tu_template_id=c.cl_list_slno WHERE
379 tu.tu_user_id=? AND c.cl_list_type=3 AND cl_deleted=0 AND tu.tu_template_id NOT IN
380 (SELECT tu_template_id FROM template_users AS tuser WHERE tu_user_id=?)" .
381 $where .
382 "ORDER BY cl_list_id,tu_user_id,c.cl_list_item_long";
383 $resTemplates = sqlStatement($sql, $arval1);
385 while ($rowTemplates = sqlFetchArray($resTemplates)) {
386 $cntxt = '';
387 if (!$_REQUEST['filter_context']) {
388 $context = sqlQuery("SELECT * FROM customlists WHERE cl_list_slno=?", array($rowTemplates['cl_list_id']));
389 $cntxt .= $context['cl_list_item_long'] . "->";
391 if (!$_REQUEST['filter_users']) {
392 $context = sqlQuery("SELECT * FROM users WHERE id=?", array($rowTemplates['tu_user_id']));
393 $cntxt .= $context['username'] . "->";
395 echo "<option value='" . htmlspecialchars($rowTemplates['cl_list_slno'] . "|" . $rowTemplates['tu_user_id'], ENT_QUOTES) . "'>" . htmlspecialchars($cntxt . $rowTemplates['cl_list_item_long'], ENT_QUOTES) . "</option>";
397 $sqlorphan = "SELECT * FROM customlists WHERE cl_list_type=3 AND cl_deleted=0 AND cl_list_slno " .
398 " NOT IN (SELECT DISTINCT tu_template_id FROM template_users) " .
399 $where .
400 " ORDER BY cl_list_id,cl_list_item_long";
401 $resorphan = sqlStatement($sqlorphan);
402 while ($roworphan = sqlFetchArray($resorphan)) {
403 $cntxt = '';
404 if (!$_REQUEST['filter_context']) {
405 $context = sqlQuery("SELECT * FROM customlists WHERE cl_list_slno=?", array($roworphan['cl_list_id']));
406 $cntxt .= $context['cl_list_item_long'] . "->";
408 echo "<option value='" . htmlspecialchars($roworphan['cl_list_slno'] . "|", ENT_QUOTES) . "'>" . htmlspecialchars($cntxt . $roworphan['cl_list_item_long'], ENT_QUOTES) . "</option>";
411 </select>
412 </div>
413 <div class="col-sm-2 text-center">
414 <button name="remove" class="btn btn-secondary" onclick="jsub_selected(document.myform,'personalized','topersonalized')">&raquo;</button><br />
415 <button name="remove" class="btn btn-secondary" onclick="check_user_category(document.myform,'topersonalized','personalized')">&laquo;</button>
416 </div>
417 <div class="col-sm-5">
418 <select multiple class="form-control" name="personalized[]" id="personalized" size="6">
419 <?php
420 $where = '';
421 if ($_REQUEST['filter_context']) {
422 $where .= " AND cl_list_id='" . $_REQUEST['filter_context'] . "'";
424 $sql = "SELECT * FROM template_users AS tu LEFT OUTER JOIN customlists AS c ON tu.tu_template_id=c.cl_list_slno WHERE
425 tu.tu_user_id=? AND c.cl_list_type=3 AND cl_deleted=0 " .
426 $where .
427 "ORDER BY c.cl_list_item_long";
428 $resTemplates = sqlStatement($sql, array($_SESSION['authUserID']));
429 while ($rowTemplates = sqlFetchArray($resTemplates)) {
430 $cntxt = '';
431 if (!$_REQUEST['filter_context']) {
432 $context = sqlQuery("SELECT * FROM customlists WHERE cl_list_slno=?", array($rowTemplates['cl_list_id']));
433 $cntxt .= $context['cl_list_item_long'] . "->";
435 echo "<option value='" . htmlspecialchars($rowTemplates['cl_list_slno'] . "|" . $rowTemplates['tu_user_id'], ENT_QUOTES) . "'>" . htmlspecialchars($cntxt . $rowTemplates['cl_list_item_long'], ENT_QUOTES) . "</option>";
438 </select>
439 </div>
440 <div class="col-12">
441 <input type="hidden" name="submitform" id="submitform" value="" />
442 <div class="w-100 overflow-auto" style="height:150px" id="itemdiv"></div>
443 </div>
444 </div>
445 </div>
446 </form>
447 </body>
448 </html>