Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cron_job_send_submission_log.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/cron/cron_job/cron_job.inc';
18 
31 {
32 
33 
40  function __construct($assetid=0)
41  {
42  parent::__construct($assetid);
43 
44  }//end constructor
45 
46 
55  protected function _createAdditional(Array &$link)
56  {
57  if (!parent::_createAdditional($link)) return FALSE;
58 
59  if (!empty($this->_tmp['asset_in_link'])) {
60  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($this->id, 'links')) {
61  trigger_localised_error('CRON0018', E_USER_WARNING);
62  return FALSE;
63  }
64 
65  $link = $this->_tmp['asset_in_link'];
66  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
67  if (is_null($asset) || !$this->setAssetToUpdate($asset)) {
68  trigger_localised_error('CRON0017', E_USER_WARNING);
69  return FALSE;
70  }
71 
72  unset($this->_tmp['asset_in_link']);
73  $GLOBALS['SQ_SYSTEM']->am->releaseLock($this->id, 'links');
74  }
75  return TRUE;
76 
77  }//end _createAdditional()
78 
79 
89  protected function _getName($short_name=FALSE)
90  {
91  $asset = $this->getAssetToUpdate();
92  if (is_null($asset)) {
93  return 'Asset-less '.$GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name');
94  } else if ($short_name) {
95  return 'Send Submission Log Job for "'.$asset->short_name.'" [#'.$asset->id.']';
96  } else {
97  return 'Send Submission Log Job for "'.$asset->name.'" [#'.$asset->id.']';
98  }
99 
100  }//end _getName()
101 
102 
109  public function displayName()
110  {
111  return $this->attr('display_name');
112 
113  }//end displayName()
114 
115 
116 
124  public function _getAllowedLinks()
125  {
126  return Array(SQ_LINK_NOTICE => Array('form_email' => Array('card' => 1)));
127 
128  }//end _getAllowedLinks()
129 
130 
140  public function canDelete()
141  {
142  if (parent::canDelete()) return TRUE;
143 
144  // read-only?
145  if ($this->attr('read_only')) return FALSE;
146 
147  $asset = $this->getAssetToUpdate();
148  if (!is_null($asset)) return $asset->adminAccess('');
149  return FALSE;
150 
151  }//end canDelete()
152 
153 
162  public function setAssetToUpdate(Form_Email $form_email)
163  {
164  if ($this->id) {
165  $existing_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_NOTICE, '', TRUE, 'major', $value='updating');
166  $link_exists = FALSE;
167  foreach ($existing_links as $old_link) {
168  if ($old_link['minorid'] == $form_email->id) {
169  $link_exists = TRUE;
170  } else {
171  $GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($old_link['linkid']);
172  }
173  }
174  if ($link_exists) {
175  return TRUE;
176  } else {
177  return (bool) $this->createLink($form_email, SQ_LINK_NOTICE, 'updating');
178  }
179  } else {
180  if (!isset($this->_tmp['asset_in_link'])) {
181  $this->_tmp['asset_in_link'] = Array();
182  }
183  $this->_tmp['asset_in_link'] = Array('minorid' => $form_email->id, 'minor_type_code' => $form_email->type());
184  return TRUE;
185  }
186 
187  }//end setAssetToUpdate()
188 
189 
196  public function getAssetToUpdate()
197  {
198  // because we need to return by ref
199  $null = NULL;
200 
201  if ($this->id) {
202  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_NOTICE, 'form_email', TRUE, 'updating');
203  } else {
204  $link = (isset($this->_tmp['asset_in_link'])) ? $this->_tmp['asset_in_link'] : Array();
205  }
206  if (empty($link)) return $null;
207 
208  return $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], 'form_email');
209 
210  }//end getAssetToUpdate()
211 
212 
224  protected function _exec(&$msg)
225  {
226  $form = $this->getAssetToUpdate();
227 
228  if (!is_null($form) && $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($form->id, 'form_email')) {
229 
230  $log_file_info = $this->_getFormSubmissionLogFile($form);
231 
232  if (is_array($log_file_info)) {
233 
234  $email_format = $this->getAttribute('email_format');
235  $email_format_val = @unserialize($email_format->value);
236 
237  $text_email = array_get_index($email_format_val, 'text_format', '');
238  $html_email = array_get_index($email_format_val, 'html_format', '');
239  $email_subject = array_get_index($email_format_val, 'subject', '');
240  $email_from = array_get_index($email_format_val, 'from', '');
241  $email_reply_to = array_get_index($email_format_val, 'from', '');
242 
243  $keywords = extract_keywords($text_email.$html_email.$email_subject.$email_from.$email_reply_to);
244  $keyword_replacements = Array();
245  foreach($keywords as $full_keyword) {
246  $modifiers = NULL;
247  $part_keyword = parse_keyword($full_keyword, $modifiers);
248  if ($part_keyword == 'submission_log_from_date') {
249  $keyword_value = date('Y-m-d H:i', $log_file_info['from']);
250  } else if ($part_keyword == 'submission_log_to_date') {
251  $keyword_value = date('Y-m-d H:i', $log_file_info['till']);
252  } else {
253  $keyword_value = $form->getKeywordReplacement($part_keyword);
254  }
255  if ($part_keyword != $full_keyword) {
256  apply_keyword_modifiers($keyword_value, $modifiers, Array('assetid' => $form->id));
257  }
258  $keyword_replacements[$full_keyword] = $keyword_value;
259  }
260 
261  $email_format->sendMail($keyword_replacements, Array($log_file_info['file']));
262 
263  // Mark the run time
264  $this->setAttrValue('previous_run', $log_file_info['till']);
265  $this->saveAttributes();
266 
267  $msg = 'Submission log sent for form #'.$form->id.' (job #'.$this->id.')';
268 
269  return SQ_CRON_JOB_COMPLETED;
270  }
271  } else {
272  // asset not found
273  trigger_localised_error('CMS0120', E_USER_WARNING);
274  }
275 
276  return SQ_CRON_JOB_ERROR;
277 
278  }//end _exec()
279 
280 
290  public static function &getActiveJobs(Form_Email $form_email)
291  {
292  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($form_email->id, SQ_LINK_NOTICE, strtolower(__CLASS__), TRUE, 'minor', 'updating');
293  $assetids = Array();
294  $cm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
295  foreach ($links as $link) {
296  $cm_links = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($cm->id, $link['majorid']);
297  if (!empty($cm_links)) {
298  // This asset is still linked to the cron manager, so its still active.
299  $assetids[] = $link['majorid'];
300  }
301  }
302  $dates = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('when', 'cron_job_send_submission_log', $assetids);
303  asort($dates, SORT_STRING);
304  $res = Array();
305  foreach ($dates as $assetid => $date) {
306  $res[] = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, strtolower(__CLASS__));
307  }
308  return $res;
309 
310  }//end getActiveJobs()
311 
312 
323  function run()
324  {
325  $res = parent::run();
326  if ($res & SQ_CRON_JOB_REMOVE) {
327  $res = $res - SQ_CRON_JOB_REMOVE;
328  }
329  return $res;
330 
331  }//end run()
332 
333 
343  {
344  $edit_fns = $form->getEditFns();
345 
346  // Pick the submissions starting from this date
347  $start_date = str_replace('--:', '00:', str_replace(':--', ':00', $this->attr('submissions_from_date')));
348  // If any date component is not setup
349  $pick_from_date = strpos($start_date, '--') !== FALSE ? '' : strtotime($start_date);
350 
351  // Last time the job actually ran
352  $previous_run = $this->attr('previous_run');
353 
354  if ($this->attr('submissions_range') == 'last_run') {
355  if (!empty($previous_run)) {
356  // If the job ran previosuly, pick it from there
357  $from_ts = $previous_run;
358  } else if ($pick_from_date) {
359  // The job is running for first time, pick since the "Start" date
360  $from_ts = $pick_from_date;
361  } else {
362  // No "Start" date configured, pick submissions since the creation of the form
363  $from_ts = $form->created;
364  }
365  } else {
366  $from_ts = !empty($pick_from_date) ? $pick_from_date : $form->created;
367  }
368 
369  if (!$from_ts) {
370  trigger_localised_error('CMS0121', E_USER_WARNING);
371  return FALSE;
372  }
373 
374  $till_ts = time();
375 
376  switch($this->attr('logs_format')) {
377  case 'csv_nhr':
378  case 'csv':
379  $csv = $edit_fns->createCSVSubmissionLogs($form, $from_ts, $till_ts);
380  $delimiter = $this->attr('csv_delimiter');
381  $csv->setDeliminator(!empty($delimiter) ? $delimiter : ',');
382 
383  if ($this->attr('logs_format') == 'csv_nhr') {
384  $empty_header = FALSE;
385  $csv->setFieldHeaders($empty_header);
386  }
387  ob_start();
388  @$csv->export(TRUE);
389  $output = ob_get_contents();
390  ob_end_clean();
391  break;
392 
393  case 'xml':
394  $o = NULL;
395  ob_start();
396  @$this->_processExportXML($form, $from_ts, $till_ts);
397  $output = ob_get_contents();
398  ob_end_clean();
399  break;
400 
401  }//end switch
402 
403  $job_name = trim(preg_replace('|[^a-z0-9\-_]+|', '_', strtolower($this->displayName())), '_');
404  $file_name = SQ_TEMP_PATH.'/submissions_log_'.$job_name.'('.date('Y-m-d H:i', $from_ts).' to '.date('Y-m-d H:i', $till_ts).')';
405  file_put_contents($file_name, $output);
406 
407  return Array(
408  'file' => $file_name,
409  'from' => $from_ts,
410  'till' => $till_ts,
411  );
412 
413  }//end getFormSubmissionLogFile()
414 
415 
426  function _processExportXML($asset, $from_ts, $to_ts)
427  {
428  $edit_fns = $asset->getEditFns();
429 
430  $from_time = ts_iso8601($from_ts);
431  $to_time = ts_iso8601($to_ts);
432  $sub_folder = $asset->getSubmissionsFolder();
433  $assetids = Array($asset->id, $sub_folder->id);
434 
435  try {
436  $bind_vars = Array(
437  'assetids' => $assetids,
438  'from_time' => $from_time,
439  'to_time' => $to_time,
440  'name' => 'submitted',
441  'context' => $GLOBALS['SQ_SYSTEM']->getContextId(),
442  );
443  $result = $edit_fns->_getSubmissionLog($bind_vars);
444  } catch (Exception $e) {
445  throw new Exception("Unable to get submission log due to database error: ".$e->getMessage());
446  }
447 
448  echo "<submissions>\n";
449  foreach ($result as $asset_info) {
450  $assetid = $asset_info['assetid'];
451  $type_code = $asset_info['type_code'];
452 
453  $sub_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $type_code);
454  if (!$sub_asset->attr('complete')) continue;
455 
456  if (trim($sub_asset->attr('xml')) == '') continue;
457  echo "\t".$sub_asset->attr('xml')."\n";
458  }
459  echo '</submissions>';
460 
461  }//end processExportXML()
462 
463 
464 
465 }//end class
466 
467 ?>