Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cron_job_future_status.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/system/cron/cron_job/cron_job.inc';
19 
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
56  protected function _createAdditional(Array &$link)
57  {
58  if (!parent::_createAdditional($link)) return FALSE;
59 
60  if (!empty($this->_tmp['asset_in_link'])) {
61  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($this->id, 'links')) {
62  trigger_localised_error('CRON0018', E_USER_WARNING);
63  return FALSE;
64  }
65 
66  $link = $this->_tmp['asset_in_link'];
67  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
68  if (is_null($asset) || !$this->setAssetToUpdate($asset)) {
69  trigger_localised_error('CRON0017', E_USER_WARNING);
70  return FALSE;
71  }
72 
73  unset($this->_tmp['asset_in_link']);
74  $GLOBALS['SQ_SYSTEM']->am->releaseLock($this->id, 'links');
75  }
76  return TRUE;
77 
78  }//end _createAdditional()
79 
80 
90  protected function _getName($short_name=FALSE)
91  {
92  $asset = $this->getAssetToUpdate();
93  if (is_null($asset)) {
94  return translate('cron_job_asset_less_future_status');
95  } else if ($short_name) {
96  return translate('cron_fs_for', translate('asset_format', $asset->short_name, $asset->id));
97  } else {
98  return translate('cron_future_status_for', translate('asset_format', $asset->name, $asset->id));
99  }
100 
101  }//end _getName()
102 
103 
111  public function _getAllowedLinks()
112  {
113  $links = parent::_getAllowedLinks();
114 
115  // make sure that we can link to every asset
116  if (empty($links[SQ_LINK_NOTICE]['asset'])) {
117  $links[SQ_LINK_NOTICE]['asset'] = Array('card' => 1);
118  }
119 
120  return $links;
121 
122  }//end _getAllowedLinks()
123 
124 
134  public function canDelete()
135  {
136  if (parent::canDelete()) return TRUE;
137 
138  // read-only?
139  if ($this->attr('read_only')) return FALSE;
140 
141  $asset = $this->getAssetToUpdate();
142  if (!is_null($asset)) return $asset->adminAccess('');
143  return FALSE;
144 
145  }//end canDelete()
146 
147 
157  public function setAttrValue($name, $value)
158  {
159  switch ($name) {
160  case 'status' :
161  $value = (int) $value;
162  if (!in_array($value, array_keys($this->_getAllStatii()))) {
163  trigger_localised_error('CRON0031', E_USER_NOTICE);
164  return FALSE;
165  }
166  break;
167 
168  case 'type' :
169  trigger_localised_error('CRON0038', E_USER_NOTICE);
170  return FALSE;
171  break;
172  }//end if
173 
174  return parent::setAttrValue($name, $value);
175 
176  }//end setAttrValue()
177 
178 
187  protected static function _getAllStatii()
188  {
189  return Array(
190  SQ_STATUS_ARCHIVED => 'ARCHIVED',
191  SQ_STATUS_UNDER_CONSTRUCTION => 'UNDER_CONSTRUCTION',
192  SQ_STATUS_LIVE => 'LIVE',
193  SQ_STATUS_EDITING => 'EDITING',
194  SQ_STATUS_LIVE_APPROVAL => 'LIVE_APPROVAL',
195  );
196 
197  }//end _getAllStatii()
198 
199 
209  public static function _getAllStatiiDescs()
210  {
211  static $descs = Array();
212 
213  if (empty($descs)) {
214  $descs[''] = '[Select Status]';
215  $statii = self::_getAllStatii();
216  foreach ($statii as $value => $code) {
217  $code = strtolower($code);
218  $status_code = 'asset_status_'.$code;
219  require_once SQ_INCLUDE_PATH.'/asset_status/'.$status_code.'.inc';
220  eval('$descs[$value] = '.$status_code.'::getDescription();');
221  }
222  }
223 
224  return $descs;
225 
226  }//end _getAllStatiiDescs()
227 
228 
235  public function statusName()
236  {
237  if (!$this->attr('status')) return '[ No Status Set ]';
238  $statii = self::_getAllStatii();
239  $code = strtolower($statii[$this->attr('status')]);
240  $status_code = 'asset_status_'.$code;
241  require_once SQ_INCLUDE_PATH.'/asset_status/'.$status_code.'.inc';
242  eval('$desc = '.$status_code.'::getDescription();');
243  return $desc;
244 
245  }//end statusName()
246 
247 
256  public function setAssetToUpdate(Asset $asset)
257  {
258  if ($this->id) {
259  $existing_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_NOTICE, '', TRUE, 'major', $value='updating');
260  $link_exists = FALSE;
261  foreach ($existing_links as $old_link) {
262  if ($old_link['minorid'] == $asset->id) {
263  $link_exists = TRUE;
264  } else {
265  $GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($old_link['linkid']);
266  }
267  }
268  if ($link_exists) {
269  return TRUE;
270  } else {
271  return (bool) $this->createLink($asset, SQ_LINK_NOTICE, 'updating');
272  }
273  } else {
274  if (!isset($this->_tmp['asset_in_link'])) {
275  $this->_tmp['asset_in_link'] = Array();
276  }
277  $this->_tmp['asset_in_link'] = Array('minorid' => $asset->id, 'minor_type_code' => $asset->type());
278  return TRUE;
279  }
280 
281  }//end setAssetToUpdate()
282 
283 
290  public function getAssetToUpdate()
291  {
292  // because we need to return by ref
293  $null = NULL;
294 
295  if ($this->id) {
296  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_NOTICE, 'asset', FALSE, 'updating');
297  } else {
298  $link = (isset($this->_tmp['asset_in_link'])) ? $this->_tmp['asset_in_link'] : Array();
299  }
300  if (empty($link)) return $null;
301 
302  return $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
303 
304  }//end getAssetToUpdate()
305 
306 
318  protected function _exec(&$msg)
319  {
320  $asset = $this->getAssetToUpdate();
321  if (!is_null($asset)) {
322 
323  $old_status = $asset->status;
324 
325  // process the status change
326  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
327  $vars = Array('assetid' => $asset->id, 'new_status' => $this->attr('status'), 'dependants_only' => $this->attr('dependants_only'));
328 
329  $status_errors = $hh->freestyleHipo('hipo_job_edit_status', $vars);
330  if (empty($status_errors)) {
331  // all OK, and we can be removed because we have done our one off task
332  $msg = translate('cron_altered_asset_status', translate('asset_format', $asset->name, $asset->id), get_status_description($old_status), $this->statusName());
333  return SQ_CRON_JOB_COMPLETED | SQ_CRON_JOB_REMOVE;
334  } else {
335  // process failed
336  trigger_localised_error('CRON0003', E_USER_WARNING, $asset->name, $asset->id);
337  // we have errors so dump them out too
338  foreach ($status_errors as $error) {
339  $line = '';
340  if (is_string($error)) {
341  $line = $error;
342  } else if (is_array($error)) {
343  if (isset($error['time'])) $line .= 'Time : '.$error['time'].'. ';
344  if (isset($error['message'])) $line .= 'Message : '.$error['message'].'.';
345  }
346  if ($line !== '') trigger_error($line, E_USER_WARNING);
347  }
348  }
349 
350  } else {
351  // asset not found
352  trigger_localised_error('CRON0020', E_USER_WARNING);
353  }
354 
355  // we can be removed because the error is beyound our control
356  return SQ_CRON_JOB_ERROR | SQ_CRON_JOB_REMOVE;
357 
358  }//end _exec()
359 
360 
371  public static function &getActiveJobs(Asset $asset)
372  {
373  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_NOTICE, strtolower(__CLASS__), TRUE, 'minor', 'updating');
374  $assetids = Array();
375  $cm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
376  foreach ($links as $link) {
377  $cm_links = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($cm->id, $link['majorid']);
378  if (!empty($cm_links)) {
379  // This asset is still linked to the cron manager, so its still active.
380  $assetids[] = $link['majorid'];
381  }
382  }
383  $dates = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('when', 'cron_job_future_status', $assetids);
384  asort($dates, SORT_STRING);
385  $res = Array();
386  foreach ($dates as $assetid => $date) {
387  $res[] = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, strtolower(__CLASS__));
388  }
389  return $res;
390 
391  }//end getActiveJobs()
392 
393 
394 }//end class
395 
396 ?>