18 require_once SQ_CORE_PACKAGE_PATH.
'/system/cron/cron_job/cron_job.inc';
45 $this->_ser_attrs = TRUE;
46 parent::__construct($assetid);
61 if (!parent::_createAdditional($link))
return FALSE;
63 if (!empty($this->_tmp[
'asset_in_link'])) {
64 if (!$GLOBALS[
'SQ_SYSTEM']->am->acquireLock($this->id,
'links')) {
65 trigger_localised_error(
'CRON0018', E_USER_WARNING);
69 foreach ($this->_tmp[
'asset_in_link'] as $side_of_link => $link) {
70 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid'], $link[
'minor_type_code']);
71 if (is_null($asset) || !$this->
setAssetInLink($asset, $side_of_link)) {
72 trigger_localised_error(
'CRON0017', E_USER_WARNING);
76 unset($this->_tmp[
'asset_in_link']);
77 $GLOBALS[
'SQ_SYSTEM']->am->releaseLock($this->
id,
'links');
96 if (is_null($asset)) {
97 return translate(
'cron_job_asset-less_future_linking');
98 }
else if ($short_name) {
99 return translate(
'cron_fl_for', translate(
'asset_format', $asset->short_name, $asset->id));
101 return translate(
'cron_future_linking_for', translate(
'asset_format', $asset->name, $asset->id));
116 $links = parent::_getAllowedLinks();
119 if (empty($links[SQ_LINK_NOTICE][
'asset'])) {
120 $links[SQ_LINK_NOTICE][
'asset'] = Array(
'card' => 2);
138 if (parent::canDelete())
return TRUE;
141 if (!is_null($asset))
return $asset->adminAccess(
'');
160 trigger_localised_error(
'CRON0039', E_USER_NOTICE);
163 case 'delete_linkid':
168 case 'delete_link_all':
169 if ($value == TRUE) {
175 return parent::setAttrValue($name, $value);
191 if ($side_of_link !=
'major' && $side_of_link !=
'minor') {
192 trigger_localised_error(
'CRON0033', E_USER_WARNING, $side_of_link);
196 if (!($asset instanceof
Asset)) {
197 trigger_localised_error(
'CRON0030', E_USER_WARNING);
202 return (
bool) $this->
createLink($asset, SQ_LINK_NOTICE, $side_of_link.
'_asset');
204 if (!isset($this->_tmp[
'asset_in_link'])) {
205 $this->_tmp[
'asset_in_link'] = Array();
207 $this->_tmp[
'asset_in_link'][$side_of_link] = Array(
'minorid' => $asset->id,
'minor_type_code' => $asset->
type());
225 if ($side_of_link !=
'major' && $side_of_link !=
'minor') {
226 trigger_localised_error(
'CRON0033', E_USER_WARNING, $side_of_link);
231 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_NOTICE,
'asset', FALSE, $side_of_link.
'_asset');
233 $link = (isset($this->_tmp[
'asset_in_link'][$side_of_link])) ? $this->_tmp[
'asset_in_link'][$side_of_link] : Array();
236 if (empty($link))
return $null;
237 return $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid'], $link[
'minor_type_code']);
255 if (is_null($minor)) {
256 return translate(
'no_minor_asset_found');
262 if ($this->
attr(
'delete_linkid')) {
263 $str .= translate(
'deleting_link_id', $this->
attr(
'delete_linkid'));
264 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLinkById($this->
attr(
'delete_linkid'));
267 $old_major = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'majorid'], $link[
'major_type_code']);
268 if (!is_null($old_major)) {
269 $str = translate(
'delete_link_for', translate(
'asset_format', $minor->name, $minor->id), translate(
'asset_format', $old_major->name, $old_major->id));
276 if ($this->
attr(
'delete_linkid') && !is_null($new_major)) {
280 if (!is_null($new_major)) {
281 $str .= translate(
'create_new_link_for', translate(
'asset_format', $minor->name, $minor->id), translate(
'asset_format', $new_major->name, $new_major->id));
300 return translate(
'no_major_asset_found');
305 if ($this->
attr(
'delete_linkid')) {
306 $str .= translate(
'deleting_link_id', $this->
attr(
'delete_linkid'));
307 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLinkById($this->
attr(
'delete_linkid'));
310 $old_minor = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid'], $link[
'minor_type_code']);
311 if (!is_null($old_minor)) {
312 $str = translate(
'delete_link_for', translate(
'asset_format', $old_minor->name, $old_minor->id), translate(
'asset_format', $major->name, $major->id));
319 if ($this->
attr(
'delete_linkid') && !is_null($new_minor)) {
323 if (!is_null($new_minor)) {
324 $str .= translate(
'create_new_link_for', translate(
'asset_format', $new_minor->name, $new_minor->id), translate(
'asset_format', $major->name, $major->id));
346 $failed_ret_val = SQ_CRON_JOB_ERROR | SQ_CRON_JOB_REMOVE;
355 $main_asset = $this->
attr(
'reverse_mode') ? $major : $minor;
356 $target_asset = $this->
attr(
'reverse_mode') ? $minor : $major;
357 $side_of_link = $this->
attr(
'reverse_mode') ?
'major' :
'minor';
360 if (is_null($main_asset)) {
361 trigger_localised_error(
'CRON0019', E_USER_WARNING);
362 return $failed_ret_val;
368 $safe_trash = $GLOBALS[
'SQ_SYSTEM']->getUserPrefs(
'user',
'SQ_USER_SAFE_TYPE3_TRASH');
369 $can_safe_trash = TRUE;
372 $exclude_self = FALSE;
376 if ($this->
attr(
'delete_link_all')){
378 $exclude_self = TRUE;
380 else if($this->
attr(
'delete_linkid') > 0){
381 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLinkById($this->
attr(
'delete_linkid'));
382 $minor = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid'], $link[
'minor_type_code']);
387 $can_safe_trash = $this->
_canSafeTrash($minor, $exclude_self);
390 $existing_links = Array();
391 if ($this->
attr(
'delete_link_all') == TRUE && $can_safe_trash) {
392 $links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($main_asset->id, SQ_SC_LINK_SIGNIFICANT,
'', TRUE, $side_of_link);
393 foreach ($links as $link) {
394 $existing_links[] = $link[
'linkid'];
396 }
else if ($this->
attr(
'delete_linkid') > 0 && $can_safe_trash) {
397 $existing_links[] = $this->
attr(
'delete_linkid');
400 log_dump($existing_links);
402 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
403 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
405 if (!is_null($target_asset)) {
407 $trash_folder = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'trash_folder');
408 if ($major->id == $trash_folder->id && !$can_safe_trash) {
409 trigger_localised_error(
'CRON0051', E_USER_WARNING, $minor->id);
410 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
411 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
412 return $failed_ret_val;
415 $link_info = $this->
attr(
'link_info');
416 if (!isset($link_info[
'value'])) {
417 $link_info[
'value'] =
'';
419 if (!isset($link_info[
'sort_order'])) {
420 $link_info[
'sort_order'] = NULL;
422 if (!isset($link_info[
'is_dependant'])) {
423 $link_info[
'is_dependant'] =
'0';
425 if (!isset($link_info[
'is_exclusive'])) {
426 $link_info[
'is_exclusive'] =
'0';
429 $linkid = $major->createLink($minor,
430 $link_info[
'link_type'],
432 $link_info[
'sort_order'],
433 $link_info[
'is_dependant'],
434 $link_info[
'is_exclusive']
437 $create_ok = ($linkid > 0);
440 trigger_localised_error(
'CRON0004', E_USER_WARNING, $major->name, $major->id);
441 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
442 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
443 return $failed_ret_val;
449 $delete_link_success = TRUE;
450 foreach ($existing_links as $linkid) {
455 if (!$delete_link_success) {
456 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
457 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
458 return $failed_ret_val;
463 $this->_tmp[
'_update_lookups_assets'][] = $minor->id;
465 $hh = $GLOBALS[
'SQ_SYSTEM']->getHipoHerder();
467 $vars = Array(
'assetids' => $this->_tmp[
'_update_lookups_assets']);
468 $lookup_errors = $hh->freestyleHipo(
'hipo_job_update_lookups', $vars);
469 if (!empty($lookup_errors)) {
470 trigger_localised_error(
'CRON0008', E_USER_WARNING, $main_asset->name, $main_asset->id);
471 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
472 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
473 return $failed_ret_val;
476 $msg =
"Successful in \n".$desc;
477 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
478 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
481 return SQ_CRON_JOB_COMPLETED | SQ_CRON_JOB_REMOVE;
497 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLinkById($linkid);
500 $old_major = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'majorid'], $link[
'major_type_code']);
502 $minor = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid'], $link[
'minor_type_code']);
503 $this->_tmp[
'_update_lookups_assets'][] = $minor->id;
504 if (!is_null($old_major) && $GLOBALS[
'SQ_SYSTEM']->am->acquireLock($old_major->id,
'links')) {
505 $delete_ok = $old_major->deleteLink($linkid, TRUE);
510 trigger_localised_error(
'CRON0007', E_USER_WARNING, $linkid);
511 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
512 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
533 $cron_mgr = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'cron_manager');
534 if (is_null($cron_mgr)) {
540 $all_jobs = $cron_mgr->getJobs(strtolower(__CLASS__), FALSE);
543 for ($i = 0, $total = count($all_jobs); $i < $total; $i++) {
544 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($all_jobs[$i]->
id, SQ_LINK_NOTICE,
'asset', FALSE, $reverse_mode ?
'major_asset' :
'minor_asset');
545 if (!empty($link) && $link[
'minorid'] == $asset->id) {
546 $dates[$i] = $all_jobs[$i]->attr(
'when');
551 asort($dates, SORT_STRING);
552 $assets_jobs = Array();
554 foreach ($dates as $i => $date) {
555 $assets_jobs[] = $all_jobs[$i];
573 $can_safe_trash = TRUE;
576 if ($minor->status >= SQ_STATUS_LIVE ) {
577 $can_safe_trash = FALSE;
580 $affected_links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($minor->id, SQ_LINK_TYPE_3 + SQ_LINK_NOTICE,
'', TRUE,
'minor');
581 foreach ($affected_links as $info) {
583 if ($info[
'major_type_code'] != $this->
type()) {
584 $can_safe_trash = FALSE;
591 $ret_val = $GLOBALS[
'SQ_SYSTEM']->am->generateGetChildrenQuery($minor,
'', TRUE, FALSE);
592 $children = $ret_val[
'sql_array'];
593 $bind_vars = $ret_val[
'bind_vars'];
595 if (!empty($children)) {
597 $children[
'select'] =
'(SELECT DISTINCT l.minorid';
598 if (isset($children[
'union_select'])) {
599 $children[
'union_select'] =
'UNION ALL SELECT sl.minorid';
603 majorid, minorid, name, linkid, status, link_type
605 '.SQ_TABLE_RUNNING_PREFIX.
'ast_lnk lnk
606 INNER JOIN '.SQ_TABLE_RUNNING_PREFIX.
'ast ast ON (ast.assetid = lnk.minorid)
609 (link_type = :link_type_3 OR link_type = :link_notice) OR
610 (status >= :status_live)
612 assetid IN ('.implode(
' ', $children).
')';
616 foreach ($bind_vars as $bind_var => $bind_value) {
624 throw new Exception (
'Unable to check if asset "'.$minor->name.
'" (#'.$minor->id.
') can be safe-trashed due to database error: '.$e->getMessage());
626 if (!empty($result)) {
627 $can_safe_trash = FALSE;
630 return $can_safe_trash;