MessagePreview.php
[Pman.Core] / MessagePreview.php
1 <?php
2
3 require_once 'Pman.php';
4
5 class Pman_Crm_MessagePreview extends Pman
6 {
7     var $masterTemplate = 'mail/MessagePreview.html';
8     
9     function getAuth()
10     {
11         if (HTML_FlexyFramework::get()->cli) {
12             return true;
13         }
14         $this->authUser = $this->getAuthUser();
15         if (!$this->authUser) {
16             return false;
17         }
18         return true;
19     }
20     
21     function get()
22     {
23         if(empty($_REQUEST['_id'])){
24             $this->jerr('id got error');
25         }
26         $mlq = DB_DataObject::factory('crm_mailing_list_message');
27         
28         $mlq->get($_REQUEST['_id']);
29         
30         $this->msg = $mlq;
31
32         $this->showHtml = isset($_REQUEST['_as_html']) ? true : false;
33         
34     }
35     
36     function post()
37     {
38         if(empty($_REQUEST['_id'])){
39             $this->jerr('id got error');
40         }
41         if(empty($_REQUEST['_action'])){
42             $this->jerr('preview type not set');
43         }
44         $mid = $_REQUEST['_id'];
45         
46         $cfg = HTML_FlexyFramework::get()->Pman_Crm;
47         
48        
49        
50         $q = DB_DataObject::factory('crm_mailing_list_queue');
51         $q->id = 'test-message-'. $mid;
52         $q->message_id = $mid;
53         $q->message_id_subject = $q->message()->subject;
54         $q->message_id_from_email = $cfg['from_email'];
55         $q->message_id_from_name = "Email Test"; 
56         
57         $q->cachedMailWithOutImages(true);
58         $r = $q->getMailerObject($this->authUser, false, false, true);
59         //print_r($r->toData());
60         $ret = $r->toData();
61         $images = file_get_contents(session_save_path() . '/email-cache-' . getenv('APACHE_RUN_USER') . '/mail/' . $q->id . '-images.txt');
62        // var_dump($images);exit;
63         
64         $ret['body'] = str_replace('%Images%', $images, $ret['body']);
65         
66         $sent = $r->send($ret);
67         if(!is_object($sent)){
68             $this->jok('SUCCESS');
69         }
70         $this->jerr('error!!:' . $sent->toString());
71         /*
72         $ret = $r->toData();
73         
74         //print_r($ret);exit;
75         $images = file_get_contents(session_save_path() . '/email-cache-' . get_current_user() . '/mail/' . $this->id . '-images.txt');
76        // var_dump($images);exit;
77         
78         $ret['body'] = str_replace('%Images%', $images, $ret['body']);
79         return $ret;
80         
81        
82        
83        
84        
85         $message = DB_DataObject::factory('crm_mailing_list_message');
86         if(!$message->get($mid)){
87             $this->jerr("Error occour on getting message!");
88         }
89         $mm = clone($message);
90         $message->processRelacements($this->authUser);
91
92         $ids = $mm->attachmentIds();
93         //$this->jerr(print_r($ids,true));
94         
95         
96         
97         
98         
99         
100         
101         
102         
103         
104          require_once 'Pman/Core/Mailer.php';
105         $i = DB_DataObject::factory('Images');
106         $i->onid = $mid;
107         $i->ontable = 'crm_mailing_list_message';
108         $i->whereAddIn('id', $ids, 'int');
109         $i->find();
110         $attachment = array();
111         while ($i->fetch()){
112             $ii = clone($i);
113             require_once 'File/MimeType.php';
114             $y = new File_MimeType();
115             $ii->ext = $y->toExt(trim((string) $ii->mimetype ));
116             $ii->file = chunk_split(base64_encode(file_get_contents($ii->getStoreName())));
117             $attachment[] = $ii;
118         }
119         
120         $cfg = HTML_FlexyFramework::get()->Pman_Crm;
121         
122         
123         $template = ($_REQUEST['_action'] == 'html') ? 'mixedMail' : 'plainMail';
124         
125         $random_hash = md5(date('r', time()));
126         $r = new Pman_Core_Mailer(array(
127             'template'=> $template,
128             'page' => $this,
129             'contents' => array(
130                 'random_hash' => $random_hash,
131                 'person' => $this->authUser,
132                 'data' => $message,
133                 'attach' => $attachment,
134                 'from' => '<'.$cfg['from_email'].'>'
135             )
136         ));
137         //print_R($r->toData());
138         $sent = $r->send();
139         if(!is_object($sent)){
140             $this->jok('SUCCESS');
141         }
142         $this->jerr('error!!:' . $sent->toString());
143         */
144         
145     }
146     
147 }