- Update the copyright notices to use the correct year and not a non-ASCII symbol
[bugdar.git] / includes / api_bug.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
24 /**
25 * API: Bug
26 *
27 * Note: When priority, severity, status, and resolution should throw a
28 * verification error, they actually set it to the default value
29 *
30 * @author Blue Static
31 * @copyright Copyright (c)2004 - 2008, Blue Static
32 * @package Bugdar
33 *
34 */
35 class BugAPI extends BSApi
36 {
37 /**
38 * Database fields
39 * @var array
40 */
41 protected $fields = array(
42 'bugid' => array(TYPE_UINT, REQ_AUTO),
43 'userid' => array(TYPE_UINT, REQ_NO),
44 'username' => array(TYPE_STR, REQ_NO),
45 'dateline' => array(TYPE_UINT, REQ_SET),
46 'product' => array(TYPE_UINT, REQ_YES),
47 'component' => array(TYPE_UINT, REQ_NO),
48 'version' => array(TYPE_UINT, REQ_YES),
49 'summary' => array(TYPE_STR, REQ_YES),
50 'priority' => array(TYPE_UINT, REQ_NO),
51 'severity' => array(TYPE_UINT, REQ_NO),
52 'status' => array(TYPE_UINT, REQ_NO),
53 'resolution' => array(TYPE_UINT, REQ_NO),
54 'assignedto' => array(TYPE_UINT, REQ_NO),
55 'duplicateof' => array(TYPE_STR, REQ_NO),
56 'dependency' => array(TYPE_STR, REQ_NO),
57 'hidden' => array(TYPE_BOOL, REQ_NO),
58 'initialreport' => array(TYPE_UINT, REQ_NO),
59 'lastposttime' => array(TYPE_UINT, REQ_NO),
60 'lastpostby' => array(TYPE_UINT, REQ_NO),
61 'lastpostbyname' => array(TYPE_STR, REQ_NO),
62 'hiddenlastposttime' => array(TYPE_UINT, REQ_NO),
63 'hiddenlastpostby' => array(TYPE_UINT, REQ_NO),
64 'hiddenlastpostbyname' => array(TYPE_STR, REQ_NO)
65 );
66
67 /**
68 * Database table
69 * @var string
70 */
71 protected $table = 'bug';
72
73 /**
74 * Table prefix
75 * @var string
76 */
77 protected $prefix = TABLE_PREFIX;
78
79 /**
80 * Subclassed set() that will intercept any custom fields and handle
81 * them appropriately, but everyting else will be passed to the parent.
82 */
83 function set($field, $value, $doclean = true, $doverify = true)
84 {
85 // it's a custom field, so add it in
86 if (preg_match('/^custom/', $field) == 1 && !isset($this->fields["$field"]))
87 {
88 $this->fields["$field"] = array(TYPE_STR, REQ_NO);
89 }
90
91 // TODO - (r1524) one day we can change this back to call_user_func_array(array($this, 'parent::set'), func_get_args())
92 parent::set($field, $value, $doclean, $doverify);
93 }
94
95 /**
96 * Set field: dateline
97 */
98 protected function set_dateline()
99 {
100 $this->set('dateline', time());
101 }
102
103 /**
104 * Post-insert
105 */
106 protected function post_insert()
107 {
108 BSApp::$db->query("INSERT INTO " . TABLE_PREFIX . "vote (bugid, votefor, voteagainst) VALUES (" . $this->insertid . ", 0, 0)");
109 }
110
111 /**
112 * Post-delete
113 */
114 protected function post_delete()
115 {
116 BSApp::$db->query("DELETE FROM " . TABLE_PREFIX . "comment WHERE bugid = " . $this->values['bugid']);
117 BSApp::$db->query("DELETE FROM " . TABLE_PREFIX . "favorite WHERE bugid = " . $this->values['bugid']);
118 BSApp::$db->query("DELETE FROM " . TABLE_PREFIX . "history WHERE bugid = " . $this->values['bugid']);
119 BSApp::$db->query("DELETE FROM " . TABLE_PREFIX . "vote WHERE bugid = " . $this->values['bugid']);
120 }
121
122 /**
123 * Verify: product
124 */
125 protected function validate_product($field)
126 {
127 if (!$this->_verifyIsNotZero('product'))
128 {
129 return false;
130 }
131
132 if (!bugdar::$datastore['product'][$this->values['product']])
133 {
134 $this->_error(new FieldException(L_INVALID_ID, $field));
135 return false;
136 }
137
138 return true;
139 }
140
141 /**
142 * Verify: componentid
143 */
144 protected function validate_component($field)
145 {
146 if ($this->values['component'] != 0)
147 {
148 $component = bugdar::$datastore['component'][ $this->values['component'] ];
149 $product = bugdar::$datastore['product'][ $this->values['product'] ];
150 $version = bugdar::$datastore['version'][ $this->values['version'] ];
151 if ($component['parentid'] != $product['productid'])
152 {
153 $this->_error(new FieldException(L_INVALID_ID, $field));
154 return false;
155 }
156 if (($version['productid'] != $component['productid'] && $version['productid'] != $product['productid']) && $version['productid'] != 0)
157 {
158 $this->_error(new FieldException(L_INVALID_ID, $field));
159 return false;
160 }
161 }
162 return true;
163 }
164
165 /**
166 * Verify: versionid
167 */
168 protected function validate_version()
169 {
170 if ($this->_verifyIsNotZero('version'))
171 {
172 return false;
173 }
174
175 if (!bugdar::$datastore['version'][ $this->values['version'] ])
176 {
177 $this->_error(new FieldException(L_INVALID_ID, $field));
178 return false;
179 }
180 return true;
181 }
182
183 /**
184 * Verify: priority
185 */
186 protected function validate_priority()
187 {
188 if (!bugdar::$datastore['priority'][ $this->values['priority'] ])
189 {
190 $this->set('priority', bugdar::$options['defaultpriority']);
191 }
192 return true;
193 }
194
195 /**
196 * Verify: severity
197 *
198 * @access private
199 */
200 protected function validate_severity()
201 {
202 if (!bugdar::$datastore['severity'][ $this->values['severity'] ])
203 {
204 $this->set('severity', bugdar::$options['defaultseverity']);
205 }
206 return true;
207 }
208
209 /**
210 * Verify: status
211 */
212 protected function validate_status()
213 {
214 if (!bugdar::$datastore['status'][ $this->values['status'] ])
215 {
216 $this->set('status', bugdar::$options['defaultstatus']);
217 }
218 return true;
219 }
220
221 /**
222 * Verify: resolution
223 */
224 protected function validate_resolution()
225 {
226 if (!bugdar::$datastore['resolution'][ $this->values['resolution'] ])
227 {
228 $this->set('resolution', bugdar::$options['defaultresolve']);
229 }
230 return true;
231 }
232
233 /**
234 * Verify: assignedto
235 *
236 * @access private
237 */
238 protected function validate_assignedto()
239 {
240 if (!bugdar::$datastore['assignto'][ $this->values['assignedto'] ] && $this->values['assignedto'] != 0)
241 {
242 $this->set('assignedto', bugdar::$options['defaultassign']);
243 }
244 return true;
245 }
246 }
247
248 ?>