3 // Copyright (C) 2009 Jason Morrill <jason@italktech.net>
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.
10 // This file is used to add an item to the list_options table
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
25 $list_id = $_GET['listid'];
26 $title = trim($_GET['newitem']);
27 $option_id = trim($_GET['newitem_abbr']);
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). "'"
36 echo json_encode(array("error"=> xl('Record already exist') ));
40 $exists_id = sqlQuery("SELECT * FROM list_options WHERE ".
41 " list_id='".$list_id."'".
42 " and option_id='".trim($option_id)."'"
45 echo json_encode(array("error"=> xl('Record already exist') ));
49 // determine the sequential order of the new item,
50 // it should be the maximum number for the specified list plus one
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 " .
60 ",'".trim($option_id)."'" .
61 ",'".trim($title). "'" .
63 ",'".$is_default."'" .
64 ",'".$option_value."'".
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') . '"}';
73 $lres = sqlStatement("SELECT * FROM list_options WHERE list_id = '$list_id' ORDER BY seq");
74 while ($lrow = sqlFetchArray($lres)) {
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']) .'"}';
83 echo '"title":"'.$lrow['title'].'"}';