fix #8131 - chinese translations
[Pman.Core] / Import / Core_email.php
index 3dfb3e8..c198d27 100644 (file)
@@ -28,9 +28,24 @@ class Pman_Core_Import_Core_email extends Pman
             'desc' => 'Update template (deletes old version?)',
             'short' => 'u',
             'default' => '',
-            'min' => 1,
-            'max' => 1,  
+            'min' => 0,
+            'max' => 0,  
+        ),
+         'use-file' => array(
+            'desc' => 'Force usage of file (so content is not editable in Management system)',
+            'short' => 'F',
+            'default' => '',
+            'min' => 0,
+            'max' => 0,  
         ),
+        'raw_content' => array(
+            'desc' => 'Raw contents of email (used by API) - not by Command line',
+            'short' => 'R',
+            'default' => '',
+            'min' => 0,
+            'max' => 0,  
+        )
+         
     );
     
     function getAuth()
@@ -40,83 +55,151 @@ class Pman_Core_Import_Core_email extends Pman
         if (!$ff->cli) {
             die("cli only");
         }
-        
+    }
+    
+    function get($part = '', $opts=array())
+    {
+        $this->updateOrCreateEmail($part, $opts, false);
     }
 
-    function get($part='', $opts){
+    function updateOrCreateEmail($part='', $opts= array(), $cm = false, $mapping = false){
         
        // DB_DataObject::debugLevel(1);
         
-        $template_name = preg_replace('/\.[a-z]+$/i', '', basename($opts['file']));
         
-        if (!file_exists($opts['file'])) {
-            $this->jerr("file does not exist : " . $opts['file']);
-        }
-        
-        
-        if (!empty($opts['master']) && !file_exists($opts['master'])) {
-            $this->jerr("master file does not exist : " . $opts['master']);
-        }
-        
-        
-         
-        $c = DB_dataObject::factory('core_email');
-        $ret = $c->get('name',$template_name);
-        if($ret && empty($opts['update'])) {
-            $this->jerr("use --update 1 to update the template..");
+        if (empty($opts['raw_content'])) {
+            $template_name = preg_replace('/\.[a-z]+$/i', '', basename($opts['file']));
+
+            if (!file_exists($opts['file'])) {
+                $this->jerr("file does not exist : " . $opts['file']);
+            }
+            
+            
+            if (!empty($opts['master']) && !file_exists($opts['master'])) {
+                $this->jerr("master file does not exist : " . $opts['master']);
+            }
+            
+            
+            if (empty($cm)) {
+                $cm = DB_dataObject::factory('core_email');
+                $ret = $cm->get('name',$template_name);
+                if($ret && empty($opts['update'])) {
+                    $this->jerr("use --update   to update the template..");
+                }
+            }
+            $mailtext = file_get_contents($opts['file']);
+        } else {
+            $template_name = $opts['name'];
+            $mailtext =  $opts['raw_content'];
         }
         
-        $mailtext = file_get_contents($opts['file']);
-        
         if (!empty($opts['master'])) {
             $body = $mailtext;
             $mailtext = file_get_contents($opts['master']);
             $mailtext = str_replace('{outputBody():h}', $body, $mailtext);
         }
         
+        if($mapping) {
+            foreach ($mapping as $k => $v) {
+                $mailtext = str_replace($k, $v, $mailtext);
+            }
+        }
+        
         require_once 'Mail/mimeDecode.php';
         require_once 'Mail/RFC822.php';
         
         $decoder = new Mail_mimeDecode($mailtext);
         $parts = $decoder->getSendArray();
+        $structure = $decoder->decode(array(
+            'include_bodies' => true,
+            'decode_bodies' => true
+        ));
         if (is_a($parts,'PEAR_Error')) {
             echo $parts->toString() . "\n";
             exit;
         }
-        
+    
         $headers = $parts[1];
         $from = new Mail_RFC822();
         $from_str = $from->parseAddressList($headers['From']);
+        if (is_a($from_str,'PEAR_Error')) {
+            echo $from_str->toString() . "\n";
+            exit;
+        }
+
         
         $from_name  = trim($from_str[0]->personal, '"');
         
         $from_email = $from_str[0]->mailbox . '@' . $from_str[0]->host;
         
-     
         
+        $bodyhtml  = '';
+        $bodytext  = '';
+        if (empty($opts['use-file'])) {
+            
+            switch($structure->ctype_primary .'/'. $structure->ctype_secondary ) {
+                case 'multipart/alternative':
+                    foreach($structure->parts as $p) {
+                        switch($p->ctype_primary .'/'. $p->ctype_secondary ) {
+                            case 'text/plain':
+                                $bodytext = $p->body;
+                                break;
+                        
+                            case 'text/html':
+                                $bodyhtml = $p->body;
+                                break;
+                                 // no default...
+                        }
+                            
+                    }
+                    break;
+                case 'text/plain':
+                    $bodytext = $parts[2];
+                    break;
+                
+                case 'text/html':
+                
+                    $bodyhtml = $parts[2];
+                    break;
+                default:
+                    var_dump($structure->ctype_primary .'/'. $structure->ctype_secondary );
+                    die("UNKNOWN TYPE");
+            }
+            
+          
+        }
         
-        if ($c->id) {
-            $cc =clone($c);
-            $c->setFrom(array(
-               'bodytext'      => $parts[2],
-               'updated_dt'     => date('Y-m-d H:i:s'),   
+        
+        
+        
+        if ($cm->id) {
+            
+            $cc =clone($cm);
+            $cm->setFrom(array(
+               'bodytext'      => $bodyhtml,
+               'plaintext' => $bodytext,
+               'updated_dt'     => date('Y-m-d H:i:s'),
+               'use_file' => !empty($opts['use-file']) ? realpath($opts['file']) : '',
             ));
             
-            $c->update($cc);
+            $cm->update($cc);
         } else {
             
-              $c->setFrom(array(
+            $cm->setFrom(array(
                 'from_name'     => $from_name,
                 'from_email'    => $from_email,
-                'subject'       => $parts[1]['Subject'],
+                'subject'       => $headers['Subject'],
                 'name'          => $template_name,
-                'bodytext'      => $parts[2],
+                'bodytext'      => $bodyhtml,
+                'plaintext'     => $bodytext,
                 'updated_dt'     => date('Y-m-d H:i:s'),
                 'created_dt'     => date('Y-m-d H:i:s'),
+                'use_file' => !empty($opts['use-file']) ? realpath($opts['file']) : '',
             ));
             
-            $c->insert();
+            $cm->insert();
         }
+        return $cm;
     }
     function output() {
         die("done\n");