mysql 8 fixes (#1639)
[openemr.git] / library / ajax / addlistitem.php
blob7ee13144b3da9ec3c2492b29c2567b54933511a1
1 <?php
2 /*
3 // Copyright (C) 2009 Jason Morrill <jason@italktech.net>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This file is used to add an item to the list_options table
12 // OUTPUT
13 // on error = NULL
14 // on succcess = JSON data, array of "value":"title" for new list of options
17 include_once("../../interface/globals.php");
19 // check for required values
20 if ($_GET['listid'] == "" || trim($_GET['newitem']) == "" || trim($_GET['newitem_abbr']) == "") {
21 exit;
24 // set the values for the new list item
25 $is_default = 0;
26 $list_id = $_GET['listid'];
27 $title = trim($_GET['newitem']);
28 $option_id = trim($_GET['newitem_abbr']);
29 $option_value = 0;
31 // make sure we're not adding a duplicate title or id
32 $exists_title = sqlQuery("SELECT * FROM list_options WHERE ".
33 " list_id='".$list_id."'".
34 " and title = '" . trim($title) . "' AND activity = 1");
35 if ($exists_title) {
36 echo json_encode(array("error"=> xl('Record already exist') ));
37 exit;
40 $exists_id = sqlQuery("SELECT * FROM list_options WHERE ".
41 " list_id='".$list_id."'".
42 " and option_id = '" . trim($option_id) . "' AND activity = 1");
43 if ($exists_id) {
44 echo json_encode(array("error"=> xl('Record already exist') ));
45 exit;
48 // determine the sequential order of the new item,
49 // it should be the maximum number for the specified list plus one
50 $seq = 0;
51 $row = sqlQuery("SELECT max(seq) as maxseq FROM list_options WHERE list_id = '" . $list_id . "' AND activity = 1");
52 $seq = $row['maxseq']+1;
54 // add the new list item
55 $rc = sqlInsert("INSERT INTO list_options ( " .
56 "list_id, option_id, title, seq, is_default, option_value " .
57 ") VALUES (" .
58 "'".$list_id."'".
59 ",'".trim($option_id)."'" .
60 ",'".trim($title). "'" .
61 ",'".$seq."'" .
62 ",'".$is_default."'" .
63 ",'".$option_value."'".
64 ")");
66 // return JSON data of list items on success
67 echo '{ "error":"", "options": [';
68 // send the 'Unassigned' empty variable
69 echo '{"id":"","title":"' . xl('Unassigned') . '"}';
70 $comma = ",";
71 $lres = sqlStatement("SELECT * FROM list_options WHERE list_id = '$list_id' AND activity = 1 ORDER BY seq");
72 while ($lrow = sqlFetchArray($lres)) {
73 echo $comma;
74 echo '{"id":"'.$lrow['option_id'].'",';
76 // translate title if translate-lists flag set and not english
77 if ($GLOBALS['translate_lists'] && $_SESSION['language_choice'] > 1) {
78 echo '"title":"' . xl($lrow['title']) .'"}';
79 } else {
80 echo '"title":"'.$lrow['title'].'"}';
84 echo "]}";
85 exit;