source: extensions/Piwecard/ws/ws_functions.inc.php @ 20421

Last change on this file since 20421 was 20421, checked in by julien1311, 11 years ago

[piwecard] email preview in admin

  • Property svn:eol-style set to LF
File size: 2.8 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5$service = &$arr[0];
6$service->addMethod('pwg.piwecard.previewEmail', 'piwecard_ws_previewEmail',
7        array(
8                'format_message'=>array(),
9                'message'=>array(),
10        ),
11        'Preview the email'
12);
13
14function piwecard_ws_previewEmail($params, $service) {
15        $format_message = $params['format_message'];
16        $message = $params['message'];
17       
18        return previewEmail($format_message, $message);
19}
20
21function previewEmail($format_message, $message) {
22        $piwecard = new Piwecard();
23
24        $template_mail = new Template(PIWECARD_MAIL_PATH.'template');
25        $smarty = $template_mail->smarty;
26       
27        if ($format_message == 'text') {
28                $message_output = nl2br($piwecard->get_text_message(stripslashes(parse($message)), $smarty));
29                $style = '';
30        } elseif ($format_message == 'html') {
31                $output = $piwecard->get_html_message(stripslashes(parse($message)), $smarty);
32                $dom_document = new DOMDocument();
33                $dom_document->loadHTML($output);
34                $styles = piwecard_create_style_array($dom_document->getElementsByTagName('style')->item(0)->nodeValue);
35                $message_output = $dom_document->getElementsByTagName('body')->item(0);
36                $email_body = $dom_document->createElement('div');
37                $email_body->setAttribute('id', 'email_body');
38                $message_output->parentNode->insertBefore($email_body, $message_output);
39                $email_body->appendChild($message_output);
40                foreach ($styles as $id => $style) {
41                        $dom_document->getElementById(str_replace('#', '', $id))->setAttribute("style", $style);
42                }
43                $message_output = $dom_document->saveHTML();
44        }
45
46        return array('message' => $message_output);
47}
48
49function piwecard_create_style_array($style) {
50        $style = str_replace(array("\n", "\t"), array('', ''), $style);
51        $tok = strtok($style, "{}");
52       
53        $sarray = array();
54        while ($tok !== false) {
55                array_push($sarray, trim($tok));
56                $tok = strtok("{}");
57        }
58
59        $styles = array();
60
61        for($i = 0; $i<count($sarray)-2; $i+=2){
62                $styles[$sarray[$i]] = $sarray[$i+1];
63        }
64
65        return $styles;
66}
67
68function parse($data) {
69        global $conf;
70       
71        set_make_full_url();
72        $ecard_parse = array(
73                        '%yourname%'            => 'Your Name',
74                        '%youremail%'           => 'Your email',
75                        '%recipientname%'       => 'Recipient name',
76                        '%recipientemail%'      => 'Recipient email',
77                        '%website%'                     => isset($conf['gallery_title']) ? $conf['gallery_title'] : '' ,
78                        '%websiteurl%'          => get_absolute_root_url() ,
79                        '%ecardurl%'            => '',
80                        '%ecardtitle%'          => 'Ecard title',
81                        '%ecardmessage%'        => 'The message of the ecard',
82                        '%pictureurl%'          => get_root_url().PIWECARD_PATH.'admin/images/image_thumb.jpg',
83                        '%pictureinfos%'        => '<small>Image infos</small>',
84                        );
85        unset_make_full_url();
86       
87        $patterns = array();
88        $replacements = array();
89        foreach ($ecard_parse as $key => $value) {
90                array_push($patterns, $key); 
91                array_push($replacements, $value);
92        }
93
94        return str_replace($patterns, $replacements, $data);
95}
96?>
Note: See TracBrowser for help on using the repository browser.