17 require_once SQ_CORE_PACKAGE_PATH.
'/system/cron/cron_job/cron_job.inc';
47 parent::__construct($assetid);
65 require_once SQ_CORE_PACKAGE_PATH.
'/system/system_asset_fns.inc';
66 if (!system_asset_fns_create_pre_check($this)) {
69 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
70 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
72 if ($linkid = parent::create($link)) {
73 if (!system_asset_fns_create_cleanup($this)) {
79 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
81 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
84 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
115 return 'Metadata Triggers Cron Job';
132 $res = parent::run();
133 if ($res & SQ_CRON_JOB_REMOVE) {
134 $res = $res - SQ_CRON_JOB_REMOVE;
155 $ret = SQ_CRON_JOB_COMPLETED;
157 $db = $GLOBALS[
'SQ_SYSTEM']->db;
161 if ($this->Attr(
'use_cron_last_run')) {
162 $cron_mgr = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'cron_manager');
163 $ignore_date = date(
'Y-m-d H:i:s', $cron_mgr->getLastRun());
165 $ignore_date = $this->Attr(
'ignore_date');
168 $max_date = date(
'Y-m-d H:i:s');
169 if (empty($ignore_date))
return $ret;
177 $mdfieldids = Array();
178 $tm = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'trigger_manager');
179 $triggers = $tm->getTriggerList();
180 foreach ($triggers as $trigger_info) {
181 $trigger = $tm->getTrigger($trigger_info[
'id']);
183 if ($trigger[
'active'] && !empty($trigger[
'data'][
'events'][
'trigger_event_metadata_time'])) {
184 foreach ($trigger[
'data'][
'conditions'] as $trigger_condition) {
185 if ($trigger_condition[
'type'] ==
'trigger_condition_metadata_time') {
186 if (isset($trigger_condition[
'data'][
'trigger_metadata_date_fieldid'])) {
187 $mdfieldids[$trigger_condition[
'data'][
'trigger_metadata_date_fieldid']] = 1;
194 if (!$this->Attr(
'use_cron_last_run')) {
195 $period_options = Array(
196 'i' => translate(
'minutes'),
197 'h' => translate(
'hours'),
198 'd' => translate(
'days'),
199 'w' => translate(
'weeks'),
200 'm' => translate(
'months'),
201 'y' => translate(
'years'),
205 $found = preg_match(
'/^([\-\+])([0-9]+)(['.implode(
'', array_keys($period_options)).
'])$/', $ignore_date, $matches);
208 $ignore_date = date(
'Y-m-d H:i:s', strtotime($max_date.
' '.$matches[1].$matches[2].$period_options[$matches[3]]));
214 if(strtotime($ignore_date) < strtotime($max_date) || ($found === 1 && $matchs[1] ==
'-')) {
215 $temp = $ignore_date;
216 $ignore_date = $max_date;
221 if (!empty($mdfieldids)) {
222 $mdfieldids_str =
'';
223 foreach (array_keys($mdfieldids) as $mdfieldid) {
224 if (!empty($mdfieldids_str)) $mdfieldids_str .=
', ';
233 mv.assetid, mv.fieldid
237 mv.fieldid IN ('.$mdfieldids_str.
')';
239 $sql .=
' AND CAST(mv.value AS varchar(255)) < :min_date AND CAST(mv.value AS varchar(255)) > :max_date';
241 $sql .=
' AND mv.value < :min_date AND mv.value > :max_date';
249 }
catch (Exception $e) {
250 throw new Exception(
'Cron Job Metadata Trigger failed to execute the DB Query: '.$e->getMessage());
251 $ret = SQ_CRON_JOB_ERROR;
255 foreach ($result as $asset_info) {
257 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($asset_info[
'assetid']);
258 if (is_null($asset))
continue;
259 $parameters = Array(
'trigger_metadata_date_fieldid' => $asset_info[
'fieldid']);
260 $GLOBALS[
'SQ_SYSTEM']->broadcastTriggerEvent(
'trigger_event_metadata_time', $asset, $parameters);
261 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset);