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). "'"
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
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 " .
53 ",'".trim($option_id)."'" .
54 ",'".trim($title). "'" .
56 ",'".$is_default."'" .
57 ",'".$option_value."'".
61 // return JSON data of list items on success
62 echo '{ "options": [';
63 // send the 'Unassigned' empty variable
64 echo '{"id":"","title":"' . xl('Unassigned') . '"}';
66 $lres = sqlStatement("SELECT * FROM list_options WHERE list_id = '$list_id' ORDER BY seq");
67 while ($lrow = sqlFetchArray($lres)) {
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']) .'"}';
76 echo '"title":"'.$lrow['title'].'"}';