*/ abstract class Mage_Core_Model_Email_Template_Abstract extends Mage_Core_Model_Template { const XML_PATH_DESIGN_EMAIL_LOGO = 'design/email/logo'; const XML_PATH_DESIGN_EMAIL_LOGO_ALT = 'design/email/logo_alt'; const XML_PATH_DESIGN_EMAIL_LOGO_WIDTH = 'design/email/logo_width'; const XML_PATH_DESIGN_EMAIL_LOGO_HEIGHT = 'design/email/logo_height'; const XML_PATH_CSS_NON_INLINE_FILES = 'design/email/css_non_inline'; protected $_cssFileCache = array(); /** * Get template code for template directive * * @param string $configPath * @param array $variables * @return string */ public function getTemplateByConfigPath($configPath, array $variables) { $template = Mage::getModel('core/email_template'); $template->loadByConfigPath($configPath, $variables); return $template->getProcessedTemplate($variables); } /** * Load template by configuration path. This enables html templates to include other html templates by their * system configuration XPATH value * * @param string $configPath The path to the config setting that defines which global/template/email/* node * should be used to load the email template * @return Mage_Core_Model_Email_Template */ public function loadByConfigPath($configPath) { $templateId = Mage::getStoreConfig($configPath); if (is_numeric($templateId)) { // Template was overridden in admin, so load template from database $this->load($templateId); } else { $defaultTemplates = Mage_Core_Model_Email_Template::getDefaultTemplates(); if (!isset($defaultTemplates[$templateId])) { return null; } $storeId = $this->getDesignConfig()->getStore(); $data = &$defaultTemplates[$templateId]; $this->setTemplateType($data['type']=='html' ? self::TYPE_HTML : self::TYPE_TEXT); $localeCode = Mage::getStoreConfig('general/locale/code', $storeId); $templateText = Mage::app()->getTranslator()->getTemplateFile( $data['file'], 'email', $localeCode ); $this->setTemplateText($templateText); $this->setId($templateId); } // Templates loaded via the {{template config_path=""}} syntax don't support the subject/vars/styles // comment blocks, so strip them out $templateText = preg_replace('//us', '', $this->getTemplateText()); // Remove comment lines $templateText = preg_replace('#\{\*.*\*\}#suU', '', $templateText); $this->setTemplateText($templateText); return $this; } /** * Return logo URL for emails * Take logo from skin if custom logo is undefined * * @param Mage_Core_Model_Store|int|string $store * @return string */ protected function _getLogoUrl($store) { $store = Mage::app()->getStore($store); $fileName = $store->getConfig(self::XML_PATH_DESIGN_EMAIL_LOGO); if ($fileName) { $uploadDir = Mage_Adminhtml_Model_System_Config_Backend_Email_Logo::UPLOAD_DIR; $fullFileName = Mage::getBaseDir('media') . DS . $uploadDir . DS . $fileName; if (file_exists($fullFileName)) { return Mage::getBaseUrl('media') . $uploadDir . '/' . $fileName; } } return Mage::getDesign()->getSkinUrl('images/logo_email.gif'); } /** * Return logo alt for emails * * @param Mage_Core_Model_Store|int|string $store * @return string */ protected function _getLogoAlt($store) { $store = Mage::app()->getStore($store); $alt = $store->getConfig(self::XML_PATH_DESIGN_EMAIL_LOGO_ALT); if ($alt) { return $alt; } return $store->getFrontendName(); } /** * Add variables that are used by transactional emails and newsletter emails * * @param $variables * @param $storeId * @return mixed */ protected function _addEmailVariables($variables, $storeId) { if (!isset($variables['store'])) { $store = Mage::app()->getStore($storeId); $variables['store'] = $store; } if (!isset($variables['logo_url'])) { $variables['logo_url'] = $this->_getLogoUrl($storeId); } if (!isset($variables['logo_alt'])) { $variables['logo_alt'] = $this->_getLogoAlt($storeId); } if (!isset($variables['logo_width'])) { $variables['logo_width'] = Mage::getStoreConfig( self::XML_PATH_DESIGN_EMAIL_LOGO_WIDTH, $storeId ); } if (!isset($variables['logo_height'])) { $variables['logo_height'] = Mage::getStoreConfig( self::XML_PATH_DESIGN_EMAIL_LOGO_HEIGHT, $storeId ); } if (!isset($variables['store_phone'])) { $variables['store_phone'] = Mage::getStoreConfig( Mage_Core_Model_Store::XML_PATH_STORE_STORE_PHONE, $storeId ); } if (!isset($variables['store_hours'])) { $variables['store_hours'] = Mage::getStoreConfig( Mage_Core_Model_Store::XML_PATH_STORE_STORE_HOURS, $storeId ); } if (!isset($variables['store_email'])) { $variables['store_email'] = Mage::getStoreConfig( Mage_Customer_Helper_Data::XML_PATH_SUPPORT_EMAIL, $storeId ); } // If template is text mode, don't include styles if (!$this->isPlain()) { $variables['non_inline_styles'] = $this->_getNonInlineCssTag(); } return $variables; } /** * Merge HTML and CSS and returns HTML that has CSS styles applied "inline" to the HTML tags. This is necessary * in order to support all email clients. * * @param $html * @return string */ protected function _applyInlineCss($html) { try { // Check to see if the {{inlinecss file=""}} directive set a CSS file to inline $inlineCssFile = $this->getInlineCssFile(); // Only run Emogrify if HTML exists if (strlen($html) && $inlineCssFile) { $cssToInline = $this->_getCssFileContent($inlineCssFile); $emogrifier = new Pelago_Emogrifier(); $emogrifier->setHtml($html); $emogrifier->setCss($cssToInline); // Don't parse inline \n"; // Load the no-inline CSS styles from theme so they can be included in the style tag $styleTagContent = $this->_getCssByConfig(self::XML_PATH_CSS_NON_INLINE_FILES); // Load the CSS that is included in the comment or is added via Transactional Emails in admin $styleTagContent .= $this->getTemplateStyles(); return sprintf($styleTagWrapper, $styleTagContent); } }