Update api_field.php
[bugdar.git] / includes / api_field.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright ©2002-2007 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 ©2002 - 2007, Blue Static
30 * @version $Revision$
31 * @package Bugdar
32 *
33 */
34 class FieldAPI extends BSApi
35 {
36 /**
37 * Fields
38 * @var array
39 */
40 protected $fields = array(
41 'fieldid' => array(TYPE_UINT, REQ_AUTO),
42 'name' => array(TYPE_STR, REQ_YES),
43 'description' => array(TYPE_STR, REQ_NO),
44 'type' => array(TYPE_STR, REQ_YES),
45 'selects' => array(TYPE_STR, REQ_NO),
46 'required' => array(TYPE_BOOL, REQ_NO),
47 'cansearch' => array(TYPE_BOOL, REQ_NO),
48 'regexmatch' => array(TYPE_STR, REQ_NO),
49 'defaultvalue' => array(TYPE_STR, REQ_NO),
50 'usedefault' => array(TYPE_BOOL, REQ_NO),
51 'maxlength' => array(TYPE_UINT, REQ_NO)
52 );
53
54 /**
55 * Database table
56 * @var string
57 */
58 protected $table = 'bugfield';
59
60 /**
61 * Table prefix
62 * @var string
63 */
64 protected $prefix = TABLE_PREFIX;
65
66 /**
67 * A static method that is used to return an array of all the allowed
68 * field types.
69 *
70 * @param string Return the display name of a type if the key is passed
71 *
72 * @return array An array of all the allowed field types
73 */
74 public static function field_types($type = null)
75 {
76 $types = array(
77 'input_text' => T('Single-Line Text Box'),
78 'input_checkbox' => T('Checkbox Flag'),
79 'select_single' => T('Drop-Down Menu'),
80 );
81
82 if ($type == null)
83 {
84 return $types;
85 }
86 else
87 {
88 return $types["$type"];
89 }
90 }
91
92 /**
93 * Post-insert
94 */
95 protected function post_insert()
96 {
97 build_user_help();
98 }
99
100 /**
101 * Post-update
102 */
103 protected function post_update()
104 {
105 build_user_help();
106 }
107
108 /**
109 * Post-delete
110 */
111 protected function post_delete()
112 {
113 BSApp::$db->query("DELETE FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . $this->values['fieldid']);
114 BSApp::$db->query("DELETE FROM " . TABLE_PREFIX . "bugfieldpermission WHERE fieldid = " . $this->values['fieldid']);
115 BSApp::$db->query("ALTER TABLE " . TABLE_PREFIX . "bug DROP custom" . $this->values['fieldid']);
116 BSApp::$db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bug");
117
118 build_user_help();
119 }
120
121 /**
122 * Validate: fieldid
123 */
124 protected function validate_fieldid($field)
125 {
126 return $this->_verifyIsNotZero($field);
127 }
128
129 /**
130 * Validate: name
131 */
132 protected function validate_name($field)
133 {
134 return $this->_verifyIsNotEmpty($field);
135 }
136
137 /**
138 * Validate: type
139 */
140 protected function validate_type()
141 {
142 if (!in_array($this->values['type'], array_keys(self::field_types())))
143 {
144 return false;
145 }
146 return true;
147 }
148
149 /**
150 * Validate: selects
151 */
152 protected function validate_selects()
153 {
154 static $serialized;
155 if ($this->values['type'] == 'select_single' || $this->record['type'] == 'select_single')
156 {
157 BSApp::debug('it works');
158 // can't use explode() here because explode() returns !empty() when splitting an empty string
159 // so we have to use preg_split with the PREG_SPLIT_NO_EMPTY flag to prevent this
160 $selects = preg_split("#\n#", trim($this->values['selects']), 0, PREG_SPLIT_NO_EMPTY);
161 array_walk($selects, 'trim');
162 if (sizeof($selects) < 1)
163 {
164 return T('You need to specify some select values.');
165 }
166 else
167 {
168 if ($serialized != serialize($selects))
169 {
170 $this->values['selects'] = $serialized = serialize($selects);
171 }
172 }
173 }
174
175 return true;
176 }
177 }
178
179 /*=====================================================================*\
180 || ###################################################################
181 || # $HeadURL$
182 || # $Id$
183 || ###################################################################
184 \*=====================================================================*/
185 ?>