fix #8131 - chinese translations
[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               
59             $m = 'notify'.$_REQUEST['evtype'];
60             $this->msg = (object)$t->$m('test@test.com', false, false, false);
61            // print_R($this->msg->mailer );
62             $this->msg->subject = $this->msg->headers['Subject'];
63             $this->msg->from_email = $mlq->from_email;
64             $this->msg->from_name = $mlq->from_name;
65             $this->msg->plaintext  = $this->msg->mailer->textbody ;
66             $this->msg->bodytext = $this->msg->mailer->htmlbody;
67             $this->msg->rcpts = $this->msg->mailer->rcpts;
68             // htmlbody 
69             //$this->plaintext = 
70             //$data->subject = $data['Subject;
71              
72             
73              
74
75             return;
76         }
77         
78         $this->msg = $mlq;
79         $this->msg->rcpts = "send to <these@people>";
80         
81         
82     }
83     
84     function post($v)
85     {
86         if(empty($_REQUEST['_id']) || empty($_REQUEST['_table'])){
87             $this->jerr('Missing Options');
88         }
89         
90         if($_REQUEST['_table'] == 'core_email'){
91             $this->coreEmailSendTest();
92         }
93         
94         $cn = DB_DataObject::factory('core_notify');
95         $cn->setFrom(array(
96             'evtype'        => "{$_REQUEST['_table']}::SendPreviewEmail",
97             'onid'          => $_REQUEST['_id'],
98             'ontable'       => $_REQUEST['_table'],
99             'person_id'     => $this->authUser->id,
100             'person_table'  => 'Person',
101             'act_when'      => $cn->sqlValue("NOW() + INTERVAL 10 MINUTE"),
102             'act_start'     => $cn->sqlValue("NOW() + INTERVAL 10 MINUTE")
103         ));
104         
105         $cn->insert();
106         
107         $sent = $cn->sendManual();
108         
109         if(get_class($sent) != 'Pman_Core_NotifySend_Exception_Success'){
110             $this->jerr($sent->getMessage());
111         }
112         
113         $this->jok("SUCCESS");
114         
115         /*
116         $mid = $_REQUEST['_id'];
117         
118         $mlq = DB_DataObject::factory($_REQUEST['_table']);
119         
120         $mlq->get($_REQUEST['_id']);
121         
122         $content = array(
123             'template' => $mlq->name,
124             'person' => $this->authUser
125         );
126         
127         $sent = DB_DataObject::factory($_REQUEST['_table'])->send($content);
128         
129         if(!is_object($sent)){
130             $this->jok('SUCCESS');
131         }
132         
133         $this->jerr('error!!:' . $sent->toString());
134         */
135     }
136     
137     function coreEmailSendTest()
138     {
139         $core_email = DB_DataObject::factory('core_email');
140         
141         if(!$core_email->get($_REQUEST['_id'])){
142             $this->jerr('Invalid Message ID');
143         }
144         
145         if(empty($core_email->test_class)){
146             $this->jerr("[{$core_email->name}] does not has test class");
147         }
148         
149         require_once "{$core_email->test_class}.php";
150         
151         $cls = str_replace('/', '_', $core_email->test_class);
152         
153         $x = new $cls;
154         
155         $method = "test_{$core_email->name}";
156         
157         if(!method_exists($x, $method)){
158             $this->jerr("{$method} does not exists in {$cls}");
159         }
160         /*
161         $content = $x->{$method}($this, $this->authUser);
162         
163         $content['bcc'] = array();
164         */
165         
166         
167         $cn = DB_DataObject::factory('core_notify');
168         $cn->setFrom(array(
169             'evtype'        => 'Core_email::testData',
170             'onid'          => $_REQUEST['_id'],
171             'ontable'       => $_REQUEST['_table'],
172             'person_id'     => $this->authUser->id,
173             'person_table'  => 'Person',
174             'act_when'      => $cn->sqlValue("NOW()"),
175             'act_start'     => $cn->sqlValue("NOW()")
176         ));
177         
178         $cn->insert();
179         
180         $sent = $cn->sendManual();
181         
182         if(get_class($sent) != 'Pman_Core_NotifySend_Exception_Success'){
183             $this->jerr($sent->getMessage());
184         }
185         
186         $this->jok("SUCCESS");
187         
188         
189         
190         
191         
192         $sent = $core_email->send($content);
193         
194         if(is_object($sent)){
195             $this->jerr("Error sending email - " . $sent->toString());
196         }
197         
198         $this->jok('SUCCESS');
199     }
200 }