/**
  * Displays the form
  *
  * @param   string  $tpl  - The template
  *
  * @throws  Exception
  * @return  mixed|void
  */
 public function display($tpl = null)
 {
     $cid = JFactory::getApplication()->input->getInt('cid', 0);
     $user = JFactory::getUser();
     // Booking id!!
     $uid = JFactory::getApplication()->input->getInt('uid', 0);
     $uuid = JFactory::getApplication()->input->get('uuid', 0);
     if (empty($cid)) {
         throw new Exception(JText::_("COM_MATUKIO_NO_ID"), 404);
     }
     // Load event (use model function)
     $emodel = JModelLegacy::getInstance('Event', 'MatukioModel');
     $event = $emodel->getItem($cid);
     $booking = null;
     if (!empty($uuid)) {
         $model = JModelLegacy::getInstance('Booking', 'MatukioModel');
         $booking = $model->getBooking($uuid);
         if (empty($booking)) {
             throw new Exception(JText::_("COM_MATUKIO_NO_BOOKING_FOUND"), 404);
         }
         $uid = $booking->id;
     }
     // With Payment Step or without?
     $steps = 3;
     if (empty($event->fees)) {
         $steps = 2;
     }
     $fields_p1 = MatukioHelperUtilsBooking::getBookingFields(1);
     $fields_p2 = MatukioHelperUtilsBooking::getBookingFields(2);
     $fields_p3 = MatukioHelperUtilsBooking::getBookingFields(3);
     // MatukioHelperUtilsBasic::expandPathway(JTEXT::_('COM_MATUKIO_EVENTS'), JRoute::_("index.php?option=com_matukio&view=eventlist"));
     // Add event to breadcrumb :)
     MatukioHelperUtilsBasic::expandPathway(JTEXT::_($event->title), JRoute::_("index.php?option=com_matukio&view=event&id=" . $cid));
     MatukioHelperUtilsBasic::expandPathway(JTEXT::_('COM_MATUKIO_EVENT_BOOKING'), "");
     $dispatcher = JDispatcher::getInstance();
     JPluginHelper::importPlugin("payment");
     $gateways = $dispatcher->trigger('onTP_GetInfo', array(MatukioHelperPayment::$matukio_payment_plugins));
     $payment = array();
     foreach ($gateways as $gway) {
         $payment[] = array("name" => $gway->id, "title" => $gway->name);
     }
     if (empty($payment)) {
         // If no payment plugins enabled then set Steps to 2 :)
         $steps = 2;
     }
     $this->gateways = $gateways;
     $this->event = $event;
     $this->uid = $uid;
     $this->uuid = $uuid;
     $this->booking = $booking;
     $this->user = $user;
     $this->steps = $steps;
     $this->payment = $payment;
     $this->fields_p1 = $fields_p1;
     $this->fields_p2 = $fields_p2;
     $this->fields_p3 = $fields_p3;
     parent::display($tpl);
 }
				</td>
			</tr>
			<tr>
				<td align="left" class="key">
					<?php 
    echo JText::_('COM_MATUKIO_EMAIL');
    ?>
:
				</td>
				<td>
					<input type="text" class="sem_inputbox" id="email" name="email" value="" size="20"/>
				</td>
			</tr>
		<?php 
} else {
    $fields = MatukioHelperUtilsBooking::getBookingFields();
    foreach ($fields as $field) {
        if ($field->type != 'spacer') {
            MatukioHelperUtilsBooking::printFieldElement($field, false, -1, "small");
        }
    }
    ?>
		<?php 
}
?>
		<tr>
			<td colspan="2">
				<div align="right">
					<input type="submit" value="<?php 
