Committed addlistitem.php after including json_encode function so as slashes and...
[openemr.git] / library / ajax / addlistitem.php
blob4a47afbc08788ad77b8d2a09f5b7a58fb8134823
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");
18 include_once("{$GLOBALS['srcdir']}/sql.inc");
20 // check for required values
21 if ($_GET['listid'] == "" || trim($_GET['newitem']) == "" || trim($_GET['newitem_abbr']) == "") exit;
23 // set the values for the new list item
24 $is_default = 0;
25 $list_id = $_GET['listid'];
26 $title = trim($_GET['newitem']);
27 $option_id = trim($_GET['newitem_abbr']);
28 $option_value = 0;
30 // make sure we're not adding a duplicate title or id
31 $exists_title = sqlQuery("SELECT * FROM list_options WHERE ".
32 " list_id='".$list_id."'".
33 " and title='".trim($title). "'"
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)."'"
44 if ($exists_id) {
45 echo json_encode(array("error"=> xl('Record already exist') ));
46 exit;
49 // determine the sequential order of the new item,
50 // it should be the maximum number for the specified list plus one
51 $seq = 0;
52 $row = sqlQuery("SELECT max(seq) as maxseq FROM list_options WHERE list_id= '".$list_id."'");
53 $seq = $row['maxseq']+1;
55 // add the new list item
56 $rc = sqlInsert("INSERT INTO list_options ( " .
57 "list_id, option_id, title, seq, is_default, option_value " .
58 ") VALUES (" .
59 "'".$list_id."'".
60 ",'".trim($option_id)."'" .
61 ",'".trim($title). "'" .
62 ",'".$seq."'" .
63 ",'".$is_default."'" .
64 ",'".$option_value."'".
65 ")"
68 // return JSON data of list items on success
69 echo '{ "error":"", "options": [';
70 // send the 'Unassigned' empty variable
71 echo '{"id":"","title":"' . xl('Unassigned') . '"}';
72 $comma = ",";
73 $lres = sqlStatement("SELECT * FROM list_options WHERE list_id = '$list_id' ORDER BY seq");
74 while ($lrow = sqlFetchArray($lres)) {
75 echo $comma;
76 echo '{"id":"'.$lrow['option_id'].'",';
78 // translate title if translate-lists flag set and not english
79 if ($GLOBALS['translate_lists'] && $_SESSION['language_choice'] > 1) {
80 echo '"title":"' . xl($lrow['title']) .'"}';
82 else {
83 echo '"title":"'.$lrow['title'].'"}';
86 echo "]}";
87 exit;