r69: Removing our nasty iff() function in place of the beautiful ternary operator
[bugdar.git] / admin / product.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # BugStrike [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 require_once('./global.php');
14 require_once('./includes/functions_datastore.php');
15
16 if (!can_perform('canadminversions'))
17 {
18 admin_login();
19 }
20
21 function construct_option_list($type, $id, $edit, $addcomponent, $addversion, $delete, $displayorder)
22 {
23 $type_display = substr_replace($type, strtoupper($type{0}), 0, 1);
24 $type = (($type == 'component') ? 'product' : $type);
25 $type_action = (($type == 'version') ? 'versionid' : 'productid');
26
27 if ($edit)
28 {
29 $opt .= '<option value="product.php?do=edit' . $type . '&' . $type_action . '=' . $id . '">Edit ' . $type_display . '</option>';
30 }
31
32 if ($delete)
33 {
34 $opt .= '<option value="product.php?do=delete' . $type . '&' . $type_action . '=' . $id . '">Delete ' . $type_display . '</option>';
35 }
36
37 if ($addcomponent)
38 {
39 $opt .= '<option value="product.php?do=addproduct&productid=' . $id . '">' . phrase('add_component') . '</option>';
40 }
41
42 if ($addversion)
43 {
44 $opt .= '<option value="product.php?do=addversion&productid=' . $id . '">' . phrase('add_version') . '</option>';
45 }
46
47 $type = strtolower($type);
48
49 $name = $type . '_' . $id;
50
51 $displayorder = (($displayorder != -1) ? '<input type="text" name="displayorder[' . $name . ']" value="' . $displayorder . '" size="4" /> ' : '');
52
53 return $displayorder . '<select id="' . $name . '" name="' . $name . '" onchange="exec_action(\'' . $name . '\')">' . $opt . '</select> <input type="button" name="gobutton" value=" ' . phrase('go') . ' " onclick="exec_action(\'' . $name . '\')" />';
54 }
55
56 // ###################################################################
57
58 if (empty($_REQUEST['do']))
59 {
60 $_REQUEST['do'] = 'modify';
61 }
62
63 // ###################################################################
64
65 if ($_REQUEST['do'] == 'killversion')
66 {
67 sanitize(array('versionid' => INT));
68
69 $db->query("DELETE FROM " . TABLE_PREFIX . "version WHERE versionid = $vars[versionid]");
70 // #*# figure out what we do with bugs
71
72 build_versions();
73
74 $admin->redirect('product.php?do=modify');
75 }
76
77 // ###################################################################
78
79 if ($_REQUEST['do'] == 'deleteversion')
80 {
81 sanitize(array('versionid' => INT));
82 $admin->page_confirm(phrase('confirm_delete_version'), 'product.php?do=killversion&amp;versionid=' . $vars['versionid']);
83 }
84
85 // ###################################################################
86
87 if ($_REQUEST['do'] == 'insertversion')
88 {
89 sanitize(array('productid' => INT, 'version' => STR, 'displayorder' => INT));
90 $db->query("INSERT INTO " . TABLE_PREFIX . "version (productid, version, displayorder) VALUES ($vars[productid], '" . addslasheslike($vars['version']) . "', $vars[displayorder])");
91 build_versions();
92 $admin->redirect('product.php?do=modify');
93 }
94
95 // ###################################################################
96
97 if ($_REQUEST['do'] == 'addversion')
98 {
99 sanitize(array('productid' => INT));
100
101 $admin->page_start(phrase('add_version'));
102
103 if ($vars['productid'] != -1)
104 {
105 $product = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "product WHERE productid = $vars[productid]");
106 if (!is_array($product))
107 {
108 $admin->error('-1');
109 }
110 }
111 else
112 {
113 $vars['productid'] = 0;
114 }
115
116 $admin->form_start('product.php', 'insertversion');
117 $admin->form_hidden_field('productid', $vars['productid']);
118 $admin->table_start();
119 $admin->table_head(phrase('add_new_version'));
120 $admin->row_input(phrase('version_title'), 'version');
121 $admin->row_input(phrase('version_display_order'), 'displayorder');
122 $admin->row_submit();
123 $admin->table_end();
124 $admin->form_end();
125
126 $admin->page_end();
127 }
128
129 // ###################################################################
130
131 if ($_REQUEST['do'] == 'updateversion')
132 {
133 sanitize(array('versionid' => INT, 'version' => STR, 'displayorder' => INT));
134
135 if (empty($vars['version']))
136 {
137 $admin->error(phrase('fill_in_version_number'));
138 }
139
140 $db->query("UPDATE " . TABLE_PREFIX . "version SET version = '" . addslasheslike($vars['version']) . "', displayorder = $vars[displayorder] WHERE versionid = $vars[versionid]");
141 build_versions();
142
143 $admin->redirect('product.php?do=modify');
144 }
145
146 // ###################################################################
147
148 if ($_REQUEST['do'] == 'editversion')
149 {
150 sanitize(array('versionid' => INT));
151
152 $admin->page_start(phrase('edit_version'));
153
154 $version = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "version WHERE versionid = $vars[versionid]");
155 if (!is_array($version))
156 {
157 $admin->error('-1');
158 }
159
160 $admin->form_start('product.php', 'updateversion');
161 $admin->form_hidden_field('versionid', $version['versionid']);
162 $admin->table_start();
163 $admin->table_head(phrase('edit_version_title', $version['version']));
164 $admin->row_input(phrase('version_title'), 'version', $version['version']);
165 $admin->row_input(phrase('version_display_order'), 'displayorder', $version['displayorder']);
166 $admin->row_submit();
167 $admin->table_end();
168 $admin->form_end();
169
170 $admin->page_end();
171 }
172
173 // ###################################################################
174
175 if ($_REQUEST['do'] == 'killproduct')
176 {
177 sanitize(array('productid' => INT));
178
179 $allprods = $db->query("SELECT * FROM " . TABLE_PREFIX . "product WHERE productid = $vars[productid] OR componentmother = $vars[productid]");
180 while ($prod = $db->fetch_array($allprods))
181 {
182 $list[] = $prod['productid'];
183 }
184
185 $db->query("DELETE FROM " . TABLE_PREFIX . "product WHERE productid IN (" . implode(', ', $list) . ")");
186 $db->query("DELETE FROM " . TABLE_PREFIX . "version WHERE productid IN (" . implode(', ', $list) . ")");
187 // #*# do bug kills here
188
189 build_products();
190
191 $admin->redirect('product.php?do=modify');
192 }
193
194 // ###################################################################
195
196 if ($_REQUEST['do'] == 'deleteproduct')
197 {
198 sanitize(array('productid' => INT));
199 $admin->page_confirm(phrase('confirm_delete_product'), 'product.php?do=killproduct&amp;productid=' . $vars['productid']);
200 }
201
202 // ###################################################################
203
204 if ($_REQUEST['do'] == 'insertproduct')
205 {
206 sanitize(array('shortname' => STR, 'title' => STR, 'componentmother' => INT, 'description' => STR, 'displayorder' => INT));
207
208 if (empty($vars['shortname']) OR empty($vars['title']))
209 {
210 $admin->error(phrase('go_back_and_fill_both_fields'));
211 }
212
213 $db->query("
214 INSERT INTO " . TABLE_PREFIX . "product
215 (shortname, title, componentmother, description, displayorder)
216 VALUES
217 ('" . addslasheslike($vars['shortname']) . "', '" . addslasheslike($vars['title']) . "',
218 $vars[componentmother], '" . addslasheslike($vars['description']) . "', $vars[displayorder])"
219 );
220 build_products();
221
222 $admin->redirect('product.php?do=modify');
223 }
224
225 // ###################################################################
226
227 if ($_REQUEST['do'] == 'addproduct')
228 {
229 sanitize(array('productid' => INT));
230
231 $admin->page_start(phrase('add_new_product'));
232
233 $admin->form_start('product.php', 'insertproduct');
234 $admin->form_hidden_field('componentmother', $vars['productid']);
235 $admin->table_start();
236 $admin->table_head(phrase('add_product'));
237 $admin->row_input(phrase('product_shortname'), 'shortname');
238 $admin->row_input(phrase('product_title'), 'title');
239 $admin->row_textarea(phrase('product_description'), 'description');
240 $admin->row_input(phrase('product_display_order'), 'displayorder');
241 $admin->row_submit();
242 $admin->table_end();
243 $admin->form_end();
244
245 $admin->page_end();
246 }
247
248 // ###################################################################
249
250 if ($_REQUEST['do'] == 'updateproduct')
251 {
252 sanitize(array('productid' => INT, 'title' => STR, 'shortname' => STR, 'description' => STR, 'displayorder' => INT));
253
254 if (empty($vars['shortname']) OR empty($vars['title']))
255 {
256 $admin->error(phrase('go_back_and_fill_both_fields'));
257 }
258
259 if (empty($vars['productid']))
260 {
261 $admin->error('-1');
262 }
263
264 $db->query("UPDATE " . TABLE_PREFIX . "product SET title = '" . addslasheslike($vars['title']) . "', shortname = '" . addslasheslike($vars['shortname']) . "', description = '" . addslasheslike($vars['description']) . "', displayorder = $vars[displayorder] WHERE productid = $vars[productid]");
265 build_products();
266
267 $admin->redirect('product.php?do=modify');
268 }
269
270 // ###################################################################
271
272 if ($_REQUEST['do'] == 'editproduct')
273 {
274 sanitize(array('productid' => INT));
275
276 $admin->page_start(phrase('edit_product'));
277
278 $product = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "product WHERE productid = $vars[productid]");
279
280 if (!is_array($product))
281 {
282 $admin->error('-1');
283 }
284
285 $admin->form_start('product.php', 'updateproduct');
286 $admin->form_hidden_field('productid', $product['productid']);
287 $admin->table_start();
288 $admin->table_head(phrase('edit_product_title', $product['title']));
289 $admin->row_input(phrase('product_title'), 'title', $product['title']);
290 $admin->row_input(phrase('product_shortname'), 'shortname', $product['shortname']);
291 $admin->row_textarea(phrase('product_description'), 'description', $product['description']);
292 $admin->row_input(phrase('product_display_order'), 'displayorder', $product['displayorder']);
293 $admin->row_submit();
294 $admin->table_end();
295 $admin->form_end();
296
297 $admin->page_end();
298 }
299
300 // ###################################################################
301
302 if ($_POST['do'] == 'displayorder')
303 {
304 foreach ((array)$_POST['displayorder'] AS $namebit => $displayorder)
305 {
306 $name = explode('_', $namebit);
307 if ($name[0] == 'product' OR $name[0] == 'version')
308 {
309 $id = intval($name[1]);
310 $order = intval($displayorder);
311 $db->query("UPDATE " . TABLE_PREFIX . "$name[0] SET displayorder = $order WHERE $name[0]id = $id");
312 }
313 }
314 build_products();
315 build_versions();
316 $admin->redirect('product.php?do=modify');
317 }
318
319 // ###################################################################
320
321 if ($_REQUEST['do'] == 'modify')
322 {
323 $admin->page_start(phrase('products_and_versions'));
324
325 $javascript = <<<EOF
326 <script type="text/javascript">
327 <!--
328 function exec_action(name)
329 {
330 window.location = document.getElementById(name).value;
331 }
332 //-->
333 </script>
334 EOF;
335
336 $admin->page_code($javascript);
337
338 $admin->form_start('product.php', 'displayorder');
339
340 $products = $db->query("SELECT * FROM " . TABLE_PREFIX . "product ORDER BY displayorder ASC");
341 while ($prod = $db->fetch_array($products))
342 {
343 if (!$prod['componentmother'])
344 {
345 $product["$prod[productid]"] = $prod;
346 }
347 else
348 {
349 $component["$prod[componentmother]"][] = $prod;
350 }
351 $version["$prod[productid]"] = array();
352 }
353 $products = (array)$product;
354
355 $versions = $db->query("SELECT * FROM " . TABLE_PREFIX . "version ORDER BY displayorder ASC");
356 while ($vers = $db->fetch_array($versions))
357 {
358 $version["$vers[productid]"]["$vers[versionid]"] = $vers;
359 }
360 $versions = (array)$version;
361
362 $admin->table_start();
363 $admin->table_head(phrase('products_versions'));
364
365 // Handle our global versions
366 if (is_array($versions['0']))
367 {
368 $admin->row_text(phrase('global_versions'), construct_option_list('product', -1, 0, 0, 1, 0, -1), 'middle', 2, 'alt3');
369 foreach ($versions['0'] AS $version)
370 {
371 $admin->row_text('-- ' . $version['version'], construct_option_list('version', $version['versionid'], 1, 0, 0, 1, $version['displayorder']), 'middle', 2, 'alt2');
372 }
373 }
374
375 // Now let's do the rest of the versions
376 foreach ($products AS $product)
377 {
378 // Product
379 $admin->row_text("<a href=\"product.php?do=editproduct&amp;productid=$product[productid]\">$product[title]</a>", construct_option_list('product', $product['productid'], 1, 1, 1, 1, $product['displayorder']), 'middle', 2, 'alt3');
380
381 // Versions
382 foreach ($versions["$product[productid]"] AS $version)
383 {
384 $admin->row_text('-- ' . $version['version'], construct_option_list('version', $version['versionid'], 1, 0, 0, 1, $version['displayorder']), 'middle', 2, 'alt2');
385 }
386
387 // Components
388 foreach ((array)$component["$product[productid]"] AS $comp)
389 {
390 $admin->row_text("-- <a href=\"product.php?do=editproduct&amp;productid=$comp[productid]\">$comp[title]</a>", construct_option_list('component', $comp['productid'], 1, 0, 1, 1, $comp['displayorder']), 'middle', 2, 'alt1');
391
392 // Component versions
393 foreach ($versions["$comp[productid]"] AS $version)
394 {
395 $admin->row_text('---- ' . $version['version'], construct_option_list('version', $version['versionid'], 1, 0, 0, 1, $version['displayorder']), 'middle', 2, 'alt2');
396 }
397 }
398 }
399
400 $admin->row_span('<input type="submit" name="button" value=" ' . phrase('save_display_order') . ' " accesskey="s" /> <input type="button" name="addproduct" value=" ' . phrase('add_new_product') . ' " onclick="window.location = \'product.php?do=addproduct\';" />', 'tfoot', 'center');
401
402 $admin->table_end();
403
404 $admin->form_end();
405
406 $admin->page_end();
407 }
408
409 /*=====================================================================*\
410 || ###################################################################
411 || # $HeadURL$
412 || # $Id$
413 || ###################################################################
414 \*=====================================================================*/
415 ?>