/** * @param Newsletter $newsletter * @param Mailinglists $recipient * @param Boolean $fakeRecipient */ function __construct($newsletter, $recipient, $fakeRecipient = false) { $this->newsletter = $newsletter; $this->mailinglists = $newsletter->MailingLists(); $this->recipient = $recipient; $this->fakeRecipient = $fakeRecipient; parent::__construct($this->newsletter->SendFrom, $this->recipient->Email); $this->populateTemplate(new ArrayData(array('UnsubscribeLink' => $this->UnsubscribeLink(), 'SiteConfig' => DataObject::get_one('SiteConfig'), 'AbsoluteBaseURL' => Director::absoluteBaseURLWithAuth()))); $this->body = $newsletter->getContentBody(); $this->subject = $newsletter->Subject; $this->ss_template = $newsletter->RenderTemplate; if ($this->body && $this->newsletter) { $text = $this->body->forTemplate(); //Recipient Fields ShortCode parsing $bodyViewer = new SSViewer_FromString($text); $text = $bodyViewer->process($this->templateData()); // Install link tracking by replacing existing links with "newsletterlink" and hash-based reference. if ($this->config()->link_tracking_enabled && !$this->fakeRecipient && preg_match_all("/<a\\s[^>]*href=\"([^\"]*)\"[^>]*>(.*)<\\/a>/siU", $text, $matches)) { if (isset($matches[1]) && ($links = $matches[1])) { $titles = isset($matches[2]) ? $matches[2] : array(); $id = (int) $this->newsletter->ID; $replacements = array(); $current = array(); // workaround as we want to match the longest urls (/foo/bar/baz) before /foo/ array_unique($links); $sorted = array_combine($links, array_map('strlen', $links)); arsort($sorted); foreach ($sorted as $link => $length) { $SQL_link = Convert::raw2sql($link); $tracked = DataObject::get_one('Newsletter_TrackedLink', "\"NewsletterID\" = '" . $id . "' AND \"Original\" = '" . $SQL_link . "'"); if (!$tracked) { // make one. $tracked = new Newsletter_TrackedLink(); $tracked->Original = $link; $tracked->NewsletterID = $id; $tracked->write(); } // replace the link $replacements[$link] = $tracked->Link(); // track that this link is still active $current[] = $tracked->ID; } // replace the strings $text = str_ireplace(array_keys($replacements), array_values($replacements), $text); } } // replace the body $output = new HTMLText(); $output->setValue($text); $this->body = $output; } }
/** * Content * Parses Markdown to HTML. * * @return string Parsed HTML */ public function Content() { $content = $this->owner->dbObject('Content'); $strContent = $content; if (method_exists($content, 'forTemplate')) { $strContent = $content->forTemplate(); } $template = SSViewer_FromString::fromString($strContent); return $this->owner->renderWith($template); }
/** * Create a template from a string instead of a .ss file * * @param string $content The template content * @param bool|void $cacheTemplate Whether or not to cache the template from string * @return SSViewer */ public static function fromString($content, $cacheTemplate = null) { $viewer = new SSViewer_FromString($content); if ($cacheTemplate !== null) { $viewer->setCacheTemplate($cacheTemplate); } return $viewer; }
/** * display a text message after order creation * * @param Order $orderObj the order object * * @return void * * @author Sascha Koehler <*****@*****.**> * @copyright 2011 pixeltricks GmbH * @since 06.01.2011 */ public function processPaymentConfirmationText($orderObj) { parent::processPaymentConfirmationText($orderObj); $variables = array('SilvercartOrder' => $orderObj); $templateVariables = new ArrayData($variables); switch ($this->PaymentChannel) { case 'invoice': $textTemplate = new SSViewer_FromString($this->InvoiceInfo); break; case 'prepayment': default: $textTemplate = new SSViewer_FromString($this->TextBankAccountInfo); } $text = HTTP::absoluteURLs($textTemplate->process($templateVariables)); return $text; }
public function testLoopIteratorIterator() { $list = new PaginatedList(new ArrayList()); $viewer = new SSViewer_FromString('<% loop List %>$ID - $FirstName<br /><% end_loop %>'); $result = $viewer->process(new ArrayData(array('List' => $list))); $this->assertEquals($result, ''); }
public function testOpenBlockExtension() { $count = 0; $parser = new SSTemplateParser(); $parser->addOpenBlock('test', function (&$res) use(&$count) { $count++; }); $template = new SSViewer_FromString("<% test %>", $parser); $template->process(new SSViewerTestFixture()); $this->assertEquals(1, $count); }
/** * Load all the template variables into the internal variables, including * the template into body. Called before send() or debugSend() * $isPlain=true will cause the template to be ignored, otherwise the GenericEmail template will be used * and it won't be plain email :) * * This function is updated to rewrite urls in a safely manner and inline css. * It will also changed the requirements backend to avoid requiring stuff in the html. */ protected function parseVariables($isPlain = false) { $origState = Config::inst()->get('SSViewer', 'source_file_comments'); Config::inst()->update('SSViewer', 'source_file_comments', false); // Workaround to avoid clutter in our rendered html $backend = Requirements::backend(); Requirements::set_backend(new MandrillRequirementsBackend()); if (!$this->parseVariables_done) { $this->parseVariables_done = true; if (!$this->original_body) { $this->original_body = $this->body; } // Parse $ variables in the base parameters $data = $this->templateData(); // Process a .SS template file $fullBody = $this->original_body; if ($this->parse_body) { try { $viewer = new SSViewer_FromString($fullBody); $fullBody = $viewer->process($data); } catch (Exception $ex) { SS_Log::log($ex->getMessage(), SS_Log::DEBUG); } // Also parse the email title try { $viewer = new SSViewer_FromString($this->subject); $this->subject = $viewer->process($data); } catch (Exception $ex) { SS_Log::log($ex->getMessage(), SS_Log::DEBUG); } if ($this->callout) { try { $viewer = new SSViewer_FromString($this->callout); $this->callout = $viewer->process($data); } catch (Exception $ex) { SS_Log::log($ex->getMessage(), SS_Log::DEBUG); } } if ($this->sidebar) { try { $viewer = new SSViewer_FromString($this->sidebar); $this->sidebar = $viewer->process($data); } catch (Exception $ex) { SS_Log::log($ex->getMessage(), SS_Log::DEBUG); } } } if ($this->ss_template && !$isPlain) { // Requery data so that updated versions of To, From, Subject, etc are included $data = $this->templateData(); $template = new SSViewer($this->ss_template); if ($template->exists()) { // Make sure we included the parsed body into layout $data->setField('Body', $fullBody); try { $fullBody = $template->process($data); } catch (Exception $ex) { SS_Log::log($ex->getMessage(), SS_Log::DEBUG); } } } // Rewrite relative URLs $this->body = self::rewriteURLs($fullBody); } Config::inst()->update('SSViewer', 'source_file_comments', $origState); Requirements::set_backend($backend); return $this; }
/** * Overloaded from {@link Widget->Content()} * to allow for controller/form linking. * * @return string HTML * * @author Sebastian Diel <*****@*****.**> * @since 20.06.2013 */ public function Content() { $renderData = array('Controller' => $this); $template = new SSViewer_FromString($this->getField('FreeText')); $freeText = HTTP::absoluteURLs($template->process(new ArrayData($renderData))); $data = new ArrayData(array('FreeText' => $freeText, 'LinkText' => $this->LinkText, 'Link' => $this->Link)); return $this->customise($data)->renderWith($this->widget->class); }
/** * sends email to defined address * * @param string $identifier identifier for email template * @param string $to recipients email address * @param array $variables array with template variables that can be called in the template * @param array $attachments absolute filename to an attachment file * * @return bool * * @author Sebastian Diel <*****@*****.**>, * Sascha Koehler <*****@*****.**> * @since 16.06.2014 */ public static function send($identifier, $to, $variables = array(), $attachments = null) { $mailObj = SilvercartShopEmail::get()->filter('Identifier', $identifier)->first(); if (!$mailObj) { return false; } $emailText = trim($mailObj->EmailText); if (is_null($emailText) || empty($emailText)) { return false; } $emailSubject = trim($mailObj->Subject); if (is_null($emailSubject) || empty($emailSubject)) { return false; } if (!is_array($variables)) { $variables = array(); } $templateVariables = new ArrayData($variables); $emailTextTemplate = new SSViewer_FromString($mailObj->EmailText); $emailText = HTTP::absoluteURLs($emailTextTemplate->process($templateVariables)); $emailSubjectTemplate = new SSViewer_FromString($mailObj->Subject); $emailSubject = HTTP::absoluteURLs($emailSubjectTemplate->process($templateVariables)); $email = new Email(SilvercartConfig::EmailSender(), $to, $emailSubject, $mailObj->EmailText); $email->setTemplate('SilvercartShopEmail'); $email->populateTemplate(array('ShopEmailSubject' => $emailSubject, 'ShopEmailMessage' => $emailText)); self::attachFiles($email, $attachments); $email->send(); if (SilvercartConfig::GlobalEmailRecipient() != '') { $email = new Email(SilvercartConfig::EmailSender(), SilvercartConfig::GlobalEmailRecipient(), $emailSubject, $mailObj->EmailText); $email->setTemplate('SilvercartShopEmail'); $email->populateTemplate(array('ShopEmailSubject' => $emailSubject, 'ShopEmailMessage' => $emailText)); $email->send(); } //Send the email to additional standard receipients from the n:m //relation AdditionalReceipients; //Email address is validated. if ($mailObj->AdditionalReceipients()->exists()) { foreach ($mailObj->AdditionalReceipients() as $additionalReceipient) { if ($additionalReceipient->getEmailAddressWithName() && Email::validEmailAddress($additionalReceipient->Email)) { $to = $additionalReceipient->getEmailAddressWithName(); } elseif ($additionalReceipient->getEmailAddress() && Email::validEmailAddress($additionalReceipient->Email)) { $to = $additionalReceipient->getEmailAddress(); } else { continue; } $email = new Email(SilvercartConfig::EmailSender(), $to, $emailSubject, $mailObj->EmailText); $email->setTemplate('SilvercartShopEmail'); $email->populateTemplate(array('ShopEmailSubject' => $emailSubject, 'ShopEmailMessage' => $emailText)); self::attachFiles($email, $attachments); $email->send(); } } }
/** * Format text with given keywords etc * * @param sting $text * @param DataObject $context * @param Member $user * @param array $extraData * @return $string */ public function format($text, $context, $user = null, $extraData = array()) { $data = $this->getTemplateData($context, $user, $extraData); // render $viewer = new SSViewer_FromString($text); try { $string = $viewer->process($data); } catch (Exception $e) { $string = $text; } return $string; }