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