DataObjects/Core_watch.php
[Pman.Core] / DataObjects / Core_email.php
index e6c2e01..e167a74 100644 (file)
@@ -136,15 +136,15 @@ class Pman_Core_DataObjects_Core_email extends DB_DataObject
             $i->update($ii);
         }
         
-//        $this->cachedMailWithOutImages(true, (get_class($this) == 'Pman_Core_DataObjects_Core_email') ? false : true);
+        $this->cachedMailWithOutImages(true, (get_class($this) == 'Pman_Core_DataObjects_Core_email') ? false : true);
 //        $this->cachedMailWithOutImages(true, false);
        
     }
     
-//    function onUpdate($old, $q,$roo)
-//    {
-//        $this->cachedMailWithOutImages(true, false);
-//    }
+    function onUpdate($old, $q,$roo)
+    {
+        $this->cachedMailWithOutImages(true, (get_class($this) == 'Pman_Core_DataObjects_Core_email') ? false : true);
+    }
 
 
     function attachmentIds()
@@ -178,6 +178,8 @@ class Pman_Core_DataObjects_Core_email extends DB_DataObject
     
     function processRelacements($replace_links = true)
     {   
+        $cfg = isset(HTML_FlexyFramework::get()->Pman_Crm) ? HTML_FlexyFramework::get()->Pman_Crm : false;
+        
         libxml_use_internal_errors (true);
         $doc = new DOMDocument('1.0', 'UTF-8');
         $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
@@ -192,29 +194,96 @@ class Pman_Core_DataObjects_Core_email extends DB_DataObject
             }
         }
         
+        $unsubscribe = false;
+        
+        if(!empty($cfg)){
+            $unsubscribe = $cfg ['server_baseurl'] . '/Crm/Unsubscribe/' . $this->id . '/{person.id}';
+        }
+       
+        foreach ($xpath->query('//a[@href]') as $a) { 
+            
+            $href = $a->getAttribute('href');
+            
+            if(preg_match('/#unsubscribe/', $href) && !empty($unsubscribe)){
+                $a->setAttribute('href', $unsubscribe);
+                continue;
+            }
+            
+            if(!preg_match('/^http(.*)/', $href)){
+                continue;
+            }
+            if (!$replace_links) {
+                continue;
+            }
+            $link = DB_DataObject::factory('crm_mailing_list_link');
+            $link->setFrom(array(
+                'url' => $href
+            ));
+            
+            if(!$link->find(true)){
+                $link->insert();
+            }
+            
+            if(!$link->id){
+                continue;
+            }
+            
+            $l = $cfg ['server_baseurl'] . '/Crm/Link/' .$this->id . '/' . $link->id . '/{person.id}.html';
+            
+            $a->setAttribute('href', $l);
+            
+        }
+        
+        if(!empty($unsubscribe)){
+            $element = $doc->createElement('img');
+        
+            $element->setAttribute('src', $cfg ['server_baseurl']  . '/Crm/Open/' . $this->id . '/{person.id}.html');
+            $element->setAttribute('width', '1');
+            $element->setAttribute('height', '1');
+
+            $html = $doc->getElementsByTagName('html');
+            $html->item(0)->appendChild($element);
+            
+            $this->plaintext = str_replace("{unsubscribe_link}", $unsubscribe, $this->plaintext);
+        }
+        
+        
         $this->bodytext = $doc->saveHTML();
         
         libxml_use_internal_errors (false);
         
         $this->bodytext = str_replace('%7B', '{', $this->bodytext ); // kludge as template is not interpretated as html.
         $this->bodytext = str_replace('%7D', '}', $this->bodytext ); // kludge as template is not interpretated as html.
-        
+         
         return;
     }
     
-    function send($obj)
-    {    
+    function send($obj, $force = true, $send = true)
+    {   
         $contents = (array)$obj;
         
-        $this->cachedMailWithOutImages(true, false);
+        if(empty($this->id)){
+            $this->get('name', $contents['template']);
+        }
+        
+        if(empty($this->id)){
+            return PEAR::raiseError("template [{$contents['template']}]has not been set");
+        }
         
         $contents['subject'] = $this->subject;
         
         $ui = posix_getpwuid(posix_geteuid());
         
+        $cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.txt';
+        
+        if(!$this->isGenerated($cachePath)){
+            $this->cachedMailWithOutImages($force, false);
+        }
+         
         require_once 'Pman/Core/Mailer.php';
         
         $templateDir = session_save_path() . '/email-cache-' . $ui['name'] ;
+        
         $r = new Pman_Core_Mailer(array(
             'template'=> $this->tableName() . '-' . $this->id,
             'templateDir' => $templateDir,
@@ -225,22 +294,22 @@ class Pman_Core_DataObjects_Core_email extends DB_DataObject
         $imageCache = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt';
         
         if(file_exists($imageCache) && filesize($imageCache)){
-            $images = json_decode(file_get_contents($imageCache));
-            echo "<PRE>";print_r($images);exit;
-            $r->images = array_merge($r->images, $images);
+            $images = json_decode(file_get_contents($imageCache), true);
+            $r->images = $images;
         }
         
         $ret = $r->toData();
         
+        if(!$send){
+            return $ret;
+        }
         
         return $r->send($ret);
     }
     
     function cachedMailWithOutImages($force = false, $replace_links = true)
     {  
-        $random_hash = md5(date('r', time()));
         
-        $this->cachedImages($random_hash);
         
         $ui = posix_getpwuid(posix_geteuid());
         
@@ -254,7 +323,9 @@ class Pman_Core_DataObjects_Core_email extends DB_DataObject
             mkdir(dirname($cachePath), 0700, true);
         }
         
-        $this->processRelacements($replace_links);
+        $random_hash = md5(date('r', time()));
+        
+        $this->cachedImages();
         
         $fh = fopen($cachePath, 'w');
 
@@ -279,8 +350,11 @@ Content-Transfer-Encoding: 7bit
 ");
         fclose($fh);
         
+        // cache body
+        
+        $this->processRelacements($replace_links);
+        
         $cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.body.html';
-          
         
         if (!file_exists(dirname($cachePath))) {
             mkdir(dirname($cachePath), 0700, true);
@@ -290,7 +364,7 @@ Content-Transfer-Encoding: 7bit
         
     }
     
-    function cachedImages($random_hash)
+    function cachedImages()
     {
         $ui = posix_getpwuid(posix_geteuid());
         
@@ -316,13 +390,6 @@ Content-Transfer-Encoding: 7bit
             if (!file_exists($i->getStoreName()) || !filesize($i->getStoreName())) {
                 continue;
             }
-//            $out = chunk_split(base64_encode(file_get_contents($i->getStoreName())));
-//            if (empty($out)) {
-//                continue;
-//            }
-            
-//            $imgfn = urlencode(preg_replace('/#.*$/i', '' , $i->filename));
-            
             
             $images["attachment-$i->id"] = array(
                 'file' => $i->getStoreName(),
@@ -330,19 +397,9 @@ Content-Transfer-Encoding: 7bit
                 'ext' => $y->toExt($i->mimetype),
                 'contentid' => "attachment-$i->id"
             );
-//            
-//            
-//            fwrite($fh, "--rel-{$random_hash}
-//Content-Type: {$i->mimetype}; name={$imgfn}
-//Content-Transfer-Encoding: base64
-//Content-ID: <attachment-{$i->id}>
-//Content-Disposition: inline; filename={$imgfn}
-//
-//" . $out  . "");
-
-            }
+        }
             
-            file_put_contents($imageCache, json_encode($images));
+        file_put_contents($imageCache, json_encode($images));
         
     }