Update to 4.0 Documentation
[openemr.git] / library / ajax / addlistitem.php
blobceabc1a8f3da07553ee837f44698c615247346f8
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) { exit; }
36 $exists_id = sqlQuery("SELECT * FROM list_options WHERE ".
37 " list_id='".$list_id."'".
38 " and option_id='".trim($option_id)."'"
40 if ($exists_id) { exit; }
42 // determine the sequential order of the new item,
43 // it should be the maximum number for the specified list plus one
44 $seq = 0;
45 $row = sqlQuery("SELECT max(seq) as maxseq FROM list_options WHERE list_id= '".$list_id."'");
46 $seq = $row['maxseq']+1;
48 // add the new list item
49 $rc = sqlInsert("INSERT INTO list_options ( " .
50 "list_id, option_id, title, seq, is_default, option_value " .
51 ") VALUES (" .
52 "'".$list_id."'".
53 ",'".trim($option_id)."'" .
54 ",'".trim($title). "'" .
55 ",'".$seq."'" .
56 ",'".$is_default."'" .
57 ",'".$option_value."'".
58 ")"
61 // return JSON data of list items on success
62 echo '{ "options": [';
63 // send the 'Unassigned' empty variable
64 echo '{"id":"","title":"' . xl('Unassigned') . '"}';
65 $comma = ",";
66 $lres = sqlStatement("SELECT * FROM list_options WHERE list_id = '$list_id' ORDER BY seq");
67 while ($lrow = sqlFetchArray($lres)) {
68 echo $comma;
69 echo '{"id":"'.$lrow['option_id'].'",';
71 // translate title if translate-lists flag set and not english
72 if ($GLOBALS['translate_lists'] && $_SESSION['language_choice'] > 1) {
73 echo '"title":"' . xl($lrow['title']) .'"}';
75 else {
76 echo '"title":"'.$lrow['title'].'"}';
79 echo "]}";
80 exit;