Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
bulkmail_job.inc
1 <?php
17 require_once SQ_PACKAGES_PATH.'/bulkmail/bulkmail_post_office/bulkmail_post_office.inc';
18 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
19 
20 
35 {
36 
37 
44  function __construct($assetid=0)
45  {
46  $this->_ser_attrs = TRUE;
47  parent::__construct($assetid);
48 
49  }//end constructor
50 
51 
58  function &getPostOffice()
59  {
60  $post_office = NULL;
61  $parents = $GLOBALS['SQ_SYSTEM']->am->getParents($this->id, 'bulkmail_post_office', TRUE);
62  if (!empty($parents)) {
63  $array_keys = array_keys($parents);
64  $assetid = array_pop($array_keys);
65  // make sure the parent post office is not in trash
66  if (!$GLOBALS['SQ_SYSTEM']->am->assetInTrash($assetid)) {
67  $post_office = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
68  }
69  }
70 
71  // use the system default post office if none is selected
72  if (is_null($post_office)) {
73  $post_office = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('bulkmail_manager');
74  }
75 
76  return $post_office;
77 
78  }//end getPostOffice()
79 
80 
89  function sendPreviewMail($recipient)
90  {
91  // check if this job is valid
92  $post_office = $this->getPostOffice();
93  $bm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('bulkmail_manager');
94  $details_info = $bm->generateJobDetails($this, $post_office);
95  $errors = $bm->isValidJob($details_info, TRUE);
96  if (!empty($errors)) {
97  trigger_localised_error('BML0001', E_USER_WARNING, $errors[0]);
98  return FALSE;
99  }
100 
101  // create the mail object
102  $server_details = $post_office->attr('server_details');
103  $driver = array_get_index($server_details, 'driver', '');
104  $mf = new Mail();
105  $mail_object = $mf->factory($driver, $server_details);
106 
107  // generate content as pre-selected user
108  $data_dir = $post_office->data_path.'/.data/'.$this->id;
109  require_once SQ_PACKAGES_PATH.'/bulkmail/bulk_mailer.inc';
110  $bm = new Bulk_Mailer();
111  $content = $bm->generateContent($details_info, $data_dir);
112 
113  // get header details, required and normal headers
114  $use_post_office_header = $this->attr('use_post_office_header');
115  if ($use_post_office_header) {
116  $from = $post_office->attr('from');
117  $subject = $post_office->attr('subject');
118  $header_details = $post_office->attr('header_details');
119  } else {
120  $from = $this->attr('from');
121  $subject = $this->attr('subject');
122  $header_details = $this->attr('header_details');
123  }
124  $header_details['from'] = $from;
125 
126  require_once SQ_FUDGE_PATH.'/general/text.inc';
127  $keywords = retrieve_keywords_replacements($subject);
128  $replacements = Array();
129  $content_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($details_info['content_id']);
130  foreach ($keywords as $word) {
131  $replacements[$word] = $content_asset->getKeywordReplacement($word);
132  }
133  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($content_asset);
134  replace_keywords($subject, $replacements);
135 
136  $subject = Bulk_Mailer::replaceContentKeywords($subject, $GLOBALS['SQ_SYSTEM']->user);
137 
138  $header_details['subject'] = $subject;
139  $header_details['To'] = $recipient; // FR: 2515
140 
141  // add 'Preview' to the subject header for preview mails
142  if (!empty($header_details['subject'])) {
143  $header_details['subject'] .= ' (Preview)';
144  }
145 
146  $text_only_content = html_entity_decode(strip_tags($content));
147 
148  // set $crlf to "\n" since we are sending the content using PEAR Mail
149  $mime = new Mail_mime("\n");
150  if (!$this->attr('content_text_only')) {
151  $mime->setHTMLBody($content);
152  }
153  $mime->setTxtBody($text_only_content);
154  $param = Array(
155  'head_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
156  'text_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
157  'html_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
158  );
159  $body = @$mime->get($param);
160  $headers = @$mime->headers($header_details);
161  $status = @$mail_object->send($recipient, $headers, $body);
162  if ($status instanceof PEAR_Error) {
163  // unable to send bulkmail
164  trigger_localised_error('BML0002', E_USER_WARNING, $status->getMessage());
165  return FALSE;
166  }
167 
168  return TRUE;
169 
170  }//end sendPreviewMail()
171 
172 
182  function processStatusChange($new_status, $update_parents=TRUE)
183  {
184  // get the current state from the bmail queue db table
185  $bm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('bulkmail_manager');
186  $queued_jobs = $bm->getQueuedJobs($this->id);
187 
188  // cannot change the status of the job is in the db table
189  if (!empty($queued_jobs)) return FALSE;
190 
191  switch ($new_status) {
192  case SQ_STATUS_LIVE:
193  // user set the job to live, do validation in addJob()
194  if (!$bm->addJob($this)) return FALSE;
195  break;
196  case SQ_STATUS_UNDER_CONSTRUCTION:
197  $job_path = $bm->getJobDataPath($this->id);
198  clear_directory($job_path);
199  break;
200  }
201 
202  return parent::processStatusChange($new_status, $update_parents);
203 
204  }//end processStatusChange()
205 
206 
215  public function generateRawContent($details)
216  {
217  // moved functionality to asset_manager.inc for reusing the codes in triggers
218  return $GLOBALS['SQ_SYSTEM']->am->generateRawContent($details);
219 
220  }//end generateRawContent()
221 
222 
223 }//end class
224 ?>