echo JText::_("COM_MATUKIO_BOOK");
?>
 /**
  * NEW Booking method for old and new form
  *
  * @throws  Exception - if db query fails
  * @return  mixed
  */
 public function book()
 {
     $payment_brutto = 0;
     $database = JFactory::getDBO();
     $post = JRequest::get('post');
     $my = JFactory::getUser();
     $input = JFactory::getApplication()->input;
     $event_id = $input->getInt('event_id', 0);
     $uid = $input->getInt('uid', 0);
     $steps = $input->getInt('steps', 3);
     $uuid = $input->get('uuid', 0, 'string');
     $id = $input->getInt('id', 0);
     $booking = null;
     if (!empty($id)) {
         // check if there is really such a booking
         $model = JModelLegacy::getInstance('Booking', 'MatukioModel');
         $booking = $model->getBooking($uuid);
         if (empty($booking)) {
             throw new Exception(JText::_("COM_MATUKIO_NO_BOOKING_FOUND"), 404);
         }
         if ($booking->id != $id) {
             throw new Exception(JText::_("COM_MATUKIO_NO_BOOKING_FOUND"), 404);
         }
     }
     $nrbooked = $input->getInt('nrbooked', 1);
     $catid = $input->getInt('catid', 0);
     $payment_method = $input->get('payment', '', 'string');
     $agb = $input->get('agb', '', 'string');
     $isWaitlist = false;
     $dispatcher = JDispatcher::getInstance();
     if (empty($event_id)) {
         throw new Exception(JText::_("COM_MATUKIO_NO_ID"), 404);
     }
     // Load event (use model function)
     $emodel = JModelLegacy::getInstance('Event', 'MatukioModel');
     $event = $emodel->getItem($event_id);
     $userid = $my->id;
     // Different fees @since 3.0
     $different_fees = $event->different_fees;
     $reason = "";
     $art = 2;
     $temp = null;
     $gebucht = MatukioHelperUtilsEvents::calculateBookedPlaces($event);
     $gebucht = $gebucht->booked;
     $allesok = 1;
     $ueber1 = JTEXT::_('COM_MATUKIO_BOOKING_WAS_SUCCESSFULL');
     $pflichtfeld = false;
     $fields = MatukioHelperUtilsEvents::getAdditionalFieldsFrontend($event);
     // Checking old required fields - backward compatibilty
     for ($i = 0; $i < 20; $i++) {
         $test = $fields[0][$i];
         if (!empty($test)) {
             $res = explode("|", $test);
             if (trim($res[1]) == "1") {
                 $value = $input->get("zusatz" . ($i + 1), '', 'string');
                 if (empty($value)) {
                     $pflichtfeld = true;
                 }
             }
         }
     }
     if (MatukioHelperSettings::getSettings("captcha", 0) == 1) {
         $ccval = $input->get("ccval", '', 'string');
         $captcha = $input->get("captcha", '', 'string');
         if (empty($captcha)) {
             $allesok = 0;
             $ueber1 = JTEXT::_('COM_MATUKIO_BOOKING_WAS_NOT_SUCCESSFULL');
             $reason = JTEXT::_('COM_MATUKIO_CAPTCHA_WRONG');
         } elseif (md5($captcha) != $ccval) {
             $allesok = 0;
             $ueber1 = JTEXT::_('COM_MATUKIO_BOOKING_WAS_NOT_SUCCESSFULL');
             $reason = JTEXT::_('COM_MATUKIO_CAPTCHA_WRONG');
         }
     }
     if (MatukioHelperSettings::getSettings("recaptcha", 0) == 1) {
         require_once JPATH_COMPONENT_ADMINISTRATOR . '/include/recaptcha/recaptchalib.php';
         $key = MatukioHelperSettings::getSettings("recaptcha_private_key", "");
         if (empty($key)) {
             throw new Exception("COM_MATUKIO_YOU_HAVE_TO_SET_A_RECAPTCHA_KEY", 500);
         }
         $resp = recaptcha_check_answer($key, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
         if (!$resp->is_valid) {
             // What happens when the CAPTCHA was entered incorrectly
             $allesok = 0;
             $ueber1 = JTEXT::_('COM_MATUKIO_BOOKING_WAS_NOT_SUCCESSFULL');
             $reason = JTEXT::_('COM_MATUKIO_CAPTCHA_WRONG') . $resp->error;
         }
     }
     $agbtext = MatukioHelperSettings::getSettings("agb_text", "");
     if ($pflichtfeld) {
         $allesok = 0;
         $ueber1 = JTEXT::_('COM_MATUKIO_BOOKING_WAS_NOT_SUCCESSFULL');
         $reason = JTEXT::_('COM_MATUKIO_REQUIRED_ADDITIONAL_FIELD_EMPTY');
     } elseif (count($temp) > 0) {
         $allesok = 0;
         $ueber1 = JTEXT::_('COM_MATUKIO_BOOKING_WAS_NOT_SUCCESSFULL');
         $reason = JTEXT::_('COM_MATUKIO_REGISTERED_FOR_THIS_EVENT');
     } elseif (MatukioHelperUtilsDate::getCurrentDate() > $event->booked) {
         echo "current: " . MatukioHelperUtilsDate::getCurrentDate();
         echo " booking: " . $event->booked;
         $allesok = 0;
         $ueber1 = JTEXT::_('COM_MATUKIO_BOOKING_WAS_NOT_SUCCESSFULL');
         $reason = JTEXT::_('COM_MATUKIO_EXCEEDED');
     } elseif ($event->maxpupil - $gebucht - $nrbooked < 0 && $event->stopbooking == 1) {
         $allesok = 0;
         $ueber1 = JTEXT::_('COM_MATUKIO_BOOKING_WAS_NOT_SUCCESSFULL');
         $reason = JTEXT::_('COM_MATUKIO_MAX_PARTICIPANT_NUMBER_REACHED');
     } elseif (!empty($agbtext)) {
         // Has to be on the end
         if (empty($agb)) {
             $allesok = 0;
             $ueber1 = JTEXT::_('COM_MATUKIO_BOOKING_WAS_NOT_SUCCESSFULL');
             $reason = JTEXT::_('COM_MATUKIO_AGB_NOT_ACCEPTED');
         }
     }
     if ($event->maxpupil - $gebucht - $nrbooked < 0 && $event->stopbooking == 0) {
         $allesok = 2;
         $ueber1 = JTEXT::_('COM_MATUKIO_ADDED_WAITLIST');
         $reason = JTEXT::_('COM_MATUKIO_YOU_ARE_BOOKED_ON_THE_WAITING_LIST');
         if (empty($booking)) {
             $isWaitlist = true;
         } else {
             // Prevent switching old booking to waitlist
             if ($booking->status == MatukioHelperUtilsBooking::$WAITLIST) {
                 $isWaitlist = true;
             } else {
                 $isWaitlist = false;
             }
         }
     }
     if ($art == 4) {
         $allesok = 1;
         $ueber1 = JTEXT::_('COM_MATUKIO_BOOKING_WAS_SUCCESSFULL');
     }
     $results = $dispatcher->trigger('onValidateBooking', $post, $event, $allesok);
     // Alles in Ordnung
     if ($allesok > 0) {
         // Buchung eintragen
         $neu = JTable::getInstance('bookings', 'Table');
         if (!$neu->bind($post)) {
             return JError::raiseError(500, $database->stderr());
         }
         $neu->semid = $event->id;
         $neu->userid = $userid;
         $firstname = $input->get('firstname', '', 'string');
         $lastname = $input->get('lastname', '', 'string');
         $neu->bookingdate = MatukioHelperUtilsDate::getCurrentDate();
         $neu->name = MatukioHelperUtilsBasic::cleanHTMLfromText($firstname . " " . $lastname);
         $neu->email = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->email);
         $neu->zusatz1 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz1);
         $neu->zusatz2 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz2);
         $neu->zusatz3 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz3);
         $neu->zusatz4 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz4);
         $neu->zusatz5 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz5);
         $neu->zusatz6 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz6);
         $neu->zusatz7 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz7);
         $neu->zusatz8 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz8);
         $neu->zusatz9 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz9);
         $neu->zusatz10 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz10);
         $neu->zusatz11 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz11);
         $neu->zusatz12 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz12);
         $neu->zusatz13 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz13);
         $neu->zusatz14 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz14);
         $neu->zusatz15 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz15);
         $neu->zusatz16 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz16);
         $neu->zusatz17 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz17);
         $neu->zusatz18 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz18);
         $neu->zusatz19 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz19);
         $neu->zusatz20 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz20);
         $fields = MatukioHelperUtilsBooking::getBookingFields();
         if (!empty($fields)) {
             $newfields = "";
             for ($i = 0; $i < count($fields); $i++) {
                 $field = $fields[$i];
                 $name = $field->field_name;
                 $newfields .= $field->id;
                 $newfields .= "::";
                 $newfields .= $input->get($name, '', 'string');
                 $newfields .= ";";
             }
             $neu->newfields = $newfields;
             if (!empty($event->fees) && $steps > 2) {
                 $neu->payment_method = $payment_method;
                 if ($different_fees == 0) {
                     $payment_brutto = $event->fees * $neu->nrbooked;
                     $coupon_code = $neu->coupon_code;
                     if (!empty($coupon_code)) {
                         $cdate = new DateTime();
                         $db = JFactory::getDBO();
                         $query = $db->getQuery(true);
                         $query->select('*')->from('#__matukio_booking_coupons')->where('code = ' . $db->quote($coupon_code) . ' AND published = 1 AND (published_up < ' . $db->quote($cdate->format('Y-m-d H:i:s')) . ' OR published_up = ' . $db->quote("0000-00-00 00:00:00") . ') ' . 'AND (published_down > ' . $db->quote($cdate->format('Y-m-d H:i:s')) . ' OR published_down = ' . $db->quote("0000-00-00 00:00:00") . ')');
                         $db->setQuery($query);
                         $coupon = $db->loadObject();
                         if (!empty($coupon)) {
                             if ($coupon->procent == 1) {
                                 // Get a procent value
                                 $payment_brutto = round($payment_brutto * ((100 - $coupon->value) / 100), 2);
                             } else {
                                 // Get a real value
                                 $payment_brutto = $payment_brutto - $coupon->value;
                             }
                             // Check how often the coupon is used and if used to often set published to 0 (since 3.0.0)
                             $coupon->hits++;
                             // Check if coupon has to be disabled now
                             if (!empty($coupon->max_hits) && $coupon->hits >= $coupon->max_hits) {
                                 $coupon->published = 0;
                             }
                             $coupontable = JTable::getInstance('coupons', 'Table');
                             if (!$coupontable->bind($coupon)) {
                                 throw new Exception(42, $database->stderr());
                             }
                             if (!$coupontable->check()) {
                                 throw new Exception(42, $database->stderr());
                             }
                             if (!$coupontable->store()) {
                                 throw new Exception(42, $database->stderr());
                             }
                             $coupontable->checkin();
                         } else {
                             // Perhaps delete this invalid field, or display an error?! Should be validated through js normally
                             throw new Exception(JText::_("COM_MATUKIO_INVALID_COUPON_CODE"), 42);
                         }
                     }
                     $neu->payment_brutto = $payment_brutto;
                 } else {
                     // Different fees
                     $payment_brutto = MatukioHelperFees::getPaymentTotal($event);
                     $neu->payment_brutto = $payment_brutto;
                     $difarray = array("places" => $input->get("places", array(), 'Array'), "types" => $input->get("ticket_fees", array(), 'Array'));
                     $neu->different_fees = json_encode($difarray);
                 }
             } elseif (!empty($event->fees)) {
                 if ($different_fees == 0) {
                     // We have disabled payment plugins but a fee
                     // Only calculate total amount
                     $payment_brutto = $event->fees * $neu->nrbooked;
                     $neu->payment_brutto = $payment_brutto;
                 } else {
                     // Different fees
                     $payment_brutto = MatukioHelperFees::getPaymentTotal($event);
                     $neu->payment_brutto = $payment_brutto;
                     $difarray = array("places" => $input->get("places", array(), 'Array'), "types" => $input->get("ticket_fees", array(), 'Array'));
                     $neu->different_fees = json_encode($difarray);
                 }
             }
         } else {
             // Only calculate total amount
             $payment_brutto = $event->fees * $neu->nrbooked;
             $neu->payment_brutto = $event->fees * $neu->nrbooked;
         }
         // Taxes
         if ($neu->payment_brutto > 0) {
             // Lets check if there are any
             if ($event->tax_id == 0) {
                 // No taxes
                 $neu->payment_netto = $neu->payment_brutto;
                 $neu->payment_tax = 0.0;
             } else {
                 $db = JFactory::getDbo();
                 $query = $db->getQuery(true);
                 $query->select("*")->from("#__matukio_taxes")->where("id = " . $db->quote($event->tax_id) . " AND published = 1");
                 $db->setQuery($query);
                 $tax = $db->loadObject();
                 if (empty($tax)) {
                     // Houston we have a problem
                     throw new Exception("Invalid tax value! Please select the correct tax in the event edit form.");
                 } else {
                     // Calculate netto
                     $minfac = 100 / (100 + $tax->value);
                     $neu->payment_netto = $neu->payment_brutto * $minfac;
                     $neu->payment_tax = $neu->payment_brutto - $neu->payment_netto;
                 }
             }
         }
         $results = $dispatcher->trigger('onBeforeSaveBooking', $neu, $event);
         // Set status since @3.1
         if (!empty($booking)) {
             // We don't update the status for old bookings
             $neu->status = $booking->status;
         } elseif ($isWaitlist) {
             // We book to the waitlist, let's set the booking like that then
             $neu->status = MatukioHelperUtilsBooking::$WAITLIST;
         } elseif (MatukioHelperSettings::_("booking_always_inactive", 0)) {
             // We set the status to pending
             $neu->status = MatukioHelperUtilsBooking::$PENDING;
         } elseif (empty($event->fees)) {
             // We set the status to active because no payment is done.. so no reason to not confirm them
             $neu->status = MatukioHelperUtilsBooking::$ACTIVE;
         } elseif ($steps == 2) {
             // We set the status to active because no payment can be done.. so no reason to not confirm them
             $neu->status = MatukioHelperUtilsBooking::$ACTIVE;
         } elseif ($payment_method == "cash") {
             // We check if the booking always active setting is set - if yes the booking is always!! active (except waitlist delete etc.)
             $neu->status = MatukioHelperUtilsBooking::$ACTIVE;
         } elseif (MatukioHelperSettings::_("booking_always_active", 0)) {
             // We check if the booking always active setting is set - if yes the booking is always!! active (except waitlist delete etc.)
             $neu->status = MatukioHelperUtilsBooking::$ACTIVE;
         } else {
             // We set the status to pending - this is going to be overwritten by the payment API (if paid etc.)
             $neu->status = MatukioHelperUtilsBooking::$PENDING;
         }
         if (!$neu->check()) {
             JError::raiseError(500, $database->stderr());
         }
         if (!$neu->store()) {
             JError::raiseError(500, $database->stderr());
         }
         $neu->checkin();
         $results = $dispatcher->trigger('onAfterBookingSave', $neu, $event);
         $ueber1 = JText::_("COM_MATUKIO_BOOKING_WAS_SUCCESSFULL");
         $booking_id = $neu->id;
     }
     if ($payment_brutto > 0 && $steps > 2) {
         // Link to the payment form
         $link = JRoute::_("index.php?option=com_matukio&view=paymentform&uuid=" . $uuid);
         $this->setRedirect($link, $reason);
     } else {
         if ($allesok > 0) {
             // We need to send the confirmation here.. we don't send it yet if the event has payment processing
             MatukioHelperUtilsEvents::sendBookingConfirmationMail($event, $neu->id, 1);
         }
         // Link to the bookingpage
         if (MatukioHelperSettings::getSettings("oldbooking_redirect_after", "bookingpage") == "bookingpage") {
             $link = JRoute::_("index.php?option=com_matukio&view=booking&uuid=" . $neu->uuid);
         } elseif (MatukioHelperSettings::getSettings("oldbooking_redirect_after", "bookingpage") == "eventpage") {
             $link = JRoute::_(MatukioHelperRoute::getEventRoute($event->id, $catid, 0, $booking_id), false);
         } else {
             // Eventlist overview
             $link = JRoute::_("index.php?option=com_matukio&view=eventlist");
         }
         $this->setRedirect($link, $ueber1 . " " . $reason);
     }
 }
 /**
  * Save the booking
  *
  * @todo    Change and Update to a function (mixing backend and frontend!!!)
  * @return  object
  */
 public function save()
 {
     // Check authorization
     if (!JFactory::getUser()->authorise('core.edit', 'com_matukio')) {
         return JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
     }
     $database = JFactory::getDBO();
     $input = JFactory::getApplication()->input;
     $art = 4;
     $event_id = $input->getInt('event_id', 0);
     $uid = $input->getInt('uid', 0);
     $uuid = $input->getInt('uuid', 0);
     $nrbooked = $input->getInt('nrbooked', 1);
     $userid = $input->getInt('userid', 0);
     $payment_method = $input->get('payment', '', 'string');
     $notify_participant = $input->getInt("notify_participant", 0);
     $notify_participant_invoice = $input->getInt("notify_participant_invoice", 0);
     if (empty($event_id)) {
         return JError::raiseError(404, 'COM_MATUKIO_NO_ID');
     }
     // Load event (use model function)
     $emodel = JModelLegacy::getInstance('Event', 'MatukioModel');
     $event = $emodel->getItem($event_id);
     $reason = "";
     if (!empty($uid)) {
         // Setting booking to changed booking
         $userid = $uid;
         // uid = Negativ
         $art = 4;
     }
     if ($art == 4) {
         $allesok = 1;
         $ueber1 = JTEXT::_('COM_MATUKIO_BOOKING_WAS_SUCCESSFULL');
     }
     // Buchung eintragen
     $neu = JTable::getInstance('bookings', 'Table');
     if (!$neu->bind(JRequest::get('post'))) {
         return JError::raiseError(500, $database->stderr());
     }
     $neu->semid = $event->id;
     $neu->userid = $userid;
     $firstname = $input->get('firstname', '', 'string');
     $lastname = $input->get('lastname', '', 'string');
     $neu->bookingdate = MatukioHelperUtilsDate::getCurrentDate();
     $neu->name = MatukioHelperUtilsBasic::cleanHTMLfromText($firstname . " " . $lastname);
     $neu->email = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->email);
     $neu->zusatz1 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz1);
     $neu->zusatz2 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz2);
     $neu->zusatz3 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz3);
     $neu->zusatz4 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz4);
     $neu->zusatz5 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz5);
     $neu->zusatz6 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz6);
     $neu->zusatz7 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz7);
     $neu->zusatz8 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz8);
     $neu->zusatz9 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz9);
     $neu->zusatz10 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz10);
     $neu->zusatz11 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz11);
     $neu->zusatz12 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz12);
     $neu->zusatz13 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz13);
     $neu->zusatz14 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz14);
     $neu->zusatz15 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz15);
     $neu->zusatz16 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz16);
     $neu->zusatz17 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz17);
     $neu->zusatz18 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz18);
     $neu->zusatz19 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz19);
     $neu->zusatz20 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz20);
     if (empty($neu->uuid)) {
         $neu->uuid = MatukioHelperPayment::getUuid(true);
     }
     $fields = MatukioHelperUtilsBooking::getBookingFields();
     if (!empty($fields)) {
         $newfields = "";
         for ($i = 0; $i < count($fields); $i++) {
             $field = $fields[$i];
             $name = $field->field_name;
             $newfields .= $field->id;
             $newfields .= "::";
             $newfields .= $input->get($name, '', 'string');
             $newfields .= ";";
         }
         $neu->newfields = $newfields;
         if (!empty($event->fees)) {
             $neu->payment_method = $payment_method;
             $payment_brutto = $event->fees * $neu->nrbooked;
             $coupon_code = $neu->coupon_code;
             if (!empty($coupon_code)) {
                 $cdate = new DateTime();
                 $db = JFactory::getDBO();
                 $query = $db->getQuery(true);
                 $query->select('*')->from('#__matukio_booking_coupons')->where('code = ' . $db->quote($coupon_code) . ' AND published = 1 AND published_up < ' . $db->quote($cdate->format('Y-m-d H:i:s')) . " AND published_down > " . $db->quote($cdate->format('Y-m-d H:i:s')));
                 $db->setQuery($query);
                 $coupon = $db->loadObject();
                 if (!empty($coupon)) {
                     if ($coupon->procent == 1) {
                         // Get a procent value
                         $payment_brutto = round($payment_brutto * ((100 - $coupon->value) / 100), 2);
                     } else {
                         $payment_brutto = $payment_brutto - $coupon->value;
                     }
                 } else {
                     // Raise an error
                     JError::raise(E_ERROR, 500, JText::_("COM_MATUKIO_INVALID_COUPON_CODE"));
                 }
             }
             $neu->payment_brutto = $payment_brutto;
         }
     }
     if (!$neu->check()) {
         return JError::raiseError(500, $database->stderr());
     }
     if (!$neu->store()) {
         return JError::raiseError(500, $database->stderr());
     }
     $neu->checkin();
     $ueber1 = JText::_("COM_MATUKIO_BOOKING_WAS_SUCCESSFULL");
     if ($userid == 0) {
         $userid = $neu->id * -1;
     }
     // Send new confirmation mail
     if ($notify_participant) {
         MatukioHelperUtilsEvents::sendBookingConfirmationMail($event, $neu->id, 11, false, $neu, $notify_participant_invoice);
     }
     $viewteilnehmerlink = JRoute::_("index.php?option=com_matukio&view=participants&cid=" . $event->id . "&art=2");
     $msg = JText::_("COM_MATUKIO_BOOKING_EDITED");
     $this->setRedirect($viewteilnehmerlink, $msg);
 }
 /**
  * Generates the csv booking details
  *
  * @param   object  $booking    - The booking
  * @param   object  $event      - The event
  * @param   string  $separator  - The separator
  *
  * @return  string
  */
 public static function getExportCSVBookingDetails($booking, $event, $separator = ";")
 {
     $html = "";
     $html .= "'" . $booking->nrbooked . "';";
     if (MatukioHelperSettings::getSettings('oldbookingform', 0) == 1) {
         // Old booking form
         if ($booking->userid < 1) {
             if (isset($booking->aname)) {
                 $html .= "'" . $booking->aname . "';";
             } else {
                 $html .= "'" . $booking->name . "';";
             }
             if (isset($booking->aemail)) {
                 $html .= "'" . $booking->aemail . "';";
             } else {
                 $html .= "'" . $booking->email . "';";
             }
         } else {
             $user = JFactory::getUser($booking->userid);
             $html .= "'" . $user->name . "';";
             $html .= "'" . $user->email . "';";
         }
     } else {
         // New booking form fields
         $fields = MatukioHelperUtilsBooking::getBookingFields();
         $fields = MatukioHelperUtilsBooking::getBookingFields();
         $fieldvals = explode(";", $booking->newfields);
         $value = array();
         foreach ($fieldvals as $val) {
             $tmp = explode("::", $val);
             if (count($tmp) > 1) {
                 $value[$tmp[0]] = $tmp[1];
             } else {
                 $value[$tmp[0]] = "";
             }
         }
         foreach ($fields as $field) {
             if ($field->type != "spacer" && $field->type != "spacertext") {
                 if (!empty($value[$field->id])) {
                     $html .= "'" . str_replace($separator, " ", $value[$field->id]) . "'" . $separator;
                 } else {
                     $html .= "''" . $separator;
                 }
             }
         }
     }
     return $html;
 }
 /**
  * Saves the form
  *
  * @throws  Exception
  * @return  object
  */
 public function save()
 {
     $input = JFactory::getApplication()->input;
     if ($input->getInt("oldform", 0) == 1) {
         $this->saveOld();
         return;
     }
     $database = JFactory::getDBO();
     $dispatcher = JDispatcher::getInstance();
     // Backend
     $art = 4;
     $event_id = $input->getInt('event_id', 0);
     $uid = $input->getInt('uid', 0);
     $userid = $input->getInt('userid', 0);
     $id = $input->getInt("id", 0);
     $notify_participant = $input->getInt("notify_participant", 0);
     $notify_participant_invoice = $input->getInt("notify_participant_invoice", 0);
     $payment_method = $input->get('payment', '', 'string');
     if (empty($event_id)) {
         JError::raiseError(404, 'COM_MATUKIO_NO_ID');
     }
     // Load event (use events helper function)
     $event = MatukioHelperUtilsEvents::getEventRecurring($event_id);
     // Different fees @since 3.0
     $different_fees = $event->different_fees;
     $reason = "";
     if (!empty($uid)) {
         if ($uid < 0) {
             // Setting booking to changed booking
             $userid = $uid;
             // uid = Negativ
         }
     }
     // Checking old required fields - backward compatibilty - only frontend - we allow everything here..
     if ($art == 4) {
         $allesok = 1;
         $ueber1 = JTEXT::_('COM_MATUKIO_BOOKING_WAS_SUCCESSFULL');
     }
     // Buchung eintragen
     $neu = JTable::getInstance('bookings', 'Table');
     if (!$neu->bind(JRequest::get('post'))) {
         return JError::raiseError(500, $database->stderr());
     }
     $neu->semid = $event->id;
     $neu->userid = $userid;
     $firstname = $input->get('firstname', '', 'string');
     $lastname = $input->get('lastname', '', 'string');
     if (empty($id)) {
         $neu->bookingdate = MatukioHelperUtilsDate::getCurrentDate();
     }
     $neu->name = trim($firstname . " " . $lastname);
     $neu->email = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->email);
     $neu->zusatz1 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz1);
     $neu->zusatz2 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz2);
     $neu->zusatz3 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz3);
     $neu->zusatz4 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz4);
     $neu->zusatz5 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz5);
     $neu->zusatz6 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz6);
     $neu->zusatz7 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz7);
     $neu->zusatz8 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz8);
     $neu->zusatz9 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz9);
     $neu->zusatz10 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz10);
     $neu->zusatz11 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz11);
     $neu->zusatz12 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz12);
     $neu->zusatz13 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz13);
     $neu->zusatz14 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz14);
     $neu->zusatz15 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz15);
     $neu->zusatz16 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz16);
     $neu->zusatz17 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz17);
     $neu->zusatz18 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz18);
     $neu->zusatz19 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz19);
     $neu->zusatz20 = MatukioHelperUtilsBasic::cleanHTMLfromText($neu->zusatz20);
     $fields = MatukioHelperUtilsBooking::getBookingFields();
     if (!empty($fields)) {
         $newfields = "";
         for ($i = 0; $i < count($fields); $i++) {
             $field = $fields[$i];
             $name = $field->field_name;
             $newfields .= $field->id;
             $newfields .= "::";
             $newfields .= $input->get($name, '', 'string');
             $newfields .= ";";
         }
         $neu->newfields = $newfields;
         if (!empty($event->fees)) {
             $neu->payment_method = $payment_method;
             if ($different_fees == 0) {
                 $payment_brutto = $event->fees * $neu->nrbooked;
                 $coupon_code = $neu->coupon_code;
                 if (!empty($coupon_code)) {
                     $cdate = new DateTime();
                     $db = JFactory::getDBO();
                     $query = $db->getQuery(true);
                     $query->select('*')->from('#__matukio_booking_coupons')->where('code = ' . $db->quote($coupon_code));
                     $db->setQuery($query);
                     $coupon = $db->loadObject();
                     if (!empty($coupon)) {
                         if ($coupon->procent == 1) {
                             // Get a procent value
                             $payment_brutto = round($payment_brutto * ((100 - $coupon->value) / 100), 2);
                         } else {
                             $payment_brutto = $payment_brutto - $coupon->value;
                         }
                     } else {
                         // Raise an error
                         JError::raise(E_ERROR, 500, JText::_("COM_MATUKIO_INVALID_COUPON_CODE"));
                     }
                 }
                 $neu->payment_brutto = $payment_brutto;
             } else {
                 // Different fees @since 3.0
                 $payment_brutto = MatukioHelperFees::getPaymentTotal($event);
                 $neu->payment_brutto = $payment_brutto;
                 $difarray = array("places" => $input->get("places", array(), 'Array'), "types" => $input->get("ticket_fees", array(), 'Array'));
                 $neu->different_fees = json_encode($difarray);
             }
             // Taxes
             if ($neu->payment_brutto > 0) {
                 // Lets check if there are any
                 if ($event->tax_id == 0) {
                     // No taxes
                     $neu->payment_netto = $neu->payment_brutto;
                     $neu->payment_tax = 0.0;
                 } else {
                     $db = JFactory::getDbo();
                     $query = $db->getQuery(true);
                     $query->select("*")->from("#__matukio_taxes")->where("id = " . $db->quote($event->tax_id) . " AND published = 1");
                     $db->setQuery($query);
                     $tax = $db->loadObject();
                     if (empty($tax)) {
                         // Houston we have a problem
                         throw new Exception("Invalid tax value! Please select the correct tax in the event edit form.");
                     } else {
                         // Calculate netto
                         $minfac = 100 / (100 + $tax->value);
                         $neu->payment_netto = $neu->payment_brutto * $minfac;
                         $neu->payment_tax = $neu->payment_brutto - $neu->payment_netto;
                     }
                 }
             }
         }
     }
     $results = $dispatcher->trigger('onBeforeSaveBooking', $neu, $event);
     if (!$neu->check()) {
         return JError::raiseError(500, $database->stderr());
     }
     if (!$neu->store()) {
         return JError::raiseError(500, $database->stderr());
     }
     $neu->checkin();
     $results = $dispatcher->trigger('onAfterBooking', $neu, $event);
     $ueber1 = JText::_("COM_MATUKIO_BOOKING_WAS_SUCCESSFULL");
     if ($userid == 0) {
         $userid = $neu->id * -1;
     }
     // Send new confirmation mail
     if ($notify_participant) {
         MatukioHelperUtilsEvents::sendBookingConfirmationMail($event, $neu->id, 11, false, $neu, $notify_participant_invoice);
     }
     switch ($this->task) {
         case 'apply':
             $msg = JText::_('COM_MATUKIO_BOOKING_APPLY');
             $link = 'index.php?option=com_matukio&controller=editbooking&task=editBooking&booking_id=' . $neu->id;
             break;
         case 'save':
         default:
             $msg = JText::_('COM_MATUKIO_BOOKING_SAVE');
             $link = 'index.php?option=com_matukio&view=bookings';
             break;
     }
     $this->setRedirect($link, $msg);
 }