DataObjects/core.sql
[Pman.Core] / DataObjects / Core_mailing_list_message.php
1 <?php
2 /**
3  * Table Definition for core_mailinglist_message
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_mailing_list_message extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11   
12     public $__table = 'core_mailing_list_message';    // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $name;                            // 
15     public $subject;                         // blob(65535)  blob
16     public $bodytext;                        // blob(65535)  blob
17     public $plaintext;
18     public $updated_dt;                      //datetime not_null
19     public $from_email;
20     public $from_name;
21     public $owner_id;
22     public $is_system;
23
24     /* the code above is auto generated do not remove the tag below */
25     ###END_AUTOCODE
26     
27     function applyFilters($q, $au, $roo)
28     {
29         if(!empty($q['search']['nameortitle'])){
30             $this->whereAdd("
31                 name LIKE '%{$this->escape($q['search']['nameortitle'])}%'
32                 OR
33                 subject LIKE '%{$this->escape($q['search']['nameortitle'])}%'
34             ");
35         }
36     }
37     
38     function beforeDelete($dependants_array, $roo)
39     {   
40         $i = DB_DataObject::factory('Images');
41         $i->onid = $this->id;
42         $i->ontable = $this->tableName();
43         $i->find();
44         while ($i->fetch()){
45             $i->beforeDelete();
46             $i->delete();
47         }
48         
49 //        $mlq = DB_DataObject::factory('crm_mailing_list_queue');
50 //        $mlq->message_id = $this->id;
51 //        $mlq->find();
52 //        while ($mlq->fetch()){
53 //            $mlq->beforeDelete();
54 //            $mlq->delete();
55 //        }
56         
57         
58     }
59     function beforeUpdate($old, $request,$roo)
60     {   
61         if (!empty($request['_make_copy'])) {
62             $this->makeCopy($roo);
63             
64         }
65          
66        
67     }
68     
69     function makeCopy($roo)
70     {
71         $c = DB_DataObject::Factory($this->tableName());
72         $c->setFrom($this);
73         $c->name = "COPY of " . $this->name;
74         $c->updated_dt = $this->sqlValue('NOW()');
75         
76         $id = $c->insert();
77         $c = DB_DataObject::Factory($this->tableName());
78         $c->get($id);
79         
80         
81         // copy images.
82         
83         $i = DB_DataObject::factory('Images');
84         $i->onid = $this->id;
85         $i->ontable = $this->tableName();
86         $i->find();
87         while ($i->fetch()){
88             
89             $new_image = DB_DataObject::factory('Images');
90             $new_image->onid = $c->id;
91             $new_image->ontable = $this->tableName();
92             $new_image->createFrom($i->getStoreName(), $i->filename);
93             
94             $map[$i->id] = $new_image->id;
95         }
96         
97         
98         libxml_use_internal_errors (true);
99         $doc = new DOMDocument('1.0', 'UTF-8');
100         $doc->loadHTML('<?xml encoding="UTF-8"><HTML><BODY>'.$this->bodytext.'</BODY></HTML>');
101         $doc->formatOutput = true;
102
103        //echo '<PRE>'; print_R($doc);
104         
105         
106         $xpath = new DOMXpath($doc);
107         foreach ($xpath->query('//img[@src]') as $img) {
108             $href = $img->getAttribute('src');
109             //var_dump($href);
110             $matches = array();
111             if (preg_match("/Images\/([0-9]+)\/([^#]+)\#image\-([0-9]+)$/", $href, $matches)) {
112                  
113                 $oid = $matches[1];
114                 
115                 if (!isset($map[$oid])) {
116                     //echo "skip no new id for $oid";
117                     continue;
118                 }
119                 $nid = $map[$oid];
120                 $nstr = "/Images/$nid/{$matches[2]}/#image-{$nid}";
121                 
122                 $img->setAttribute('src',  str_replace($matches[0], $nstr, $href ));
123                     
124                  
125             }
126         }
127         $cc = clone($c);
128         $c->bodytext = $doc->saveHTML();
129         $c->update($cc);
130         libxml_use_internal_errors (false);
131         
132         
133         $roo->jok("duplicated");
134         
135         
136         
137         
138     }
139     
140     
141     function onInsert($request,$roo)
142     {   
143         $i = DB_DataObject::factory('Images');
144         $i->whereAdd('onid = 0');
145         $i->find();
146         while ($i->fetch()){
147             $i->onid = $this->id;
148             $i->update();
149         }
150        
151     }
152     
153     
154     function attachmentIds()
155     {
156         
157          $roo = HTML_FlexyFramework::get()->page;
158         
159         libxml_use_internal_errors (true);
160         $doc = new DOMDocument('1.0', 'UTF-8');
161         $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
162         
163         $xpath = new DOMXpath($doc);
164         $ret = array();
165         
166         foreach ($xpath->query('//img[@src]') as $img) { // process images!
167             $href = $img->getAttribute('src');
168             $cid = explode('#', $href);
169             if(!isset($cid[1])){
170                 continue;
171             }
172             $cid = explode('-', $cid[1]);
173             if (!isset($cid[1])||!is_numeric($cid[1])) {
174                 continue;
175             }
176             $ret[] = $cid[1];
177         }
178        // print_r($ret);
179         return $ret;
180     }
181     /**
182      * process replacements is run to generate a template - not the final content..
183      *
184      */
185     
186     function processRelacements($replace_links = true)
187     {
188         $roo = HTML_FlexyFramework::get()->page;
189         
190         $cfg = HTML_FlexyFramework::get()->Pman_Crm;
191         
192         libxml_use_internal_errors (true);
193         $doc = new DOMDocument('1.0', 'UTF-8');
194         $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
195         
196         $xpath = new DOMXpath($doc);
197         
198         foreach ($xpath->query('//img[@src]') as $img) { // process images!
199             $href = $img->getAttribute('src');
200             $cid = explode('#', $href);
201             if(isset($cid[1])){
202                 $img->setAttribute('src', 'cid:' . $cid[1]);
203             }
204         }
205         $unsubscribe = $cfg ['server_baseurl'] . '/Crm/Unsubscribe/' . $this->id . '/{person.id}';
206         
207        
208         foreach ($xpath->query('//a[@href]') as $a) { 
209             
210             $href = $a->getAttribute('href');
211             
212             if(preg_match('/#unsubscribe/', $href)){
213                 $a->setAttribute('href', $unsubscribe);
214                 continue;
215             }
216             
217             if(!preg_match('/^http(.*)/', $href)){
218                 continue;
219             }
220             if (!$replace_links) {
221                 continue;
222             }
223             $link = DB_DataObject::factory('crm_mailing_list_link');
224             $link->setFrom(array(
225                 'url' => $href
226             ));
227             
228             if(!$link->find(true)){
229                 $link->insert();
230             }
231             
232             if(!$link->id){
233                 continue;
234             }
235             
236             $l = $cfg ['server_baseurl'] . '/Crm/Link/' .$this->id . '/' . $link->id . '/{person.id}.html';
237             
238             $a->setAttribute('href', $l);
239             
240         }
241         
242         $element = $doc->createElement('img');
243         
244         $element->setAttribute('src', $cfg ['server_baseurl']  . '/Crm/Open/' . $this->id . '/{person.id}.html');
245         $element->setAttribute('width', '1');
246         $element->setAttribute('height', '1');
247         
248         $html = $doc->getElementsByTagName('html');
249         $html->item(0)->appendChild($element);
250         
251         $this->bodytext = $doc->saveHTML();
252         
253         libxml_use_internal_errors (false);
254         
255         /*
256         $this->bodytext = str_replace("{person.firstname}", htmlspecialchars($person->firstname), $this->bodytext);
257         $this->bodytext = str_replace("{person.lastname}", htmlspecialchars($person->lastname), $this->bodytext);
258         $this->bodytext = str_replace("{person.name}", htmlspecialchars($person->name), $this->bodytext);
259          
260         
261         $this->plaintext = str_replace("{person.firstname}", $person->firstname, $this->plaintext);
262         $this->plaintext = str_replace("{person.lastname}", $person->lastname, $this->plaintext);
263         $this->plaintext = str_replace("{person.name}", $person->name, $this->plaintext);
264         */
265         $this->plaintext = str_replace("{unsubscribe_link}", $unsubscribe, $this->plaintext);
266         $this->bodytext = str_replace('%7B', '{', $this->bodytext ); // kludge as template is not interpretated as html.
267         $this->bodytext = str_replace('%7D', '}', $this->bodytext ); // kludge as template is not interpretated as html.
268          
269         
270         
271         return;
272     }
273     function send($obj)
274     {
275         
276          
277         $contents = (array)$obj;
278         
279         $q = DB_DataObject::factory('crm_mailing_list_queue');
280         $q->id = 'test-message-'. $this->id;
281         $q->message_id = $this->id;
282         $q->message_id_subject = $this->subject;
283         $q->message_id_from_email = $this->from_email;
284         $q->message_id_from_name = $this->from_name;
285         
286         $q->cachedMailWithOutImages(true, false);
287         
288         $contents['subject'] = $this->subject;
289         
290         require_once 'Pman/Core/Mailer.php';
291         
292         $templateDir = session_save_path() . '/email-cache-' . get_current_user() ;
293         $r = new Pman_Core_Mailer(array(
294             'template'=> $q->id,
295             'templateDir' => $templateDir,
296             'page' => $q,
297             'contents' => $contents
298             //array(
299             //    'person' => $person,
300             //    'subject' => $this->message_id_subject,
301            // )
302         ));
303         
304         
305          
306         ///print_r($r->toData());
307         $ret = $r->toData();
308         $images = file_get_contents(session_save_path() . '/email-cache-' . get_current_user() . '/mail/' . $q->id . '-images.txt');
309        // var_dump($images);exit;
310         
311         $ret['body'] = str_replace('%Images%', $images, $ret['body']);
312         
313         return $r->send($ret);
314     }
315     
316 }