Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
bulkmail_job_edit_fns.inc
1 <?php
18 require_once SQ_PACKAGES_PATH.'/bulkmail/bulkmail_post_office/bulkmail_post_office_edit_fns.inc';
19 require_once SQ_FUDGE_PATH.'/datetime_field/datetime_field.inc';
20 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
21 require_once SQ_FUDGE_PATH.'/general/www.inc';
22 
23 
38 {
39 
40 
45  function __construct()
46  {
47  parent::__construct();
48  unset($this->static_screens['preview']);
49  $this->static_screens['details']['force_unlock'] = FALSE;
50 
51  }//end constructor
52 
53 
54 //-- VALIDATION --//
55 
56 
67  function paintValidation(&$asset, &$o, $prefix)
68  {
69  // set a hidden field with initial value of '0'
70  // set value to '1' when the validate button is clicked
71  $o->openRaw();
72  ?><p style="margin-top: 5px; margin-bottom: 5px"><?php
73  echo translate('bm_job_validation_validate_job');
74  ?><br /><?php
75  echo normal_button($prefix.'_validation_button', translate('bm_job_validation_button'), 'onClick= validate(); this.form.submit();');
76  echo hidden_field($prefix.'_validation_hidden', '0');
77  ?></p><?php
78  $o->closeRaw();
79 
80  // report errors if the the hidden field is set to '1'
81  if (isset($_REQUEST[$prefix.'_validation_hidden']) && $_REQUEST[$prefix.'_validation_hidden'] == 1) {
82  $post_office = $asset->getPostOffice();
83  $bm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('bulkmail_manager');
84  $details_info = $bm->generateJobDetails($asset, $post_office);
85  $errors = $bm->isValidJob($details_info, TRUE);
86  $o->openField(translate('bm_job_validation_result'));
87  if (empty($errors)) {
88  echo translate('bm_job_validation_successful');
89  } else {
90  ?>
91  <table class="sq-backend-table">
92  <tr>
93  <th class="sq-backend-table-header"><?php echo translate('bm_configuration'); ?></th>
94  <th class="sq-backend-table-header"><?php echo translate('bm_warning'); ?></th>
95  </tr>
96  <?php
97  foreach ($errors as $error) {
98  $error_msg = explode(':', $error, 2);
99  ?><tr>
100  <td><?php echo $error_msg[0]; ?></td>
101  <td><span class="sq-backend-warning"><?php echo $error_msg[1]; ?></span></td>
102  </tr><?php
103  }
104  ?></table><?php
105  }
106  $o->closeField();
107  }
108 
109  // javascript to update the value of hidden validation form element
110  ?>
111  <script language="JavaScript" type="text/javascript">
112  function validate() {
113  var prefix = '<?php echo $prefix; ?>';
114  var id = prefix + '_validation_hidden';
115  var element = document.getElementById(id);
116  element.value = '1';
117  }
118  </script>
119  <?php
120 
121  }//end paintValidation()
122 
123 
134  function processValidation(&$asset, &$o, $prefix)
135  {
136  return TRUE;
137 
138  }//end processValidation()
139 
140 
141 //-- WARNING --//
142 
143 
154  function paintWarning(&$asset, &$o, $prefix)
155  {
156  ?><span class="sq-backend-warning"><?php echo translate('bm_warning_changes_no_effect'); ?></span><?php
157  return TRUE;
158 
159  }//end paintWarning()
160 
161 
172  function processWarning(&$asset, &$o, $prefix)
173  {
174  return TRUE;
175 
176  }//end processWarning()
177 
178 
188  function showWarningSection(&$asset, $prefix)
189  {
190  $bulkmail_manager = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('bulkmail_manager');
191  $job = $bulkmail_manager->getQueuedJobs($asset->id);
192 
193  return (empty($job)) ? FALSE : TRUE;
194 
195  }//end showWarningSection()
196 
197 
198 //-- HEADER --//
199 
200 
211  function paintHeaderDetailsCheckBox(&$asset, &$o, $prefix)
212  {
213  $write_access = $asset->writeAccess('attributes');
214  $use_post_office_header = $asset->attr('use_post_office_header');
215 
216  // paint checkbox
217  if ($write_access) {
218  check_box($prefix.'_use_post_office_header', TRUE, $use_post_office_header);
219  } else {
220  ?>
221  <img src="<?php echo sq_web_path('lib'); ?>/web/images/<?php echo $use_post_office_header ? 'tick' : 'cross'; ?>.gif" width="15" height="15" />
222  <?php
223  }
224  $post_office = $asset->getPostOffice();
225  echo get_asset_tag_line($post_office->id);
226 
227  return $write_access;
228 
229  }//end paintHeaderDetailsCheckBox()
230 
231 
242  function processHeaderDetailsCheckBox(&$asset, &$o, $prefix)
243  {
244  if (!$asset->writeAccess('attributes')) return FALSE;
245 
246  // save whether to use post office header details
247  $use_header_details = isset($_POST[$prefix.'_use_post_office_header']);
248  $asset->setAttrValue('use_post_office_header', $use_header_details);
249 
250  return TRUE;
251 
252  }//end processHeaderDetailsCheckBox()
253 
254 
264  function showJobHeader(&$asset, $prefix)
265  {
266  return !($asset->attr('use_post_office_header'));
267 
268  }//end showJobHeader()
269 
270 
271 //-- PROGRESS --//
272 
273 
284  function paintJobQueue(&$asset, &$o, $prefix)
285  {
286  $write_access = $asset->writeAccess('attributes');
287  $bulkmail_manager = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('bulkmail_manager');
288  $bulkmail_manager_edit_fns = $bulkmail_manager->getEditFns();
289  $o->openField('');
290  $jobs = $bulkmail_manager->getQueuedJobs($asset->id);
291  $bulkmail_manager_edit_fns->_paintJobQueue($asset, $jobs);
292  $o->closeField();
293 
294  $o->openField('');
295  $ms = $GLOBALS['SQ_SYSTEM']->getMessagingService();
296  if (isset($asset->_tmp['bulkmail.job.*'])){ //try using result from showProgressSection()
297  $logs = $asset->_tmp['bulkmail.job.*'];
298  unset($asset->_tmp['bulkmail.job.*']);
299  } else {
300  $logs = $ms->getMessages(0, 'bulkmail.job.*', Array(), Array(), NULL, NULL, 'name', Array('assetid' => $asset->id));
301  $logs = array_slice($logs, 0, 5);
302  }
303 
304  if (!empty($logs)) {
305  ?>
306  <table class="sq-backend-table">
307  <tr>
308  <th class="sq-backend-table-header"><?php echo translate('date'); ?></th>
309  <th class="sq-backend-table-header"><?php echo translate('subject'); ?></th>
310  <th class="sq-backend-table-header"><?php echo translate('body'); ?></th>
311  </tr>
312  <?php
313  foreach ($logs as $log_entry) {
314  ?>
315  <tr>
316  <td align="left" class="sq-backend-table-cell"><?php echo ts_iso8601($log_entry['sent']); ?></td>
317  <td align="left" class="sq-backend-table-cell"><?php echo $log_entry['subject']; ?></td>
318  <td align="left" class="sq-backend-table-cell"><?php echo $log_entry['body']; ?></td>
319  </tr>
320  <?php
321  }
322  ?></table><?php
323  }
324  $o->closeField();
325 
326  return $write_access;
327 
328  }//end paintJobQueue()
329 
330 
341  function processJobQueue(&$asset, &$o, $prefix)
342  {
343  $write_access = $asset->writeAccess('attributes');
344  $bulkmail_manager = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('bulkmail_manager');
345  $bulkmail_manager_edit_fns = $bulkmail_manager->getEditFns();
346  if (!$write_access) return;
347 
348  $jobs = $bulkmail_manager->getQueuedJobs($asset->id);
349  $bulkmail_manager_edit_fns->_processJobQueue($asset, $jobs);
350 
351  return TRUE;
352 
353  }//end processJobQueue()
354 
355 
365  function showProgressSection(&$asset, $prefix)
366  {
367  $bulkmail_manager = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('bulkmail_manager');
368  $job = $bulkmail_manager->getQueuedJobs($asset->id);
369 
370  $ms = $GLOBALS['SQ_SYSTEM']->getMessagingService();
371  $logs = $ms->getMessages(0, 'bulkmail.job.*', Array(), Array(), NULL, NULL, 'name', Array('assetid' => $asset->id));
372  $logs = array_slice($logs, 0, 5);
373  $asset->_tmp['bulkmail.job.*'] = $logs;
374 
375  return ((empty($job) && empty($logs)) ? FALSE : TRUE);
376 
377  }//end showProgressSection()
378 
379 
380 //-- PREVIEWING --//
381 
382 
393  function paintPreviewAddress(&$asset, &$o, $prefix)
394  {
395  $write_access = $asset->writeAccess('attributes');
396 
397  $current_user = $GLOBALS['SQ_SYSTEM']->user;
398  $email = $current_user->attr('email');
399 
400  if ($write_access) {
401  text_box($prefix.'_preview_address', $email);
402  } else {
403  echo $email;
404  }
405 
406  return $write_access;
407 
408  }//end paintPreviewAddress()
409 
410 
421  function processPreviewAddress(&$asset, &$o, $prefix)
422  {
423  if (!$asset->writeAccess('attributes')) return FALSE;
424 
425  $recipient = array_get_index($_REQUEST, $prefix.'_preview_address', '');
426  if (empty($recipient) || !valid_email($recipient)) {
427  trigger_localised_error('BML0013', E_USER_WARNING, $recipient);
428  return FALSE;
429  }
430 
431  if ($asset->sendPreviewMail($recipient)) {
432  $msg_reps = Array(
433  'assetid' => $GLOBALS['SQ_SYSTEM']->currentUserId(),
434  'email_addr' => $recipient,
435  );
436 
437  $ms = $GLOBALS['SQ_SYSTEM']->getMessagingService();
438  $message = $ms->newMessage(Array(), 'bulkmail.preview', $msg_reps);
439 
440  $message->parameters['assetid'] = $asset->id;
441  $message->parameters['version'] = substr($asset->version, 0, strrpos($asset->version, '.'));
442  $message->send();
443 
444  return TRUE;
445  } else {
446  return FALSE;
447  }
448 
449  }//end processPreviewAddress()
450 
451 
462  function paintPreviewLog(&$asset, &$o, $prefix)
463  {
464  $write_access = $asset->writeAccess('attributes');
465  $ms = $GLOBALS['SQ_SYSTEM']->getMessagingService();
466  $logs = $ms->getMessages(0, 'bulkmail.preview', Array(), Array(), NULL, NULL, 'name', Array('assetid' => $asset->id));
467  $logs = array_slice($logs, 0, 5);
468 
469  if (!empty($logs)) {
470  ?>
471  <table class="sq-backend-table">
472  <tr>
473  <th class="sq-backend-table-header"><?php echo translate('date'); ?></th>
474  <th class="sq-backend-table-header"><?php echo translate('subject'); ?></th>
475  <th class="sq-backend-table-header"><?php echo translate('body'); ?></th>
476  </tr>
477  <?php
478  foreach ($logs as $log_entry) {
479  ?>
480  <tr>
481  <td align="left" class="sq-backend-table-cell"><?php echo ts_iso8601($log_entry['sent']); ?></td>
482  <td align="left" class="sq-backend-table-cell"><?php echo $log_entry['subject']; ?></td>
483  <td align="left" class="sq-backend-table-cell"><?php echo $log_entry['body']; ?></td>
484  </tr>
485  <?php
486  }
487  ?></table><?php
488  } else {
489  echo translate('bm_job_preview_never');
490  }
491 
492  return $write_access;
493 
494  }//end paintPreviewLog()
495 
496 
507  function processPreviewLog(&$asset, &$o, $prefix)
508  {
509  return TRUE;
510 
511  }//end processPreviewLog()
512 
513 
524  public function paintContentBodyFilter(Bulkmail_Job $asset, Backend_Outputter $o, $prefix)
525  {
526  $write_access = $asset->writeAccess('attributes');
527  $field_prefix = $prefix.'_content_body_filter';
528 
529  $enabled = $asset->attr('content_body_filter');
530  $match = $asset->attr('content_body_filter_match');
531  $filter_text = $asset->attr('content_body_filter_text');
532 
533  if ($write_access) {
534  // Enable/disable drop-down
535  // Match (drop-down) \ disable when "disable" selected
536  // Text (text) /
537  $contents = Array(
538  1 => translate('enabled'),
539  0 => translate('disabled'),
540  );
541 
542  combo_box($field_prefix.'_enabled', $contents, FALSE, $enabled, FALSE);
543 
544  echo '<div id="'.$field_prefix.'_settings" style="padding: 1em 0em;">';
545  $contents = Array(
546  1 => translate('matches'),
547  0 => translate('does_not_match'),
548  );
549  echo translate('bulkmail_job_filter_explanation').'<br/>';
550  combo_box($field_prefix.'_match', $contents, FALSE, $match, FALSE);
551  echo ' ';
552  text_box($field_prefix.'_text', $filter_text, 30);
553  echo '</div>';
554 
555  } else {
556  if ($enabled) {
557  if (!empty($filter_text)) {
558  echo translate('enabled').'<br/>';
559  echo translate('bulkmail_job_filter_explanation').'<br/>';
560  if ($match) {
561  echo translate('bulkmail_job_filter_enabled_format_true', $filter_text);
562  } else {
563  echo translate('bulkmail_job_filter_enabled_format_false', $filter_text);
564  }
565  } else {
566  echo '<span class="sq-backend-warning">';
567  echo translate('bulkmail_job_enabled_but_not_valid');
568  echo '</span><br/>';
569  echo translate('bulkmail_job_enabled_but_not_valid_explanation');
570  }
571  } else {
572  echo translate('disabled');
573  }
574  }
575 
576  return TRUE;
577 
578  }//end paintContentBodyFilter()
579 
580 
591  public function processContentBodyFilter(Bulkmail_Job $asset, Backend_Outputter $o, $prefix)
592  {
593  // If no access to attributes then rack off
594  if (!$asset->writeAccess('attributes')) return FALSE;
595 
596  $field_prefix = $prefix.'_content_body_filter';
597  $asset->setAttrValue('content_body_filter', (bool)$_POST[$field_prefix.'_enabled']);
598  $asset->setAttrValue('content_body_filter_match', (bool)$_POST[$field_prefix.'_match']);
599  $asset->setAttrValue('content_body_filter_text', $_POST[$field_prefix.'_text']);
600 
601  return $asset->saveAttributes();
602 
603  }//end processContentBodyFilter()
604 
605 
616  function paintUserSubscriptions(&$asset, &$o, $prefix)
617  {
618  $write_access = $asset->writeAccess('attributes');
619  $subscription_enabled = $asset->attr('user_subscriptions_status');
620  $subscriptions = $asset->attr('subscriptions');
621 
622  $option_type = Array(
623  'matches' => 'Matches',
624  'contains' => 'Contains',
625  );
626 
627  $new_rule_type = Array(
628  '0' => '-- Add New Rule --',
629  'metadata' => 'Asset Metadata field',
630  );
631 
632  if ($subscription_enabled) {
633  $o->openField(translate('categories'));
634  echo '<table class ="sq-backend-table" border="1" style="margin-bottom: 7px">';
635  echo '<tr><td rowspan =2 width="12%">Name</td><td colspan="3">Rules</td></tr>';
636  echo '<tr><td width="15%">Type</td><td>Details</td><td>Delete?</td></tr>';
637 
638  foreach ($subscriptions as $name => $subscription) {
639  hidden_field($prefix.'_subscriptions['.$name.']', '');
640  $tr = FALSE;
641  echo "<tr>";
642  $rowspan = ($write_access) ? (count($subscription) + 1) : count($subscription);
643  if (empty($subscription)) $rowspan++;
644  echo '<th rowspan='.$rowspan.'>'.$name.'</th>';
645  if (!empty($subscription)) {
646  foreach ($subscription as $index => $rules) {
647  if ($tr) echo "<tr>";
648  $type_data = array_keys($rules);
649  echo '<td>'.$new_rule_type[$type_data[0]].'</td>';
650  echo '<td>';
651  if ($write_access) {
652  asset_finder($prefix.'_subscriptions['.$name.']['.$index.']['.$type_data[0].'][assetid]', $rules[$type_data[0]]['assetid'], Array('metadata_field' => 'D'));
653  } else {
654  echo get_asset_tag_line($rules[$type_data[0]]['assetid']);
655  }
656  echo '&nbsp;&nbsp;&nbsp;';
657  if ($write_access) {
658  combo_box($prefix.'_subscriptions['.$name.']['.$index.']['.$type_data[0].'][rule_cond]', $option_type, FALSE, $rules[$type_data[0]]['rule_cond']);
659  } else {
660  echo $rules[$type_data[0]]['rule_cond'];
661  }
662  echo '&nbsp;&nbsp;&nbsp;';
663  if ($write_access) {
664  text_box($prefix.'_subscriptions['.$name.']['.$index.']['.$type_data[0].'][rule_text]', $rules[$type_data[0]]['rule_text'], 30);
665  } else {
666  echo $rules[$type_data[0]]['rule_text'];
667  }
668 
669  echo '</td><td>';
670  if ($write_access) check_box($prefix.'_subscriptions['.$name.']['.$index.'][delete]');
671  echo "</td></tr>";
672 
673  $tr = TRUE;
674  }//end foreach
675  } else {
676  echo '<td colspan=3></td></tr>';
677  }
678  if ($write_access) {
679  ?>
680  <tr>
681  <td colspan=4>
682  <?php
683  combo_box($prefix.'_subscriptions['.$name.'][new_rule]', $new_rule_type, FALSE);
684  ?>
685  </td>
686  </tr>
687  <?php
688  }
689  }//end foreach
690  echo "</table>";
691  if ($write_access) {
692  label(translate('new_category'), $prefix.'_new_category_name');
693  echo '&nbsp;';
694  text_box($prefix.'_new_category_name', '', 15);
695  }
696  $o->closeField();
697 
698  }//end if
699 
700  return $write_access;
701 
702  }//end paintUserSubscriptions()
703 
704 
715  function processUserSubscriptions(&$asset, &$o, $prefix)
716  {
717  $write_access = $asset->writeAccess('attributes');
718  $subscription_enabled = $asset->attr('user_subscriptions_status');
719 
720  if ($write_access) {
721  $new_subscriptions = Array();
722  if ($subscription_enabled) {
723  if (isset($_POST[$prefix.'_subscriptions'])) {
724  foreach ($_POST[$prefix.'_subscriptions'] as $sub_name => $subscription) {
725  foreach ($subscription as $index => $subscription_data) {
726  if ($index === 'new_rule') {
727  if ($subscription['new_rule'] !== '0') {
728  $new_subscriptions[$sub_name][][$subscription['new_rule']] = Array(
729  'assetid' => 0,
730  'rule_cond' => 'contains',
731  'rule_text' => '',
732  );
733  }
734  continue;
735  }
736 
737  if (!isset($subscription_data['delete'])) {
738  $category = array_keys($subscription_data);
739  $new_subscriptions[$sub_name][][$category[0]] = Array(
740  'assetid' => $subscription_data[$category[0]]['assetid']['assetid'],
741  'rule_cond' => array_get_index($subscription_data[$category[0]], 'rule_cond', 'contains'),
742  'rule_text' => array_get_index($subscription_data[$category[0]], 'rule_text',''),
743  );
744  }
745  }
746  }
747  }
748  $new_category_name = array_get_index($_POST, $prefix.'_new_category_name', '');
749  if (!empty($new_category_name)) {
750  $new_subscriptions[$new_category_name] = Array();
751  }
752 
753  $asset->setAttrValue('subscriptions', $new_subscriptions);
754  return $asset->saveAttributes();
755  }
756  }
757 
758  return $write_access;
759 
760  }//end processUserSubscriptions()
761 
762 
773  function paintScheduling(&$asset, &$o, $prefix)
774  {
775  $write_access = $asset->writeAccess('attributes');
776  $scheduling = $asset->attr('scheduled_time');
777  $interval_opt = Array(
778  'empty_val' => 'None Selected',
779  'hourly' => 'Hourly',
780  'daily' => 'Daily',
781  'weekly' => 'Weekly',
782  'monthly' => 'Monthly',
783  'yearly' => 'Yearly',
784  );
785  $interval = array_get_index(array_keys($scheduling), 0, 'empty_val');
786  $start_date = array_get_index($scheduling, $interval, date('Y-m-d H:i').':00');
787  ?>
788  <table>
789  <tr>
790  <td>
791  <?php
792  echo '<b>'.translate('start_date').'</b>';
793  ?>
794  </td>
795  <td>
796  <?php
797  if ($write_access) {
798  $parameters = Array(
799  'min' => '2012-01-01 00:00:00',
800  'max' => ts_iso8601(time() + (60 * 60 * 24 * 365 * 4)),
801  'allow_circa' => '0',
802  'show' => Array('y', 'm', 'd', 'h', 'i'),
803  'null' => Array(),
804  'style' => Array(
805  'y' => 's',
806  'm' => 's',
807  'd' => 's',
808  'h' => 's',
809  'i' => 's',
810  ),
811  );
812 
813  $parameters['print_format'] = 'j M Y H:i';
814  $from = new Datetime_Field($prefix.'_from', $start_date, $parameters);
815 
816  $from->printField();
817  } else {
818  if ($start_date == date('Y-m-d H:i').':00') {
819  echo translate('not_configured');
820  } else {
821  echo $start_date;
822  }
823  }
824  ?>
825  </td>
826  </tr>
827  <tr>
828  <td>
829  <?php
830  echo '<b>'.translate('repeating').'</b>';
831  ?>
832  </td>
833  <td>
834  <?php
835  if ($write_access) {
836  combo_box($prefix.'_repeating', $interval_opt, FALSE, $interval);
837  } else {
838  echo $interval_opt[$interval];
839  }
840  ?>
841  </td>
842  </tr>
843  </table>
844  <?php
845 
846  return $write_access;
847 
848  }//end paintScheduling()
849 
850 
861  function processScheduling(&$asset, &$o, $prefix)
862  {
863  $write_access = $asset->writeAccess('attributes');
864  if ($write_access) {
865  $interval = array_get_index($_POST, $prefix.'_repeating', 'empty_val');
866  $parameters = Array(
867  'min' => '2012-01-01 00:00:00',
868  'max' => ts_iso8601(time() + (60 * 60 * 24 * 365 * 4)),
869  'allow_circa' => '0',
870  'show' => Array('y', 'm', 'd', 'h', 'i'),
871  'null' => Array(),
872  'style' => Array(
873  'y' => 's',
874  'm' => 's',
875  'd' => 's',
876  'h' => 's',
877  'i' => 's',
878  ),
879  );
880 
881  $parameters['print_format'] = 'j M Y H:i';
882  $value = NULL;
883  $from = new Datetime_Field($prefix.'_from', $value, $parameters);
884  $from->processField();
885 
886  $scheduling = Array();
887  if ($interval != 'empty_val') {
888  $scheduling = Array(
889  $interval => $from->value,
890  );
891  }
892  $asset->setAttrValue('scheduled_time', $scheduling);
893  $asset->setAttrValue('last_scheduled_run', 0);
894  }
895 
896  return $write_access;
897 
898  }//end processScheduling()
899 
900 
901 }//end class
902 
903 ?>