Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cron_job_metadata_triggers.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/cron/cron_job/cron_job.inc';
18 
37 {
38 
45  function __construct($assetid=0)
46  {
47  parent::__construct($assetid);
48 
49  }//end constructor
50 
51 
63  function create(&$link)
64  {
65  require_once SQ_CORE_PACKAGE_PATH.'/system/system_asset_fns.inc';
66  if (!system_asset_fns_create_pre_check($this)) {
67  return FALSE;
68  }
69  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
70  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
71 
72  if ($linkid = parent::create($link)) {
73  if (!system_asset_fns_create_cleanup($this)) {
74  $linkid = FALSE;
75  }
76  }
77 
78  if ($linkid) {
79  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
80  } else {
81  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
82  }
83 
84  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
85  return $linkid;
86 
87  }//end create()
88 
89 
98  public function canDelete()
99  {
100  return FALSE;
101 
102  }//end canDelete()
103 
104 
113  function _getName($short_name=FALSE)
114  {
115  return 'Metadata Triggers Cron Job';
116 
117  }//end _getName()
118 
119 
130  function run()
131  {
132  $res = parent::run();
133  if ($res & SQ_CRON_JOB_REMOVE) {
134  $res = $res - SQ_CRON_JOB_REMOVE;
135  }
136  return $res;
137 
138  }//end run()
139 
140 
153  function _exec(&$msg)
154  {
155  $ret = SQ_CRON_JOB_COMPLETED;
156 
157  $db = $GLOBALS['SQ_SYSTEM']->db;
158 
159  // work out min date - if 'use_cron_last_run' is true, use cron manager's last run as min date here
160  // otherwise use 'ignore_date' attribute value
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());
164  } else {
165  $ignore_date = $this->Attr('ignore_date');
166  }
167 
168  $max_date = date('Y-m-d H:i:s');
169  if (empty($ignore_date)) return $ret;
170 
171  // 1. get all the triggers with a Metadata Time event
172  // and get all the metadsta fields for each one
173  //
174  // xxx: note that this code is highly dependent on the internal structure of the triggers
175  // This code could be made more independent once the the trigger manager provides the desired functions
176  // to browse its triggers
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']);
182  // we would like to know the field id of metadata specified in active triggers that use metadata time event
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;
188  }
189  }
190  }
191  }
192  }
193 
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'),
202  );
203 
204  // check to see if the $ignore_time is relative or fixed. If relative lets make a proper time out of it
205  $found = preg_match('/^([\-\+])([0-9]+)(['.implode('', array_keys($period_options)).'])$/', $ignore_date, $matches);
206 
207  if($found === 1) {
208  $ignore_date = date('Y-m-d H:i:s', strtotime($max_date.' '.$matches[1].$matches[2].$period_options[$matches[3]]));
209  }
210  }
211 
212  // if we are having the cut off date in past then we should swap min_date <=> max_date or else the
213  // sql will be wrong and we would not get desired results
214  if(strtotime($ignore_date) < strtotime($max_date) || ($found === 1 && $matchs[1] == '-')) {
215  $temp = $ignore_date;
216  $ignore_date = $max_date;
217  $max_date = $temp;
218  }
219 
220  // 2. get all the asset which have an expired date for those mdata fields
221  if (!empty($mdfieldids)) {
222  $mdfieldids_str = '';
223  foreach (array_keys($mdfieldids) as $mdfieldid) {
224  if (!empty($mdfieldids_str)) $mdfieldids_str .= ', ';
225  $mdfieldids_str .= MatrixDAL::quote($mdfieldid);
226  }
227 
228  // for performance reasons we directly query the database instead of using AM/MM functions
229  // Note that this query doesn't catch default metadata values
230  $result = Array();
231  $db = MatrixDAL::getDb();
232  $sql = 'SELECT
233  mv.assetid, mv.fieldid
234  FROM
235  sq_ast_mdata_val mv
236  WHERE
237  mv.fieldid IN ('.$mdfieldids_str.')';
238  if (MatrixDAL::getDbType() === 'oci') {
239  $sql .= ' AND CAST(mv.value AS varchar(255)) < :min_date AND CAST(mv.value AS varchar(255)) > :max_date';
240  } else {
241  $sql .= ' AND mv.value < :min_date AND mv.value > :max_date';
242  }
243 
244  try {
245  $query = MatrixDAL::preparePdoQuery($sql);
246  MatrixDAL::bindValueToPdo($query, 'min_date', $ignore_date, PDO::PARAM_STR);
247  MatrixDAL::bindValueToPdo($query, 'max_date', $max_date, PDO::PARAM_STR);
248  $result = MatrixDAL::executePdoAll($query);
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;
252  }
253 
254  // 3. broadcast trigger_event_metadata_time event for all the expired assets
255  foreach ($result as $asset_info) {
256  // fire the trigger for that mdata field and asset
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);
262  }
263  }
264 
265  return $ret;
266 
267  }//end _exec()
268 
269 
270 }//end class
271 
272 ?>