fix #7121 - crm fixes to email
[Pman.Core] / DataObjects / Core_email.php
1 <?php
2 /**
3  * Table Definition for core_email
4  */
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_email 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_email';    // 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     public $active;
24     public $bcc_group_id;
25     public $test_class;
26      
27     /* the code above is auto generated do not remove the tag below */
28     ###END_AUTOCODE
29     
30     function applyFilters($q, $au, $roo)
31     {
32         $tn = $this->tableName();
33         
34         if(!empty($q['search']['nameortitle'])){
35             $this->whereAdd("
36                 $tn.name LIKE '%{$this->escape($q['search']['nameortitle'])}%'
37                 OR
38                 $tn.subject LIKE '%{$this->escape($q['search']['nameortitle'])}%'
39             ");
40         }
41         $cgm = DB_DataObject::Factory('core_group_member')->tableName();;
42       
43         $this->selectAdd("
44             (
45                 SELECT 
46                     count(id) 
47                 FROM 
48                     {$cgm}
49                 WHERE 
50                     to_group_id = {$cgm}.group_id
51             )  AS group_member_count,
52             
53             (
54                 SELECT 
55                     count(id) 
56                 FROM 
57                     {$cgm}
58                 WHERE 
59                     bcc_group_id = {$cgm}.group_id
60             )  AS bcc_group_member_count
61         ");
62
63         $this->selectAdd("
64             (
65                 SELECT 
66                     COUNT(DISTINCT(crm_person_id))
67                 FROM 
68                     core_notify cn 
69                 WHERE
70                     evtype = 'MAIL'
71                 AND 
72                     person_table = 'crm_person'
73                 AND 
74                     crm_person_id != 0 
75                 AND
76                     ontable = 'crm_mailing_list_queue'
77                 AND 
78                     onid IN 
79                     (
80                         SELECT 
81                             id
82                         FROM 
83                             crm_mailing_list_queue cmlq 
84                         WHERE message_id = crm_mailing_list_message.id
85                     )
86                 AND 
87                     event_id > 0
88                 AND 
89                     sent IS NOT NULL
90             )  
91             +
92             (
93                 SELECT 
94                     COUNT(DISTINCT(person_id))
95                 FROM 
96                     core_notify cn 
97                 WHERE
98                     evtype = 'MAIL'
99                 AND 
100                     person_table = 'Person'
101                 AND 
102                     person_id != 0 
103                 AND
104                     ontable = 'crm_mailing_list_queue'
105                 AND 
106                     onid IN 
107                     (
108                         SELECT 
109                             id
110                         FROM 
111                             crm_mailing_list_queue cmlq 
112                         WHERE message_id = crm_mailing_list_message.id
113                     )
114                 AND 
115                     event_id > 0
116                 AND 
117                     sent IS NOT NULL
118             ) 
119             AS sent_to_count
120         ");
121
122         $this->selectAdd("
123             (
124                 SELECT 
125                     COUNT(DISTINCT(crm_person_id))
126                 FROM 
127                     core_notify cn 
128                 WHERE
129                     evtype = 'MAIL'
130                 AND 
131                     person_table = 'crm_person'
132                 AND 
133                     crm_person_id != 0 
134                 AND
135                     ontable = 'crm_mailing_list_queue'
136                 AND 
137                     onid IN 
138                     (
139                         SELECT 
140                             id
141                         FROM 
142                             crm_mailing_list_queue cmlq 
143                         WHERE message_id = crm_mailing_list_message.id
144                     )
145                 AND 
146                     event_id > 0
147                 AND 
148                     sent IS NOT NULL
149                 AND 
150                     is_open = 1
151             )  
152             +
153             (
154                 SELECT 
155                     COUNT(DISTINCT(person_id))
156                 FROM 
157                     core_notify cn 
158                 WHERE
159                     evtype = 'MAIL'
160                 AND 
161                     person_table = 'Person'
162                 AND 
163                     person_id != 0 
164                 AND
165                     ontable = 'crm_mailing_list_queue'
166                 AND 
167                     onid IN 
168                     (
169                         SELECT 
170                             id
171                         FROM 
172                             crm_mailing_list_queue cmlq 
173                         WHERE message_id = crm_mailing_list_message.id
174                     )
175                 AND 
176                     event_id > 0
177                 AND 
178                     sent IS NOT NULL
179                 AND 
180                     is_open = 1
181             ) 
182             AS opened_by_count
183         ");
184
185         $this->selectAdd("
186         (
187             SELECT 
188                 MAX(sent)
189             FROM 
190                 core_notify cn 
191             WHERE
192                 evtype = 'MAIL'
193             AND
194                 ontable = 'crm_mailing_list_queue'
195             AND 
196                 onid IN 
197                 (
198                     SELECT 
199                         id
200                     FROM 
201                         crm_mailing_list_queue cmlq 
202                     WHERE message_id = crm_mailing_list_message.id
203                 )
204             AND 
205                 event_id > 0
206             AND 
207                 sent IS NOT NULL
208         ) 
209         AS last_sent
210         ");
211         
212         if (!empty($_REQUEST['_hide_system_emails'])) {
213             $this->whereAddIn("!{$this->tableName()}.name", array('EVENT_ERRORS_REPORT'), 'string');
214         }
215         
216     }
217     
218     function beforeDelete($dependants_array, $roo)
219     {   
220         $i = DB_DataObject::factory('Images');
221         $i->onid = $this->id;
222         $i->ontable = $this->tableName();
223         $i->find();
224         while ($i->fetch()){
225             $i->beforeDelete();
226             $i->delete();
227         }
228     }
229     
230     function beforeUpdate($old, $request,$roo)
231     {   
232         if (!empty($request['_make_copy'])) {
233             $this->makeCopy($roo);
234             
235         }
236         
237         if ($this->to_group_id != -1) {
238                    
239             $c = DB_DataObject::factory('core_group_member');            
240             $c->group_id = $this->to_group_id;
241                         
242             if (!$c->count() && empty($request['_ignore_group_count'])) {
243                 $roo->jerr('Failed to create email template - No member found in recieptent group',array('errcode'=> 100));
244             }
245         }
246     }
247     
248     function makeCopy($roo)
249     {
250         $c = DB_DataObject::Factory($this->tableName());
251         $c->setFrom($this);
252         $c->name = "COPY of " . $this->name;
253         $c->updated_dt = $this->sqlValue('NOW()');
254         
255         $id = $c->insert();
256         $c = DB_DataObject::Factory($this->tableName());
257         $c->get($id);
258         
259         
260         // copy images.
261         
262         $i = DB_DataObject::factory('Images');
263         $i->onid = $this->id;
264         $i->ontable = $this->tableName();
265         $i->find();
266         while ($i->fetch()){
267             
268             $new_image = DB_DataObject::factory('Images');
269             $new_image->onid = $c->id;
270             $new_image->ontable = $this->tableName();
271             $new_image->createFrom($i->getStoreName(), $i->filename);
272             
273             $map[$i->id] = $new_image->id;
274         }
275         
276         
277         libxml_use_internal_errors (true);
278         $doc = new DOMDocument('1.0', 'UTF-8');
279         $doc->loadHTML('<?xml encoding="UTF-8"><HTML><BODY>'.$this->bodytext.'</BODY></HTML>');
280         $doc->formatOutput = true;
281
282        //echo '<PRE>'; print_R($doc);
283         
284         
285         $xpath = new DOMXpath($doc);
286         foreach ($xpath->query('//img[@src]') as $img) {
287             $href = $img->getAttribute('src');
288             //var_dump($href);
289             $matches = array();
290             if (preg_match("/Images\/([0-9]+)\/([^#]+)\#image\-([0-9]+)$/", $href, $matches)) {
291                  
292                 $oid = $matches[1];
293                 
294                 if (!isset($map[$oid])) {
295                     //echo "skip no new id for $oid";
296                     continue;
297                 }
298                 $nid = $map[$oid];
299                 $nstr = "/Images/$nid/{$matches[2]}/#image-{$nid}";
300                 
301                 $img->setAttribute('src',  str_replace($matches[0], $nstr, $href ));
302                     
303                  
304             }
305         }
306         $cc = clone($c);
307         $c->bodytext = $doc->saveHTML();
308         $c->update($cc);
309         libxml_use_internal_errors (false);
310         
311         
312         $roo->jok("duplicated");
313     }
314     
315     function onInsert($q,$roo)
316     {   
317         $i = DB_DataObject::factory('Images');
318         $i->onid = 0;
319         $i->ontable = $this->tableName();
320         $i->find();
321         while ($i->fetch()){
322             $ii = clone ($i);
323             $i->onid = $this->id;
324             $i->update($ii);
325         }
326         
327         $this->cachedMailWithOutImages(true, (get_class($this) == 'Pman_Core_DataObjects_Core_email') ? false : true);
328 //        $this->cachedMailWithOutImages(true, false);
329        
330     }
331     
332     function onUpdate($old, $q,$roo)
333     {
334         $this->cachedMailWithOutImages(true, (get_class($this) == 'Pman_Core_DataObjects_Core_email') ? false : true);
335     }
336
337
338     function attachmentIds()
339     {
340         libxml_use_internal_errors (true);
341         $doc = new DOMDocument('1.0', 'UTF-8');
342         $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
343         
344         $xpath = new DOMXpath($doc);
345         $ret = array();
346         
347         foreach ($xpath->query('//img[@src]') as $img) { // process images!
348             $href = $img->getAttribute('src');
349             $cid = explode('#', $href);
350             if(!isset($cid[1])){
351                 continue;
352             }
353             $cid = explode('-', $cid[1]);
354             if (!isset($cid[1])||!is_numeric($cid[1])) {
355                 continue;
356             }
357             $ret[] = $cid[1];
358         }
359         
360         return $ret;
361     }
362     /**
363      * process replacements is run to generate a template - not the final content..
364      *
365      */
366     
367     function processRelacements($replace_links = true)
368     {   
369         $cfg = isset(HTML_FlexyFramework::get()->Pman_Crm) ? HTML_FlexyFramework::get()->Pman_Crm : false;
370         
371         libxml_use_internal_errors (true);
372         $doc = new DOMDocument('1.0', 'UTF-8');
373         $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
374         
375         $xpath = new DOMXpath($doc);
376         
377         foreach ($xpath->query('//img[@src]') as $img) { // process images!
378             $href = $img->getAttribute('src');
379             $hash = explode('#', $href);
380             // we name all our cid's as attachment-*
381             // however the src url may be #image-*
382             
383             
384             if(!isset($hash[1])){
385                 continue;
386             }
387             $cid = explode('-', $hash[1]);
388             if(!empty($cid[1])){
389                 $img->setAttribute('src', 'cid:attachment-' . $cid[1]);
390             }
391         }
392         
393         $unsubscribe = $this->unsubscribe_url();
394         
395         foreach ($xpath->query('//a[@href]') as $a) { 
396             
397             $href = $a->getAttribute('href');
398             
399             if(preg_match('/#unsubscribe/', $href) && !empty($unsubscribe)){
400                 $a->setAttribute('href', $unsubscribe);
401                 continue;
402             }
403             
404             if(!preg_match('/^http(.*)/', $href)){
405                 continue;
406             }
407             if (!$replace_links) {
408                 continue;
409             }
410             if (empty($cfg)) {
411                 continue;
412             }
413             // not available if server_baseurl not set... and crm module not used.
414             $link = DB_DataObject::factory('crm_mailing_list_link');
415             $link->setFrom(array(
416                 'url' => $href
417             ));
418             
419             if(!$link->find(true)){
420                 $link->insert();
421             }
422             
423             if(!$link->id){
424                 continue;
425             }
426             
427             $l = $cfg ['server_baseurl'] . '/Crm/Link/' .$this->id . '/' . $link->id . '/{person.id}.html';
428             
429             $a->setAttribute('href', $l);
430             
431         }
432         
433         if(!empty($unsubscribe) && !empty($cfg)){
434             $element = $doc->createElement('img');
435             $element->setAttribute('mailembed', 'no');
436             $element->setAttribute('src', $cfg ['server_baseurl']  . '/Crm/Open/' . $this->id . '/{person.id}.html');
437             $element->setAttribute('width', '1');
438             $element->setAttribute('height', '1');
439
440             $html = $doc->getElementsByTagName('html');
441             if ($html->length) {
442                 $html->item(0)->appendChild($element);
443             }
444             
445             $this->plaintext = str_replace("{unsubscribe_link}", $unsubscribe, $this->plaintext);
446         }
447         
448         
449         $this->bodytext = $doc->saveHTML();
450         
451         libxml_use_internal_errors (false);
452         
453         $this->bodytext = str_replace('%7B', '{', $this->bodytext ); // kludge as template is not interpretated as html.
454         $this->bodytext = str_replace('%7D', '}', $this->bodytext ); // kludge as template is not interpretated as html.
455          
456         return;
457     }
458     
459     function unsubscribe_url()
460     {
461         $unsubscribe = false;
462         
463         $cfg = isset(HTML_FlexyFramework::get()->Pman_Crm) ? HTML_FlexyFramework::get()->Pman_Crm : false;
464         
465         if(!empty($cfg)){
466             $unsubscribe = $cfg ['server_baseurl'] . '/Crm/Unsubscribe/' . $this->id . '/{person.id}';
467         }
468         
469         return $unsubscribe;
470     }
471     
472     /**
473      * convert email with contents into a core mailer object. - ready to send..
474      * @param Object|Array $obj Object (or array) to send @see Pman_Core_Mailer
475      *    + subject
476      *    + rcpts || person   << if person is set - then it goes to them...
477      *    + rcpts_group (string) << name of group - normally to send admin emails.. (if set, then bcc_group is ignored.)
478      *    + replace_links
479      *    + template
480      *    + mailer_opts
481      *    + person << who it actually goes to..
482      *    
483      * @param bool $force - force re-creation of cached version of email.
484      *
485      * @returns Pman_Core_Mailer||PEAR_Error
486      */
487     
488     function toMailer($obj,$force=false)
489     {
490         require_once 'PEAR.php';
491         
492         $p = new PEAR();
493         $contents = (array)$obj;
494         
495         if(empty($this->id) && !empty($contents['template'])){
496             $this->get('name', $contents['template']);
497         }
498              
499         
500         if(empty($this->active)){
501             return $p->raiseError("template [{$contents['template']}] is Disabled");
502         }
503         
504         
505         if(empty($this->id)){
506             return $p->raiseError("template [{$contents['template']}] has not been set");
507         }
508         
509         // fill in BCC
510         
511         if (!empty($this->bcc_group_id) && $this->bcc_group_id > 0 && empty($contents['bcc']) && empty($contents['rcpts_group'])) {
512             $admin_grp = DB_DAtaObject::Factory('core_group')->load($this->bcc_group_id);
513             
514             $admin = $admin_grp ?  $admin_grp->members('email') : false;
515             
516             if (empty($admin) && $admin_grp->name != 'Empty Group') { // allow 'empty group mname'
517                 return $p->raiseError("template [{$contents['template']}] - bcc group is empty");
518             }
519             
520             $contents['bcc'] = $admin ;
521         }
522         if (!empty($contents['rcpts_group'])) {
523             
524             $admin = DB_DAtaObject::Factory('core_group')->lookupMembers($contents['rcpts_group'],'email');
525             
526             if (empty($admin)) {
527                 return $p->raiseError("Trying to send to {$contents['rcpts_group']} - group is empty");
528             }
529             $contents['rcpts'] = $admin;
530         }
531         if (empty($contents['rcpts']) && $this->to_group_id > 0) {
532             $members = $this->to_group()->members();
533             $contents['rcpts'] = array();
534             foreach($this->to_group()->members() as $m) {
535                 $contents['rcpts'][] = $m->email;
536             }
537             //var_dump($contents['rcpts']);
538             
539         }
540         //subject replacement
541         if(empty($contents['subject'])){
542            $contents['subject'] = $this->subject; 
543         }
544         
545         if (!empty($contents['subject_replace'])) {
546             
547             // do not use the mapping 
548             if (isset($contents['mapping'])) {
549                 foreach ($contents['mapping'] as $pattern => $replace) {
550                     $contents['subject'] = preg_replace($pattern,$replace,$contents['subject']);
551                 }
552             }
553             
554             foreach ($contents as $k => $v) {
555                 if (is_string($v)) {
556                     $contents['subject'] = str_replace('{'. $k . '}', $v, $contents['subject']);
557                 }
558             }
559         }
560         
561         if(!empty($contents['rcpts']) && is_array($contents['rcpts'])){
562             $contents['rcpts'] = implode(',', $contents['rcpts']);
563         }     
564         
565         $ui = posix_getpwuid(posix_geteuid());
566         
567         $cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.txt';
568         
569         if($force || !$this->isGenerated($cachePath)){
570             $this->cachedMailWithOutImages($force, empty($contents['replace_links']) ? false : $contents['replace_links']);
571         }
572          
573         require_once 'Pman/Core/Mailer.php';
574         
575         $templateDir = session_save_path() . '/email-cache-' . $ui['name'] ;
576         
577         $cfg = array(
578             'template'=> $this->tableName() . '-' . $this->id,
579             'templateDir' => $templateDir,
580             'page' => $this,
581             'contents' => $contents,
582             'css_embed' => true, // we should always try and do this with emails...
583         );
584         
585         if (isset($contents['rcpts'])) {
586             $cfg['rcpts'] = $contents['rcpts'];
587         }
588         
589         if (isset($contents['attachments'])) {
590             $cfg['attachments'] = $contents['attachments'];
591         }
592         
593         if (isset($contents['mailer_opts']) && is_array($contents['mailer_opts'])) {
594             $cfg = array_merge($contents['mailer_opts'], $cfg);
595         }
596         
597         if(isset($contents['css_inline'])){
598             $cfg['css_inline'] = $contents['css_inline'];
599         }
600         
601         $r = new Pman_Core_Mailer($cfg);
602         
603         $imageCache = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt';
604         
605         if(file_exists($imageCache) && filesize($imageCache)){
606             $images = json_decode(file_get_contents($imageCache), true);
607             $r->images = $images;
608         }
609         
610         return $r;
611     }
612     function toMailerData($obj,$force=false)
613     {   
614         $r = $this->toMailer($obj, $force);
615         if (is_a($r, 'PEAR_Error')) {
616             return $r;
617         }
618         return $r->toData();
619     }
620     
621     /**
622      *
623      * DEPRICATED !!! - DO NOT USE THIS !!!
624      *
625      * use: toMailerData() -- to return the email data..
626      * or
627      * $mailer = $core_email->toMailer($obj, false);
628      * $sent = is_a($mailer,'PEAR_Error') ? false : $mailer->send();
629
630      * toMailer($obj, false)->send()
631      *
632      * 
633      */
634     
635     function send($obj, $force = true, $send = true)
636     {   
637         if (!$send) {
638             return $this->toMailerData($obj,$force);
639         }
640         
641         $r = $this->toMailer($obj, $force);
642         
643         if (is_a($r, 'PEAR_Error')) {
644             return $r;
645         }
646         
647         return $r->send();
648     }
649     
650     function cachedMailWithOutImages($force = false, $replace_links = true)
651     {  
652         
653         $ui = posix_getpwuid(posix_geteuid());
654         
655         $cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.txt';
656           
657         if (!$force && $this->isGenerated($cachePath)) {
658             return;
659         }
660         
661         if (!file_exists(dirname($cachePath))) {
662             mkdir(dirname($cachePath), 0700, true);
663         }
664         
665         $random_hash = md5(date('r', time()));
666         
667         $this->cachedImages();
668         
669         $fh = fopen($cachePath, 'w');
670
671         fwrite($fh, implode("\n", array(
672             "From: {if:t.messageFrom}{t.messageFrom:h}{else:}{t.messageFrom():h}{end:}",
673             "To: {if:t.person}{t.person.getEmailFrom():h}{else:}{rcpts:h}{end:}",
674             "Subject: {t.subject:h} ",
675             "X-Message-ID: {t.id} ",
676             "{if:t.replyTo}Reply-To: {t.replyTo:h}{end:}",
677             "{if:t.mailgunVariables}X-Mailgun-Variables: {t.mailgunVariables:h}{end:}"
678         ))."\n");
679         
680         
681 // note the extra space to finish the last line..
682         fwrite($fh, " " . "
683 Content-Type: multipart/alternative; boundary=alt-{$random_hash}
684
685 --alt-{$random_hash}
686 Content-Type: text/plain; charset=utf-8; format=flowed
687 Content-Transfer-Encoding: 7bit
688
689 {$this->plaintext}
690
691 ");
692         fclose($fh);
693         
694         // cache body
695         
696         $this->processRelacements($replace_links);
697         
698         $cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.body.html';
699         
700         if (!file_exists(dirname($cachePath))) {
701             mkdir(dirname($cachePath), 0700, true);
702         }
703         
704         if (empty($this->use_file)) {
705             file_put_contents($cachePath, $this->bodytext);
706             return;
707         }
708         // use-file -- uses the original template...
709         $mailtext = file_get_contents($this->use_file);        
710          
711         require_once 'Mail/mimeDecode.php';
712         require_once 'Mail/RFC822.php';
713         
714         $decoder = new Mail_mimeDecode($mailtext);
715         $parts = $decoder->getSendArray();
716         file_put_contents($cachePath,$parts[2]);
717          
718     }
719     
720     function cachedImages()
721     {
722         $ui = posix_getpwuid(posix_geteuid());
723         
724         $imageCache = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt';
725         
726         $ids = $this->attachmentIds();
727         
728          
729         $fh = fopen($imageCache, 'w');
730         
731         $i = DB_DataObject::factory('Images');
732         $i->onid = $this->id;
733         $i->ontable = $this->tableName();
734         $i->whereAddIn('id', $ids, 'int');
735         $i->find();
736         
737         $images = array();
738         
739         require_once 'File/MimeType.php';
740         $y = new File_MimeType();
741         
742         while ($i->fetch()){
743             if (!file_exists($i->getStoreName()) || !filesize($i->getStoreName())) {
744                 continue;
745             }
746             
747             $images["attachment-{$i->id}"] = array(
748                 'file' => $i->getStoreName(),
749                 'mimetype' => $i->mimetype,
750                 'ext' => $y->toExt($i->mimetype),
751                 'contentid' => "attachment-$i->id"
752             );
753         }
754             
755         file_put_contents($imageCache, json_encode($images));
756         
757     }
758     
759     function isGenerated($cachePath)
760     {
761         if (!file_exists($cachePath) || !filesize($cachePath)) {
762             return false;
763         }
764         
765         if (!empty($this->use_file)) {
766             $ctime = filemtime($cachePath);
767             $mtime = filemtime($this->use_file);
768             if($ctime >= $mtime){
769                 return true;
770             }
771             return false;
772             
773         }
774         
775         
776         
777         $ctime = filemtime($cachePath);
778         $mtime = array();
779         $mtime[] = $this->updated_dt;
780         $i = DB_DataObject::factory('Images');
781         $i->onid = $this->id;
782         $i->ontable = $this->tableName();
783         $i->selectAdd();
784         $i->selectAdd('max(created) as created');
785         $i->find(true);
786         $mtime[] = $i->created;
787         if($ctime >= strtotime(max($mtime))){
788             return true;
789         }
790         
791         return false;
792     }
793     
794     function messageFrom()
795     {
796         if (empty($this->from_name)) {
797             return trim($this->from_email);
798         }
799         return trim('"' . addslashes($this->from_name) . '" <' . $this->from_email. '>')  ;
800     }
801     
802     function formatDate($dt, $format = 'd/M/Y')
803     {
804         return date($format, strtotime($dt));
805     } 
806     
807     
808      // fixme - this is now in core/udatedatabase..
809     
810     function initMail($mail_template_dir,  $name, $master='')
811     {
812         $cm = DB_DataObject::factory('core_email');
813         if ($cm->get('name', $name)) {
814             return;
815         }
816         
817 //        $basedir = $this->bootLoader->rootDir . $mail_template_dir;
818         
819         $opts = array();
820         
821         $opts['file'] = $mail_template_dir. $name .'.html';
822         if (!empty($master)) {
823             $opts['master'] = $mail_template_dir . $master .'.html';
824         }
825         //print_r($opts);
826         require_once 'Pman/Core/Import/Core_email.php';
827         $x = new Pman_Core_Import_Core_email();
828         $x->get('', $opts);
829          
830     }
831     
832     
833     function testData($person, $dt , $core_notify)
834     {
835          
836         // should return the formated email???
837         $pg = HTML_FlexyFramework::get()->page;
838         
839          
840         
841         
842         if(empty($this->test_class)){
843             $pg->jerr("[{$this->name}] does not has test class");
844         }
845         
846         require_once "{$this->test_class}.php";
847         
848         $cls = str_replace('/', '_', $this->test_class);
849         
850         $x = new $cls;
851         
852         $method = "test_{$this->name}";
853         
854         if(!method_exists($x, $method)){
855             $pg->jerr("{$method} does not exists in {$cls}");
856         }
857         
858         $content = $x->{$method}($this, $person);
859         $content['to'] = $person->getEmailFrom();
860
861         $content['bcc'] = array();
862         $data = $this->toMailerData($content);
863         return $data;
864         
865            
866     }
867     
868     function to_group()
869     {
870         $g = DB_DataObject::Factory('core_group');
871         $g->get($this->to_group_id);
872         return $g;
873     }
874 }