17 require_once SQ_CORE_PACKAGE_PATH.
'/system/cron/cron_job/cron_job.inc';
42 parent::__construct($assetid);
57 if (!parent::_createAdditional($link))
return FALSE;
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);
65 $link = $this->_tmp[
'asset_in_link'];
66 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid'], $link[
'minor_type_code']);
68 trigger_localised_error(
'CRON0017', E_USER_WARNING);
72 unset($this->_tmp[
'asset_in_link']);
73 $GLOBALS[
'SQ_SYSTEM']->am->releaseLock($this->
id,
'links');
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.
']';
97 return 'Send Submission Log Job for "'.$asset->name.
'" [#'.$asset->id.
']';
111 return $this->
attr(
'display_name');
126 return Array(SQ_LINK_NOTICE => Array(
'form_email' => Array(
'card' => 1)));
142 if (parent::canDelete())
return TRUE;
145 if ($this->
attr(
'read_only'))
return FALSE;
148 if (!is_null($asset))
return $asset->adminAccess(
'');
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) {
171 $GLOBALS[
'SQ_SYSTEM']->am->deleteAssetLink($old_link[
'linkid']);
177 return (
bool) $this->
createLink($form_email, SQ_LINK_NOTICE,
'updating');
180 if (!isset($this->_tmp[
'asset_in_link'])) {
181 $this->_tmp[
'asset_in_link'] = Array();
183 $this->_tmp[
'asset_in_link'] = Array(
'minorid' => $form_email->id,
'minor_type_code' => $form_email->
type());
202 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_NOTICE,
'form_email', TRUE,
'updating');
204 $link = (isset($this->_tmp[
'asset_in_link'])) ? $this->_tmp[
'asset_in_link'] : Array();
206 if (empty($link))
return $null;
208 return $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid'],
'form_email');
228 if (!is_null($form) && $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo($form->id,
'form_email')) {
232 if (is_array($log_file_info)) {
235 $email_format_val = @unserialize($email_format->value);
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',
'');
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) {
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']);
253 $keyword_value = $form->getKeywordReplacement($part_keyword);
255 if ($part_keyword != $full_keyword) {
256 apply_keyword_modifiers($keyword_value, $modifiers, Array(
'assetid' => $form->id));
258 $keyword_replacements[$full_keyword] = $keyword_value;
261 $email_format->sendMail($keyword_replacements, Array($log_file_info[
'file']));
264 $this->
setAttrValue(
'previous_run', $log_file_info[
'till']);
267 $msg =
'Submission log sent for form #'.$form->id.
' (job #'.$this->
id.
')';
269 return SQ_CRON_JOB_COMPLETED;
273 trigger_localised_error(
'CMS0120', E_USER_WARNING);
276 return SQ_CRON_JOB_ERROR;
292 $links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($form_email->id, SQ_LINK_NOTICE, strtolower(__CLASS__), TRUE,
'minor',
'updating');
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)) {
299 $assetids[] = $link[
'majorid'];
302 $dates = $GLOBALS[
'SQ_SYSTEM']->am->getAttributeValuesByName(
'when',
'cron_job_send_submission_log', $assetids);
303 asort($dates, SORT_STRING);
305 foreach ($dates as $assetid => $date) {
306 $res[] = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid, strtolower(__CLASS__));
325 $res = parent::run();
326 if ($res & SQ_CRON_JOB_REMOVE) {
327 $res = $res - SQ_CRON_JOB_REMOVE;
344 $edit_fns = $form->getEditFns();
347 $start_date = str_replace(
'--:',
'00:', str_replace(
':--',
':00', $this->
attr(
'submissions_from_date')));
349 $pick_from_date = strpos($start_date,
'--') !== FALSE ?
'' : strtotime($start_date);
352 $previous_run = $this->
attr(
'previous_run');
354 if ($this->
attr(
'submissions_range') ==
'last_run') {
355 if (!empty($previous_run)) {
357 $from_ts = $previous_run;
358 }
else if ($pick_from_date) {
360 $from_ts = $pick_from_date;
363 $from_ts = $form->created;
366 $from_ts = !empty($pick_from_date) ? $pick_from_date : $form->created;
370 trigger_localised_error(
'CMS0121', E_USER_WARNING);
376 switch($this->
attr(
'logs_format')) {
379 $csv = $edit_fns->createCSVSubmissionLogs($form, $from_ts, $till_ts);
380 $delimiter = $this->
attr(
'csv_delimiter');
381 $csv->setDeliminator(!empty($delimiter) ? $delimiter :
',');
383 if ($this->
attr(
'logs_format') ==
'csv_nhr') {
384 $empty_header = FALSE;
385 $csv->setFieldHeaders($empty_header);
389 $output = ob_get_contents();
397 $output = ob_get_contents();
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);
408 'file' => $file_name,
428 $edit_fns = $asset->getEditFns();
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);
437 'assetids' => $assetids,
438 'from_time' => $from_time,
439 'to_time' => $to_time,
440 'name' =>
'submitted',
441 'context' => $GLOBALS[
'SQ_SYSTEM']->getContextId(),
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());
448 echo
"<submissions>\n";
449 foreach ($result as $asset_info) {
450 $assetid = $asset_info[
'assetid'];
451 $type_code = $asset_info[
'type_code'];
453 $sub_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid, $type_code);
454 if (!$sub_asset->attr(
'complete'))
continue;
456 if (trim($sub_asset->attr(
'xml')) ==
'')
continue;
457 echo
"\t".$sub_asset->attr(
'xml').
"\n";
459 echo
'</submissions>';