DataObjects/Core_watch.php
[Pman.Core] / DataObjects / Core_email.php
index e668cad..e167a74 100644 (file)
@@ -136,14 +136,14 @@ 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, false);
+        $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);
+        $this->cachedMailWithOutImages(true, (get_class($this) == 'Pman_Core_DataObjects_Core_email') ? false : true);
     }
 
 
@@ -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,20 +194,82 @@ 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;
         
+        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());
@@ -213,12 +277,13 @@ class Pman_Core_DataObjects_Core_email extends DB_DataObject
         $cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.txt';
         
         if(!$this->isGenerated($cachePath)){
-            $this->cachedMailWithOutImages(true, false);
+            $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,
@@ -235,6 +300,10 @@ class Pman_Core_DataObjects_Core_email extends DB_DataObject
         
         $ret = $r->toData();
         
+        if(!$send){
+            return $ret;
+        }
+        
         return $r->send($ret);
     }