- Update the copyright notices to use the correct year and not a non-ASCII symbol
[bugdar.git] / includes / api_field.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright (c)2004-2008 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 require_once ISSO . '/Api.php';
23 require_once('./includes/functions_datastore.php');
24
25 /**
26 * API: Field
27 *
28 * @author Blue Static
29 * @copyright Copyright (c)2004 - 2008, Blue Static
30 * @package Bugdar
31 *
32 */
33 class FieldAPI extends BSApi
34 {
35 /**
36 * Fields
37 * @var array
38 */
39 protected $fields = array(
40 'fieldid' => array(TYPE_UINT, REQ_AUTO),
41 'name' => array(TYPE_STR, REQ_YES),
42 'description' => array(TYPE_STR, REQ_NO),
43 'type' => array(TYPE_STR, REQ_YES),
44 'selects' => array(TYPE_STR, REQ_NO),
45 'required' => array(TYPE_BOOL, REQ_NO),
46 'cansearch' => array(TYPE_BOOL, REQ_NO),
47 'regexmatch' => array(TYPE_STR, REQ_NO),
48 'defaultvalue' => array(TYPE_STR, REQ_NO),
49 'usedefault' => array(TYPE_BOOL, REQ_NO),
50 'maxlength' => array(TYPE_UINT, REQ_NO)
51 );
52
53 /**
54 * Database table
55 * @var string
56 */
57 protected $table = 'bugfield';
58
59 /**
60 * Table prefix
61 * @var string
62 */
63 protected $prefix = TABLE_PREFIX;
64
65 /**
66 * A static method that is used to return an array of all the allowed
67 * field types.
68 *
69 * @param string Return the display name of a type if the key is passed
70 *
71 * @return array An array of all the allowed field types
72 */
73 public static function field_types($type = null)
74 {
75 $types = array(
76 'input_text' => T('Single-Line Text Box'),
77 'input_checkbox' => T('Checkbox Flag'),
78 'select_single' => T('Drop-Down Menu'),
79 );
80
81 if ($type == null)
82 {
83 return $types;
84 }
85 else
86 {
87 return $types["$type"];
88 }
89 }
90
91 /**
92 * Post-insert
93 */
94 protected function post_insert()
95 {
96 build_user_help();
97 }
98
99 /**
100 * Post-update
101 */
102 protected function post_update()
103 {
104 build_user_help();
105 }
106
107 /**
108 * Post-delete
109 */
110 protected function post_delete()
111 {
112 BSApp::$db->query("DELETE FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . $this->values['fieldid']);
113 BSApp::$db->query("DELETE FROM " . TABLE_PREFIX . "bugfieldpermission WHERE fieldid = " . $this->values['fieldid']);
114 BSApp::$db->query("ALTER TABLE " . TABLE_PREFIX . "bug DROP custom" . $this->values['fieldid']);
115 BSApp::$db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bug");
116
117 build_user_help();
118 }
119
120 /**
121 * Validate: fieldid
122 */
123 protected function validate_fieldid($field)
124 {
125 return $this->_verifyIsNotZero($field);
126 }
127
128 /**
129 * Validate: name
130 */
131 protected function validate_name($field)
132 {
133 return $this->_verifyIsNotEmpty($field);
134 }
135
136 /**
137 * Validate: type
138 */
139 protected function validate_type()
140 {
141 if (!in_array($this->values['type'], array_keys(self::field_types())))
142 {
143 return false;
144 }
145 return true;
146 }
147
148 /**
149 * Validate: selects
150 */
151 protected function validate_selects()
152 {
153 static $serialized;
154 if ($this->values['type'] == 'select_single' || $this->record['type'] == 'select_single')
155 {
156 BSApp::debug('it works');
157 // can't use explode() here because explode() returns !empty() when splitting an empty string
158 // so we have to use preg_split with the PREG_SPLIT_NO_EMPTY flag to prevent this
159 $selects = preg_split("#\n#", trim($this->values['selects']), 0, PREG_SPLIT_NO_EMPTY);
160 array_walk($selects, 'trim');
161 if (sizeof($selects) < 1)
162 {
163 return T('You need to specify some select values.');
164 }
165 else
166 {
167 if ($serialized != serialize($selects))
168 {
169 $this->values['selects'] = $serialized = serialize($selects);
170 }
171 }
172 }
173
174 return true;
175 }
176 }
177
178 ?>