fix #8131 - chinese translations
[Pman.Core] / MessagePreview.php
index fdf97f9..384cdbd 100644 (file)
@@ -2,10 +2,14 @@
 
 require_once 'Pman.php';
 
-class Pman_Crm_MessagePreview extends Pman
+class Pman_Core_MessagePreview extends Pman
 {
     var $masterTemplate = 'mail/MessagePreview.html';
     
+    
+    var $showHtml;
+    var $msg;
+    
     function getAuth()
     {
         if (HTML_FlexyFramework::get()->cli) {
@@ -18,130 +22,179 @@ class Pman_Crm_MessagePreview extends Pman
         return true;
     }
     
-    function get()
+    function get($v, $opts=array())
     {
-        if(empty($_REQUEST['_id'])){
-            $this->jerr('id got error');
+        if((empty($_REQUEST['_id']) && empty($_REQUEST['template_name']) )|| empty($_REQUEST['_table'])){
+            $this->jerr('Missing Options');
         }
-        $mlq = DB_DataObject::factory('crm_mailing_list_message');
         
-        $mlq->get($_REQUEST['_id']);
+        $mlq = DB_DataObject::factory($_REQUEST['_table']);
+        if (!empty($_REQUEST['template_name'])) {
+            $res = $mlq->get('name', $_REQUEST['template_name']);
+        } else {
+            $res = $mlq->get($_REQUEST['_id']);
+        }
+        if (!$res) {
+            $this->jerr("invalid id/name");
+        }
         
-        $this->msg = $mlq;
-
         $this->showHtml = isset($_REQUEST['_as_html']) ? true : false;
         
+        
+        if (isset($_REQUEST['ontable']) && !empty($_REQUEST['onid']) && !empty($_REQUEST['evtype'])) {
+            $tn = preg_replace('/[^a-z_]+/i', '', $_REQUEST['ontable']);
+            
+            $t = DB_DataObject::factory($tn);
+            if (!is_a($t, 'DB_DataObject') && !is_a($t, 'PDO_DataObject')) {
+                $this->jerr("invalid URL");
+            }
+            if (!$t->get($_REQUEST['onid'])) {
+                $this->jerr("invalid id");
+            }
+            if (!method_exists($t,'notify'.$_REQUEST['evtype'])) {
+                $this->jerr("invalid evtype");
+            }
+              
+            $m = 'notify'.$_REQUEST['evtype'];
+            $this->msg = (object)$t->$m('test@test.com', false, false, false);
+           // print_R($this->msg->mailer );
+            $this->msg->subject = $this->msg->headers['Subject'];
+            $this->msg->from_email = $mlq->from_email;
+            $this->msg->from_name = $mlq->from_name;
+            $this->msg->plaintext  = $this->msg->mailer->textbody ;
+            $this->msg->bodytext = $this->msg->mailer->htmlbody;
+            $this->msg->rcpts = $this->msg->mailer->rcpts;
+            // htmlbody 
+            //$this->plaintext = 
+            //$data->subject = $data['Subject;
+             
+            
+             
+
+            return;
+        }
+        
+        $this->msg = $mlq;
+        $this->msg->rcpts = "send to <these@people>";
+        
+        
     }
     
-    function post()
+    function post($v)
     {
-        if(empty($_REQUEST['_id'])){
-            $this->jerr('id got error');
+        if(empty($_REQUEST['_id']) || empty($_REQUEST['_table'])){
+            $this->jerr('Missing Options');
+        }
+        
+        if($_REQUEST['_table'] == 'core_email'){
+            $this->coreEmailSendTest();
         }
-        if(empty($_REQUEST['_action'])){
-            $this->jerr('preview type not set');
+        
+        $cn = DB_DataObject::factory('core_notify');
+        $cn->setFrom(array(
+            'evtype'        => "{$_REQUEST['_table']}::SendPreviewEmail",
+            'onid'          => $_REQUEST['_id'],
+            'ontable'       => $_REQUEST['_table'],
+            'person_id'     => $this->authUser->id,
+            'person_table'  => 'Person',
+            'act_when'      => $cn->sqlValue("NOW() + INTERVAL 10 MINUTE"),
+            'act_start'     => $cn->sqlValue("NOW() + INTERVAL 10 MINUTE")
+        ));
+        
+        $cn->insert();
+        
+        $sent = $cn->sendManual();
+        
+        if(get_class($sent) != 'Pman_Core_NotifySend_Exception_Success'){
+            $this->jerr($sent->getMessage());
         }
+        
+        $this->jok("SUCCESS");
+        
+        /*
         $mid = $_REQUEST['_id'];
         
-        $cfg = HTML_FlexyFramework::get()->Pman_Crm;
+        $mlq = DB_DataObject::factory($_REQUEST['_table']);
         
-       
-       
-        $q = DB_DataObject::factory('crm_mailing_list_queue');
-        $q->id = 'test-message-'. $mid;
-        $q->message_id = $mid;
-        $q->message_id_subject = $q->message()->subject;
-        $q->message_id_from_email = $cfg['from_email'];
-        $q->message_id_from_name = "Email Test"; 
+        $mlq->get($_REQUEST['_id']);
         
-        $q->cachedMailWithOutImages(true);
-        $r = $q->getMailerObject($this->authUser, false, false, true);
-        //print_r($r->toData());
-        $ret = $r->toData();
-        $images = file_get_contents(session_save_path() . '/email-cache-' . getenv('APACHE_RUN_USER') . '/mail/' . $q->id . '-images.txt');
-       // var_dump($images);exit;
+        $content = array(
+            'template' => $mlq->name,
+            'person' => $this->authUser
+        );
         
-        $ret['body'] = str_replace('%Images%', $images, $ret['body']);
+        $sent = DB_DataObject::factory($_REQUEST['_table'])->send($content);
         
-        $sent = $r->send($ret);
         if(!is_object($sent)){
             $this->jok('SUCCESS');
         }
+        
         $this->jerr('error!!:' . $sent->toString());
-        /*
-        $ret = $r->toData();
-        
-        //print_r($ret);exit;
-        $images = file_get_contents(session_save_path() . '/email-cache-' . get_current_user() . '/mail/' . $this->id . '-images.txt');
-       // var_dump($images);exit;
-        
-        $ret['body'] = str_replace('%Images%', $images, $ret['body']);
-        return $ret;
-        
-       
-       
-       
-       
-        $message = DB_DataObject::factory('crm_mailing_list_message');
-        if(!$message->get($mid)){
-            $this->jerr("Error occour on getting message!");
+        */
+    }
+    
+    function coreEmailSendTest()
+    {
+        $core_email = DB_DataObject::factory('core_email');
+        
+        if(!$core_email->get($_REQUEST['_id'])){
+            $this->jerr('Invalid Message ID');
         }
-        $mm = clone($message);
-        $message->processRelacements($this->authUser);
-
-        $ids = $mm->attachmentIds();
-        //$this->jerr(print_r($ids,true));
         
+        if(empty($core_email->test_class)){
+            $this->jerr("[{$core_email->name}] does not has test class");
+        }
+        
+        require_once "{$core_email->test_class}.php";
+        
+        $cls = str_replace('/', '_', $core_email->test_class);
         
+        $x = new $cls;
         
+        $method = "test_{$core_email->name}";
         
+        if(!method_exists($x, $method)){
+            $this->jerr("{$method} does not exists in {$cls}");
+        }
+        /*
+        $content = $x->{$method}($this, $this->authUser);
         
+        $content['bcc'] = array();
+        */
         
         
+        $cn = DB_DataObject::factory('core_notify');
+        $cn->setFrom(array(
+            'evtype'        => 'Core_email::testData',
+            'onid'          => $_REQUEST['_id'],
+            'ontable'       => $_REQUEST['_table'],
+            'person_id'     => $this->authUser->id,
+            'person_table'  => 'Person',
+            'act_when'      => $cn->sqlValue("NOW()"),
+            'act_start'     => $cn->sqlValue("NOW()")
+        ));
         
+        $cn->insert();
         
+        $sent = $cn->sendManual();
         
-         require_once 'Pman/Core/Mailer.php';
-        $i = DB_DataObject::factory('Images');
-        $i->onid = $mid;
-        $i->ontable = 'crm_mailing_list_message';
-        $i->whereAddIn('id', $ids, 'int');
-        $i->find();
-        $attachment = array();
-        while ($i->fetch()){
-            $ii = clone($i);
-            require_once 'File/MimeType.php';
-            $y = new File_MimeType();
-            $ii->ext = $y->toExt(trim((string) $ii->mimetype ));
-            $ii->file = chunk_split(base64_encode(file_get_contents($ii->getStoreName())));
-            $attachment[] = $ii;
+        if(get_class($sent) != 'Pman_Core_NotifySend_Exception_Success'){
+            $this->jerr($sent->getMessage());
         }
         
-        $cfg = HTML_FlexyFramework::get()->Pman_Crm;
+        $this->jok("SUCCESS");
         
         
-        $template = ($_REQUEST['_action'] == 'html') ? 'mixedMail' : 'plainMail';
         
-        $random_hash = md5(date('r', time()));
-        $r = new Pman_Core_Mailer(array(
-            'template'=> $template,
-            'page' => $this,
-            'contents' => array(
-                'random_hash' => $random_hash,
-                'person' => $this->authUser,
-                'data' => $message,
-                'attach' => $attachment,
-                'from' => '<'.$cfg['from_email'].'>'
-            )
-        ));
-        //print_R($r->toData());
-        $sent = $r->send();
-        if(!is_object($sent)){
-            $this->jok('SUCCESS');
+        
+        
+        $sent = $core_email->send($content);
+        
+        if(is_object($sent)){
+            $this->jerr("Error sending email - " . $sent->toString());
         }
-        $this->jerr('error!!:' . $sent->toString());
-        */
         
+        $this->jok('SUCCESS');
     }
-    
 }