/**
  * Generate the newsletter text.
  *
  * @return mixed
  * @throws Exception
  */
 public function generateText()
 {
     if (!$this->theme instanceof AvisotaNewsletterTheme) {
         throw new Exception('A newsletter need a theme!');
     }
     AvisotaStatic::pushNewsletter($this);
     $template = new AvisotaNewsletterTemplate($this->theme->getHtmlTemplate());
     $template->title = $this->subject;
     foreach ($this->theme->getAreas() as $areaName) {
         $template->{$areaName} = $this->generateContentText($areaName);
     }
     $template->newsletter = $this;
     $buffer = $template->parse();
     // reset static information
     AvisotaStatic::popNewsletter();
     return $buffer;
 }
 /**
  * Send a reminder to the given mailing lists
  * or all unconfirmed, not reminded mailing lists, the recipient has subscribed.
  *
  * @param array|null $listIds
  *
  * @throws AvisotaSubscriptionException
  */
 public function sendRemind(array $listIds = null, $force = false)
 {
     if (!$GLOBALS['TL_CONFIG']['avisota_send_notification']) {
         return false;
     }
     if (!$this->id) {
         throw new AvisotaSubscriptionException($this, 'This recipient has no ID!');
     }
     if ($listIds !== null) {
         $listIds = array_filter(array_map('intval', $listIds));
         if (!count($listIds)) {
             return false;
         }
     }
     $this->loadLanguageFile('avisota_subscription');
     $time = time();
     $reminderTime = $GLOBALS['TL_CONFIG']['avisota_notification_time'] * 24 * 60 * 60;
     $list = \Database::getInstance()->prepare("SELECT l.* FROM orm_avisota_recipient_to_mailing_list t\n\t\t\t\t\t   INNER JOIN orm_avisota_mailing_list l\n\t\t\t\t\t   ON l.id=t.list\n\t\t\t\t\t   WHERE t.recipient=? AND t.confirmed=?" . ($force ? '' : "AND t.confirmationSent>0\n\t\t\t\t\t     AND (\n\t\t\t\t\t         (t.reminderSent=0 AND UNIX_TIMESTAMP()-t.confirmationSent>?)\n\t\t\t\t\t       OR\n\t\t\t\t\t         (t.reminderSent>0 AND UNIT_TIMESTAMP()-t.reminderSent>(?+(?*t.reminderCount/2) AND t.reminderCount<?)\n\t\t\t\t\t     )") . ($listIds !== null ? " AND t.list IN (" . implode(',', $listIds) . ")" : '') . "ORDER BY l.title")->execute($this->id, '', $reminderTime, $reminderTime, $reminderTime, $GLOBALS['TL_CONFIG']['avisota_notification_count']);
     $listsByPage = array();
     while ($list->next()) {
         $listData = $list->row();
         // generate a token
         if (empty($listData['token'])) {
             $listData['token'] = substr(md5(mt_rand() . '-' . $this->id . '-' . $list->id . '-' . $this->email . '-' . time()), 0, 8);
         }
         // set send time
         $listData['reminderSent'] = $time;
         $pageId = $list->integratedRecipientManageSubscriptionPage ? $list->integratedRecipientManageSubscriptionPage : $GLOBALS['objPage']->id;
         $listsByPage[$pageId][$list->id] = $listData;
     }
     foreach ($listsByPage as $pageId => $lists) {
         $page = $this->getPageDetails($pageId);
         $titles = array();
         $tokens = array();
         foreach ($lists as $listData) {
             $titles[] = $listData['title'];
             $tokens[] = $listData['token'];
         }
         $url = $this->generateFrontendUrl($page->row()) . '?subscribetoken=' . implode(',', $tokens);
         $plainTemplate = new AvisotaNewsletterTemplate($GLOBALS['TL_CONFIG']['avisota_template_notification_mail_plain']);
         $plainTemplate->content = sprintf($GLOBALS['TL_LANG']['avisota_subscription']['notification']['plain'], implode(', ', $titles), $url);
         $htmlTemplate = new AvisotaNewsletterTemplate($GLOBALS['TL_CONFIG']['avisota_template_notification_mail_html']);
         $htmlTemplate->title = $GLOBALS['TL_LANG']['avisota']['subscribe']['subject'];
         $htmlTemplate->content = sprintf($GLOBALS['TL_LANG']['avisota_subscription']['notification']['html'], implode(', ', $titles), $url);
         $email = new Mail();
         $email->setSubject($GLOBALS['TL_LANG']['avisota_subscription']['notification']['subject']);
         $email->setText($plainTemplate->parse());
         $email->setHtml($htmlTemplate->parse());
         $transport = AvisotaTransport::getTransportModule();
         $transport->transportEmail($this->email, $email);
         foreach ($lists as $listData) {
             $this->log('Send subscription reminder for recipient ' . $this->email . ' in mailing list "' . $listData['title'] . '"', 'AvisotaIntegratedRecipient::sendRemind', TL_INFO);
             \Database::getInstance()->prepare("UPDATE orm_avisota_recipient_to_mailing_list SET reminderSent=?, reminderCount=reminderCount+1, token=? WHERE recipient=? AND list=?")->execute($listData['reminderSent'], $listData['token'], $this->id, $listData['id']);
         }
     }
     // HOOK: add custom logic
     if (isset($GLOBALS['TL_HOOKS']['avisotaIntegratedRecipientSendSubscriptionReminder']) && is_array($GLOBALS['TL_HOOKS']['avisotaIntegratedRecipientSendSubscriptionReminder'])) {
         foreach ($GLOBALS['TL_HOOKS']['avisotaIntegratedRecipientSendSubscriptionReminder'] as $callback) {
             $this->import($callback[0]);
             $this->{$callback}[0]->{$callback}[1]($this, $listsByPage);
         }
     }
     return $listsByPage;
 }
 /**
  * Compile the current element
  */
 protected function compile($mode)
 {
     $images = array();
     $auxDate = array();
     // Get all images
     foreach ($this->multiSRC as $pathname) {
         if (isset($images[$pathname]) || !file_exists(TL_ROOT . '/' . $pathname)) {
             continue;
         }
         // Single files
         if (is_file(TL_ROOT . '/' . $pathname)) {
             $file = new File($pathname);
             $this->parseMetaFile(dirname($pathname), true);
             $metaData = $this->arrMeta[$file->basename];
             if ($metaData[0] == '') {
                 $metaData[0] = str_replace('_', ' ', preg_replace('/^[0-9]+_/', '', $file->filename));
             }
             if ($file->isGdImage) {
                 $images[$pathname] = array('name' => $file->basename, 'singleSRC' => $pathname, 'alt' => $metaData[0], 'imageUrl' => $metaData[1], 'caption' => $metaData[2]);
                 $auxDate[] = $file->mtime;
             }
             continue;
         }
         $subfiles = scan(TL_ROOT . '/' . $pathname);
         $this->parseMetaFile($pathname);
         // Folders
         foreach ($subfiles as $subfile) {
             if (is_dir(TL_ROOT . '/' . $pathname . '/' . $subfile)) {
                 continue;
             }
             $file = new File($pathname . '/' . $subfile);
             if ($file->isGdImage) {
                 $metaData = $this->arrMeta[$subfile];
                 if ($metaData[0] == '') {
                     $metaData[0] = str_replace('_', ' ', preg_replace('/^[0-9]+_/', '', $file->filename));
                 }
                 $images[$pathname . '/' . $subfile] = array('name' => $file->basename, 'singleSRC' => $pathname . '/' . $subfile, 'alt' => $metaData[0], 'imageUrl' => $metaData[1], 'caption' => $metaData[2]);
                 $auxDate[] = $file->mtime;
             }
         }
     }
     // Sort array
     switch ($this->sortBy) {
         default:
         case 'name_asc':
             uksort($images, 'basename_natcasecmp');
             break;
         case 'name_desc':
             uksort($images, 'basename_natcasercmp');
             break;
         case 'date_asc':
             array_multisort($images, SORT_NUMERIC, $auxDate, SORT_ASC);
             break;
         case 'date_desc':
             array_multisort($images, SORT_NUMERIC, $auxDate, SORT_DESC);
             break;
         case 'meta':
             $images = array();
             foreach ($this->arrAux as $k) {
                 if (strlen($k)) {
                     $images[] = $images[$k];
                 }
             }
             break;
         case 'random':
             shuffle($images);
             break;
     }
     $images = array_values($images);
     $total = count($images);
     $limit = $total;
     $offset = 0;
     $rowcount = 0;
     if (!$this->perRow) {
         $this->perRow = 1;
     }
     $colwidth = floor(100 / $this->perRow);
     $maxImageWidth = TL_MODE == 'BE' ? floor(640 / $this->perRow) : floor($GLOBALS['TL_CONFIG']['maxImageWidth'] / $this->perRow);
     $body = array();
     // Rows
     for ($i = $offset; $i < $limit; $i = $i + $this->perRow) {
         $class_tr = '';
         if ($rowcount == 0) {
             $class_tr = ' row_first';
         }
         if ($i + $this->perRow >= count($images)) {
             $class_tr = ' row_last';
         }
         $class_eo = $rowcount % 2 == 0 ? ' even' : ' odd';
         // Columns
         for ($j = 0; $j < $this->perRow; $j++) {
             $class_td = '';
             if ($j == 0) {
                 $class_td = ' col_first';
             }
             if ($j == $this->perRow - 1) {
                 $class_td = ' col_last';
             }
             $cell = new stdClass();
             $key = 'row_' . $rowcount . $class_tr . $class_eo;
             // Empty cell
             if (!is_array($images[$i + $j]) || $j + $i >= $limit) {
                 $cell->class = 'col_' . $j . $class_td;
                 $body[$key][$j] = $cell;
                 continue;
             }
             // Add size and margin
             $images[$i + $j]['size'] = $this->size;
             $images[$i + $j]['imagemargin'] = $this->imagemargin;
             $images[$i + $j]['fullsize'] = $this->fullsize;
             $this->addImageToTemplate($cell, $images[$i + $j], $maxImageWidth);
             // Add column width and class
             $cell->colWidth = $colwidth . '%';
             $cell->class = 'col_' . $j . $class_td;
             $body[$key][$j] = $cell;
         }
         ++$rowcount;
     }
     switch ($mode) {
         case NL_HTML:
             $templateName = 'nl_gallery_default_html';
             // Use a custom template
             if (TL_MODE == 'NL' && $this->galleryHtmlTpl != '') {
                 $templateName = $this->galleryHtmlTpl;
             }
             break;
         case NL_PLAIN:
             $templateName = 'nl_gallery_default_plain';
             // Use a custom template
             if (TL_MODE == 'NL' && $this->galleryPlainTpl != '') {
                 $templateName = $this->galleryPlainTpl;
             }
     }
     $template = new AvisotaNewsletterTemplate($templateName);
     $template->body = $body;
     $template->headline = $this->headline;
     $this->Template->images = $template->parse();
 }