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