-
Notifications
You must be signed in to change notification settings - Fork 36
/
NewsletterTemplatesController.php
243 lines (205 loc) · 5.81 KB
/
NewsletterTemplatesController.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* TemplatesController
*
* @package Infinitas.Newsletter.Controller
*/
/**
* TemplatesController
*
* @copyright Copyright (c) 2010 Carl Sutton ( dogmatic69 )
* @link http://www.infinitas-cms.org
* @package Infinitas.Newsletter.Controller
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @since 0.5a
*
* @author Carl Sutton <dogmatic69@infinitas-cms.org>
*/
class NewsletterTemplatesController extends NewsletterAppController {
/**
* Template sample text
*
* @var string
*/
public $sampleText = '<p>This is some sample text to test your template</p>';
/**
* List all templates
*
* @return void
*/
public function admin_index() {
$this->Paginator->settings = array(
'fields' => array(
$this->modelClass . '.id',
$this->modelClass . '.name',
$this->modelClass . '.created',
$this->modelClass . '.modified',
)
);
$templates = $this->Paginator->paginate(null, $this->Filter->filter);
$filterOptions = $this->Filter->filterOptions;
$filterOptions['fields'] = array(
'name'
);
$this->set(compact('templates', 'filterOptions'));
}
/**
* View a template
*
* @param string $id the template id
*
* @return void
*/
public function admin_view($id = null) {
if (!$id) {
$this->notice('invalid');
}
$this->set('newsletterTemplate', $this->{$this->modelClass}->read(null, $id));
}
/**
* Export newsletters
*
* @param string $id the newsletter id
*
* @return void
*/
public function admin_export($id = null) {
if (!$id) {
$this->notice('invalid');
}
$template = $this->{$this->modelClass}->read(array('name', 'description', 'author', 'header', 'footer'), $id);
if (empty($template)) {
$this->notice('invalid');
}
$pattern = "/src=[\\\"']?([^\\\"']?.*(png|jpg|gif|jpeg))[\\\"']?/i";
preg_match_all($pattern, $template[$this->modelClass]['header'], $images);
$path = TMP . 'cache' . DS . 'newsletter' . DS . $this->modelClass . DS . $template[$this->modelClass]['name'];
$Folder = new Folder($path, 0777);
$slash = $Folder->correctSlashFor($path);
App::import('File');
App::import('Folder');
$File = new File($path . DS . $this->modelClass . '.xml', true, 0777);
$imageFiles = array();
if (!empty($images[1])) {
foreach ($images[1] as $img) {
$img = str_replace('/', $slash, $img);
$img = str_replace('\\', $slash . $slash, $img);
$imageFiles[] = $img;
if (is_file(APP . 'webroot' . $img)) {
$Folder->create(dirname($path . $img), 0777);
$File->path = APP . 'webroot' . $img;
$File->copy(dirname($path . $img) . DS . basename($img));
}
}
}
$xml[$this->modelClass]['name'] = 'Infinitas Newsletter Template';
$xml[$this->modelClass]['generator'] = 'Infinitas Template Generator';
$xml[$this->modelClass]['version'] = Configure::read('Infinitas.version');
$xml[$this->modelClass][$this->modelClass] = $template[$this->modelClass]['name'];
$xml[$this->modelClass]['description'] = $template[$this->modelClass]['description'];
$xml[$this->modelClass]['author'] = $template[$this->modelClass]['author'];
$xml['data']['header'] = $template[$this->modelClass]['header'];
$xml['data']['footer'] = $template[$this->modelClass]['footer'];
$xml['files']['images'] = $imageFiles;
App::Import('Helper', 'Xml');
$Xml = new XmlHelper();
$File->path = $path . DS . $this->modelClass . '.xml';
$File->write($Xml->serialize($xml));
App::import('Vendor', 'Zip', array('file' => 'zip.php'));
$Zip = new CreateZipFile();
$Zip->zipDirectory($path, null);
$File = new File($path . DS . $this->modelClass . '.zip', true, 0777);
$File->write($Zip->getZippedfile());
$this->view = 'Media';
$params = array(
'id' => $this->modelClass . '.zip',
'name' => $template[$this->modelClass]['name'],
'download' => true,
'extension' => 'zip',
'path' => $path . DS
);
$this->set($params);
$Folder = new Folder($path);
$Folder->read();
$Folder->delete($path);
}
/**
* Preview a template
*
* @param string $id the template id
*
* @return void
*/
public function admin_preview($id = null) {
$this->layout = 'ajax';
if (!$id) {
return $this->set('data', __d('newsletter', 'The template was not found'));
}
$template = $this->{$this->modelClass}->read(array('header', 'footer'), $id);
$this->set('data', $template[$this->modelClass]['header'] . $this->sampleText . $template[$this->modelClass]['footer']);
}
/**
* Handle mass action deletes
*
* @param array $ids list of ids to delete
*
* @return boolean
*/
public function __massActionDelete($ids) {
return $this->MassAction->delete($this->__canDelete($ids));
}
/**
* Check if the templates can be deleted
*
* @param array $ids Ids to be deleted
*
* @return array
*/
private function __canDelete($ids) {
$newsletters = $this->{$this->modelClass}->Newsletter->find('list', array(
'fields' => array(
'Newsletter.newsletter_template_id',
'Newsletter.newsletter_template_id'
),
'conditions' => array(
'Newsletter.newsletter_template_id' => $ids
)
));
foreach ($ids as $k => $v) {
if (isset($newsletters[$v])) {
unset($ids[$k]);
}
}
if (empty($ids)) {
$this->notice(
__d('newsletter', 'There are some newsletters using that template.'),
array(
'redirect' => true
)
);
}
$campaigns = $this->{$this->modelClass}->NewsletterCampaign->find('list', array(
'fields' => array(
'NewsletterCampaign.newsletter_template_id',
'NewsletterCampaign.newsletter_template_id'
),
'conditions' => array(
'NewsletterCampaign.newsletter_template_id' => $ids
)
));
foreach ($ids as $k => $v) {
if (isset($campaigns[$v])) {
unset($ids[$k]);
}
}
if (empty($ids)) {
$this->notice(
__d('newsletter', 'There are some campaigns using that template.'),
array(
'redirect' => true
)
);
}
return $ids;
}
}