Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_schedule_bulkmail_job.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
18 require_once SQ_PACKAGES_PATH.'/bulkmail/bulk_mailer.inc';
19 
30 {
31 
32 
51  public static function execute($settings, &$state)
52  {
53  $bulkmail_job_id = array_get_index($settings, 'bulkmail_job_id', 0);
54 
55  if ($bulkmail_job_id == 0) return FALSE;
56 
57  $asset_to_send = array_get_index($settings, 'asset_to_send', 0);
58  $current_asset = array_get_index($settings, 'current_asset', FALSE);
59 
60 
61  if (empty($state['asset'])) {
62  // grab the asset if assetid is given, but not the asset.
63  if (empty($state['assetid'])) {
64  return FALSE;
65  } else {
66  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
67  }
68  }
69 
70  if (is_null($state['asset'])) return FALSE;
71 
72  if ($current_asset) {
73  $asset_to_send = $state['asset']->id;
74  }
75 
76  $am = $GLOBALS['SQ_SYSTEM']->am;
77  $bm = $am->getSystemAsset('bulkmail_manager');
78  $job_asset = $am->getasset($bulkmail_job_id);
79 
80  // if we don't ahve the asset to send yet then
81  // pick up the asset that is configured int he job itself
82  if ($asset_to_send == 0) {
83  $asset_to_send = $job_asset->attr('content_id');
84  }
85 
86  $subscriptions = array_get_index($settings, 'subscriptions', Array());
87  $sub_status = array_get_index($settings, 'subscriptions_status', FALSE);
88  $recipient = array_get_index($settings, 'recipient', Array());
89 
90 
91  $post_office = $job_asset->getPostOffice();
92 
93  // have all the informatin we want?
94  // add a ad-hoc bulkmail job
95  $is_a_bm = get_class($post_office) == 'Bulkmail_Manager';
96  $from = $job_asset->attr('from');
97  $recipient = empty($recipient) ? $job_asset->attr('recipients') : $recipient;
98  $gen_as = $job_asset->attr('content_gen_as');
99  $po_headers = $job_asset->attr('use_post_office_header') ? TRUE : FALSE;
100  $headers = $po_headers ? $post_office->attr('header_details') : $job_asset->attr('header_details');
101  $subject = $po_headers ? $post_office->attr('subject') : $job_asset->attr('subject');
102  $server_headers = $is_a_bm ? Array() : $post_office->attr('server_details');
103  $design = $job_asset->attr('content_design') ? $job_asset->attr('content_design') : 0;
104  $layout = $job_asset->attr('content_layout') ? $job_asset->attr('content_layout') : 0;
105  $post_office_id = $is_a_bm ? 0 : $post_office->id;
106 
107  // if the subscription is turned on but no categories are added then borrow from the job
108  if ($sub_status && empty($subscriptions)) {
109  $subscriptions = $job_asset->attr('subscriptions');
110  }
111 
112  // if the subscription is enabled and also we have the config available
113  // check to see if the passed asset is a search page. It it isn't then
114  // we won't be able to search for the assets to send. In this case play
115  // with the asset to send and see if it indeed matches our subscripion
116  $is_search_page = $GLOBALS['SQ_SYSTEM']->am->getAsset($asset_to_send, 'search_page', TRUE);
117 
118  if ($sub_status && !empty($subscriptions) && is_null($is_search_page)) {
119  $to_send_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($asset_to_send);
120  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
121  $bulk_mailer = new Bulk_Mailer();
122  $users_to_mail = $bulk_mailer->getAllRecipients($asset_to_send, $recipient);
123  // remove all the recipient and add it in the foreach loop below
124  // depending on the subscriptions selected by the individual users
125  $recipient = Array();
126 
127  foreach ($users_to_mail['recipient'] as $user_info) {
128  $user_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($user_info['assetid']);
129  if (!is_null($user_asset)) {
130  foreach ($subscriptions as $sub_name => $sub_info) {
131  foreach ($sub_info as $index => $info) {
132  $match = FALSE;
133  $category_type = array_keys($info);
134  $user_metadata = $mm->getMetadataValueByAssetid($user_asset->id, $info[$category_type[0]]['assetid']);
135  $rule_text = $info[$category_type[0]]['rule_text'];
136 
137  $old_frontend_asset = $GLOBALS['SQ_SYSTEM']->frontend_asset;
138  $GLOBALS['SQ_SYSTEM']->frontend_asset = $to_send_asset;
139  $GLOBALS['SQ_SYSTEM']->replaceKeyword($rule_text);
140  $GLOBALS['SQ_SYSTEM']->frontend_asset = $old_frontend_asset;
141 
142  if ($info[$category_type[0]]['rule_cond'] == 'contains') {
143  $match = (stripos($user_metadata, $rule_text) !== FALSE);
144  } else {
145  // needs a exact match
146  $match = (strtolower($user_metadata) == strtolower($rule_text));
147  }
148  if ($match) {
149  $recipient[] = $user_info['assetid'];
150  }
151  }//end foreach
152  }//end foreach
153  }//end if
154  }//end foreach
155  $subscriptions = Array();
156  }//end if
157 
158  if (!empty($recipient)) {
159  if (!$bm->createAdHocJob($from, $recipient, $subject, $asset_to_send, $gen_as, $headers, $post_office_id, $server_headers, $design, $layout, $subscriptions))
160  return FALSE;
161  }
162 
163  return Array(
164  'job_id' => $job_asset->id,
165  );
166 
167  }//end execute()
168 
169 
180  public static function getInterface($settings, $prefix, $write_access=FALSE)
181  {
182  $bulkmail_job_id = array_get_index($settings, 'bulkmail_job_id', 0);
183  $subscriptions = array_get_index($settings, 'subscriptions', Array());
184  $sub_status = array_get_index($settings, 'subscriptions_status', FALSE);
185  $asset_to_send = array_get_index($settings, 'asset_to_send', 0);
186  $current_asset = array_get_index($settings, 'current_asset', FALSE);
187  $recipient = array_get_index($settings, 'recipient', Array());
188 
189  $option_type = Array(
190  'matches' => 'Matches',
191  'contains' => 'Contains',
192  );
193 
194  $new_rule_type = Array(
195  '0' => '-- Add New Rule --',
196  'metadata' => 'Asset Metadata field',
197  );
198 
199 
200  ob_start();
201 
202  ?><table>
203  <tr>
204  <td>
205  <?php
206  echo translate('asset_to_send');
207  ?>
208  </td>
209  <td>
210  <?php
211  if ($write_access) {
212  asset_finder($prefix.'[asset_to_send]', $asset_to_send);
213  check_box($prefix.'[current_asset]', '1', $current_asset, '', 'id="'.$prefix.'_current_asset"');
214  label(translate('use_current_asset'), $prefix.'_current_asset');
215  } else {
216  if ($asset_to_send) {
217  echo get_asset_tag_line($asset_to_send);
218  echo "<br />";
219  }
220  echo translate('use_current_asset');
221  echo '&nbsp;&nbsp;<img src="'.sq_web_path('lib').'/web/images/'.($current_asset ? 'tick' : 'cross').'.gif" alt="'.($current_asset ? translate('yes') : translate('no')).'" /> ';
222 
223  }
224  ?>
225  </td>
226  </tr>
227  <tr>
228  <td>
229  <?php
230  echo translate('bulkmail_job');
231  ?>
232  </td>
233  <td>
234  <?php
235  if ($write_access) {
236  asset_finder($prefix.'[bulkmail_job_id]', $bulkmail_job_id, Array('bulkmail_job' => 'I'));
237  } else {
238  if ($bulkmail_job_id) {
239  echo get_asset_tag_line($bulkmail_job_id);
240  } else {
241  echo translate('not_configured');
242  }
243  }
244  ?>
245  </td>
246  </tr>
247  <tr>
248  <td valign="top">
249  <?php
250  echo translate('recipients');
251  ?>
252  </td>
253  <td>
254  <?php
255  if ($write_access) {
256  multiple_asset_finder($prefix.'[recipient]', $recipient, Array('user' => 'D', 'user_group' => 'D'));
257  } else {
258  if ($recipient) {
259  foreach ($recipient as $res) {
260  echo get_asset_tag_line($res);
261  echo "<br />";
262  }
263  } else {
264  echo translate('not_configured');
265  }
266  }
267  ?>
268  </td>
269  </tr>
270  <tr>
271  <td>
272  <?php
273  echo translate('enable_subscription_filter');
274  ?>
275  </td>
276  <td>
277  <?php
278  if ($write_access) {
279  check_box($prefix.'[subscriptions_status]', '1', $sub_status);
280  } else {
281  echo '&nbsp;&nbsp;<img src="'.sq_web_path('lib').'/web/images/'.($sub_status ? 'tick' : 'cross').'.gif" alt="'.($sub_status ? translate('yes') : translate('no')).'" /> ';
282  }
283  ?>
284  </td>
285  </tr>
286  <?php
287  if ($sub_status) {
288  ?>
289  <tr>
290  <td valign="top">
291  <?php
292  echo translate('filters');
293  ?>
294  </td>
295  <td>
296  <?php
297  echo '<table class ="sq-backend-table" border="1" style="margin-bottom: 7px">';
298  echo '<tr><td rowspan =2 width="12%">Name</td><td colspan="3">Rules</td></tr>';
299  echo '<tr><td width="15%">Type</td><td>Details</td><td>Delete?</td></tr>';
300 
301  foreach ($subscriptions as $name => $subscription) {
302  hidden_field($prefix.'[subscriptions]['.$name.']', '');
303  $tr = FALSE;
304  echo "<tr>";
305  $rowspan = ($write_access) ? (count($subscription) + 1) : count($subscription);
306  if (empty($subscription)) $rowspan++;
307  echo '<th rowspan='.$rowspan.'>'.$name.'</th>';
308  if (!empty($subscription)) {
309  foreach ($subscription as $index => $rules) {
310  if ($tr) echo "<tr>";
311  $type_data = array_keys($rules);
312  echo '<td>'.$new_rule_type[$type_data[0]].'</td>';
313  echo '<td>';
314  if ($write_access) {
315  asset_finder($prefix.'[subscriptions]['.$name.']['.$index.']['.$type_data[0].'][assetid]', $rules[$type_data[0]]['assetid'], Array('metadata_field' => 'D'));
316  } else {
317  echo get_asset_tag_line($rules[$type_data[0]]['assetid']);
318  }
319  echo '&nbsp;&nbsp;&nbsp;';
320  if ($write_access) {
321  combo_box($prefix.'[subscriptions]['.$name.']['.$index.']['.$type_data[0].'][rule_cond]', $option_type, FALSE, $rules[$type_data[0]]['rule_cond']);
322  } else {
323  echo $rules[$type_data[0]]['rule_cond'];
324  }
325  echo '&nbsp;&nbsp;&nbsp;';
326  if ($write_access) {
327  text_box($prefix.'[subscriptions]['.$name.']['.$index.']['.$type_data[0].'][rule_text]', $rules[$type_data[0]]['rule_text'], 30);
328  } else {
329  echo $rules[$type_data[0]]['rule_text'];
330  }
331 
332  echo '</td><td>';
333  if ($write_access) check_box($prefix.'[subscriptions]['.$name.']['.$index.'][delete]');
334  echo "</td></tr>";
335 
336  $tr = TRUE;
337  }//end foreach
338  } else {
339  echo '<td colspan=3></td></tr>';
340  }
341  if ($write_access) {
342  ?>
343  <tr>
344  <td colspan=4>
345  <?php
346  combo_box($prefix.'[subscriptions]['.$name.'][new_rule]', $new_rule_type, FALSE);
347  ?>
348  </td>
349  </tr>
350  <?php
351  }
352  }//end foreach
353  echo "</table>";
354  if ($write_access) {
355  label(translate('new_category'), $prefix.'[new_category_name]');
356  echo '&nbsp;';
357  text_box($prefix.'[new_category_name]', '', 15);
358  }
359 
360  ?>
361  </td>
362  </tr>
363  <?php
364  }
365  ?>
366 
367  </table>
368  <?php
369 
370  $html = ob_get_clean();
371 
372  return $html;
373 
374  }//end getInterface()
375 
376 
388  public static function processInterface(&$settings, $request_data)
389  {
390  $settings['bulkmail_job_id'] = array_get_index($request_data['bulkmail_job_id'], 'assetid', 0);
391  $settings['subscriptions_status'] = array_get_index($request_data, 'subscriptions_status', FALSE);
392  $settings['asset_to_send'] = array_get_index($request_data['asset_to_send'], 'assetid', 0);
393  $settings['current_asset'] = array_get_index($request_data, 'current_asset', FALSE);
394 
395 
396  $raw_userids = $request_data['recipient'];
397  $userids = Array();
398  foreach ($raw_userids as $user) {
399  if (!empty($user['assetid'])) {
400  $userids[] = $user['assetid'];
401  }
402  }
403  $settings['recipient'] = $userids;
404 
405  if ($settings['current_asset']) $settings['asset_to_send'] = 0;
406 
407  if ($settings['subscriptions_status']) {
408  $new_subscriptions = Array();
409  $subscriptions = array_get_index($request_data, 'subscriptions', Array());
410  $new_category_name = array_get_index($request_data, 'new_category_name', '');
411  if (!empty($subscriptions)) {
412  foreach ($subscriptions as $sub_name => $subscription) {
413  foreach ($subscription as $index => $subscription_data) {
414  if ($index === 'new_rule') {
415  if ($subscription['new_rule'] !== '0') {
416  $new_subscriptions[$sub_name][][$subscription['new_rule']] = Array(
417  'assetid' => 0,
418  'rule_cond' => 'contains',
419  'rule_text' => '',
420  );
421  }
422  continue;
423  }
424 
425  if (!isset($subscription_data['delete'])) {
426  $category = array_keys($subscription_data);
427  $new_subscriptions[$sub_name][][$category[0]] = Array(
428  'assetid' => $subscription_data[$category[0]]['assetid']['assetid'],
429  'rule_cond' => array_get_index($subscription_data[$category[0]], 'rule_cond', 'contains'),
430  'rule_text' => array_get_index($subscription_data[$category[0]], 'rule_text',''),
431  );
432  }
433  }
434  }
435  }
436 
437  if (!empty($new_category_name)) {
438  $new_subscriptions[$new_category_name] = Array();
439  }
440  $settings['subscriptions'] = $new_subscriptions;
441  }
442 
443  return FALSE;
444 
445  }//end processInterface()
446 
447 
448 }//end class
449 
450 ?>