/**
  * Initialize the mail functionality to the recipient
  * 
  * @global object $objDatabase
  * @global array $_ARRAYLANG
  * @global integer $_LANGID
  * @global array $_CONFIG
  * @param integer $eventId
  * @param integer $actionId
  * @param integer $regId
  * @param string $mailTemplate
  */
 function sendMail($eventId, $actionId, $regId = null, $mailTemplate = null)
 {
     global $objDatabase, $_ARRAYLANG, $_CONFIG;
     $this->mailList = array();
     // Loads the mail template which needs for this action
     $this->loadMailList($actionId, $mailTemplate);
     if (!empty($this->mailList)) {
         $objEvent = new \Cx\Modules\Calendar\Controller\CalendarEvent($eventId);
         $objRegistration = null;
         if (!empty($regId)) {
             $objRegistration = new \Cx\Modules\Calendar\Controller\CalendarRegistration($objEvent->registrationForm, $regId);
             list($registrationDataText, $registrationDataHtml) = $this->getRegistrationData($objRegistration);
             $query = 'SELECT `v`.`value`, `n`.`default`, `f`.`type`
                       FROM ' . DBPREFIX . 'module_' . $this->moduleTablePrefix . '_registration_form_field_value AS `v`
                       INNER JOIN ' . DBPREFIX . 'module_' . $this->moduleTablePrefix . '_registration_form_field_name AS `n`
                       ON `v`.`field_id` = `n`.`field_id`
                       INNER JOIN ' . DBPREFIX . 'module_' . $this->moduleTablePrefix . '_registration_form_field AS `f`
                       ON `v`.`field_id` = `f`.`id`
                       WHERE `v`.`reg_id` = ' . $regId . '
                       AND (
                              `f`.`type` = "salutation"
                           OR `f`.`type` = "firstname"
                           OR `f`.`type` = "lastname"
                           OR `f`.`type` = "mail"
                       )';
             $objResult = $objDatabase->Execute($query);
             $arrDefaults = array();
             $arrValues = array();
             if ($objResult !== false) {
                 while (!$objResult->EOF) {
                     if (!empty($objResult->fields['default'])) {
                         $arrDefaults[$objResult->fields['type']] = explode(',', $objResult->fields['default']);
                     }
                     $arrValues[$objResult->fields['type']] = $objResult->fields['value'];
                     $objResult->MoveNext();
                 }
             }
             $regSalutation = !empty($arrValues['salutation']) ? $arrDefaults['salutation'][$arrValues['salutation'] - 1] : '';
             $regFirstname = !empty($arrValues['firstname']) ? $arrValues['firstname'] : '';
             $regLastname = !empty($arrValues['lastname']) ? $arrValues['lastname'] : '';
             $regMail = !empty($arrValues['mail']) ? $arrValues['mail'] : '';
             $regType = $objRegistration->type == 1 ? $_ARRAYLANG['TXT_CALENDAR_REG_REGISTRATION'] : $_ARRAYLANG['TXT_CALENDAR_REG_SIGNOFF'];
             $regSearch = array('[[REGISTRATION_TYPE]]', '[[REGISTRATION_SALUTATION]]', '[[REGISTRATION_FIRSTNAME]]', '[[REGISTRATION_LASTNAME]]', '[[REGISTRATION_EMAIL]]');
             $regReplace = array($regType, $regSalutation, $regFirstname, $regLastname, $regMail);
         }
         $domain = ASCMS_PROTOCOL . "://" . $_CONFIG['domainUrl'] . ASCMS_PATH_OFFSET . "/";
         $date = date(parent::getDateFormat() . " - H:i:s");
         $eventTitle = $objEvent->title;
         $eventStart = $objEvent->all_day ? date(parent::getDateFormat(), $objEvent->startDate) : date(parent::getDateFormat() . " (H:i:s)", $objEvent->startDate);
         $eventEnd = $objEvent->all_day ? date(parent::getDateFormat(), $objEvent->endDate) : date(parent::getDateFormat() . " (H:i:s)", $objEvent->endDate);
         $placeholder = array('[[TITLE]]', '[[START_DATE]]', '[[END_DATE]]', '[[LINK_EVENT]]', '[[LINK_REGISTRATION]]', '[[USERNAME]]', '[[FIRSTNAME]]', '[[LASTNAME]]', '[[URL]]', '[[DATE]]');
         $recipients = $this->getSendMailRecipients($actionId, $objEvent, $regId, $objRegistration);
         $objMail = new \phpmailer();
         if ($_CONFIG['coreSmtpServer'] > 0) {
             $arrSmtp = \SmtpSettings::getSmtpAccount($_CONFIG['coreSmtpServer']);
             if ($arrSmtp !== false) {
                 $objMail->IsSMTP();
                 $objMail->Host = $arrSmtp['hostname'];
                 $objMail->Port = $arrSmtp['port'];
                 $objMail->SMTPAuth = true;
                 $objMail->Username = $arrSmtp['username'];
                 $objMail->Password = $arrSmtp['password'];
             }
         }
         $objMail->CharSet = CONTREXX_CHARSET;
         $objMail->From = $_CONFIG['coreAdminEmail'];
         $objMail->FromName = $_CONFIG['coreGlobalPageTitle'];
         $objMail->AddReplyTo($_CONFIG['coreAdminEmail']);
         foreach ($recipients as $mailAdress => $langId) {
             if (!empty($mailAdress)) {
                 $langId = $this->getSendMailLangId($actionId, $mailAdress, $langId);
                 if ($objUser = \FWUser::getFWUserObject()->objUser->getUsers($filter = array('email' => $mailAdress, 'is_active' => true))) {
                     $userNick = $objUser->getUsername();
                     $userFirstname = $objUser->getProfileAttribute('firstname');
                     $userLastname = $objUser->getProfileAttribute('lastname');
                 } else {
                     $userNick = $mailAdress;
                     if (!empty($regId) && $mailAdress == $regMail) {
                         $userFirstname = $regFirstname;
                         $userLastname = $regLastname;
                     } else {
                         $userFirstname = '';
                         $userLastname = '';
                     }
                 }
                 $mailTitle = $this->mailList[$langId]['mail']->title;
                 $mailContentText = !empty($this->mailList[$langId]['mail']->content_text) ? $this->mailList[$langId]['mail']->content_text : strip_tags($this->mailList[$langId]['mail']->content_html);
                 $mailContentHtml = !empty($this->mailList[$langId]['mail']->content_html) ? $this->mailList[$langId]['mail']->content_html : $this->mailList[$langId]['mail']->content_text;
                 // actual language of selected e-mail template
                 $contentLanguage = $this->mailList[$langId]['lang_id'];
                 if ($actionId == self::MAIL_NOTFY_NEW_APP && $objEvent->arrSettings['confirmFrontendEvents'] == 1) {
                     $eventLink = $domain . "/cadmin/index.php?cmd={$this->moduleName}&act=modify_event&id={$objEvent->id}&confirm=1";
                 } else {
                     $eventLink = \Cx\Core\Routing\Url::fromModuleAndCmd($this->moduleName, 'detail', $contentLanguage, array('id' => $objEvent->id, 'date' => $objEvent->startDate))->toString();
                 }
                 $regLink = \Cx\Core\Routing\Url::fromModuleAndCmd($this->moduleName, 'register', $contentLanguage, array('id' => $objEvent->id, 'date' => $objEvent->startDate))->toString();
                 $replaceContent = array($eventTitle, $eventStart, $eventEnd, $eventLink, $regLink, $userNick, $userFirstname, $userLastname, $domain, $date);
                 $mailTitle = str_replace($placeholder, $replaceContent, $mailTitle);
                 $mailContentText = str_replace($placeholder, $replaceContent, $mailContentText);
                 $mailContentHtml = str_replace($placeholder, $replaceContent, $mailContentHtml);
                 if (!empty($regId)) {
                     $mailTitle = str_replace($regSearch, $regReplace, $mailTitle);
                     $mailContentText = str_replace($regSearch, $regReplace, $mailContentText);
                     $mailContentHtml = str_replace($regSearch, $regReplace, $mailContentHtml);
                     $mailContentText = str_replace('[[REGISTRATION_DATA]]', $registrationDataText, $mailContentText);
                     $mailContentHtml = str_replace('[[REGISTRATION_DATA]]', $registrationDataHtml, $mailContentHtml);
                 }
                 /*echo "send to: ".$mailAdress."<br />";
                   echo "send title: ".$mailTitle."<br />";*/
                 $objMail->Subject = $mailTitle;
                 $objMail->Body = $mailContentHtml;
                 $objMail->AltBody = $mailContentText;
                 $objMail->AddAddress($mailAdress);
                 $objMail->Send();
                 $objMail->ClearAddresses();
             }
         }
     }
 }
 /**
  * show date and time by user settings > several day view
  *      
  * @param object  $objEvent             Event object
  * @param string  $separatorDateTime    Date time separator
  * @param string  $separatorSeveralDays SeveralDays separator
  * @param boolean $showClock            true to show clock, false to hide
  * @param integer $part                 Part of the multi date event
  * 
  * @return null
  */
 function getMultiDateBlock($objEvent, $separatorDateTime, $separatorSeveralDays, $showClock, $part)
 {
     global $_ARRAYLANG;
     $this->sepDateTime = html_entity_decode($separatorDateTime);
     if ($part == 1) {
         // parse part 1 (start)
         //date
         $this->date = date(parent::getDateFormat(), $objEvent->startDate);
         //time
         $this->time = date('H:i', $objEvent->startDate);
         //show / hide clock
         $showClock && $this->time != '' ? $this->clock = '&nbsp;' . $_ARRAYLANG['TXT_CALENDAR_OCLOCK'] : ($this->clock = '');
         //add separator for several days
         if ($this->clock != '') {
             $this->clock .= html_entity_decode($separatorSeveralDays);
         } else {
             $this->time .= html_entity_decode($separatorSeveralDays);
         }
     } else {
         // parse part 2 (end)
         //date
         $this->date = date(parent::getDateFormat(), $objEvent->endDate);
         //time
         $this->time = date('H:i', $objEvent->endDate);
         //show / hide clock
         $showClock && $this->time != '' ? $this->clock = '&nbsp;' . $_ARRAYLANG['TXT_CALENDAR_OCLOCK'] : ($this->clock = '');
     }
 }
 /**
  * Export the registered userd of the given event
  *      
  * @param integer $eventId          Event id
  * @param integer $registrationType Registration type
  * 
  * @return mixed csv file with registered users list
  */
 function exportRegistrations($eventId, $registrationType)
 {
     global $_ARRAYLANG, $_LANGID;
     if (empty($eventId)) {
         \Cx\Core\Csrf\Controller\Csrf::header("Location: index.php?cmd=" . $this->moduleName);
         return;
     }
     switch ($registrationType) {
         case 'r':
             $getRegistrations = true;
             $getDeregistrations = false;
             $getWaitlist = false;
             break;
         case 'd':
             $getRegistrations = false;
             $getDeregistrations = true;
             $getWaitlist = false;
             break;
         case 'w':
             $getRegistrations = false;
             $getDeregistrations = false;
             $getWaitlist = true;
             break;
         default:
             $getRegistrations = true;
             $getDeregistrations = true;
             $getWaitlist = true;
             break;
     }
     parent::getFrontendLanguages();
     $objEvent = new \Cx\Modules\Calendar\Controller\CalendarEvent($eventId);
     $filename = urlencode($objEvent->title) . ".csv";
     $objRegistrationManager = new \Cx\Modules\Calendar\Controller\CalendarRegistrationManager($eventId, $getRegistrations, $getDeregistrations, $getWaitlist);
     $objRegistrationManager->getRegistrationList();
     if (!empty($objRegistrationManager->registrationList)) {
         header("Content-Type: text/comma-separated-values; charset=" . CONTREXX_CHARSET, true);
         header("Content-Disposition: attachment; filename=\"{$filename}\"", true);
         print $_ARRAYLANG['TXT_CALENDAR_FIRST_EXPORT'] . $this->csvSeparator;
         print $_ARRAYLANG['TXT_CALENDAR_TYPE'] . $this->csvSeparator;
         print $_ARRAYLANG['TXT_CALENDAR_EVENT'] . $this->csvSeparator;
         print $_ARRAYLANG['TXT_CALENDAR_LANG'] . $this->csvSeparator;
         $firstKey = key($objRegistrationManager->registrationList);
         foreach ($objRegistrationManager->registrationList[$firstKey]->fields as $id => $arrField) {
             if ($arrField['type'] != 'fieldset') {
                 print self::escapeCsvValue(html_entity_decode($arrField['name'], ENT_QUOTES)) . $this->csvSeparator;
             }
         }
         print "\r\n";
         foreach ($objRegistrationManager->registrationList as $key => $objRegistration) {
             if (intval($objRegistration->firstExport) == 0) {
                 $objRegistration->tagExport();
             }
             print date(parent::getDateFormat(), $objRegistration->firstExport) . $this->csvSeparator;
             if ($objRegistration->type == '1') {
                 print $_ARRAYLANG['TXT_CALENDAR_REG_REGISTRATION'] . $this->csvSeparator;
             } else {
                 if ($objRegistration->type == '2') {
                     print $_ARRAYLANG['TXT_CALENDAR_WAITLIST'] . $this->csvSeparator;
                 } else {
                     print $_ARRAYLANG['TXT_CALENDAR_REG_SIGNOFF'] . $this->csvSeparator;
                 }
             }
             print html_entity_decode($objEvent->title, ENT_QUOTES) . " - " . date(parent::getDateFormat(), $objRegistration->eventDate) . $this->csvSeparator;
             if ($objRegistration->langId == null) {
                 print $this->arrFrontendLanguages[$_LANGID]['name'] . $this->csvSeparator;
             } else {
                 print $this->arrFrontendLanguages[$objRegistration->langId]['name'] . $this->csvSeparator;
             }
             foreach ($objRegistration->fields as $id => $arrField) {
                 $output = array();
                 switch ($arrField['type']) {
                     case 'inputtext':
                     case 'mail':
                     case 'textarea':
                     case 'seating':
                     case 'firstname':
                     case 'lastname':
                         print self::escapeCsvValue(html_entity_decode($arrField['value'], ENT_QUOTES)) . $this->csvSeparator;
                         break;
                     case 'salutation':
                     case 'select':
                     case 'radio':
                     case 'checkbox':
                         $options = explode(",", $arrField['default']);
                         $values = explode(",", $arrField['value']);
                         foreach ($values as $key => $value) {
                             $arrValue = explode('[[', $value);
                             $value = $arrValue[0];
                             $input = str_replace(']]', '', $arrValue[1]);
                             if (!empty($input)) {
                                 $arrOption = explode('[[', $options[$value - 1]);
                                 $output[] = $arrOption[0] . ": " . $input;
                             } else {
                                 if ($options[0] == '' && $value == 1) {
                                     $options[$value - 1] = '1';
                                 }
                                 $output[] = $options[$value - 1];
                             }
                         }
                         print html_entity_decode(self::escapeCsvValue(join(", ", $output)), ENT_QUOTES) . $this->csvSeparator;
                         break;
                     case 'agb':
                         print ($arrField['value'] ? $_ARRAYLANG["TXT_{$this->moduleLangVar}_YES"] : $_ARRAYLANG["TXT_{$this->moduleLangVar}_NO"]) . $this->csvSeparator;
                         break;
                 }
             }
             print "\r\n";
         }
         exit;
     } else {
         \Cx\Core\Csrf\Controller\Csrf::header("Location: index.php?cmd=" . $this->moduleName);
         return;
     }
 }