/** * Build Newsletter Content * * @param object $campaign Campaign Object * @param boolean $stripHtmlAndBodyTags Strip out <html> & <body> tags? * @return string Campaign HTML */ public function buildNewsletter($campaign, $stripHtmlAndBodyTags = false) { //are we overriding content with template vs using stories? if ($campaign->template == '-1') { $campaignTemplate = $campaign->html_content; $campaignPrimaryStories = ''; $campaignSecondaryStories = ''; } else { //instantiate objects $newsletterTemplate = new Template($this->_db); $newsletterPrimaryStory = new PrimaryStory($this->_db); $newsletterSecondaryStory = new SecondaryStory($this->_db); //get the campaign template $newsletterTemplate->load($campaign->template); $campaignTemplate = $newsletterTemplate->template; //get primary & secondary colors $primaryTitleColor = $newsletterTemplate->primary_title_color ? $newsletterTemplate->primary_title_color : '#000000'; if (strlen($primaryTitleColor) <= 7 && !strstr($primaryTitleColor, ';')) { $primaryTitleColor = 'font-size:20px;font-weight:bold;color:' . $primaryTitleColor . ';font-family:arial;line-height:100%;margin-bottom:10px;'; } $primaryTextColor = $newsletterTemplate->primary_text_color ? $newsletterTemplate->primary_text_color : '#444444'; if (strlen($primaryTextColor) <= 7 && !strstr($primaryTextColor, ';')) { $primaryTextColor = 'font-size:14px;font-weight:normal;color:' . $primaryTextColor . ';font-family:arial;margin-bottom:50px;'; } $secondaryTitleColor = $newsletterTemplate->secondary_title_color ? $newsletterTemplate->secondary_title_color : '#666666'; if (strlen($secondaryTitleColor) <= 7 && !strstr($secondaryTitleColor, ';')) { $secondaryTitleColor = 'font-size:15px;font-weight:bold;color:' . $secondaryTitleColor . ';font-family:arial;line-height:150%;'; } $secondaryTextColor = $newsletterTemplate->secondary_text_color ? $newsletterTemplate->secondary_text_color : '#999999'; if (strlen($secondaryTextColor) <= 7 && !strstr($secondaryTextColor, ';')) { $secondaryTextColor = 'font-size:12px;color:' . $secondaryTextColor . ';font-family:arial;'; } //get and format primary stories $campaignPrimaryStories = ''; $primaryStories = $newsletterPrimaryStory->getStories($campaign->id); foreach ($primaryStories as $pStory) { $campaignPrimaryStories .= '<span style="display:block;page-break-inside:avoid;' . $primaryTitleColor . '">'; $campaignPrimaryStories .= $pStory->title; $campaignPrimaryStories .= '</span>'; $campaignPrimaryStories .= '<span style="display:block;page-break-inside:avoid;' . $primaryTextColor . '">'; $campaignPrimaryStories .= $pStory->story; //do we have a readmore link if ($pStory->readmore_link) { $readmore_title = $pStory->readmore_title ? $pStory->readmore_title : 'Read More ›'; $campaignPrimaryStories .= "<br /><br /><a href=\"{$pStory->readmore_link}\" target=\"\">{$readmore_title}</a>"; } $campaignPrimaryStories .= '</span>'; } //get secondary stories $campaignSecondaryStories = '<br /><br />'; $secondaryStories = $newsletterSecondaryStory->getStories($campaign->id); foreach ($secondaryStories as $sStory) { $campaignSecondaryStories .= '<span style="display:block;page-break-inside:avoid;' . $secondaryTitleColor . '">'; $campaignSecondaryStories .= $sStory->title; $campaignSecondaryStories .= '</span>'; $campaignSecondaryStories .= '<span style="display:block;page-break-inside:avoid;' . $secondaryTextColor . '">'; $campaignSecondaryStories .= $sStory->story; //do we have a readmore link if ($sStory->readmore_link) { $readmore_title = $sStory->readmore_title ? $sStory->readmore_title : 'Read More ›'; $campaignSecondaryStories .= "<br /><br /><a href=\"{$sStory->readmore_link}\" target=\"\">{$readmore_title}</a>"; } $campaignSecondaryStories .= '</span>'; $campaignSecondaryStories .= '<br /><br />'; } } //get the hub $hub = $_SERVER['SERVER_NAME']; //build link to newsletters for email $link = 'https://' . $hub . '/newsletter/' . $campaign->alias; //replace placeholders in template $campaignParsed = str_replace("{{LINK}}", $link, $campaignTemplate); $campaignParsed = str_replace("{{ALIAS}}", $campaign->alias, $campaignParsed); $campaignParsed = str_replace("{{TITLE}}", $campaign->name, $campaignParsed); $campaignParsed = str_replace("{{ISSUE}}", $campaign->issue, $campaignParsed); // Handle the AUTOGEN sections if (preg_match_all("(\\{.*?\\}\\})", $campaignPrimaryStories, $matches) !== FALSE) { foreach ($matches[0] as &$match) { // A field to hold HTML content for the section $html = ''; // Hold onto the original token $originMatch = $match; // Perform some cleanup, stripping $match = ltrim($match, "{{"); $match = rtrim($match, "}}"); // Explode on the delimiter $parts = explode("_", $match); // Make sure we're doing "it" on the right token if ($parts[0] == "AUTOGEN") { // Get the content $enabledPlugins = Event::trigger('newsletter.onGetEnabledDigests'); // Ascertain the key, based on plugin ordering $key = array_keys($enabledPlugins, strtolower($parts[1]))[0]; // Get the content for the desired plugin $content = Event::trigger('newsletter.onGetLatest', array($parts[2]))[$key]; // Apply the view template $view = new \Hubzero\Component\View(array()); // Written emphatically, set the paths and whatnot $view->setName('storytemplates'); $view->setLayout(strtolower($parts[3])); $view->setBasePath(PATH_CORE . DS . 'components' . DS . 'com_newsletter' . DS . 'admin'); // Pass the object through to the view $view->object = $content; $view->addTemplatePath(PATH_CORE . DS . 'components' . DS . 'com_newsletter' . DS . 'admin' . DS . 'views' . DS . 'storytemplates' . DS . 'tmpl'); // Oh, what's this a buffer hijack? ob_start(); // Render the view within the buffer. $view->display(); // Grab the buffer's content $html = ob_get_contents(); // Clear and close the buffer ob_end_clean(); // Do some string replacement on the original token. $campaignPrimaryStories = str_replace($originMatch, $html, $campaignPrimaryStories); } } } $campaignParsed = str_replace("{{PRIMARY_STORIES}}", $campaignPrimaryStories, $campaignParsed); $campaignParsed = str_replace("{{SECONDARY_STORIES}}", $campaignSecondaryStories, $campaignParsed); $campaignParsed = str_replace("{{COPYRIGHT}}", date("Y"), $campaignParsed); $campaignParsed = str_replace('src="/site', 'src="' . substr(PATH_APP, strlen(PATH_ROOT)) . '/site', $campaignParsed); //replace .org, .com., .net, .edu 's // if ($campaign->type == 'html') // { // $campaignParsed = str_replace(".org", "​.org", $campaignParsed); // $campaignParsed = str_replace(".com", "​.com", $campaignParsed); // $campaignParsed = str_replace(".net", "​.net", $campaignParsed); // $campaignParsed = str_replace(".edu", "​.edu", $campaignParsed); // } //do we want to strip <html> & <body> tags if ($stripHtmlAndBodyTags) { $campaignParsed = preg_replace('/<html[^>]*>/', '', $campaignParsed); $campaignParsed = preg_replace('/<body[^>]*>/', '', $campaignParsed); $campaignParsed = str_replace('</body>', '', $campaignParsed); $campaignParsed = str_replace('</html>', '', $campaignParsed); } return $campaignParsed; }