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