Guest User

grtrrt

a guest
Mar 23rd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. <?php
  2. /**
  3. * Mirasvit
  4. *
  5. * This source file is subject to the Mirasvit Software License, which is available at http://mirasvit.com/license/.
  6. * Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
  7. * If you wish to customize this module for your needs.
  8. * Please refer to http://www.magentocommerce.com for more information.
  9. *
  10. * @category Mirasvit
  11. * @package Follow Up Email
  12. * @version 1.0.2
  13. * @build 441
  14. * @copyright Copyright (C) 2015 Mirasvit (http://mirasvit.com/)
  15. */
  16.  
  17.  
  18. class Mirasvit_EmailDesign_Model_Template extends Mage_Core_Model_Abstract
  19. {
  20. protected $_areas = null;
  21. protected $_design = null;
  22.  
  23. protected function _construct()
  24. {
  25. $this->_init('emaildesign/template');
  26. }
  27.  
  28. public function getAreas()
  29. {
  30. if ($this->_areas == null) {
  31. if ($this->getDesign()) {
  32. $this->_areas = $this->getDesign()->getAreas();
  33. } else {
  34. $this->_areas = array('content' => 'Content');
  35. }
  36. }
  37.  
  38. return $this->_areas;
  39. }
  40.  
  41. public function setDesign($design)
  42. {
  43. $this->_design = $design;
  44.  
  45. return $this;
  46. }
  47.  
  48. public function getDesign()
  49. {
  50. if ($this->_design == null && $this->getDesignId()) {
  51. $this->_design = Mage::getModel('emaildesign/design')->load($this->getDesignId());
  52. }
  53.  
  54. return $this->_design;
  55. }
  56.  
  57. public function getAreaContent($code)
  58. {
  59. $areas = $this->getAreasContent();
  60. if (isset($areas[$code])) {
  61. return $areas[$code];
  62. }
  63.  
  64. return false;
  65. }
  66.  
  67. public function setAreaContent($code, $content)
  68. {
  69. $areas = $this->getAreasContent();
  70. $areas[$code] = $content;
  71. $this->setAreasContent($areas);
  72.  
  73. return $this;
  74. }
  75.  
  76. public function getPreviewSubject()
  77. {
  78. $variables = Mage::helper('email/event')->getRandomEventArgs();
  79. $variables['preview'] = true;
  80.  
  81. $appEmulation = Mage::getSingleton('core/app_emulation');
  82. $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($variables['store_id']);
  83.  
  84. $result = $this->getProcessedTemplateSubject($variables);
  85.  
  86. $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
  87.  
  88. return $result;
  89. }
  90.  
  91. public function getPreviewContent()
  92. {
  93. $variables = Mage::helper('email/event')->getRandomEventArgs();
  94. $variables['preview'] = true;
  95.  
  96. $appEmulation = Mage::getSingleton('core/app_emulation');
  97. $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($variables['store_id']);
  98.  
  99. $result = $this->getProcessedTemplate($variables);
  100.  
  101. $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
  102.  
  103. if ($this->getDesign()->getTemplateType() == Mirasvit_EmailDesign_Model_Design::TEMPLATE_TYPE_TEXT) {
  104. $result = nl2br($result);
  105. }
  106.  
  107. return $result;
  108. }
  109.  
  110. public function getProcessedTemplate($variables = null)
  111. {
  112. $tpl = $this->getDesign()->getTemplate();
  113.  
  114. $result = $this->_render($tpl, $variables);
  115.  
  116. if ($this->getDesign()->getTemplateType() == Mirasvit_EmailDesign_Model_Design::TEMPLATE_TYPE_HTML) {
  117. $result = Mage::helper('emaildesign')->styleHtml($result);
  118. }
  119.  
  120. return $result;
  121. }
  122.  
  123. public function getProcessedTemplateSubject($variables = null)
  124. {
  125. return $this->_render($this->getSubject(), $variables);
  126. }
  127.  
  128. protected function _render($tpl, $variables = null)
  129. {
  130. if (!is_array($variables)) {
  131. $variables = array();
  132. }
  133.  
  134. foreach ($this->getAreasContent() as $area => $content) {
  135. $variables['area_'.$area] = $content;
  136. }
  137.  
  138. $block = Mage::app()->getLayout()->createBlock('emaildesign/template');
  139.  
  140. $result = $block->render($tpl, $variables);
  141.  
  142. return $result;
  143. }
  144.  
  145. public function export()
  146. {
  147. $this->setAreasContent64(base64_encode(serialize($this->getAreasContent())));
  148. $this->setDesignTitle($this->getDesign()->getTitle());
  149.  
  150. $xml = $this->toXml(array('title', 'description', 'subject', 'design_title', 'areas_content64'));
  151.  
  152. $path = Mage::getSingleton('emaildesign/config')->getTemplatePath().DS.$this->getTitle().'.xml';
  153.  
  154. file_put_contents($path, $xml);
  155.  
  156. return $path;
  157. }
  158.  
  159. public function import($path)
  160. {
  161. $content = file_get_contents($path);
  162. $xml = new Varien_Simplexml_Element($content);
  163. $template = $xml->asArray();
  164.  
  165. $template['areas_content'] = base64_decode($template['areas_content64']);
  166.  
  167. $model = $this->getCollection()
  168. ->addFieldToFilter('title', $template['title'])
  169. ->getFirstItem();
  170. $model->addData($template);
  171.  
  172. $design = Mage::getModel('emaildesign/design')->getCollection()
  173. ->addFieldToFilter('title', $template['design_title'])
  174. ->getFirstItem();
  175.  
  176. $model->setDesignId($design->getId())
  177. ->save();
  178.  
  179. return $model;
  180. }
  181. }
Add Comment
Please, Sign In to add comment