3 * Table Definition for core_mailinglist_message
5 require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Core_mailing_list_message extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'core_mailing_list_message'; // table name
13 public $id; // int(11) not_null primary_key auto_increment
15 public $subject; // blob(65535) blob
16 public $bodytext; // blob(65535) blob
18 public $updated_dt; //datetime not_null
24 /* the code above is auto generated do not remove the tag below */
27 function applyFilters($q, $au, $roo)
29 if(!empty($q['search']['nameortitle'])){
31 name LIKE '%{$this->escape($q['search']['nameortitle'])}%'
33 subject LIKE '%{$this->escape($q['search']['nameortitle'])}%'
38 function beforeDelete($dependants_array, $roo)
40 $i = DB_DataObject::factory('Images');
42 $i->ontable = $this->tableName();
49 // $mlq = DB_DataObject::factory('crm_mailing_list_queue');
50 // $mlq->message_id = $this->id;
52 // while ($mlq->fetch()){
53 // $mlq->beforeDelete();
59 function beforeUpdate($old, $request,$roo)
61 if (!empty($request['_make_copy'])) {
62 $this->makeCopy($roo);
69 function makeCopy($roo)
71 $c = DB_DataObject::Factory($this->tableName());
73 $c->name = "COPY of " . $this->name;
74 $c->updated_dt = $this->sqlValue('NOW()');
77 $c = DB_DataObject::Factory($this->tableName());
83 $i = DB_DataObject::factory('Images');
85 $i->ontable = $this->tableName();
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);
94 $map[$i->id] = $new_image->id;
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;
103 //echo '<PRE>'; print_R($doc);
106 $xpath = new DOMXpath($doc);
107 foreach ($xpath->query('//img[@src]') as $img) {
108 $href = $img->getAttribute('src');
111 if (preg_match("/Images\/([0-9]+)\/([^#]+)\#image\-([0-9]+)$/", $href, $matches)) {
115 if (!isset($map[$oid])) {
116 //echo "skip no new id for $oid";
120 $nstr = "/Images/$nid/{$matches[2]}/#image-{$nid}";
122 $img->setAttribute('src', str_replace($matches[0], $nstr, $href ));
128 $c->bodytext = $doc->saveHTML();
130 libxml_use_internal_errors (false);
133 $roo->jok("duplicated");
141 function onInsert($request,$roo)
143 $i = DB_DataObject::factory('Images');
144 $i->whereAdd('onid = 0');
147 $i->onid = $this->id;
154 function attachmentIds()
157 $roo = HTML_FlexyFramework::get()->page;
159 libxml_use_internal_errors (true);
160 $doc = new DOMDocument('1.0', 'UTF-8');
161 $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
163 $xpath = new DOMXpath($doc);
166 foreach ($xpath->query('//img[@src]') as $img) { // process images!
167 $href = $img->getAttribute('src');
168 $cid = explode('#', $href);
172 $cid = explode('-', $cid[1]);
173 if (!isset($cid[1])||!is_numeric($cid[1])) {
182 * process replacements is run to generate a template - not the final content..
186 function processRelacements($replace_links = true)
188 $roo = HTML_FlexyFramework::get()->page;
190 $cfg = HTML_FlexyFramework::get()->Pman_Crm;
192 libxml_use_internal_errors (true);
193 $doc = new DOMDocument('1.0', 'UTF-8');
194 $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
196 $xpath = new DOMXpath($doc);
198 foreach ($xpath->query('//img[@src]') as $img) { // process images!
199 $href = $img->getAttribute('src');
200 $cid = explode('#', $href);
202 $img->setAttribute('src', 'cid:' . $cid[1]);
205 $unsubscribe = $cfg ['server_baseurl'] . '/Crm/Unsubscribe/' . $this->id . '/{person.id}';
208 foreach ($xpath->query('//a[@href]') as $a) {
210 $href = $a->getAttribute('href');
212 if(preg_match('/#unsubscribe/', $href)){
213 $a->setAttribute('href', $unsubscribe);
217 if(!preg_match('/^http(.*)/', $href)){
220 if (!$replace_links) {
223 $link = DB_DataObject::factory('crm_mailing_list_link');
224 $link->setFrom(array(
228 if(!$link->find(true)){
236 $l = $cfg ['server_baseurl'] . '/Crm/Link/' .$this->id . '/' . $link->id . '/{person.id}.html';
238 $a->setAttribute('href', $l);
242 $element = $doc->createElement('img');
244 $element->setAttribute('src', $cfg ['server_baseurl'] . '/Crm/Open/' . $this->id . '/{person.id}.html');
245 $element->setAttribute('width', '1');
246 $element->setAttribute('height', '1');
248 $html = $doc->getElementsByTagName('html');
249 $html->item(0)->appendChild($element);
251 $this->bodytext = $doc->saveHTML();
253 libxml_use_internal_errors (false);
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);
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);
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.
277 $contents = (array)$obj;
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;
286 $q->cachedMailWithOutImages(true, false);
288 $contents['subject'] = $this->subject;
290 require_once 'Pman/Core/Mailer.php';
292 $templateDir = session_save_path() . '/email-cache-' . get_current_user() ;
293 $r = new Pman_Core_Mailer(array(
295 'templateDir' => $templateDir,
297 'contents' => $contents
299 // 'person' => $person,
300 // 'subject' => $this->message_id_subject,
306 ///print_r($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;
311 $ret['body'] = str_replace('%Images%', $images, $ret['body']);
313 return $r->send($ret);