Sponsorenverwaltung - Team StarCraft e.V.
 All Data Structures Files Functions Variables
dropdown_fields.php
Go to the documentation of this file.
1 <?php
2  /**
3  * @file dropdown_fields.php
4  *
5  * @brief Provides application logic for management of persistent dropdownfield
6  * entries used in sponsor-detailview as well as for filter tasks.
7  *
8  * @details
9  *
10  * This file requires inc/common.php, inc/templates/edit_dropdown.tpl
11  * as well as inc/templates/dropdown_fields.tpl to work properly.
12  *
13  *
14  * @copyright (c) 2013, Team StarCraft e.V.
15  * @version 1.0.0
16  * @author Joachim Wagner
17  * @author Daniel Seichter
18  * @date 02.07.2013
19  */
20 
21 
22  /// @cond MAINPART
23  // include
24  include("inc/common.php");
25 
26  // Usercake
27  if(!accessGranted($_SERVER['PHP_SELF'])) {
28  if (isUserLoggedIn()) {
29  exitWithErrorTemplate(array('Die angeforderte Seite ist gesperrt.'));
30  }
31  else {
32  exitWithErrorTemplateAndRedirect(array('Die angeforderte Seite ist gesperrt oder geschützt.'), 'login.php', 2);
33  }
34  }
35 
36  // disable all output sections in template
37  $displayProductionFacility = FALSE;
38  $displayStatus = FALSE;
39  $displayCategory = FALSE;
40  $displayCar = FALSE;
41  $smarty->assign('displayProductionFacility', $displayProductionFacility);
42  $smarty->assign('displayStatus', $displayStatus);
43  $smarty->assign('displayCategory', $displayCategory);
44  $smarty->assign('displayCar', $displayCar);
45 
46 
47  //manage several dropdown fields
48  if(isset($_GET['production_facility_id']) && ctype_digit($_GET['production_facility_id'])) {
49  // extract the id and show details of the selected production facility
50  $productionFacilityId = $_GET['production_facility_id'];
51 
52  // ajax request to renew lock / unlock
53  if(isset($_GET['ajax']) && ctype_digit($_GET['ajax'])) {
54  if ($_GET['ajax'] == 1) {
55  if(isset($_GET['unlock']) && ctype_digit($_GET['unlock'])) {
56  if ($_GET['unlock'] == 1) {
57  unlockProductionFacility($productionFacilityId);
58  exit;
59  }
60  }
61  else {
62  renewProductionFacilityLock($productionFacilityId);
63  exit;
64  }
65  }
66  }
67 
68  // lock tablerow
69  if (isProductionFacilityLocked($productionFacilityId)) {
70  $errors [] = 'Dieser Eintrag wird gerade von einem anderen Benutzer bearbeitet und ist gesperrt!';
71  exitWithErrorTemplateAndRedirect(null, 'dropdown_fields.php', 2);
72  }
73  // contiune here if not locked
74  lockProductionFacility($productionFacilityId);
75 
76  //get details
77  $productionFacilityDetails = fetchProductionFacilityDetails($productionFacilityId);
78 
79  // if the form was posted
80  if (!empty($_POST)) {
81 
82  // if the production facility should be deleted
83  if(isset($_POST['delete']) && ctype_digit($_POST['delete'])) {
84  if($_POST['delete'] == $productionFacilityId){
85  if (deleteProductionFacility($productionFacilityId)) {
86  $successes[] = "Die Fertigungsmöglichkeit wurde erfolgreich gelöscht.";
87  exitWithSuccessTemplateAndRedirect(NULL, 'dropdown_fields.php?show=0', 2);
88  }
89  else {
90  $errors [] = "Ein Datenbankfehler ist aufgetreten (eventuell wird die Fertigungsmöglichkeit noch benutzt).";
91  }
92  }
93  }
94 
95  // if the production facility should be updated
96  $update = htmlSpecialChars(trim($_POST['update']), ENT_QUOTES);
97  if(!empty($update)) {
98  if(strcmp($productionFacilityDetails['name'], $update) != 0) {
99  updateProductionFacility($update, $productionFacilityDetails['id']);
100  unlockProductionFacility($productionFacilityId);
101  $successes[] = "Der 'Name' wurde erfolgreich aktualisiert.";
102  exitWithSuccessTemplateAndRedirect(NULL, 'dropdown_fields.php?show=0', 2);
103  }
104  }
105  else {
106  $errors[] = "Geben Sie einen gültigen 'Namen'.";
107  $productionFacilityDetails['name'] = $update;
108  }
109  }
110 
111  $displayProductionFacility = TRUE;
112  $smarty->assign('lockDuration', EDIT_SECTION_LOCK_DURATION);
113  $smarty->assign('production_facilities', $productionFacilityDetails);
114  $smarty->assign('displayProductionFacility',$displayProductionFacility);
115  displayTemplateWithErrorsAndSuccesses('Fertigungsmöglichkeiten bearbeiten','edit_dropdown.tpl');
116  }
117  else if(isset($_GET['status_id']) && ctype_digit($_GET['status_id'])) {
118 
119  // extract the id and show details of the selected sponsor status
120  $statusId = $_GET['status_id'];
121 
122  // ajax request to renew lock / unlock
123  if(isset($_GET['ajax']) && ctype_digit($_GET['ajax'])) {
124  if ($_GET['ajax'] == 1) {
125  if(isset($_GET['unlock']) && ctype_digit($_GET['unlock'])) {
126  if ($_GET['unlock'] == 1) {
127  unlockStatus($statusId);
128  exit;
129  }
130  }
131  else {
132  renewStatusLock($statusId);
133  exit;
134  }
135  }
136  }
137 
138  // lock tablerow
139  if (isStatusLocked($statusId)) {
140  $errors [] = 'Dieser Eintrag wird gerade von einem anderen Benutzer bearbeitet und ist gesperrt!';
141  exitWithErrorTemplateAndRedirect(null, 'dropdown_fields.php', 2);
142  }
143  // contiune here if not locked
144  lockStatus($statusId);
145 
146  $statusDetails = fetchStatusDetails($statusId);
147 
148  // if the form was posted
149  if (!empty($_POST)) {
150 
151  // if the status should be deleted
152  if(isset($_POST['delete']) && ctype_digit($_POST['delete'])) {
153  if($_POST['delete'] == $statusId){
154  if (deleteStatus($statusId)) {
155  $successes[] = "Der Status wurde erfolgreich gelöscht.";
156  exitWithSuccessTemplateAndRedirect(NULL, 'dropdown_fields.php?show=1', 2);
157  } else {
158  $errors [] = "Ein Datenbankfehler ist aufgetreten (eventuell wird der Status noch benutzt).";
159  }
160  }
161  }
162 
163  // if the status should be updated
164  $update = htmlSpecialChars(trim($_POST['update']), ENT_QUOTES);
165  if(!empty($update)) {
166  if(strcmp($statusDetails['name'], $update) != 0) {
167  updateStatus($update, $statusDetails['id']);
168  unlockStatus($statusId);
169  $successes[] = "Der 'Name' wurde erfolgreich aktualisiert.";
170  exitWithSuccessTemplateAndRedirect(NULL, 'dropdown_fields.php?show=1', 2);
171  }
172  }
173  else {
174  $errors[] = "Geben Sie einen gültigen 'Namen'.";
175  $statusDetails['name'] = $update;
176  }
177  }
178 
179  $displayStatus = TRUE;
180  $smarty->assign('lockDuration', EDIT_SECTION_LOCK_DURATION);
181  $smarty->assign('status', $statusDetails);
182  $smarty->assign('displayStatus',$displayStatus);
183  displayTemplateWithErrorsAndSuccesses('Status bearbeiten','edit_dropdown.tpl');
184  }
185  else if(isset($_GET['category_id']) && ctype_digit($_GET['category_id'])) {
186  // extract the id and show details of the selected category
187  $categoryId = $_GET['category_id'];
188 
189  // ajax request to renew lock / unlock
190  if(isset($_GET['ajax']) && ctype_digit($_GET['ajax'])) {
191  if ($_GET['ajax'] == 1) {
192  if(isset($_GET['unlock']) && ctype_digit($_GET['unlock'])) {
193  if ($_GET['unlock'] == 1) {
194  unlockCategory($categoryId);
195  exit;
196  }
197  }
198  else {
199  renewCategoryLock($categoryId);
200  exit;
201  }
202  }
203  }
204 
205  // lock tablerow
206  if (isCategoryLocked($categoryId)) {
207  $errors [] = 'Dieser Eintrag wird gerade von einem anderen Benutzer bearbeitet und ist gesperrt!';
208  exitWithErrorTemplateAndRedirect(null, 'dropdown_fields.php', 2);
209  }
210  // contiune here if not locked
211  lockCategory($categoryId);
212 
213  $categoryDetails = fetchCategoryDetails($categoryId);
214 
215  // if the form was posted
216  if (!empty($_POST)) {
217 
218  // if the categorie should be deleted
219  if(isset($_POST['delete']) && ctype_digit($_POST['delete'])) {
220  if($_POST['delete'] == $categoryId){
221  if (deleteCategory($categoryId)) {
222  $successes[] = "Die Kategorie wurde erfolgreich gelöscht.";
223  exitWithSuccessTemplateAndRedirect(NULL, 'dropdown_fields.php?show=2', 2);
224  }
225  else {
226  $errors [] = "Ein Datenbankfehler ist aufgetreten (eventuell wird die Kategorie noch benutzt).";
227  }
228  }
229  }
230 
231  // if the category should be updated
232  $update = htmlSpecialChars(trim($_POST['update']), ENT_QUOTES);
233  if(!empty($update)) {
234  if(strcmp($categoryDetails['name'], $update) != 0) {
235  updateCategory($update, $categoryDetails['id']);
236  unlockCategory($categoryId);
237  $successes[] = "Der 'Name' wurde erfolgreich aktualisiert.";
238  exitWithSuccessTemplateAndRedirect(NULL, 'dropdown_fields.php?show=2', 2);
239  }
240  }
241  else {
242  $errors[] = "Geben Sie einen gültigen 'Namen' an.";
243  $categoryDetails['name'] = $update;
244  }
245  }
246 
247  $displayCategory = TRUE;
248  $smarty->assign('lockDuration', EDIT_SECTION_LOCK_DURATION);
249  $smarty->assign('category', $categoryDetails);
250  $smarty->assign('displayCategory',$displayCategory);
251  displayTemplateWithErrorsAndSuccesses('Kategorie bearbeiten','edit_dropdown.tpl');
252  }
253  else if(isset($_GET['car_id']) && ctype_digit($_GET['car_id'])) {
254 
255  // extract the id and show details of the car
256  $carId = $_GET['car_id'];
257 
258  // ajax request to renew lock / unlock
259  if(isset($_GET['ajax']) && ctype_digit($_GET['ajax'])) {
260  if ($_GET['ajax'] == 1) {
261  if(isset($_GET['unlock']) && ctype_digit($_GET['unlock'])) {
262  if ($_GET['unlock'] == 1) {
263  unlockCar($carId);
264  exit;
265  }
266  }
267  else {
268  renewCarLock($carId);
269  exit;
270  }
271  }
272  }
273 
274  // lock tablerow
275  if (isCarLocked($carId)) {
276  $errors [] = 'Dieser Eintrag wird gerade von einem anderen Benutzer bearbeitet und ist gesperrt!';
277  exitWithErrorTemplateAndRedirect(null, 'dropdown_fields.php', 2);
278  }
279  // contiune here if not locked
280  lockCar($carId);
281 
282  $carDetails = fetchCarDetails($carId);
283 
284  // if the form was posted
285  if (!empty($_POST)) {
286 
287  // if the car should be deleted
288  if(isset($_POST['delete']) && ctype_digit($_POST['delete'])) {
289  if($_POST['delete'] == $carId){
290  if (deleteCar($carId)) {
291  $successes[] = "Das Fahrzeug wurde erfolgreich gelöscht.";
292  exitWithSuccessTemplateAndRedirect(NULL, 'dropdown_fields.php?show=3', 2);
293  }
294  else {
295  $errors [] = "Ein Datenbankfehler ist aufgetreten (eventuell wird der Fahrzeugname noch benutzt).";
296  }
297  }
298  }
299 
300  // if the car should be updated
301  $update = htmlSpecialChars(trim($_POST['update']), ENT_QUOTES);
302  if(!empty($update)) {
303  if(strcmp($carDetails['name'], $update) != 0) {
304  updateCar($update, $carDetails['id']);
305  unlockCar($carId);
306  $successes[] = "Der 'Name' wurde erfolgreich aktualisiert.";
307  exitWithSuccessTemplateAndRedirect(NULL, 'dropdown_fields.php?show=3', 2);
308  }
309  }
310  else {
311  $errors[] = "Geben Sie einen gültigen 'Namen' an.";
312  $carDetails['name'] = $update;
313  }
314  }
315 
316  $displayCar = TRUE;
317  $smarty->assign('lockDuration', EDIT_SECTION_LOCK_DURATION);
318  $smarty->assign('car', $carDetails);
319  $smarty->assign('displayCar',$displayCar);
320  displayTemplateWithErrorsAndSuccesses('Fahrzeug bearbeiten','edit_dropdown.tpl');
321  }
322  else {
323  if (!empty($_POST)) {
324 
325  // add a new production facility
326  if (isset($_POST["newProductionFacility"])) {
327  if(!empty($_POST["newProductionFacility"])) {
328  $newProductionFacility = htmlSpecialChars(trim($_POST['newProductionFacility']), ENT_QUOTES);
329  addProductionFacility($newProductionFacility);
330  $successes[] = "Fertigungsmöglichkeit wurde hinzugefügt.";
331  }
332  else {
333  $errors[] = 'Geben Sie einen gültigen Namen für die Fertigungsmöglichkeit an.';
334  }
335  }
336 
337  // add a new sponsor status
338  if (isset($_POST["newStatus"])) {
339  if(!empty($_POST["newStatus"])) {
340  $newStatus = htmlSpecialChars(trim($_POST['newStatus']), ENT_QUOTES);
341  addStatus($newStatus);
342  $successes[] = "Status wurde hinzugefügt.";
343  }
344  else {
345  $errors[] = 'Geben Sie einen gültigen Namen für den Status an.';
346  }
347  }
348 
349  // add a new category
350  if (isset($_POST["newCategory"])) {
351  if(!empty($_POST["newCategory"])) {
352  $newCategory = htmlSpecialChars(trim($_POST['newCategory']), ENT_QUOTES);
353  addCategory($newCategory);
354  $successes[] = "Fahrzeugkategorie wurde hinzugefügt.";
355  }
356  else {
357  $errors[] = 'Geben Sie einen gültigen Namen für die Fahrzeugkategorie an.';
358  }
359  }
360 
361  // add a new car
362  if (isset($_POST["newCar"])) {
363  if(!empty($_POST["newCar"])) {
364  $newCar = htmlSpecialChars(trim($_POST['newCar']), ENT_QUOTES);
365  addCar($newCar);
366  $successes[] = "Fahrzeug wurde hinzugefügt.";
367  }
368  else {
369  $errors[] = 'Geben Sie einen gültigen Namen für das Fahrzeug an.';
370  }
371  }
372 
373  }
374 
375  // show table production facilities
376  $productionFacilities = fetchAllProductionFacilities();
377  // show table status
378  $status = fetchAllStatus();
379  // show table category
380  $categories = fetchAllCategories();
381  // show table category
382  $cars = fetchAllCars();
383 
384  //sort
385  if(isset($_GET['dir0']) && ctype_digit($_GET['dir0'])) {
386  $direction = ($_GET['dir0'] == 0) ? SORT_ASC : SORT_DESC;
387  kdsort($productionFacilities, 1, $direction);
388  $sort[] = array('colIdx' => 1,
389  'direction' => $_GET['dir0']);
390  }
391  else {
392  $sort[] = array('colIdx' => 1,
393  'direction' => 0);
394  }
395 
396  if(isset($_GET['dir1']) && ctype_digit($_GET['dir1'])) {
397  $direction = ($_GET['dir1'] == 0) ? SORT_ASC : SORT_DESC;
398  kdsort($status, 1, $direction);
399  $sort[] = array('colIdx' => 1,
400  'direction' => $_GET['dir1']);
401 
402  }
403  else {
404  $sort[] = array('colIdx' => 1,
405  'direction' => 0);
406  }
407  if(isset($_GET['dir2']) && ctype_digit($_GET['dir2'])) {
408  $direction = ($_GET['dir2'] == 0) ? SORT_ASC : SORT_DESC;
409  kdsort($categories, 1, $direction);
410  $sort[] = array('colIdx' => 1,
411  'direction' => $_GET['dir2']);
412 
413  }
414  else {
415  $sort[] = array('colIdx' => 1,
416  'direction' => 0);
417  }
418 
419  if(isset($_GET['dir3']) && ctype_digit($_GET['dir3'])) {
420  $direction = ($_GET['dir3'] == 0) ? SORT_ASC : SORT_DESC;
421  kdsort($cars, 1, $direction);
422  $sort[] = array('colIdx' => 1,
423  'direction' => $_GET['dir3']);
424 
425  }
426  else {
427  $sort[] = array('colIdx' => 1,
428  'direction' => 0);
429  }
430 
431  //selected section
432  if(isset($_GET['show']) && ctype_digit($_GET['show'])) {
433  $section = $_GET['show'];
434  }
435  else {
436  //set default selected section
437  $section = 0;
438  }
439  $smarty->assign('section', $section);
440 
441  // display tempate
442  $smarty->assign('sort', $sort);
443  $smarty->assign('production_facilities', $productionFacilities);
444  $smarty->assign('status', $status);
445  $smarty->assign('categories', $categories);
446  $smarty->assign('cars', $cars);
447  displayTemplateWithErrorsAndSuccesses('Einstellungsfelder','dropdown_fields.tpl');
448  }
449  /// @endcond
450 ?>