]> src.bluestatic.org Git - bugdar.git/blob - admin/product.php
Switch the 'modify' code of admin/field.php to use templates
[bugdar.git] / admin / product.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright (c)2004-2009 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 $fetchtemplates = array(
23 'product',
24 'product_edit',
25 'version_edit'
26 );
27
28 // TODO - add API for product.php
29
30 require_once('./global.php');
31 require_once('./includes/functions_datastore.php');
32
33 if (!can_perform('canadminversions'))
34 {
35 admin_login();
36 }
37
38 function construct_option_list($type_display, $type, $id, $edit, $addcomponent, $addversion, $delete, $displayorder)
39 {
40 global $bugsys;
41
42 $type = ($type == 'component' ? 'product' : $type);
43 $type_action = (($type == 'version') ? 'versionid' : 'productid');
44
45 if ($edit)
46 {
47 $opt .= '<option value="product.php?do=edit' . $type . '&' . $type_action . '=' . $id . '">' . sprintf(T('Edit %1$s'), $type_display) . '</option>';
48 }
49
50 if ($delete)
51 {
52 $opt .= '<option value="product.php?do=delete' . $type . '&' . $type_action . '=' . $id . '">' . sprintf(T('Delete %1$s'), $type_display) . '</option>';
53 }
54
55 if ($addcomponent)
56 {
57 $opt .= '<option value="product.php?do=addproduct&productid=' . $id . '">' . T('Add Component') . '</option>';
58 }
59
60 if ($addversion)
61 {
62 $opt .= '<option value="product.php?do=addversion&productid=' . $id . '">' . T('Add Version') . '</option>';
63 }
64
65 $type = strtolower($type);
66
67 $name = $type . '_' . $id;
68
69 $displayorder = (($displayorder != -1) ? '<input type="text" name="displayorder[' . $name . ']" value="' . $displayorder . '" size="4" class="input" style="width:25px" /> ' : '');
70
71 return $displayorder . '<select id="' . $name . '" name="' . $name . '" onchange="exec_action(\'' . $name . '\')" class="input">' . $opt . '</select> <input type="button" class="button" name="gobutton" value=" ' . T('Go') . ' " onclick="exec_action(\'' . $name . '\')" />';
72 }
73
74 // ###################################################################
75
76 if (empty($_REQUEST['do']))
77 {
78 $_REQUEST['do'] = 'modify';
79 }
80
81 // ###################################################################
82
83 if ($_REQUEST['do'] == 'killversion')
84 {
85 $input->inputClean('versionid', TYPE_UINT);
86 $db->query("DELETE FROM " . TABLE_PREFIX . "version WHERE versionid = " . $input->in['versionid']);
87 $db->query("DELETE FROM " . TABLE_PREFIX . "bug WHERE version = " . $input->in['versionid']);
88
89 build_versions();
90
91 $admin->redirect('product.php?do=modify');
92 }
93
94 // ###################################################################
95
96 if ($_REQUEST['do'] == 'deleteversion')
97 {
98 $admin->page_confirm(T('Are you sure you want to delete this version? Doing so will do <strong>delete all the bugs with this version</strong>.'), 'product.php', 'killversion', array('versionid' => $input->inputClean('versionid', TYPE_UINT)));
99 }
100
101 // ###################################################################
102
103 if ($_REQUEST['do'] == 'insertversion')
104 {
105 $db->query("INSERT INTO " . TABLE_PREFIX . "version (productid, version, displayorder, obsolete) VALUES (" . $input->inputClean('productid', TYPE_UINT) . ", '" . $input->input_escape('version') . "', " . $input->inputClean('displayorder', TYPE_UINT) . ", " . $input->inputClean('obsolete', TYPE_UINT) . ")");
106 build_versions();
107 $admin->redirect('product.php?do=modify');
108 }
109
110 // ###################################################################
111
112 if ($_REQUEST['do'] == 'addversion')
113 {
114 $product = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "product WHERE productid = " . $input->inputClean('productid', TYPE_UINT));
115 if (!$product)
116 {
117 $admin->error(L_INVALID_ID);
118 }
119
120 $tpl = new BSTemplate('version_edit');
121 $tpl->vars['product'] = $product;
122 $tpl->evaluate()->flush();
123 }
124
125 // ###################################################################
126
127 if ($_REQUEST['do'] == 'updateversion')
128 {
129 if (empty($input->in['version']))
130 {
131 $admin->error(T('Please fill in a version number.'));
132 }
133
134 $db->query("UPDATE " . TABLE_PREFIX . "version SET version = '" . $input->inputEscape('version') . "', displayorder = " . $input->inputClean('displayorder', TYPE_UINT) . ", obsolete = " . $input->inputClean('obsolete', TYPE_UINT) . " WHERE versionid = " . $input->inputClean('versionid', TYPE_UINT));
135 build_versions();
136
137 $admin->redirect('product.php?do=modify');
138 }
139
140 // ###################################################################
141
142 if ($_REQUEST['do'] == 'editversion')
143 {
144 $tpl = new BSTemplate('version_edit');
145
146 $tpl->vars['version'] = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "version WHERE versionid = " . $input->inputClean('versionid', TYPE_UINT));
147 if (!$tpl->vars['version'])
148 {
149 $admin->error(L_INVALID_ID);
150 }
151
152 $tpl->evaluate()->flush();
153 }
154
155 // ###################################################################
156
157 if ($_REQUEST['do'] == 'killproduct')
158 {
159 $input->inputClean('productid', TYPE_UINT);
160 $allprods = $db->query("SELECT * FROM " . TABLE_PREFIX . "product WHERE productid = " . $input->in['productid'] . " OR parentid = " . $input->in['productid']);
161 foreach ($allprods as $prod)
162 {
163 $list[] = $prod['productid'];
164 }
165
166 $db->query("DELETE FROM " . TABLE_PREFIX . "product WHERE productid IN (" . implode(', ', $list) . ")");
167 $db->query("DELETE FROM " . TABLE_PREFIX . "version WHERE productid IN (" . implode(', ', $list) . ")");
168 $db->query("DELETE FROM " . TABLE_PREFIX . "bug WHERE product IN (" . implode(', ', $list) . ")");
169
170 build_products();
171
172 $admin->redirect('product.php?do=modify');
173 }
174
175 // ###################################################################
176
177 if ($_REQUEST['do'] == 'deleteproduct')
178 {
179 $admin->page_confirm(T('Are you sure you want to delete this product and all of it\'s versions and components <strong>and any bugs that have been assigned those products or components</strong>?'), 'product.php', 'killproduct', array('productid' => $input->inputClean('productid', TYPE_UINT)));
180 }
181
182 // ###################################################################
183
184 if ($_REQUEST['do'] == 'insertproduct')
185 {
186 if (empty($input->in['title']))
187 {
188 $admin->error(T('Please go back and fill in the title field.'));
189 }
190
191 $db->query("
192 INSERT INTO " . TABLE_PREFIX . "product
193 (title, parentid, description, displayorder)
194 VALUES
195 ('" . $input->inputEscape('title') . "', " . $input->inputClean('parentid', TYPE_UINT) . ",
196 '" . $input->inputEscape('description') . "', " . $input->inputClean('displayorder', TYPE_UINT) . "
197 )"
198 );
199 build_products();
200
201 $admin->redirect('product.php?do=modify');
202 }
203
204 // ###################################################################
205
206 if ($_REQUEST['do'] == 'addproduct')
207 {
208 $tpl = new BSTemplate('product_edit');
209 $tpl->vars['parent'] = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "product WHERE productid = " . $input->inputClean('productid', TYPE_UINT));
210 $tpl->evaluate()->flush();
211 }
212
213 // ###################################################################
214
215 if ($_REQUEST['do'] == 'updateproduct')
216 {
217 if (empty($input->in['title']))
218 {
219 $admin->error(T('Please go back and fill in the title field.'));
220 }
221
222 if (empty($input->in['productid']))
223 {
224 $admin->error(L_INVALID_ID);
225 }
226
227 $db->query("UPDATE " . TABLE_PREFIX . "product SET title = '" . $input->inputEscape('title') . "', description = '" . $input->inputEscape('description') . "', displayorder = " . $input->inputClean('displayorder', TYPE_UINT) . " WHERE productid = " . $input->inputClean('productid', TYPE_UINT));
228 build_products();
229
230 $admin->redirect('product.php?do=modify');
231 }
232
233 // ###################################################################
234
235 if ($_REQUEST['do'] == 'editproduct')
236 {
237 $tpl = new BSTemplate('product_edit');
238 $tpl->vars['product'] = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "product WHERE productid = " . $input->in['productid']);
239 if (!is_array($tpl->vars['product']))
240 {
241 $admin->error(L_INVALID_ID);
242 }
243 $tpl->vars['parent'] = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "product WHERE productid = {$tpl->vars['product']['parentid']}");
244
245 $tpl->evaluate()->flush();
246 }
247
248 // ###################################################################
249
250 if ($_POST['do'] == 'displayorder')
251 {
252 $input->inputClean('displayorder', TYPE_UINT);
253 if (is_array($input->in['displayorder']))
254 {
255 foreach ($input->in['displayorder'] as $namebit => $order)
256 {
257 $name = explode('_', $namebit);
258 if ($name[0] == 'product' || $name[0] == 'version')
259 {
260 $id = $input->clean($name[1], TYPE_UINT);
261 $db->query("UPDATE " . TABLE_PREFIX . "$name[0] SET displayorder = $order WHERE $name[0]id = $id");
262 }
263 }
264 build_products();
265 build_versions();
266 }
267
268 admin_flash_redirect('product.php?do=modify');
269 }
270
271 // ###################################################################
272
273 if ($_REQUEST['do'] == 'modify')
274 {
275 $tpl = new BSTemplate('product');
276
277 $products_get = $db->query("SELECT * FROM " . TABLE_PREFIX . "product ORDER BY displayorder ASC");
278 $tpl->vars['products'] = $tpl->vars['components'] = $tpl->vars['version'] = array();
279 foreach ($products_get as $prod)
280 {
281 if (!$prod['parentid'])
282 {
283 $tpl->vars['products'][$prod['productid']] = $prod;
284 }
285 else
286 {
287 $tpl->vars['components'][$prod['parentid']][] = $prod;
288 }
289 $tpl->vars['version'][$prod['productid']] = array();
290 }
291
292 $versions_get = $db->query("SELECT * FROM " . TABLE_PREFIX . "version ORDER BY displayorder ASC");
293 foreach ($versions_get as $vers)
294 {
295 $tpl->vars['versions'][$vers['productid']][$vers['versionid']] = $vers;
296 }
297
298 $tpl->evaluate()->flush();
299 }
300
301 ?>