/[drupal]/contributions/modules/simplenews_template/simplenews_template.module
ViewVC logotype

Contents of /contributions/modules/simplenews_template/simplenews_template.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.10 - (show annotations) (download) (as text)
Mon Feb 2 09:07:23 2009 UTC (9 months, 3 weeks ago) by tbarregren
Branch: MAIN
CVS Tags: HEAD
Changes since 1.9: +276 -259 lines
File MIME type: text/x-php
Brought HEAD up to date.
1 <?php
2
3 // $Id: simplenews_template.module,v 1.9.2.2 2009/01/31 20:32:48 tbarregren Exp $
4
5
6 /**
7 * @file
8 * Extends the Simplenews module by providing a themable template with
9 * configurable header, footer and style.
10 *
11 * Author:
12 * Thomas Barregren <http://drupal.org/user/16678>.
13 *
14 * Sponsors:
15 * Spoon Media <http://www.spoon.com.au/>
16 * Webbredaktören <http://www.webbredaktoren.se>
17 */
18
19
20 /*****************************************************************************
21 * HOOKS
22 *****************************************************************************/
23
24 /**
25 * Implementation of hook_perm().
26 */
27 function simplenews_template_perm() {
28 return array('access newsletter preview');
29 }
30
31 /**
32 * Implementation of hook_mail_alter.
33 */
34 function simplenews_template_mail_alter(&$message) {
35 global $processed;
36 $function = "_simplenews_template_mail_alter_{$message['id']}";
37 if (function_exists($function)) {
38 $function($message);
39 } else if (!$processed) {
40 _simplenews_template_mail_alter_simplenews_node($message);
41 $processed = TRUE;
42 }
43 }
44
45 /**
46 * Implementation of hook_menu().
47 */
48 function simplenews_template_menu() {
49 $items = array();
50
51 $items['node/%/preview'] = array(
52 'title' => 'Preview',
53 'page callback' => '_simplenews_template_preview',
54 'page arguments' => array(1),
55 'access callback' => '_simplenews_template_preview_access',
56 'access arguments' => array(1),
57 'type' => MENU_LOCAL_TASK,
58 );
59
60 return $items;
61 }
62
63 /**
64 * Access callback for preview page.
65 */
66 function _simplenews_template_preview_access($nid) {
67 $node = node_load($nid);
68 return $node && $node->type == 'simplenews' && user_access('access newsletter preview');
69 }
70
71 /**
72 * Implementation of hook_form_alter().
73 */
74 function simplenews_template_form_alter(&$form, $form_state, $form_id) {
75 if ($form_id == 'simplenews_admin_types_form') {
76 $tid = $form['tid']['#value'];
77 _simplenews_template_form_alter_newsletter_settings($form, $tid);
78 }
79 }
80
81
82 /*****************************************************************************
83 * SIMPLENEWS TEMPLATING CALLBACKS
84 *****************************************************************************/
85
86 /**
87 * Apply template to a newsletter as its sent.
88 */
89 function _simplenews_template_mail_alter_simplenews_node(&$message) {
90 // Setup newsletter data
91 $tid = reset($message['params']['context']['account']->tids);
92 if (!empty($message['params']['context']['account']->tids)) {
93 $tid = reset($message['params']['context']['account']->tids);
94 } else {
95 $tid = $message['params']['context']['newsletter']->tid;
96 }
97
98 $newsletter = taxonomy_get_term($tid);
99 $node = $message['params']['context']['node'];
100 $content = $message['body']['body'];
101
102 // Retrive and filter the header content
103 $header = _simplenews_template_get_header($tid);
104 $header = check_markup($header, _simplenews_template_get_header_format($tid), false);
105
106 // Retrive and filter the footer content
107 $footer = _simplenews_template_get_footer($tid);
108 $footer = check_markup($footer, _simplenews_template_get_footer_format($tid), false);
109
110 // Add headers and footer
111 $content = theme('simplenews_template_content', $newsletter->name, $node->title, $header, $content, $footer);
112
113 // Fetch Simplenews Template styling for this newsletter
114 $style = _simplenews_template_get_css($tid);
115 $bgcolor = _simplenews_template_get_bgcolor($tid);
116
117 // Markup node body with Simplenews Template style
118 $content = theme('simplenews_template_mail', $newsletter->name, $node->title, $content, $style, $bgcolor);
119
120 // Run HTML and CSS through Emogrifier, if available
121 $content = _simplenews_template_emogrify($content, $css);
122
123 $message['body']['body'] = $content;
124 }
125
126 /**
127 * Apply template to a newsletter node.
128 */
129 function _simplenews_template_preview($nid) {
130 $node = node_load($nid);
131 $node = node_build_content($node);
132
133 // Setup newsletter data
134 $tid = $node->simplenews['tid'];
135 $newsletter = taxonomy_get_term($tid);
136 $content = $node->content['body']['#value'];
137
138 // Retrive and filter the header content
139 $header = _simplenews_template_get_header($tid);
140 $header = check_markup($header, _simplenews_template_get_header_format($tid), false);
141
142 // Retrive and filter the footer content
143 $footer = _simplenews_template_get_footer($tid);
144 $footer = check_markup($footer, _simplenews_template_get_footer_format($tid), false);
145
146 // Add headers and footer
147 $content = theme('simplenews_template_content', $newsletter->name, $node->title, $header, $content, $footer);
148
149 // Fetch Simplenews Template styling for this newsletter
150 $style = _simplenews_template_get_css($tid);
151 $style = _simplenews_template_nodify_style($style, '#preview');
152 $bgcolor = _simplenews_template_get_bgcolor($tid);
153
154 // Markup node body with Simplenews Template style
155 $content = theme('simplenews_template_mail', $newsletter->name, $node->title, $content, $style, $bgcolor);
156
157 return '<div id="preview">'. $content .'</div>';
158 }
159
160
161 /*****************************************************************************
162 * ALTER SIMPLENEWS ADMIN SETTINGS FORM (NEWSLETTER SETTINGS)
163 *****************************************************************************/
164
165 /**
166 * Add Simplenews Template settings to the node type forms.
167 */
168 function _simplenews_template_form_alter_newsletter_settings(&$form, $tid = null) {
169 $form['simplenews_template']['#weight'] = 1;
170 $form['#submit'][] = '_simplenews_template_form_alter_newsletter_settings_submit';
171 _simplenews_template_form_alter_header($form, $tid);
172 _simplenews_template_form_alter_footer($form, $tid);
173 _simplenews_template_form_alter_style($form, $tid);
174 }
175
176 /**
177 * Handle settings submission - save Simplenews Template values.
178 */
179 function _simplenews_template_form_alter_newsletter_settings_submit($form, $form_state) {
180 // Fetch tid directly
181 if ($form_state['values']['tid']) {
182 $tid = $tidedit = $form_state['values']['tid'];
183 }
184
185 // Fetch tid from name lookup in current vocabulary
186 else {
187 $tidedit = '';
188 $terms = taxonomy_get_term_by_name($form_state['values']['name']);
189 foreach ($terms as $key => $term) {
190 if ($term->vid == $form_state['values']['vid']) {
191 $tid = $term->tid;
192 break;
193 }
194 return;
195 }
196 }
197
198 // Set Drupal variables
199 variable_set("simplenews_template_header_$tid", $form_state['values']["simplenews_template_header_$tidedit"]);
200 variable_set("simplenews_template_header_format_$tid", $form_state['values']["simplenews_template_header_format_$tidedit"]);
201 variable_set("simplenews_template_footer_$tid", $form_state['values']["simplenews_template_footer_$tidedit"]);
202 variable_set("simplenews_template_footer_format_$tid", $form_state['values']["simplenews_template_footer_format_$tidedit"]);
203 variable_set("simplenews_template_bgcolor_$tid", $form_state['values']["simplenews_template_bgcolor_$tidedit"]);
204 variable_set("simplenews_template_css_$tid", $form_state['values']["simplenews_template_css_$tidedit"]);
205 }
206
207 function _simplenews_template_form_alter_header(&$form, $tid) {
208 // Header fieldset
209 $form['simplenews_template']['simplenews_template_header'] = array(
210 '#type' => 'fieldset',
211 '#title' => t('Header'),
212 '#description' => t('The header common for all issues of this newsletter.'),
213 '#collapsible' => true,
214 '#collapsed' => true,
215 );
216
217 // Header
218 $form['simplenews_template']['simplenews_template_header']["simplenews_template_header_$tid"] = array(
219 '#type' => 'textarea',
220 '#title' => t('Header'),
221 '#description' => t("Enter the content of the newsletter's header."),
222 '#default_value' => _simplenews_template_get_header($tid),
223 );
224
225 // Header input format
226 $form['simplenews_template']['simplenews_template_header']["simplenews_template_header_format_$tid"] = filter_form(_simplenews_template_get_header_format($tid), 1, array("simplenews_template_header_format_$tid"));
227 }
228
229 function _simplenews_template_form_alter_footer(&$form, $tid) {
230 // Footer filedset
231 $form['simplenews_template']['simplenews_template_footer'] = array(
232 '#type' => 'fieldset',
233 '#title' => t('Footer'),
234 '#description' => t('The footer common for all issues of this newsletter.'),
235 '#collapsible' => true,
236 '#collapsed' => true,
237 );
238
239 // Footer
240 $form['simplenews_template']['simplenews_template_footer']["simplenews_template_footer_$tid"] = array(
241 '#type' => 'textarea',
242 '#title' => t('Footer'),
243 '#description' => t("Enter the content of the newsletter's footer."),
244 '#default_value' => _simplenews_template_get_footer($tid),
245 );
246
247 // Footer input format
248 $form['simplenews_template']['simplenews_template_footer']["simplenews_template_footer_format_$tid"] = filter_form(_simplenews_template_get_footer_format($tid), 1, array("simplenews_template_footer_format_$tid"));
249 }
250
251 function _simplenews_template_form_alter_style(&$form, $tid) {
252 // Style fieldset
253 $form['simplenews_template']['simplenews_template_style'] = array(
254 '#type' => 'fieldset',
255 '#title' => t('Style'),
256 '#description' => t('The style to be applied to all issues of this newsletter'),
257 '#collapsible' => true,
258 '#collapsed' => true,
259 );
260
261 // Background color
262 $form['simplenews_template']['simplenews_template_style']["simplenews_template_bgcolor_$tid"] = array(
263 '#type' => 'textfield',
264 '#title' => t('Body background color'),
265 '#description' => t('Enter the backgound color of the body.'),
266 '#default_value' => _simplenews_template_get_bgcolor($tid),
267 );
268
269 // CSS
270 $form['simplenews_template']['simplenews_template_style']["simplenews_template_css_$tid"] = array(
271 '#type' => 'textarea',
272 '#title' => t('CSS'),
273 '#description' => t('Enter the CSS rules of the body.'),
274 '#default_value' => _simplenews_template_get_css($tid),
275 );
276 }
277
278
279 /*****************************************************************************
280 * THEME FUNCTIONS
281 *****************************************************************************/
282
283 /**
284 * Implementation of hook_theme().
285 */
286 function simplenews_template_theme() {
287 return array(
288 'simplenews_template_content' => array(
289 'arguments' => array(
290 'newletter_name' => null,
291 'issue_title' => null,
292 'header' => null,
293 'issue_body' => null,
294 'footer' => null,
295 ),
296 ),
297 'simplenews_template_mail' => array(
298 'arguments' => array(
299 'newsletter_name' => null,
300 'issue_title' => null,
301 'issue_body' => null,
302 'style' => null,
303 'bgcolor' => null,
304 ),
305 ),
306 );
307 }
308
309 /**
310 * Theme function for the actual content.
311 */
312 function theme_simplenews_template_content($newsletter_name, $issue_title, $header, $issue_body, $footer) {
313 // Theme the header
314 if ($header) {
315 $content = "<div class=\"simplenews-template-header\">\n$header\n</div>\n";
316 }
317
318 // Theme the body
319 if ($header || $footer) {
320 $content .= "<div class=\"simplenews-template-body\">\n<h1>$issue_title</h1>\n$issue_body\n</div>\n";
321 }
322 else {
323 $content .= $issue_body;
324 }
325
326 // Theme the footer
327 if ($footer) {
328 $content .= "<div class=\"simplenews-template-footer\">\n$footer\n</div>\n";
329 }
330
331 return $content;
332 }
333
334 /**
335 * Themeable function for the HTML e-mail content.
336 * Based on http://www.mailchimp.com/resources/templates/.
337 */
338 function theme_simplenews_template_mail($newsletter_name, $issue_title, $issue_body, $style, $bgcolor) {
339 return <<<EOT
340 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
341 <html>
342 <head>
343 <title>$newsletter_name: $issue_title</title>
344 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
345 <style type="text/css">
346 <!--
347 $style
348 -->
349 </style>
350 </head>
351 <body bgcolor="$bgcolor">
352 <table class="simplenews-template-background" width="100%" bgcolor="$bgcolor" cellpadding="0" cellspacing="0"><tr><td valign="top" align="center">
353 <table class="simplenews-template-content" width="100%" cellpadding="0" cellspacing="0"><tr><td align="left">
354 $issue_body
355 </td></tr></table>
356 </td></tr></table>
357 </body>
358 </html>
359 EOT;
360 }
361
362
363 /*****************************************************************************
364 * OVERRIDDING SOME THEMEABLE FUNCTIONS - WORKS ONLY WITH PHPTEMPLATE
365 *****************************************************************************/
366
367 /**
368 * Implementation of hook_theme_registry_alter().
369 */
370 function simplenews_template_theme_registry_alter(&$theme_registry) {
371 // Steal theme_simplenews_newsletter_body() from Simplenews
372 if (!empty($theme_registry['simplenews_newsletter_body'])) {
373 $theme_registry['simplenews_newsletter_body']['function'] = '_simplenews_template_theme_simplenews_newsletter_body';
374 }
375 }
376
377 /**
378 * Hi-jacker to stop the title from being inserted above the HTML in the mail.
379 */
380 function _simplenews_template_theme_simplenews_newsletter_body($node, $title, $language = NULL) {
381 return $node->body;
382 }
383
384 /**
385 * If the Emogrifier <http://www.pelagodesign.com/sidecar/emogrifier/> exists,
386 * the CSS definitions in $css are inserted into tags within $html based on the
387 * CSS selectors.
388 */
389 function _simplenews_template_emogrify($html, $css) {
390 $path = drupal_get_path('module', 'simplenews_template');
391 $path = "./$path/emogrifier.php";
392
393 if (is_file($path)) {
394 include_once $path;
395 $emogrifier = new Emogrifier();
396 $emogrifier->setHTML($html);
397 $emogrifier->setCSS($css);
398 $html = $emogrifier->emogrify();
399 }
400
401 return $html;
402 }
403
404 /**
405 * Parses CSS and prepends 'div.node div.content' to all selectors.
406 */
407 function _simplenews_template_nodify_style($css, $prepend = 'div.node div.content') {
408 $pattern = '/\s*([^{]+){([^}]*)}/';
409 preg_match_all($pattern, $css, $matches);
410
411 foreach ($matches[1] as $key => $selector_string) {
412 foreach (explode(',', $selector_string) as $key2 => $selector) {
413 $selectors = ($selectors ? $selectors .', ' : '') . $prepend .' '. trim($selector);
414 }
415 $new_css = ($new_css ? $new_css ."\n" : '') . $selectors . ' {'. trim($matches[2][$key]) .'}';
416 unset($selectors);
417 }
418
419 return $new_css;
420 }
421
422
423 /*****************************************************************************
424 * VARIABLES
425 *****************************************************************************/
426
427 /**
428 * Returns the header.
429 */
430 function _simplenews_template_get_header($tid) {
431 return variable_get("simplenews_template_header_$tid", '');
432 }
433
434 /**
435 * Returns header format.
436 */
437 function _simplenews_template_get_header_format($tid) {
438 return variable_get("simplenews_template_header_format_$tid", FILTER_FORMAT_DEFAULT);
439 }
440
441 /**
442 * Returns the footer.
443 */
444 function _simplenews_template_get_footer($tid) {
445 return variable_get("simplenews_template_footer_$tid", '');
446 }
447
448 /**
449 * Returns footer format.
450 */
451 function _simplenews_template_get_footer_format($tid) {
452 return variable_get("simplenews_template_footer_format_$tid", FILTER_FORMAT_DEFAULT);
453 }
454
455 /**
456 * Returns the background color.
457 */
458 function _simplenews_template_get_bgcolor($tid) {
459 return variable_get("simplenews_template_bgcolor_$tid", '#ffffff');
460 }
461
462 /**
463 * Returns the CSS.
464 */
465 function _simplenews_template_get_css($tid) {
466 return variable_get("simplenews_template_css_$tid", '');
467 }

  ViewVC Help
Powered by ViewVC 1.1.2