0fea0af0961e1f159c438534cf64a54c3346afd2
[Pman.Core] / MessagePreview.php
1 <?php
2
3 require_once 'Pman.php';
4
5 class Pman_Core_MessagePreview extends Pman
6 {
7     var $masterTemplate = 'mail/MessagePreview.html';
8     
9     
10     var $showHtml;
11     var $msg;
12     
13     function getAuth()
14     {
15         if (HTML_FlexyFramework::get()->cli) {
16             return true;
17         }
18         $this->authUser = $this->getAuthUser();
19         if (!$this->authUser) {
20             return false;
21         }
22         return true;
23     }
24     
25     function get($v, $opts=array())
26     {
27  
28         if((empty($_REQUEST['_id']) && empty($_REQUEST['template_name']) )|| empty($_REQUEST['_table'])){
29             $this->jerr('Missing Options');
30         }
31         
32         $mlq = DB_DataObject::factory($_REQUEST['_table']);
33         if (!empty($_REQUEST['template_name'])) {
34             $res = $mlq->get('name', $_REQUEST['template_name']);
35         } else {
36             $res = $mlq->get($_REQUEST['_id']);
37         }
38         if (!$res) {
39             $this->jerr("invalid id/name");
40         }
41         
42         $this->showHtml = isset($_REQUEST['_as_html']) ? true : false;
43         
44         
45         if (isset($_REQUEST['ontable']) && !empty($_REQUEST['onid']) && !empty($_REQUEST['evtype'])) {
46             $tn = preg_replace('/[^a-z_]+/i', '', $_REQUEST['ontable']);
47             
48             $t = DB_DataObject::factory($tn);
49             if (!is_a($t, 'DB_DataObject') && !is_a($t, 'PDO_DataObject')) {
50                 $this->jerr("invalid URL");
51             }
52             if (!$t->get($_REQUEST['onid'])) {
53                 $this->jerr("invalid id");
54             }
55             if (!method_exists($t,'notify'.$_REQUEST['evtype'])) {
56                 $this->jerr("invalid evtype");
57             }
58             $m = 'notify'.$_REQUEST['evtype'];
59             $this->msg = (object)$t->$m('test@test.com', false, false, false);
60            // print_R($this->msg->mailer );
61             $this->msg->subject = $this->msg->headers['Subject'];
62             $this->msg->from_email = $mlq->from_email;
63             $this->msg->from_name = $mlq->from_name;
64             $this->msg->plaintext  = $this->msg->mailer->textbody ;
65             $this->msg->bodytext = $this->msg->mailer->htmlbody;
66             $this->msg->rcpts = $this->msg->mailer->rcpts;
67             // htmlbody 
68             //$this->plaintext = 
69             //$data->subject = $data['Subject;
70              
71             
72              
73
74             return;
75         }
76         
77         $this->msg = $mlq;
78         $this->msg->rcpts = "send to <these@people>";
79         
80         
81     }
82     
83     function post($v)
84     {
85         if(empty($_REQUEST['_id']) || empty($_REQUEST['_table'])){
86             $this->jerr('Missing Options');
87         }
88         
89         if($_REQUEST['_table'] == 'core_email'){
90             $this->coreEmailSendTest();
91         }
92         
93         $cn = DB_DataObject::factory('core_notify');
94         $cn->setFrom(array(
95             'evtype'        => "{$_REQUEST['_table']}::SendPreviewEmail",
96             'onid'          => $_REQUEST['_id'],
97             'ontable'       => $_REQUEST['_table'],
98             'person_id'     => $this->authUser->id,
99             'person_table'  => 'Person',
100             'act_when'      => $cn->sqlValue("NOW() + INTERVAL 10 MINUTE"),
101             'act_start'     => $cn->sqlValue("NOW() + INTERVAL 10 MINUTE")
102         ));
103         
104         $cn->insert();
105         
106         $sent = $cn->sendManual();
107         
108         if(get_class($sent) != 'Pman_Core_NotifySend_Exception_Success'){
109             $this->jerr($sent->getMessage());
110         }
111         
112         $this->jok("SUCCESS");
113         
114         /*
115         $mid = $_REQUEST['_id'];
116         
117         $mlq = DB_DataObject::factory($_REQUEST['_table']);
118         
119         $mlq->get($_REQUEST['_id']);
120         
121         $content = array(
122             'template' => $mlq->name,
123             'person' => $this->authUser
124         );
125         
126         $sent = DB_DataObject::factory($_REQUEST['_table'])->send($content);
127         
128         if(!is_object($sent)){
129             $this->jok('SUCCESS');
130         }
131         
132         $this->jerr('error!!:' . $sent->toString());
133         */
134     }
135     
136     function coreEmailSendTest()
137     {
138         $core_email = DB_DataObject::factory('core_email');
139         
140         if(!$core_email->get($_REQUEST['_id'])){
141             $this->jerr('Invalid Message ID');
142         }
143         
144         if(empty($core_email->test_class)){
145             $this->jerr("[{$core_email->name}] does not has test class");
146         }
147         
148         require_once "{$core_email->test_class}.php";
149         
150         $cls = str_replace('/', '_', $core_email->test_class);
151         
152         $x = new $cls;
153         
154         $method = "test_{$core_email->name}";
155         
156         if(!method_exists($x, $method)){
157             $this->jerr("{$method} does not exists in {$cls}");
158         }
159         /*
160         $content = $x->{$method}($this, $this->authUser);
161         
162         $content['bcc'] = array();
163         */
164         
165         
166         $cn = DB_DataObject::factory('core_notify');
167         $cn->setFrom(array(
168             'evtype'        => 'Core_email::testData',
169             'onid'          => $_REQUEST['_id'],
170             'ontable'       => $_REQUEST['_table'],
171             'person_id'     => $this->authUser->id,
172             'person_table'  => 'Person',
173             'act_when'      => $cn->sqlValue("NOW()"),
174             'act_start'     => $cn->sqlValue("NOW()")
175         ));
176         
177         $cn->insert();
178         
179         $sent = $cn->sendManual();
180         
181         if(get_class($sent) != 'Pman_Core_NotifySend_Exception_Success'){
182             $this->jerr($sent->getMessage());
183         }
184         
185         $this->jok("SUCCESS");
186         
187         
188         
189         
190         
191         $sent = $core_email->send($content);
192         
193         if(is_object($sent)){
194             $this->jerr("Error sending email - " . $sent->toString());
195         }
196         
197         $this->jok('SUCCESS');
198     }
199 }