Sponsorenverwaltung - Team StarCraft e.V.
 All Data Structures Files Functions Variables
sponsors.php
Go to the documentation of this file.
1 <?php
2  /**
3  * @file sponsors.php
4  *
5  * @brief Corefile for all sponsormanagement tasks.
6  *
7  * @details
8  * This file is the entry point to almost all sponsormanagement tasks. It
9  * provides the application logic for viewing, creating, editing and deleting
10  * sponsors. This file is very complex and should only be changed if you
11  * took some time to familiarise yourself with its logic and error-handling.
12  *
13  * Please consider having a look at the inc/templates/sponsor*.tpl-files for
14  * an ever deeper insight of the mechanisms working in here.
15  *
16  * This file depends on common.php.
17  *
18  * @copyright (c) 2013, Team StarCraft e.V.
19  * @version 1.0.0
20  * @author Daniel Seichter
21  * @author Alexander Vorndran (small parts)
22  */
23 
24  /// @cond MAINPART
25  // include
26  include("inc/common.php");
27 
28 
29  // Usercake
30  if(!accessGranted($_SERVER['PHP_SELF'])) {
31  if (isUserLoggedIn()) {
32  exitWithErrorTemplate(array('Die angeforderte Seite ist gesperrt.'));
33  }
34  else {
35  exitWithErrorTemplateAndRedirect(array('Die angeforderte Seite ist gesperrt oder geschützt.'), 'login.php', 2);
36  }
37  }
38 
39 
40  if(!empty($_GET['result'])) {
41  // this result identifier is the key to obtain the data the user has seen last
42  $resultIdentifier = $_GET['result'];
43  if($loggedInUser->isResultAvailable($resultIdentifier)) {
44  $smarty->assign('resultIdentifier',$resultIdentifier);
45  }
46  }
47 
48  // HANDLE NEW SPONSOR
49  if (isset($_GET['id']) && ($_GET['id'] == 'new')) {
50  if (isset($_GET['editsec']) && ctype_digit($_GET['editsec'])) {
51  //form was posted
52  if (!empty($_POST)) {
53  $sponsor = array();
54  // get and check name
55  $sponsor['name'] = htmlSpecialChars(trim($_POST['name']), ENT_QUOTES);
56  if (empty($sponsor['name'])) {
57  $errors[] = "Geben Sie einen gültigen 'Namen' an!";
58  }
59  // get and check street
60  $sponsor['street'] = htmlSpecialChars(trim($_POST['street']), ENT_QUOTES);
61  if (empty($sponsor['street'])) {
62  $errors[] = "Geben Sie eine gültige 'Straße' an!";
63  }
64  //get housenumber
65  $sponsor['houseNumber'] = htmlSpecialChars(trim($_POST['houseNumber']), ENT_QUOTES);
66  if (empty($sponsor['houseNumber'])) {
67  $errors[] = "Geben Sie eine gültige 'Haunummer' an!";
68  }
69  //get zipCode
70  $sponsor['zipCode'] = htmlSpecialChars(trim($_POST['zipCode']), ENT_QUOTES);
71  if (empty($sponsor['zipCode'])) {
72  $errors[] = "Geben Sie eine gültige 'PLZ' an!";
73  }
74  //get town
75  $sponsor['town'] = htmlSpecialChars(trim($_POST['town']), ENT_QUOTES);
76  if (empty($sponsor['town'])) {
77  $errors[] = "Geben Sie eine gültige 'Stadt' an!";
78  }
79  //get country
80  $sponsor['country'] = htmlSpecialChars(trim($_POST['country']), ENT_QUOTES);
81  if (empty($sponsor['country'])) {
82  $errors[] = "Geben Sie eine gültiges 'Land' an!";
83  }
84  //get statusId
85  $sponsor['statusId'] = $_POST['statusId'];
86  if ($sponsor['statusId'] == -1) {
87  $errors[] = "Geben Sie einen gültigen 'Status' an!";
88  }
89 
90 
91  //no errors occured
92  if (count($errors) == 0) {
93  $newSponsorId = addSponsor($sponsor);
94  if (!empty($newSponsorId)) {
95  //continue with edit
96  $_GET['id'] = (string)$newSponsorId;
97  $successes[] = "Der Sponsor wurde erfolgreich hinzugefügt";
98  }
99  else {
100  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
101  }
102  unset($sponsor);
103  }
104  else {
105  //back to new sponsor view
106  //enable edit mode for section 1
107  $smarty->assign('edit', array('section' => 1,
108  'lockDuration' => -1));
109  $smarty->assign('dropdownStatus', fetchAllStatus());
110  $smarty->assign('isNewSponsor', TRUE);
111  $smarty->assign('sponsor', $sponsor);
112  displayTemplateWithErrorsAndSuccesses('Neuen Sponsor hinzufügen','sponsor.tpl');
113  exit;
114  }
115 
116  }
117  }
118  else {
119  // handle new sponsor view
120  //enable edit mode for section 1
121  $smarty->assign('edit', array('section' => 1,
122  'lockDuration' => -1));
123 
124  // display template
125  $smarty->assign('dropdownStatus', fetchAllStatus());
126  $smarty->assign('isNewSponsor', TRUE);
127  $smarty->assign('sponsor',FALSE);
128  displayTemplateWithErrorsAndSuccesses('Neuen Sponsor hinzufügen','sponsor.tpl');
129  exit;
130  }
131  }
132 
133  //HANLDE SPONSOR EDIT
134  if(isset($_GET['id']) && ctype_digit($_GET['id'])) {
135  // AJAX REQUESTS
136  if (isset($_GET['ajax']) && ctype_digit($_GET['ajax'])) {
137  if ($_GET['ajax'] == 1) {
138  // renew section lock
139  if (isset($_GET['editsec']) && ctype_digit($_GET['editsec'])) {
140  renewSponsorEditSectionLock($_GET['editsec'], $_GET['id']);
141  exit;
142  }
143  // unlock section
144  if(isset($_GET['unlocksec']) && ctype_digit($_GET['unlocksec'])) {
145  unlockSponsorEditSection($_GET['unlocksec'],$_GET['id']);
146  exit;
147  }
148  }
149  }
150 
151  // send the user back to the search result if he came from there
152  // doesn't work if there is an edit
153  if(isset($_REQUEST['ref'])&&(strcmp(trim($_REQUEST['ref']), 'search')==0)) {
154  $backToSearch = TRUE;
155  $smarty->assign('backToSearch',TRUE);
156  } else {
157  $backToSearch = FALSE;
158  $smarty->assign('backToSearch',FALSE);
159  }
160 
161  // DELETE SPONSOR
162  if(!empty($_POST['deleteSponsor'][$_GET['id']]) && ctype_digit($_POST['deleteSponsor'][$_GET['id']])) {
163  $sponsorIdToDelete = $_POST['deleteSponsor'][$_GET['id']];
164  if ($sponsorIdToDelete == $_GET['id']) {
165 
166  $result = deleteSponsor($sponsorIdToDelete);
167  if($result === FALSE) {
168  $errors[] = "Es ist ein Datenbankfehler aufgetreten!";
169  } else if($result===-1){
170  $errors[] = "Löschen fehlgeschlagen, da mindestens ein Bereich momentan von einem anderen Benutzer bearbeitet wird.";
171  } else if($result===-2){
172  $errors[] = "Es ist ein Datenbankfehler aufgetreten!";
173  } else {
174  $url = "sponsors".($backToSearch ? '_search':'').".php?sort=1&dir=0".(isset($resultIdentifier)?'&result='.$resultIdentifier:'');
175  exitWithSuccessTemplateAndRedirect(array("Der Sponsor wurde erfolgreich gelöscht!"), $url, 2);
176  }
177  }
178  }
179 
180 
181  // FETCH SPONSOR DATA
182  // fetch sponsor base data
183  $sponsor = fetchSponsorDetails($_GET['id'], FALSE);
184  // fetch sponsor production facilities
185  $sponsor['productionFacilities'] = fetchSponsorProductionFacilities($sponsor['id']);
186  // fetch sponsor contact persons
187  $sponsor['contactPersons'] = fetchSponsorContactPersons($sponsor['id']);
188  // fetch sponsor cars
189  $sponsor['cars'] = fetchSponsorCarDetails($sponsor['id']);
190  // fetch data for each sponsor car
191  if ($sponsor['cars']) {
192  foreach ($sponsor['cars'] as &$car) {
193  // fetch car details
194  $car['events'] = fetchSponsorCarEvents($car['id']);
195  $car['financialDonations'] = fetchSponsorCarFinancialDonations($car['id']);
196  $car['materialDonations'] = fetchSponsorCarMaterialDonation($car['id']);
197  $car['producedParts'] = fetchSponsorCarProducedParts($car['id']);
198  $car['attachments'] = fetchSponsorCarAttachments($car['id']);
199  }
200  }
201 
202 
203  // EDIT SPONSOR DATA
204  // edit part (lock section & posts)
205  if(isset($_GET['editsec']) && ctype_digit($_GET['editsec'])) {
206  if (!isSponsorEditSectionLocked($_GET['editsec'], $sponsor['id'])) {
207  lockSponsorEditSection($_GET['editsec'],$sponsor['id']);
208 
209  // enable edit mode
210  $smarty->assign('edit', array('section' => $_GET['editsec'],
211  'lockDuration' => EDIT_SECTION_LOCK_DURATION));
212 
213  switch ($_GET['editsec']) {
214  case 1: {
215  // edit sponsor details
216  $smarty->assign('dropdownStatus', fetchAllStatus());
217 
218  // form was posted
219  if (!empty($_POST)) {
220  // get new name
221  $newSponsorName = htmlSpecialChars(trim($_POST['name']), ENT_QUOTES);
222  if (!empty($newSponsorName)) {
223  if(strcmp($sponsor['name'], $newSponsorName) != 0) {
224  $sponsor['name'] = $newSponsorName;
225  $changed[] = "Der 'Name' wurde erfolgreich aktualisiert.";
226  }
227  }
228  else {
229  $sponsor['name'] = $newSponsorName;
230  $errors[] = "Geben Sie einen gültigen 'Namen' an!";
231  }
232  // get new street
233  $newSponsorStreet = htmlSpecialChars(trim($_POST['street']), ENT_QUOTES);
234  if (!empty($newSponsorStreet)) {
235  if (strcmp($sponsor['street'], $newSponsorStreet) != 0) {
236  $sponsor['street'] = $newSponsorStreet;
237  $changed[] = "Die 'Straße' wurde erfolgreich aktualisiert.";
238  }
239  } else {
240  $sponsor['street'] = $newSponsorStreet;
241  $errors[] = "Geben Sie eine gültige 'Straße' an!";
242  }
243  // get new houseNumber
244  $newSponsorHouseNumber = htmlSpecialChars(trim($_POST['houseNumber']), ENT_QUOTES);
245  if (!empty($newSponsorHouseNumber)) {
246  if (strcmp($sponsor['houseNumber'], $newSponsorHouseNumber) != 0) {
247  $sponsor['houseNumber'] = $newSponsorHouseNumber;
248  $changed[] = "Die 'Hausnummer' wurde erfolgreich aktualisiert.";
249  }
250  }
251  else {
252  $sponsor['houseNumber'] = $newSponsorHouseNumber;
253  $errors[] = "Geben Sie eine gültige 'Hausnummer' an!";
254  }
255  // get new zipCode
256  $newSponsorZipCode = htmlSpecialChars(trim($_POST['zipCode']), ENT_QUOTES);
257  if (!empty($newSponsorZipCode)) {
258  if (strcmp($sponsor['zipCode'], $newSponsorZipCode) != 0) {
259  $sponsor['zipCode'] = $newSponsorZipCode;
260  $changed[] = "Die 'PLZ' wurde erfolgreich aktualisiert.";
261  }
262  }
263  else {
264  $sponsor['zipCode'] = $newSponsorZipCode;
265  $errors[] = "Geben Sie eine gültige 'PLZ' an!";
266  }
267  // get new town
268  $newSponsorTown = htmlSpecialChars(trim($_POST['town']), ENT_QUOTES);
269  if (!empty($newSponsorTown)) {
270  if (strcmp($sponsor['town'], $newSponsorTown) != 0) {
271  $sponsor['town'] = $newSponsorTown;
272  $changed[] = "Die 'Stadt' wurde erfolgreich aktualisiert.";
273  }
274  }
275  else {
276  $sponsor['town'] = $newSponsorTown;
277  $errors[] = "Geben Sie eine gültige 'Stadt' an!";
278  }
279  // get new country
280  $newSponsorCountry = htmlSpecialChars(trim($_POST['country']), ENT_QUOTES);
281  if (!empty($newSponsorCountry)) {
282  if (strcmp($sponsor['country'], $newSponsorCountry) != 0) {
283  $sponsor['country'] = $newSponsorCountry;
284  $changed[] = "Das 'Land' wurde erfolgreich aktualisiert.";
285  }
286  }
287  else {
288  $sponsor['country'] = $newSponsorCountry;
289  $errors[] = "Geben Sie ein gültiges 'Land' an!";
290  }
291  // get new website
292  $newSponsorWebsite = addSchemeToURL($_POST['website']);
293  if (strcmp($sponsor['website'], $newSponsorWebsite) != 0) {
294  $sponsor['website'] = $newSponsorWebsite;
295  $changed[] = "Die 'Webseite' wurde erfolgreich aktualisiert.";
296  }
297  // get new statusId
298  $newSponsorStatusId = $_POST['statusId'];
299  if ($newSponsorStatusId != -1) {
300  if (strcmp($sponsor['statusId'], $newSponsorStatusId) != 0) {
301  $sponsor['statusId'] = $newSponsorStatusId;
302  $changed[] = "Der 'Status' wurde erfolgreich aktualisiert.";
303  $statusDetails = fetchStatusDetails($sponsor['statusId']);
304  if (!empty($statusDetails)) {
305  $sponsor['statusName'] = $statusDetails['name'];
306  }
307  }
308  }
309  else {
310  $sponsor['statusId'] = $newSponsorStatusId;
311  $errors[] = "Wählen Sie einen gültigen 'Status' aus!";
312  }
313  // get new logo
314  if (!empty($_FILES['sponsorLogo']['name'])) {
315  // file was posted
316  if ($_FILES['sponsorLogo']['error'] == 0) {
317  if (($_FILES["sponsorLogo"]["type"] == "image/gif")
318  || ($_FILES["sponsorLogo"]["type"] == "image/jpeg")
319  || ($_FILES["sponsorLogo"]["type"] == "image/jpg")
320  || ($_FILES["sponsorLogo"]["type"] == "image/pjpeg")
321  || ($_FILES["sponsorLogo"]["type"] == "image/x-png")
322  || ($_FILES["sponsorLogo"]["type"] == "image/png")) {
323 
324  if (count($errors) == 0) {
325  // only change logo if no errors occured
326 
327  // delete old logo
328  if (file_exists(ABS_PATH.$sponsor['sponsorLogo']) && !empty($sponsor['sponsorLogo'])) {
329  //unlink original file
330  unlink(ABS_PATH.$sponsor['sponsorLogo']);
331  //unlink scaled images
332  $pathParts = pathinfo($sponsor['sponsorLogo']);
333  $files = glob(ABS_PATH.$pathParts['dirname'].'/_scaled/'.$pathParts['filename'].'*.'.$pathParts['extension']);
334  foreach($files as $file){
335  if (file_exists($file)) {
336  unlink($file);
337  }
338  }
339  }
340  $pathParts = pathinfo($_FILES['sponsorLogo']['name']);
341 
342  $newFilename = prepareFilename($pathParts['filename']).'_'.uniqid().'.'.$pathParts['extension'];
343 
344  if (move_uploaded_file($_FILES['sponsorLogo']['tmp_name'], ABS_PATH.'data/logos/'.$newFilename)) {
345  $sponsor['sponsorLogo'] = 'data/logos/'.$newFilename;
346  $changed[] = "Der 'Logo' wurde erfolgreich aktualisiert.";
347  }
348  else {
349  $errors[] = "Fehler beim internen Verarbeiten des 'Logos'.";
350  }
351  }
352 
353  }
354  else {
355  $errors[] = "Das aktuelle Bildformat wird nicht unterstützt! (Folgende Formate werden unterstützt: *.jpg, *.gif, *.png)";
356  }
357  }
358  else {
359  $errors[] = "Unbekannter Fehler beim Upload des 'Logos'.";
360  }
361  }
362  else {
363  if (isset($_POST['sponsorLogoDelete']) && ctype_digit($_POST['sponsorLogoDelete'])) {
364  if ($_POST['sponsorLogoDelete'] == 1) {
365  // delete old logo
366  if (file_exists(ABS_PATH.$sponsor['sponsorLogo']) && !empty($sponsor['sponsorLogo'])) {
367  //unlink original file
368  unlink(ABS_PATH.$sponsor['sponsorLogo']);
369  //unlink scaled images
370  $pathParts = pathinfo($sponsor['sponsorLogo']);
371  $files = glob(ABS_PATH.$pathParts['dirname'].'/_scaled/'.$pathParts['filename'].'*.'.$pathParts['extension']);
372  foreach($files as $file){
373  if (file_exists($file)) {
374  unlink($file);
375  }
376  }
377  }
378  $sponsor['sponsorLogo'] = '';
379  $changed[] = "Das 'Logo' wurde erfolgreich gelöscht.";
380  }
381  }
382  }
383 
384  // get new last contact timestamp
385  $newSponsorLastContactDateTimestamp = strToTime(trim($_POST['lastContactDateTimestamp']));
386  if ($newSponsorLastContactDateTimestamp != FALSE) {
387  if (strcmp($sponsor['lastContactDateTimestamp'], $newSponsorLastContactDateTimestamp) != 0) {
388  $sponsor['lastContactDateTimestamp'] = $newSponsorLastContactDateTimestamp;
389  $changed[] = "Das 'Datum' des letzten Kontaktes wurde erfolgreich aktualisiert.";
390  }
391  }
392  else {
393  if (!empty($_POST['lastContactDateTimestamp'])) {
394  $errors[] = "Geben Sie ein gültiges 'Datum' des letzten Kontaktes im Format: 'dd.mm.yyyy hh:mm' an!";
395  }
396  }
397 
398  // get new last contact person
399  $newSponsorLastContactPerson = htmlSpecialChars(trim($_POST['lastContactPerson']), ENT_QUOTES);
400  if (strcmp($sponsor['lastContactPerson'], $newSponsorLastContactPerson) != 0) {
401  $sponsor['lastContactPerson'] = $newSponsorLastContactPerson;
402  $changed[] = "Die 'Person' des letzten Kontaktes wurde erfolgreich aktualisiert.";
403  }
404  // get new hints
405  $newSponsorHints = htmlSpecialChars(trim($_POST['hints']), ENT_QUOTES);
406  if (strcmp($sponsor['hints'], $newSponsorHints) != 0) {
407  $sponsor['hints'] = $newSponsorHints;
408  $changed[] = "Die 'Hinweise' wurden erfolgreich aktualisiert.";
409  }
410 
411 
412  // apply changes (no errors occured)
413  // update database set
414  if (count($errors) == 0) {
415  if (!empty($changed)) {
416  if (updateSponsorDetails($sponsor)) {
417  // display successes
418  $successes = array_merge($successes, $changed);
419 
420  }
421  else {
422  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
423  }
424  }
425  }
426  // unlock section and disable edit mode
427  if (count($errors) == 0) {
428  // unlock
429  unlockSponsorEditSection($_GET['editsec'],$_GET['id']);
430  // disable edit mode
431  $smarty->assign('edit', NULL);
432  }
433  }
434  break;
435  }
436 
437  case 2: {
438  // edit contact person details
439  // form was posted
440  if (!empty($_POST)) {
441  if(isset($_POST['cnt']) && ctype_digit($_POST['cnt'])) {
442 
443  // get number of contact persons to edit
444  $curCnt = ($sponsor['contactPersons'] === FALSE) ? 0 : count($sponsor['contactPersons']);
445  if ($_POST['cnt'] == $curCnt) {
446  for ($i = 1; $i <= $curCnt; $i++) {
447  if (!empty($_POST['id_'.$i])) {
448  $id = $_POST['id_'.$i];
449 
450  // check delete
451  if(isset($_POST['delete_'.$i]) && ctype_digit($_POST['delete_'.$i])) {
452  if($_POST['delete_'.$i] == $id) {
453  $sponsorContactPersonsIdsToDelete[] = $id;
454  continue;
455  }
456  }
457 
458  // update
459  // get new contact person name
460  $newContactPersonName = htmlSpecialChars(trim($_POST['name_'.$i]), ENT_QUOTES);
461  if (!empty($newContactPersonName)) {
462  if (strcmp($sponsor['contactPersons'][$i-1]['name'], $newContactPersonName) != 0) {
463  $sponsor['contactPersons'][$i-1]['name'] = $newContactPersonName;
464  $changed[] = "Ansprechpartner #".$i.": Der 'Name' wurde erfolgreich aktualisiert.";
465  }
466  }
467  else {
468  $sponsor['contactPersons'][$i-1]['name'] = $newContactPersonName;
469  $errors[] = "Ansprechpartner #".$i.": Geben Sie einen gültigen 'Namen' an!";
470  }
471  // get new contact person email
472  $newContactPersonEmail = trim($_POST['email_'.$i]);
473  if (isValidEmail($newContactPersonEmail)) {
474  if (strcmp($sponsor['contactPersons'][$i-1]['email'], $newContactPersonEmail) != 0) {
475  $sponsor['contactPersons'][$i-1]['email'] = $newContactPersonEmail;
476  $changed[] = "Ansprechpartner #".$i.": Die 'E-Mail-Adresse' wurde erfolgreich aktualisiert.";
477  }
478  }
479  else {
480  $sponsor['contactPersons'][$i-1]['email'] = $newContactPersonEmail;
481  $errors[] = "Ansprechpartner #".$i.": Geben Sie eine gültige 'E-Mail-Adresse' an!";
482  }
483  // get new contact person phone
484  $newContactPersonPhone = trim($_POST['phone_'.$i]);
485  if (isValidPhone($newContactPersonPhone)) {
486  if (strcmp($sponsor['contactPersons'][$i-1]['phone'], $newContactPersonPhone) != 0) {
487  $sponsor['contactPersons'][$i-1]['phone'] = $newContactPersonPhone;
488  $changed[] = "Ansprechpartner #".$i.": Die 'Telefonnummer' wurde erfolgreich aktualisiert.";
489  }
490  }
491  else {
492  $sponsor['contactPersons'][$i-1]['phone'] = $newContactPersonPhone;
493  $errors[] = "Ansprechpartner #".$i.": Geben Sie eine gültige 'Telefonnummer' an!";
494  }
495  // get new contact person mobile (opt)
496  $newContactPersonMobile = trim($_POST['mobile_'.$i]);
497  if (!empty($newContactPersonMobile)) {
498  if (isValidMobile($newContactPersonMobile)) {
499  if (strcmp($sponsor['contactPersons'][$i-1]['mobile'], $newContactPersonMobile) != 0) {
500  $sponsor['contactPersons'][$i-1]['mobile'] = $newContactPersonMobile;
501  $changed[] = "Ansprechpartner #".$i.": Die 'Handynummer' wurde erfolgreich aktualisiert.";
502  }
503  }
504  else {
505  $sponsor['contactPersons'][$i-1]['mobile'] = $newContactPersonMobile;
506  $errors[] = "Ansprechpartner #".$i.": Geben Sie eine gültige 'Handynummer' an!";
507  }
508  }
509  // get new contact person position (opt)
510  $newContactPersonPosition = htmlSpecialChars(trim($_POST['position_'.$i]), ENT_QUOTES);
511  if (!empty($newContactPersonPosition)) {
512  if (strcmp($sponsor['contactPersons'][$i-1]['position'], $newContactPersonPosition) != 0) {
513  $sponsor['contactPersons'][$i-1]['position'] = $newContactPersonPosition;
514  $changed[] = "Ansprechpartner #".$i.": Die 'Position' wurde erfolgreich aktualisiert.";
515  }
516 
517  }
518  // get new contact person newsletter (opt)
519  $newContactPersonNewsletter = $_POST['newsletter_'.$i];
520  if ($sponsor['contactPersons'][$i-1]['newsletter'] != $newContactPersonNewsletter) {
521  $sponsor['contactPersons'][$i-1]['newsletter'] = $newContactPersonNewsletter;
522  $changed[] = "Ansprechpartner #".$i.": Die 'Newsletter'-Option wurde erfolgreich aktualisiert.";
523  }
524 
525 
526  }
527  else {
528  $errors[] = "Ein unbekannter Fehler ist aufgetreten!";
529  break;
530  }
531  }
532  }
533  else {
534  $errors[] = "Ein unbekannter Fehler ist aufgetreten!__";
535  }
536  }
537  else {
538  $errors[] = "Ein unbekannter Fehler ist aufgetreten!";
539  }
540 
541 
542  if(isset($_POST['newContactPerson']) && ctype_digit($_POST['newContactPerson'])) {
543  if ($_POST['newContactPerson'] == 1) {
544  // set sponsor id
545  $newContactPerson['sponsor_id'] = $sponsor['id'];
546  // get contact person name
547  $newContactPerson['name'] = htmlSpecialChars(trim($_POST['name_new']), ENT_QUOTES);
548  if (empty($newContactPerson['name'])) {
549  $errors [] = "neuer Ansprechpartner: Geben Sie einen gültigen 'Namen' an!";
550  }
551  // get contact person email
552  $newContactPerson['email'] = trim($_POST['email_new']);
553  if (!isValidEmail($newContactPerson['email'])) {
554  $errors[] = "neuer Ansprechpartner: Geben Sie eine gültige 'E-Mail-Adresse' an!";
555  }
556  // get contact person phone
557  $newContactPerson['phone'] = trim($_POST['phone_new']);
558  if (!isValidPhone($newContactPerson['phone'])) {
559  $errors[] = "neuer Ansprechpartner: Geben Sie eine gültige 'Telefonnummer' an!";
560  }
561  // get contact person mobile (opt)
562  $newContactPerson['mobile'] = trim($_POST['mobile_new']);
563  if (!empty($newContactPerson['mobile'])) {
564  if (!isValidMobile($newContactPerson['mobile'])) {
565  $errors[] = "neuer Ansprechpartner: Geben Sie eine gültige 'Handynummer' an!";
566  }
567  }
568  // get contact person position (opt)
569  $newContactPerson['position'] = htmlSpecialChars(trim($_POST['position_new']), ENT_QUOTES);
570 
571  // get contact person newsletter (opt)
572  $newContactPerson['newsletter'] = $_POST['newsletter_new'];
573 
574  if (count($errors) != 0) {
575  $smarty->assign('newContactPerson', $newContactPerson);
576  }
577  }
578  }
579 
580 
581  // apply changes (no errors occured)
582  // delete database set
583  if (count($errors) == 0) {
584  if (!empty($sponsorContactPersonsIdsToDelete)) {
585  $newContactPersonsArray = array();
586  for ($i = 0; $i < count($sponsor['contactPersons']); $i++) {
587  if (in_array($sponsor['contactPersons'][$i]['id'], $sponsorContactPersonsIdsToDelete)) {
588  if (deleteSponsorContactPerson($sponsor['contactPersons'][$i]['id'])) {
589  $successes [] = "Ansprechpartner #".($i+1)." wurde erfolgreich gelöscht.";
590  }
591  else {
592  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
593  }
594  }
595  else {
596  $newContactPersonsArray[] = $sponsor['contactPersons'][$i];
597  }
598  }
599  $sponsor['contactPersons'] = $newContactPersonsArray;
600  }
601 
602 
603  // add database set
604  if (isset($newContactPerson)) {
605  if (addSponsorContactPerson($newContactPerson)) {
606  if (is_array($sponsor['contactPersons'])) {
607  //add entry
608  array_push($sponsor['contactPersons'], $newContactPerson);
609  }
610  else {
611  //no entries before
612  $sponsor['contactPersons'] = array($newContactPerson);
613  }
614  $successes[] = "Ansprechpartner erfolgreich hinzugefügt";
615  }
616  else {
617  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
618  }
619  }
620 
621 
622  // update database set
623  if (!empty($changed)) {
624  if(updateSponsorContactPersons($sponsor['contactPersons'])) {
625  // display successes
626  $successes = array_merge($successes, $changed);
627  }
628  else {
629  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
630  }
631  }
632  }
633 
634  // unlock section and disable edit mode
635  if (count($errors) == 0) {
636  // unlock
637  unlockSponsorEditSection($_GET['editsec'],$_GET['id']);
638  // disable edit mode
639  $smarty->assign('edit', NULL);
640  }
641 
642  }
643  break;
644  }
645 
646  case 3: {
647  // edit production facilities
648  $smarty->assign('productionFacilities', fetchAllProductionFacilities());
649 
650  // form was posted
651  if (!empty($_POST)) {
652  if(isset($_POST['cnt']) && ctype_digit($_POST['cnt'])) {
653 
654  $curCnt = ($sponsor['productionFacilities'] === FALSE) ? 0 : count($sponsor['productionFacilities']);
655  // get number of production facilities to edit
656  if ($_POST['cnt'] == $curCnt) {
657 
658  for ($i = 1; $i <= $curCnt; $i++) {
659  if (!empty($_POST['id_'.$i])) {
660  $id = $_POST['id_'.$i];
661 
662  // check delete
663  if(isset($_POST['delete_'.$i]) && ctype_digit($_POST['delete_'.$i])) {
664  if($_POST['delete_'.$i] == $id) {
665  $sponsorProductionFacilityIdsToDelete[] = $id;
666  continue;
667  }
668  }
669 
670  // get new production facility id
671  $newProductionFacilityId = $_POST['productionFacilityId_'.$i];
672  if ($newProductionFacilityId != -1) {
673  if (strcmp($sponsor['productionFacilities'][$i-1]['productionFacilityId'], $newProductionFacilityId) != 0) {
674  $sponsor['productionFacilities'][$i-1]['productionFacilityId'] = $newProductionFacilityId;
675  $productionFacilityDetails = fetchProductionFacilityDetails($newProductionFacilityId);
676  $sponsor['productionFacilities'][$i-1]['name'] = $productionFacilityDetails['output'];
677  $changed[] = "Fertigungsmöglichkeit #".$i.": Der 'Name' wurde erfolgreich aktualisiert.";
678  }
679  }
680  else {
681  $errors[] = "Fertigungsmöglichkeit #".$i.": Wählen Sie einen gültigen 'Namen' aus!";
682  $sponsor['productionFacilities'][$i-1]['productionFacilityId'] = $newProductionFacilityId;
683  }
684  // get new production facility comments
685  $newProductionFacilityComments = htmlSpecialChars(trim($_POST['comments_'.$i]), ENT_QUOTES);
686  if (strcmp($sponsor['productionFacilities'][$i-1]['comments'], $newProductionFacilityComments) != 0) {
687  $sponsor['productionFacilities'][$i-1]['comments'] = $newProductionFacilityComments;
688  $changed[] = "Fertigungsmöglichkeit #".$i.": Die 'Bemerkung' wurde erfolgreich aktualisiert.";
689  }
690  }
691  else {
692  $errors[] = "Ein unbekannter Fehler ist aufgetreten!";
693  break;
694  }
695  }
696  }
697  else {
698  $errors[] = "Ein unbekannter Fehler ist aufgetreten!";
699  }
700  }
701  else {
702  $errors[] = "Ein unbekannter Fehler ist aufgetreten!";
703  }
704 
705  // apply changes
706  // new production facility
707  if(isset($_POST['newProductionFacility']) && ctype_digit($_POST['newProductionFacility'])) {
708  if ($_POST['newProductionFacility'] == 1) {
709  // comments
710  $newSponsorProductionFacility['comments'] = htmlSpecialChars(trim($_POST['comments_new']), ENT_QUOTES);
711  // set sponsor id
712  $newSponsorProductionFacility['sponsor_id'] = $sponsor['id'];
713  // get production facility id
714  $newSponsorProductionFacility['productionFacilityId'] = $_POST['productionFacilityId_new'];
715  if ($newSponsorProductionFacility['productionFacilityId'] == -1) {
716  $smarty->assign('newSponsorProductionFacility', $newSponsorProductionFacility);
717  $errors[] = "neue Fertigungsmöglichkeit: Wählen Sie einen gültigen 'Namen' aus!";
718  }
719  }
720  }
721 
722 
723  // apply changes (no errors occured)
724  // delete database set
725  if (count($errors) == 0) {
726  $newProductionFacilitiesArray = array();
727  if (!empty($sponsorProductionFacilityIdsToDelete)) {
728  for ($i = 0; $i < count($sponsor['productionFacilities']); $i++) {
729  if (in_array($sponsor['productionFacilities'][$i]['id'], $sponsorProductionFacilityIdsToDelete)) {
730  if (deleteSponsorProductionFacility($sponsor['productionFacilities'][$i]['id'])) {
731  $successes [] = "Fertigungsmöglichkeit #".($i+1)." wurde erfolgreich gelöscht.";
732  } else {
733  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
734  }
735  }
736  else {
737  $newProductionFacilitiesArray[] = $sponsor['productionFacilities'][$i];
738  }
739 
740  }
741  $sponsor['productionFacilities'] = $newProductionFacilitiesArray;
742  }
743 
744  // add database set
745  if (isset($newSponsorProductionFacility)) {
746  if (addSponsorProductionFacility($newSponsorProductionFacility)) {
747  $productionFacilityDetails = fetchProductionFacilityDetails($newSponsorProductionFacility['productionFacilityId']);
748  $newSponsorProductionFacility['name'] = $productionFacilityDetails['name'];
749 
750  if (is_array($sponsor['productionFacilities'])) {
751  //add entry
752  array_push($sponsor['productionFacilities'], $newSponsorProductionFacility);
753  }
754  else {
755  //no entries before
756  $sponsor['productionFacilities'] = array($newSponsorProductionFacility);
757  }
758  $successes[] = "Fertigungsmöglichkeit erfolgreich hinzugefügt";
759  }
760  else {
761  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
762  }
763  }
764 
765 
766  // update database set
767  if (!empty($changed)) {
768  if(updateSponsorProductionFacilities($sponsor['productionFacilities'])) {
769  // display successes
770  $successes = array_merge($successes, $changed);
771  }
772  else {
773  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
774  }
775  }
776  }
777 
778  // unlock section and disable edit mode
779  if (count($errors) == 0) {
780  // unlock
781  unlockSponsorEditSection($_GET['editsec'],$_GET['id']);
782  // disable edit mode
783  $smarty->assign('edit', NULL);
784  }
785 
786  }
787  break;
788  }
789  case 4: {
790  // edit sponsor cars
791  $smarty->assign('dropdownCategories', fetchAllCategories());
792 
793  //get possible new cars
794  $smarty->assign('possibleNewCars', fetchPossibleNewSponsorCars($sponsor['id']));
795 
796 
797  // form was posted
798  if (!empty($_POST)) {
799  // delete
800  if(!empty($_POST['deleteCar']) && !empty($_POST['deleteCar'][$_GET['carId']]) && ctype_digit($_POST['deleteCar'][$_GET['carId']])) {
801  $sponsorCarIdToDelete = $_POST['deleteCar'][$_GET['carId']];
802  if ($sponsorCarIdToDelete == $_GET['carId']) {
803 
804  if (deleteSponsorCar($sponsorCarIdToDelete)) {
805  $successes[] = 'Das Sponsorenfahrzeug wurde erfolgreich gelöscht!';
806  //delete car in array
807  foreach ($sponsor['cars'] as $key => $theCar){
808  if ($theCar['id'] == $sponsorCarIdToDelete) {
809  unset($sponsor['cars'][$key]);
810  }
811  }
812  //unset clicked car
813  unset($_GET['carId']);
814 
815  //renew possible cars
816  $smarty->assign('possibleNewCars', fetchPossibleNewSponsorCars($sponsor['id']));
817  }
818  else {
819  $errors[] = "Es ist ein Datenbankfehler aufgetreten!";
820  }
821  break;
822  }
823  }
824 
825  //edit
826  if(!empty($_POST['cars'])) {
827  if (is_array($_POST['cars'])) {
828  // handle each car
829  foreach ($_POST['cars'] as $carKey => $curPostedCar) {
830  if (is_numeric($carKey)) {
831  // car is already in db
832 
833  // get current car in $sponsors['cars']
834  $carIdx = $carKey - 1;
835  $curCar = &$sponsor['cars'][$carIdx];
836 
837  // get new categorieId
838  $newCategorieId = $curPostedCar['categoryId'];
839  if ($newCategorieId != -1) {
840  if (strcmp($curCar['categoryId'], $newCategorieId) != 0) {
841  $curCar['categoryId'] = $newCategorieId;
842  $changedCarDetails[$carIdx][] = "Fahrzeug '".$curCar['name']."': Die 'Kategorie' wurde erfolgreich aktualisiert.";
843  $categoryDetails = fetchCategoryDetails($curCar['categoryId']);
844  if (!empty($categoryDetails)) {
845  $curCar['categoryName'] = $categoryDetails['name'];
846  }
847  }
848  }
849  else {
850  $curCar['categoryId'] = $newCategorieId;
851  $errors[] = "Fahrzeug '".$curCar['name']."': Wählen Sie eine gültige 'Kategorie' aus!";
852  }
853 
854  // get new codetermination
855  $newCodetermination = $curPostedCar['codetermination'];
856  if ($newCodetermination != -1) {
857  if (strcmp($curCar['codetermination'], $newCodetermination) != 0) {
858  $curCar['codetermination'] = $newCodetermination;
859  $changedCarDetails[$carIdx][] = "Fahrzeug '".$curCar['name']."': Das 'Mitbestimmungsrecht' wurde erfolgreich aktualisiert.";
860  }
861  }
862  else {
863  $curCar['codetermination'] = $newCodetermination;
864  $errors[] = "Fahrzeug '".$curCar['name']."': Wählen Sie ein gültiges 'Mitbestimmungsrecht' aus!";
865  }
866 
867  // get new hints
868  $newHints = htmlSpecialChars(trim($curPostedCar['hints']), ENT_QUOTES);
869  if (strcmp($curCar['hints'], $newHints) != 0) {
870  $curCar['hints'] = $newHints;
871  $changedCarDetails[$carIdx][] = "Fahrzeug '".$curCar['name']."': Die 'Hinweise' wurden erfolgreich aktualisiert.";
872  }
873 
874 
875  // get new events data
876  foreach ($curPostedCar['events'] as $eventKey => $curPostedEvent) {
877  if (is_numeric($eventKey)) {
878  // event is already in db
879 
880  // get current event in $sponsors['cars']['events']
881  $eventIdx = $eventKey - 1;
882  $curEvent = &$curCar['events'][$eventIdx];
883 
884  // check delete
885  if(isset($curPostedEvent['delete']) && ctype_digit($curPostedEvent['delete'])) {
886  if ($curPostedEvent['delete'] == $curEvent['id']) {
887  $eventsToDelete[$carIdx][] = $curEvent['id'];
888  continue;
889  }
890  }
891 
892  //get new timestamp
893  $newTimestamp = strToTime(trim($curPostedEvent['timestamp']));
894  if ($newTimestamp != FALSE) {
895  if (strcmp($curEvent['timestamp'], $newTimestamp) != 0) {
896  $curEvent['timestamp'] = $newTimestamp;
897  $changedEvents[$carIdx][] = "Fahrzeug '".$curCar['name']."': Event: #".$eventKey." Das 'Datum' wurde erfolgreich aktualisiert.";
898  }
899  }
900  else {
901  //clear field -> show placeholder in template
902  $curEvent['timestamp'] = '';
903  $errors[] = "Fahrzeug '".$curCar['name']."': Event: #".$eventKey." Geben Sie ein gültiges 'Datum' im Format: 'dd.mm.yyyy' an!";
904  }
905 
906  //get new event (name)
907  $newEventName = htmlSpecialChars(trim($curPostedEvent['event']), ENT_QUOTES);
908  if (!empty($newEventName)) {
909  if (strcmp($curEvent['event'], $newEventName) != 0) {
910  $curEvent['event'] = $newEventName;
911  $changedEvents[$carIdx][] = "Fahrzeug '".$curCar['name']."': Event: #".$eventKey." Die 'Veranstaltung' wurde erfolgreich aktualisiert.";
912  }
913  }
914  else {
915  $curEvent['event'] = $newEventName;
916  $errors[] = "Fahrzeug '".$curCar['name']."': Event: #".$eventKey." Geben Sie eine gültige 'Veranstaltung' an!";
917  }
918  }
919  else {
920  if ((!empty($curPostedEvent['isNew'])) && ($curPostedEvent['isNew'] == 1)) {
921  // event is a new event
922 
923  // get timestamp
924  $curCar['newEvent']['timestamp'] = strToTime(trim($curPostedEvent['timestamp']));
925  if (empty($curCar['newEvent']['timestamp'])) {
926  //clear field -> show placeholder in template
927  $curCar['newEvent']['timestamp'] = '';
928  $errors[] = "Fahrzeug '".$curCar['name']."': neues Event: Geben Sie ein gültiges 'Datum' im Format: 'dd.mm.yyyy' an!";
929  }
930 
931  // get event (name)
932  $curCar['newEvent']['event'] = htmlSpecialChars(trim($curPostedEvent['event']), ENT_QUOTES);
933  if (empty($curCar['newEvent']['event'])) {
934  $errors[] = "Fahrzeug '".$curCar['name']."': neues Event: Geben Sie eine gültige 'Veranstaltung' an!";
935  }
936  $curCar['newEvent']['sponsorCarId'] = $curCar['id'];
937  }
938  }
939  }
940 
941  // get new financial donations data
942  foreach ($curPostedCar['financialDonations'] as $financialDonationKey => $curPostedFinancialDonation) {
943  if (is_numeric($financialDonationKey)) {
944  // financial donation is already in db
945 
946  // get current financial donation in $sponsors['cars']['financialDonations']
947  $financialDonationIdx = $financialDonationKey - 1;
948  $curFinancialDonation = &$curCar['financialDonations'][$financialDonationIdx];
949 
950  // check delete
951  if(isset($curPostedFinancialDonation['delete']) && ctype_digit($curPostedFinancialDonation['delete'])) {
952  if ($curPostedFinancialDonation['delete'] == $curFinancialDonation['id']) {
953  $financialDonationsToDelete[$carIdx][] = $curFinancialDonation['id'];
954  continue;
955  }
956  }
957 
958  //get new timestamp
959  $newTimestamp = strToTime(trim($curPostedFinancialDonation['timestamp']));
960  if ($newTimestamp != FALSE) {
961  if (strcmp($curFinancialDonation['timestamp'], $newTimestamp) != 0) {
962  $curFinancialDonation['timestamp'] = $newTimestamp;
963  $changedFinancialDonations[$carIdx][] = "Fahrzeug '".$curCar['name']."': Finanzspende: #".$financialDonationKey." Das 'Datum' wurde erfolgreich aktualisiert.";
964  }
965  }
966  else {
967  //clear field -> show placeholder in template
968  $curFinancialDonation['timestamp'] = '';
969  $errors[] = "Fahrzeug '".$curCar['name']."': Finanzspende: #".$financialDonationKey." Geben Sie ein gültiges 'Datum' im Format: 'dd.mm.yyyy' an!";
970  }
971 
972  //get new amount
973  $newAmount = str_replace(array(' ', 'EUR', '€',','), array('','','','.'), trim($curPostedFinancialDonation['amount']));
974  if (is_numeric($newAmount)) {
975  if (floatval($curFinancialDonation['amount']) != floatval($newAmount)) {
976  $curFinancialDonation['amount'] = floatval($newAmount);
977  $changedFinancialDonations[$carIdx][] = "Fahrzeug '".$curCar['name']."': Finanzspende: #".$financialDonationKey." Der 'Betrag' wurde erfolgreich aktualisiert.";
978  }
979  }
980  else {
981  $curFinancialDonation['amount'] = $newAmount;
982  $errors[] = "Fahrzeug '".$curCar['name']."': Finanzspende: #".$financialDonationKey." Geben Sie einen gültigen 'Betrag' an!";
983  }
984  }
985  else {
986  if ((!empty($curPostedFinancialDonation['isNew'])) && ($curPostedFinancialDonation['isNew'] == 1)) {
987  // financialDonation is a new financial donations
988 
989  // get timestamp
990  $curCar['newFinancialDonation']['timestamp'] = strToTime(trim($curPostedFinancialDonation['timestamp']));
991  if (empty($curCar['newFinancialDonation']['timestamp'])) {
992  //clear field -> show placeholder in template
993  $curCar['newFinancialDonation']['timestamp'] = '';
994  $errors[] = "Fahrzeug '".$curCar['name']."': neue Finanzspende: Geben Sie ein gültiges 'Datum' im Format: 'dd.mm.yyyy' an!";
995  }
996 
997  //get amount
998  $amount = str_replace(array(' ', 'EUR', '€',','), array('','','','.'), trim($curPostedFinancialDonation['amount']));
999  if (!is_numeric($amount)) {
1000  $errors[] = "Fahrzeug '".$curCar['name']."': neue Finanzspende: Geben Sie einen gültigen 'Betrag' an!";
1001  $curCar['newFinancialDonation']['amount'] = $amount;
1002  }
1003  else {
1004  $curCar['newFinancialDonation']['amount'] = floatval($amount);
1005  }
1006  $curCar['newFinancialDonation']['sponsorCarId'] = $curCar['id'];
1007  }
1008  }
1009  }
1010 
1011  // get new material donations data
1012  foreach ($curPostedCar['materialDonations'] as $materialDonationKey => $curPostedMaterialDonation) {
1013  if (is_numeric($materialDonationKey)) {
1014  // material donation is already in db
1015 
1016  // get current material donation in $sponsors['cars']['materialDonations']
1017  $materialDonationIdx = $materialDonationKey - 1;
1018  $curMaterialDonation = &$curCar['materialDonations'][$materialDonationIdx];
1019 
1020  // check delete
1021  if(isset($curPostedMaterialDonation['delete']) && ctype_digit($curPostedMaterialDonation['delete'])) {
1022  if ($curPostedMaterialDonation['delete'] == $curMaterialDonation['id']) {
1023  $materialDonationsToDelete[$carIdx][] = $curMaterialDonation['id'];
1024  continue;
1025  }
1026  }
1027 
1028  //get new timestamp
1029  $newTimestamp = strToTime(trim($curPostedMaterialDonation['timestamp']));
1030  if ($newTimestamp != FALSE) {
1031  if (strcmp($curMaterialDonation['timestamp'], $newTimestamp) != 0) {
1032  $curMaterialDonation['timestamp'] = $newTimestamp;
1033  $changedMaterialDonations[$carIdx][] = "Fahrzeug '".$curCar['name']."': Sachspende: #".$materialDonationKey." Das 'Datum' wurde erfolgreich aktualisiert.";
1034  }
1035  }
1036  else {
1037  //clear field -> show placeholder in template
1038  $curMaterialDonation['timestamp'] = '';
1039  $errors[] = "Fahrzeug '".$curCar['name']."': Sachspende: #".$materialDonationKey." Geben Sie ein gültiges 'Datum' im Format: 'dd.mm.yyyy' an!";
1040  }
1041 
1042  //get new object
1043  $newObject = htmlSpecialChars(trim($curPostedMaterialDonation['object']), ENT_QUOTES);
1044  if (!empty($newObject)) {
1045  if (strcmp($curMaterialDonation['object'], $newObject) != 0) {
1046  $curMaterialDonation['object'] = $newObject;
1047  $changedMaterialDonations[$carIdx][] = "Fahrzeug '".$curCar['name']."': Sachspende: #".$materialDonationKey." Der 'Gegenstand' wurde erfolgreich aktualisiert.";
1048  }
1049  }
1050  else {
1051  $curMaterialDonation['object'] = $newObject;
1052  $errors[] = "Fahrzeug '".$curCar['name']."': Sachspende: #".$materialDonationKey." Geben Sie einen gültigen 'Gegenstand' an!";
1053  }
1054 
1055  //get new amount
1056  $newAmount = str_replace(array(' ', 'EUR', '€',','), array('','','','.'), trim($curPostedMaterialDonation['amount']));
1057  if (is_numeric($newAmount)) {
1058  if (floatval($curMaterialDonation['amount']) != floatval($newAmount)) {
1059  $curMaterialDonation['amount'] = floatval($newAmount);
1060  $changedMaterialDonations[$carIdx][] = "Fahrzeug '".$curCar['name']."': Sachspende: #".$materialDonationKey." Der 'Wert' wurde erfolgreich aktualisiert.";
1061  }
1062  }
1063  else {
1064  $curMaterialDonation['amount'] = $newAmount;
1065  $errors[] = "Fahrzeug '".$curCar['name']."': Sachspende: #".$materialDonationKey." Geben Sie einen gültigen 'Wert' an!";
1066  }
1067  }
1068  else {
1069  if ((!empty($curPostedMaterialDonation['isNew'])) && ($curPostedMaterialDonation['isNew'] == 1)) {
1070  // material donation is a new financial donations
1071 
1072  // get timestamp
1073  $curCar['newMaterialDonation']['timestamp'] = strToTime(trim($curPostedMaterialDonation['timestamp']));
1074  if (empty($curCar['newMaterialDonation']['timestamp'])) {
1075  //clear field -> show placeholder in template
1076  $curCar['newMaterialDonation']['timestamp'] = '';
1077  $errors[] = "Fahrzeug '".$curCar['name']."': neue Sachspende: Geben Sie ein gültiges 'Datum' im Format: 'dd.mm.yyyy' an!";
1078  }
1079  // get object
1080  $curCar['newMaterialDonation']['object'] = htmlSpecialChars(trim($curPostedMaterialDonation['object']), ENT_QUOTES);
1081  if (empty($curCar['newMaterialDonation']['object'])) {
1082  $errors[] = "Fahrzeug '".$curCar['name']."': neues Sachspende: Geben Sie einen gültigen 'Gegenstand' an!";
1083  }
1084  // get amount
1085  $amount = str_replace(array(' ', 'EUR', '€',','), array('','','','.'), trim($curPostedMaterialDonation['amount']));
1086  if (!is_numeric($amount)) {
1087  $errors[] = "Fahrzeug '".$curCar['name']."': neue Sachspende: Geben Sie einen gültigen 'Wert' an!";
1088  $curCar['newMaterialDonation']['amount'] = $amount;
1089  }
1090  else {
1091  $curCar['newMaterialDonation']['amount'] = floatval($amount);
1092  }
1093  $curCar['newMaterialDonation']['sponsorCarId'] = $curCar['id'];
1094  }
1095  }
1096  }
1097 
1098 
1099  // get new produced parts data
1100  foreach ($curPostedCar['producedParts'] as $producedPartKey => $curPostedProducedPart) {
1101  if (is_numeric($producedPartKey)) {
1102  // produced part is already in db
1103 
1104  // get current produced part in $sponsors['cars']['producedParts']
1105  $producedPartIdx = $producedPartKey - 1;
1106  $curProducedPart = &$curCar['producedParts'][$producedPartIdx];
1107 
1108  // check delete
1109  if(isset($curPostedProducedPart['delete']) && ctype_digit($curPostedProducedPart['delete'])) {
1110  if ($curPostedProducedPart['delete'] == $curProducedPart['id']) {
1111  $producedPartsToDelete[$carIdx][] = $curProducedPart['id'];
1112  continue;
1113  }
1114  }
1115 
1116  //get new timestamp
1117  $newTimestamp = strToTime(trim($curPostedProducedPart['timestamp']));
1118  if ($newTimestamp != FALSE) {
1119  if (strcmp($curProducedPart['timestamp'], $newTimestamp) != 0) {
1120  $curProducedPart['timestamp'] = $newTimestamp;
1121  $changedProducedParts[$carIdx][] = "Fahrzeug '".$curCar['name']."': gefertigtes Teil #".$producedPartKey.": Das 'Datum' wurde erfolgreich aktualisiert.";
1122  }
1123  }
1124  else {
1125  //clear field -> show placeholder in template
1126  $curProducedPart['timestamp'] = '';
1127  $errors[] = "Fahrzeug '".$curCar['name']."': gefertigtes Teil #".$producedPartKey.": Geben Sie ein gültiges 'Datum' im Format: 'dd.mm.yyyy' an!";
1128  }
1129 
1130  //get new name
1131  $newName = htmlSpecialChars(trim($curPostedProducedPart['name']), ENT_QUOTES);
1132  if (!empty($newName)) {
1133  if (strcmp($curProducedPart['name'], $newName) != 0) {
1134  $curProducedPart['name'] = $newName;
1135  $changedProducedParts[$carIdx][] = "Fahrzeug '".$curCar['name']."': gefertigtes Teil #".$producedPartKey.": Der 'Gegenstand' wurde erfolgreich aktualisiert.";
1136  }
1137  }
1138  else {
1139  $curProducedPart['name'] = $newName;
1140  $errors[] = "Fahrzeug '".$curCar['name']."': gefertigtes Teil #".$producedPartKey.": Geben Sie einen gültigen 'Gegenstand' an!";
1141  }
1142 
1143  //get new amount (value)
1144  $newValue = str_replace(array(' ', 'EUR', '€',','), array('','','','.'), trim($curPostedProducedPart['value']));
1145  if (is_numeric($newValue)) {
1146  if (floatval($curProducedPart['value']) != floatval($newValue)) {
1147  $curProducedPart['value'] = floatval($newValue);
1148  $changedProducedParts[$carIdx][] = "Fahrzeug '".$curCar['name']."': gefertigtes Teil #".$producedPartKey.": Der 'Wert' wurde erfolgreich aktualisiert.";
1149  }
1150  }
1151  else {
1152  $curProducedPart['value'] = $newValue;
1153  $errors[] = "Fahrzeug '".$curCar['name']."': gefertigtes Teil #".$producedPartKey.": Geben Sie einen gültigen 'Wert' an!";
1154  }
1155  }
1156  else {
1157  if ((!empty($curPostedProducedPart['isNew'])) && ($curPostedProducedPart['isNew'] == 1)) {
1158  // produced part is a new produced part
1159 
1160  // get timestamp
1161  $curCar['newProducedPart']['timestamp'] = strToTime(trim($curPostedProducedPart['timestamp']));
1162  if (empty($curCar['newProducedPart']['timestamp'])) {
1163  //clear field -> show placeholder in template
1164  $curCar['newProducedPart']['timestamp'] = '';
1165  $errors[] = "Fahrzeug '".$curCar['name']."': neues gefertigtes Teil: Geben Sie ein gültiges 'Datum' im Format: 'dd.mm.yyyy' an!";
1166  }
1167  // get name
1168  $curCar['newProducedPart']['name'] = htmlSpecialChars(trim($curPostedProducedPart['name']), ENT_QUOTES);
1169  if (empty($curCar['newProducedPart']['name'])) {
1170  $errors[] = "Fahrzeug '".$curCar['name']."': neues gefertigtes Teil: Geben Sie einen gültigen 'Gegenstand' an!";
1171  }
1172  // get amount (value)
1173  $value = str_replace(array(' ', 'EUR', '€',','), array('','','','.'), trim($curPostedProducedPart['value']));
1174  if (!is_numeric($value)) {
1175  $errors[] = "Fahrzeug '".$curCar['name']."': neue gefertigtes Teil: Geben Sie einen gültigen 'Wert' an!";
1176  $curCar['newProducedPart']['value'] = $value;
1177  }
1178  else {
1179  $curCar['newProducedPart']['value'] = floatval($value);
1180  }
1181  $curCar['newProducedPart']['sponsorCarId'] = $curCar['id'];
1182  }
1183  }
1184  }
1185 
1186 
1187  // get new attachments data
1188  foreach ($curPostedCar['attachments'] as $attachmentKey => $curPostedAttachment) {
1189  if (is_numeric($attachmentKey)) {
1190  // attachment is already in db
1191 
1192  // get current attachment in $sponsors['cars']['attachments']
1193  $attachmentIdx = $attachmentKey - 1;
1194  $curAttachment = &$curCar['attachments'][$attachmentIdx];
1195 
1196  // check delete
1197  if(isset($curPostedAttachment['delete']) && ctype_digit($curPostedAttachment['delete'])) {
1198  if ($curPostedAttachment['delete'] == $curAttachment['id']) {
1199  $attachmentsToDelete[$carIdx][] = $curAttachment['id'];
1200  continue;
1201  }
1202  }
1203 
1204  //get new filename
1205  $newFileName = htmlSpecialChars(trim($curPostedAttachment['filename']), ENT_QUOTES);
1206  if (!empty($newFileName)) {
1207  if (strcmp($curAttachment['filename'], $newFileName) != 0) {
1208  $curAttachment['filename'] = $newFileName;
1209  $changedAttachments[$carIdx][] = "Fahrzeug '".$curCar['name']."': Anlage #".$attachmentKey.": Der 'Dateiname' wurde erfolgreich aktualisiert.";
1210  }
1211  }
1212  else {
1213  $curAttachment['filename'] = $newFileName;
1214  $errors[] = "Fahrzeug '".$curCar['name']."': Anlage #".$producedPartKey.": Geben Sie einen gültigen 'Dateinamen' an!";
1215  }
1216 
1217  }
1218  else {
1219  if ((!empty($curPostedAttachment['isNew'])) && ($curPostedAttachment['isNew'] == 1)) {
1220  // attachment is a new attachment
1221 
1222  // get filename
1223  $curCar['newAttachment']['filename'] = htmlSpecialChars(trim($curPostedAttachment['filename']), ENT_QUOTES);
1224  if (empty($curCar['newAttachment']['filename'])) {
1225  $errors[] = "Fahrzeug '".$curCar['name']."': neue Anlage: Geben Sie einen gültigen 'Dateinamen' an!";
1226  }
1227 
1228  //get new file
1229  $postedAttachmentFileName = $_FILES['cars']['name'][$carKey]['attachments']['new']['file'];
1230  $postedAttachmentError = $_FILES['cars']['error'][$carKey]['attachments']['new']['file'];
1231  $postedAttachmentTmpName = $_FILES['cars']['tmp_name'][$carKey]['attachments']['new']['file'];
1232 
1233  if (!empty($postedAttachmentFileName)) {
1234  if ($postedAttachmentError == 0) {
1235  if (count($errors) == 0) {
1236  // only change if no errors occured
1237  $pathParts = pathinfo($postedAttachmentFileName);
1238  $newFilename = prepareFilename($pathParts['filename']).'_'.uniqid().'.'.$pathParts['extension'];
1239  if (move_uploaded_file($postedAttachmentTmpName, ABS_PATH.'data/attachments/'.$newFilename)) {
1240  $curCar['newAttachment']['path'] = 'data/attachments/'.$newFilename;
1241  }
1242  else {
1243  $errors[] = "Fahrzeug '".$curCar['name']."': neue Anlage: Es ist ein Fehler beim internen Verarbeiten der 'Anlage' aufgetreten!";
1244  }
1245  }
1246  }
1247  else {
1248  $errors[] = "Fahrzeug '".$curCar['name']."': neue Anlage: Es ist ein unbekannter Fehler beim Upload aufgetreten!";
1249  }
1250  }
1251 
1252  else {
1253  $errors[] = "Fahrzeug '".$curCar['name']."': neue Anlage: Geben Sie eine gültige 'Datei' an!";
1254  }
1255 
1256  $curCar['newAttachment']['sponsorCarId'] = $curCar['id'];
1257  }
1258  }
1259  }
1260 
1261  }
1262  else {
1263  // car is a new car
1264  if(isset($curPostedCar['id']) && ctype_digit($curPostedCar['id'])) {
1265  if($curPostedCar['id'] != -1) {
1266  $sponsor['newCar']['id'] = $curPostedCar['id'];
1267  }
1268  }
1269  }
1270  }
1271 
1272  }
1273  else {
1274  $errors[] = "Ein unbekannter Fehler ist aufgetreten!";
1275  }
1276  }
1277  else {
1278  $errors[] = "Ein unbekannter Fehler ist aufgetreten!";
1279  }
1280 
1281 
1282  // apply changes (no errors occured)
1283  if (count($errors) == 0) {
1284  foreach ($_POST['cars'] as $carKey => $curPostedCar) {
1285  //contiune if curPostedCar is the new car
1286  if (!is_numeric($carKey)) {
1287  continue;
1288  }
1289 
1290  // get current car in $sponsors['cars']
1291  $carIdx = $carKey - 1;
1292  $curCar = &$sponsor['cars'][$carIdx];
1293 
1294  // update sponsor car details
1295  if (!empty($changedCarDetails[$carIdx])) {
1296  if(updateSponsorCarDetails($curCar)) {
1297  // display successes
1298  $successes = array_merge($successes, $changedCarDetails[$carIdx]);
1299  }
1300  else {
1301  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1302  }
1303  }
1304 
1305  // delete sponsor car events
1306  $newEventsArray = array();
1307  if (!empty($eventsToDelete[$carIdx])) {
1308  for ($i = 0; $i < count($curCar['events']); $i++) {
1309  if (in_array($curCar['events'][$i]['id'], $eventsToDelete[$carIdx])) {
1310  if (deleteSponsorCarEvent($curCar['events'][$i]['id'])) {
1311  $successes[] = "Fahrzeug '".$curCar['name']."': Die 'Veranstaltung ".$curCar['events'][$i]['event']."' wurde erfolgreich gelöscht.";
1312  } else {
1313  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1314  }
1315  }
1316  else {
1317  $newEventsArray[] = $curCar['events'][$i];
1318  }
1319 
1320  }
1321  $curCar['events'] = $newEventsArray;
1322  }
1323  // update sponsor car event details
1324  if (!empty($changedEvents[$carIdx])) {
1325  if(updateSponsorCarEvents($curCar['events'])) {
1326  // display successes
1327  $successes = array_merge($successes, $changedEvents[$carIdx]);
1328  }
1329  else {
1330  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1331  }
1332  }
1333  // add sponsor car event
1334  if (isset($curCar['newEvent'])) {
1335  if (addSponsorCarEvent($curCar['newEvent'])) {
1336  if (is_array($curCar['events'])) {
1337  //add entry
1338  array_push($curCar['events'], $curCar['newEvent']);
1339  }
1340  else {
1341  //no entries before
1342  $curCar['events'] = array($curCar['newEvent']);
1343  }
1344  $successes[] = "Fahrzeug '".$curCar['name']."': Die 'Veranstaltung' wurde erfolgreich hinzugefügt.";
1345  }
1346  else {
1347  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1348  }
1349  }
1350 
1351  // delete sponsor car financial donation
1352  $newFinancialDonationsArray = array();
1353  if (!empty($financialDonationsToDelete[$carIdx])) {
1354  for ($i = 0; $i < count($curCar['financialDonations']); $i++) {
1355  if (in_array($curCar['financialDonations'][$i]['id'], $financialDonationsToDelete[$carIdx])) {
1356  if (deleteSponsorCarFinancialDonation($curCar['financialDonations'][$i]['id'])) {
1357  $successes[] = "Fahrzeug '".$curCar['name']."': Die 'Finanzspende #".($i+1)."' wurde erfolgreich gelöscht.";
1358  } else {
1359  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1360  }
1361  }
1362  else {
1363  $newFinancialDonationsArray[] = $curCar['financialDonations'][$i];
1364  }
1365 
1366  }
1367  $curCar['financialDonations'] = $newFinancialDonationsArray;
1368  }
1369  // update sponsor car financial donation details
1370  if (!empty($changedFinancialDonations[$carIdx])) {
1371  if(updateSponsorCarFinancialDonations($curCar['financialDonations'])) {
1372  // display successes
1373  $successes = array_merge($successes, $changedFinancialDonations[$carIdx]);
1374  }
1375  else {
1376  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1377  }
1378  }
1379  // add sponsor car financial donation
1380  if (isset($curCar['newFinancialDonation'])) {
1381  if (addSponsorCarFinancialDonation($curCar['newFinancialDonation'])) {
1382  if (is_array($curCar['financialDonations'])) {
1383  //add entry
1384  array_push($curCar['financialDonations'], $curCar['newFinancialDonation']);
1385  }
1386  else {
1387  //no entries before
1388  $curCar['financialDonations'] = array($curCar['newFinancialDonation']);
1389  }
1390  $successes[] = "Fahrzeug '".$curCar['name']."': Die 'Finanzspende' wurde erfolgreich hinzugefügt.";
1391  }
1392  else {
1393  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1394  }
1395  }
1396 
1397 
1398  // delete sponsor car material donation
1399  $newMaterialDonationsArray = array();
1400  if (!empty($materialDonationsToDelete[$carIdx])) {
1401  for ($i = 0; $i < count($curCar['materialDonations']); $i++) {
1402  if (in_array($curCar['materialDonations'][$i]['id'], $materialDonationsToDelete[$carIdx])) {
1403  if (deleteSponsorCarMaterialDonation($curCar['materialDonations'][$i]['id'])) {
1404  $successes[] = "Fahrzeug '".$curCar['name']."': Die 'Sachspende #".($i+1)."' wurde erfolgreich gelöscht.";
1405  } else {
1406  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1407  }
1408  }
1409  else {
1410  $newMaterialDonationsArray[] = $curCar['materialDonations'][$i];
1411  }
1412 
1413  }
1414  $curCar['materialDonations'] = $newMaterialDonationsArray;
1415  }
1416  // update sponsor car material donation details
1417  if (!empty($changedMaterialDonations[$carIdx])) {
1418  if (updateSponsorCarMaterialDonations($curCar['materialDonations'])) {
1419  // display successes
1420  $successes = array_merge($successes, $changedMaterialDonations[$carIdx]);
1421  }
1422  else {
1423  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1424  }
1425  }
1426  // add sponsor car material donation
1427  if (isset($curCar['newMaterialDonation'])) {
1428  if (addSponsorCarMaterialDonation($curCar['newMaterialDonation'])) {
1429  if (is_array($curCar['materialDonations'])) {
1430  //add entry
1431  array_push($curCar['materialDonations'], $curCar['newMaterialDonation']);
1432  }
1433  else {
1434  //no entries before
1435  $curCar['materialDonations'] = array($curCar['newMaterialDonation']);
1436  }
1437  $successes[] = "Fahrzeug '".$curCar['name']."': Die 'Sachspende' wurde erfolgreich hinzugefügt.";
1438  }
1439  else {
1440  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1441  }
1442  }
1443 
1444 
1445  // delete sponsor car produced part
1446  $newProducedPartsArray = array();
1447  if (!empty($producedPartsToDelete[$carIdx])) {
1448  for ($i = 0; $i < count($curCar['producedParts']); $i++) {
1449  if (in_array($curCar['producedParts'][$i]['id'], $producedPartsToDelete[$carIdx])) {
1450  if (deleteSponsorCarProducedPart($curCar['producedParts'][$i]['id'])) {
1451  $successes[] = "Fahrzeug '".$curCar['name']."': Das 'gefertigte Teil #".($i+1)."' wurde erfolgreich gelöscht.";
1452  } else {
1453  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1454  }
1455  }
1456  else {
1457  $newProducedPartsArray[] = $curCar['producedParts'][$i];
1458  }
1459 
1460  }
1461  $curCar['producedParts'] = $newProducedPartsArray;
1462  }
1463  // update sponsor car produced part details
1464  if (!empty($changedProducedParts[$carIdx])) {
1465  if (updateSponsorCarProducedParts($curCar['producedParts'])) {
1466  // display successes
1467  $successes = array_merge($successes, $changedProducedParts[$carIdx]);
1468  }
1469  else {
1470  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1471  }
1472  }
1473  // add sponsor car produced part
1474  if (isset($curCar['newProducedPart'])) {
1475  if (addSponsorCarProducedPart($curCar['newProducedPart'])) {
1476  if (is_array($curCar['producedParts'])) {
1477  //add entry
1478  array_push($curCar['producedParts'], $curCar['newProducedPart']);
1479  }
1480  else {
1481  //no entries before
1482  $curCar['producedParts'] = array($curCar['newProducedPart']);
1483  }
1484  $successes[] = "Fahrzeug '".$curCar['name']."': Das 'gefertigte Teil' wurde erfolgreich hinzugefügt.";
1485  }
1486  else {
1487  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1488  }
1489  }
1490 
1491 
1492  // delete sponsor car attachment
1493  $newAttachmentsArray = array();
1494  if (!empty($attachmentsToDelete[$carIdx])) {
1495  for ($i = 0; $i < count($curCar['attachments']); $i++) {
1496  if (in_array($curCar['attachments'][$i]['id'], $attachmentsToDelete[$carIdx])) {
1497  if (deleteSponsorCarAttachment($curCar['attachments'][$i]['id'])) {
1498  //delete file
1499  if (file_exists(ABS_PATH.$curCar['attachments'][$i]['path'])) {
1500  unlink(ABS_PATH.$curCar['attachments'][$i]['path']);
1501  }
1502  $successes[] = "Fahrzeug '".$curCar['name']."': Die 'Anlage ".$curCar['attachments'][$i]['filename']."' wurde erfolgreich gelöscht.";
1503  } else {
1504  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1505  }
1506  }
1507  else {
1508  $newAttachmentsArray[] = $curCar['attachments'][$i];
1509  }
1510 
1511  }
1512  $curCar['attachments'] = $newAttachmentsArray;
1513  }
1514 
1515  // update sponsor car attachment details
1516  if (!empty($changedAttachments[$carIdx])) {
1517  if (updateSponsorCarAttachments($curCar['attachments'])) {
1518  // display successes
1519  $successes = array_merge($successes, $changedAttachments[$carIdx]);
1520  }
1521  else {
1522  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1523  }
1524  }
1525 
1526  // add sponsor car attachment
1527  if (isset($curCar['newAttachment'])) {
1528  if (addSponsorCarAttachment($curCar['newAttachment'])) {
1529  if (is_array($curCar['attachments'])) {
1530  //add entry
1531  array_push($curCar['attachments'], $curCar['newAttachment']);
1532  }
1533  else {
1534  //no entries before
1535  $curCar['attachments'] = array($curCar['newAttachment']);
1536  }
1537  $successes[] = "Fahrzeug '".$curCar['name']."': Das 'Anlage' wurde erfolgreich hinzugefügt.";
1538  }
1539  else {
1540  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1541  }
1542  }
1543  }
1544 
1545  //add new car
1546  if (isset($sponsor['newCar'])) {
1547  $newSponsorCarId = addSponsorCar($sponsor['newCar']['id'], $sponsor['id']);
1548  if ($newSponsorCarId) {
1549  // add to cars array
1550  if (is_array($sponsor['cars'])) {
1551  //add entry
1552  array_push($sponsor['cars'], fetchSponsorCarDetails(null, $newSponsorCarId));
1553  }
1554  else {
1555  //no entries before
1556  $sponsor['cars'] = array(fetchSponsorCarDetails(null, $newSponsorCarId));
1557  }
1558  $successes[] = "Das Fahrzeug wurde erfolgreich hinzugefügt.";
1559  // sort new cars array (carId)
1560  kdsort($sponsor['cars'], array_search('carId', array_keys($sponsor['cars'][0])), SORT_DESC);
1561  //choose new car as seleected car
1562  $smarty->assign('clickedCarId', $newSponsorCarId);
1563  //renew possible cars
1564  $possibleNewCars = fetchAllCars();
1565  foreach($possibleNewCars as $key => $possibleNewCar) {
1566  if (empty($sponsor['cars'])) {
1567  break;
1568  }
1569  foreach ($sponsor['cars'] as $_car) {
1570  if ($_car['carId'] == $possibleNewCar['id']) {
1571  unset($possibleNewCars[$key]);
1572  }
1573  }
1574  }
1575  $smarty->assign('possibleNewCars', $possibleNewCars);
1576  }
1577  else {
1578  $errors[] = 'Es ist ein Datenbankfehler aufgetreten!';
1579  }
1580  }
1581  }
1582 
1583  // unlock section and disable edit mode
1584  if (count($errors) == 0) {
1585  if (isset($sponsor['newCar'])) {
1586  //renew lock to lock new Car and keep edit mode enabled
1587  renewSponsorEditSectionLock($_GET['editsec'], $_GET['id']);
1588  }
1589  else {
1590  // unlock
1591  unlockSponsorEditSection($_GET['editsec'],$_GET['id']);
1592  // disable edit mode
1593  $smarty->assign('edit', NULL);
1594  }
1595  }
1596  }
1597 
1598  // set clicked car
1599  if(isset($_GET['carId']) && ctype_digit($_GET['carId'])) {
1600  $smarty->assign('clickedCarId', $_GET['carId']);
1601  }
1602 
1603  break;
1604  }
1605  default:
1606  break;
1607  }
1608 
1609 
1610  }
1611  else {
1612  $errors [] = 'Dieser Bereich wird gerade von einem anderen Benutzer bearbeitet und ist gesperrt!';
1613  exitWithErrorTemplateAndRedirect(null, 'sponsors.php?id='.$sponsor['id'], 2);
1614  }
1615  }
1616 
1617 
1618  //fetch last edit data
1619  $lastChange = fetchSponsorLatestEditData($sponsor['id']);
1620  if (!empty($lastChange)) {
1621  $sponsor['lastChangeDateTimestamp'] = $lastChange['timestamp'];
1622  $sponsor['lastChangeUserId'] = $lastChange['userId'];
1623  $sponsor['lastChangeUserName'] = $lastChange['userName'];
1624  }
1625 
1626  // fetch list of changes for edit section 0
1627  $lastChangesForEditSections = fetchSponsorEditDataForEditSections($sponsor['id'], NUMBER_OF_CHANGES_SHOWN_PER_EDIT_SECTION);
1628  $smarty->assign('lastChangesForEditSections',$lastChangesForEditSections);
1629 
1630  // display template
1631  $smarty->assign('sponsor',$sponsor);
1632  displayTemplateWithErrorsAndSuccesses("Sponsordetails: '".$sponsor['name']."'",'sponsor.tpl');
1633 
1634  }
1635  else {
1636  // LIST VIEW OF ALL SPONSORS
1637 
1638 
1639  if(isset($resultIdentifier)&&$loggedInUser->isResultAvailable($resultIdentifier)) {
1640  // the result seems to be available try to load it
1641  $sponsorIds = $loggedInUser->loadResult($resultIdentifier);
1642  if(!$sponsorIds==FALSE) {
1643  $sponsors = array();
1644  foreach($sponsorIds as $sponsorId) {
1645  $sponsors[] = fetchSponsorDetails($sponsorId);
1646  }
1647 
1648  // list view of the matched entries
1649  if(!empty($sponsors)) {
1650  foreach ($sponsors as &$sponsor) {
1651  $sponsor['productionFacilities'] = fetchSponsorProductionFacilities($sponsor['id']);
1652  $sponsor['cars'] = fetchSponsorCarNames($sponsor['id']);
1653  }
1654  }
1655 
1656  // display template
1657  $smarty->assign('resultIdentifier',$resultIdentifier);
1658  } else {
1659  exitWithErrorTemplateAndRedirect(array("Es ist ein Fehler aufgetreten. Sie werden in Kürze weitergeleitet"),'sponsors.php?sort=1&dir=0',2);
1660  }
1661  } else {
1662  // sponsor list view
1663  $sponsors = fetchAllSponsors();
1664 
1665  $tmp = array();
1666  foreach ($sponsors as &$sponsor) {
1667  $sponsor['productionFacilities'] = fetchSponsorProductionFacilities($sponsor['id']);
1668  $sponsor['cars'] = fetchSponsorCarNames($sponsor['id']);
1669  }
1670  }
1671 
1672 
1673  // filtering
1674  $filterActive = FALSE;
1675 
1676  // the user wants to filter by
1677  if(isset($_POST['selectStatus']) && $_POST['selectStatus']!=-1) {
1678  if(ctype_digit($_POST['selectStatus'])) {
1679  // they id seems to be valid to obtain it
1680  $statusId = $_POST['selectStatus'];
1681  // reassign so the selection persists
1682  $smarty->assign('selectedStatus',$statusId);
1683  if(is_array($sponsors)) {
1684  // iterate over all the sponsors
1685  foreach($sponsors as $id => $sponsor) {
1686  if(!empty($sponsor['statusId'])) {
1687  $contains = FALSE;
1688  if(!($statusId == $sponsor['statusId'])) {
1689  // the sponsors status didn't match the chosen one so delete it from the array
1690  unset($sponsors[$id]);
1691  }
1692  } else {
1693  unset($sponsors[$id]);
1694  }
1695  }
1696  $filterActive = $filterActive || TRUE;
1697  }
1698  } else {
1699  $smarty->assign('selectedStatus',-1);
1700  }
1701  } else {
1702  $smarty->assign('selectedStatus',-1);
1703  }
1704 
1705  // the user wants to filter by categorie
1706  if(isset($_POST['selectCategory']) && $_POST['selectCategory']!=-1) {
1707  if(ctype_digit($_POST['selectCategory'])) {
1708  // save the category id for later because category is saved per car
1709  // so if no car is selected there's no way filtering by category
1710  $categoryId = $_POST['selectCategory'];
1711  $smarty->assign('selectedCategory',$categoryId);
1712  $filterActive = $filterActive || TRUE;
1713  } else {
1714  $smarty->assign('selectedCategory',-1);
1715  }
1716  } else {
1717  $smarty->assign('selectedCategory',-1);
1718  }
1719 
1720 
1721  // if the filter button is pressed and something else but the default was selected
1722  if(isset($_POST['selectProductionFacility'])&&$_POST['selectProductionFacility']!=-1) {
1723  // do some validation
1724  if(ctype_digit($_POST['selectProductionFacility'])) {
1725  // obtain the id of the production facility from the POST
1726  $productionFacilityId = $_POST['selectProductionFacility'];
1727  // reassign so that the selection persists
1728  $smarty->assign('selectedFacility',$productionFacilityId);
1729  if(is_array($sponsors)) {
1730  // iterrate over all the (remaining) sponsors
1731  foreach($sponsors as $id => $sponsor) {
1732  // only preceed if the sponsor has any production facilities
1733  if(!empty($sponsor['productionFacilities'])) {
1734  $cFacilities = $sponsor['productionFacilities'];
1735  $contains = FALSE;
1736  // iterate over all the production facilities
1737  foreach($cFacilities as $facility) {
1738  if($productionFacilityId == $facility['productionFacilityId']) {
1739  // the sponsor has the wanted production faciliy
1740  $contains = TRUE;
1741  break;
1742  }
1743  }
1744  if(!$contains) {
1745  // the sponsor doesn't match the critiria so remove it
1746  unset($sponsors[$id]);
1747  }
1748  } else {
1749  // the sponsor has no production facilities at all so remove if
1750  unset($sponsors[$id]);
1751  }
1752  }
1753  $filterActive = $filterActive || TRUE;
1754  }
1755  }
1756  } else {
1757  $smarty->assign('selectedFacility',-1);
1758  }
1759 
1760  // the user chose to filter by car
1761  if(isset($_POST['selectCar'])&&$_POST['selectCar']!=-1) {
1762  // do some validation
1763  if(ctype_digit($_POST['selectCar'])) {
1764  // obtain the id of the selected car
1765  $carId = $_POST['selectCar'];
1766  // assign it to the template so the selection will persist
1767  $smarty->assign('selectedCar',$carId);
1768  if(is_array($sponsors)) {
1769  // iterate over all sponsors
1770  foreach($sponsors as $id => $sponsor) {
1771  // check if the current sponsor has sponsorcars
1772  if(!empty($sponsor['cars'])) {
1773  $cCars = $sponsor['cars'];
1774  $contains = FALSE;
1775  // iterate over all the cars
1776  foreach($cCars as $car) {
1777  if($carId == $car['id']) {
1778  // the car matched the filter
1779  $contains = TRUE;
1780  // check if the user also wants so filter by category (which is saved per car)
1781  if(isset($_POST['selectCategory']) && $_POST['selectCategory']!=-1) {
1782  if(ctype_digit($_POST['selectCategory'])) {
1783  // obtain the id of the category
1784  $categoryId = $_POST['selectCategory'];
1785  $sponsorCarsDetails = fetchSponsorCarDetails($sponsor['id']);
1786  foreach ($sponsorCarsDetails as $details) {
1787  if(strcmp($details['name'],$car['name'])==0) {
1788  $contains = $contains && ($details['categoryId']==$categoryId);
1789  }
1790  }
1791  }
1792  }
1793  if($contains) {
1794  break;
1795  }
1796  }
1797  }
1798  if(!$contains) {
1799  unset($sponsors[$id]);
1800  }
1801  } else {
1802  unset($sponsors[$id]);
1803  }
1804  }
1805  }
1806  $filterActive = $filterActive || TRUE;
1807  }
1808  } else {
1809  $smarty->assign('selectedCar',-1);
1810  }
1811 
1812  // one ore more filters were active
1813  if($filterActive) {
1814  // prevents errors if the user goes back to the result page
1815  header("Cache-Control: max-age=600");
1816  foreach($sponsors as $sponsor) {
1817  $tmp[] = $sponsor['id'];
1818  }
1819  // this result identifier is the key to obtain the data later
1820  $resultIdentifier = $loggedInUser->storeFilterResult($tmp);
1821  $smarty->assign('resultIdentifier',$resultIdentifier);
1822  }
1823 
1824  // sort result
1825  if(isset($_GET['sort']) && ctype_digit($_GET['sort']) && !empty($sponsors)) {
1826  $columnIdx = $_GET['sort'];
1827  if(isset($_GET['dir']) && ctype_digit($_GET['dir'])) {
1828  $direction = ($_GET['dir'] == 0) ? SORT_ASC : SORT_DESC;
1829  // sort array
1830  kdsort($sponsors, $columnIdx, $direction);
1831  // assign sort attributes
1832  $smarty->assign('sort', array('colIdx' => $columnIdx,
1833  'direction' => $_GET['dir']));
1834  }
1835  }
1836 
1837  // the entries will later be present in the dropdown selectors
1838  $productionFacilities = fetchAllProductionFacilities();
1839  $sponsorStatus = fetchAllStatus();
1840  $cars = fetchAllCars();
1841  $categories = fetchAllCategories();
1842 
1843  // display template
1844  $smarty->assign('dropdownCategories',$categories);
1845  $smarty->assign('dropdownCars',$cars);
1846  $smarty->assign('dropdownStatus',$sponsorStatus);
1847  $smarty->assign('dropdownFacilities',$productionFacilities);
1848  $smarty->assign('sponsors',$sponsors);
1849  displayTemplateWithErrorsAndSuccesses('Sponsorenübersicht','sponsors.tpl');
1850  }
1851  /// @endcond
1852 ?>