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