Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cron_job_future_lineage_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/system/cron/cron_job/cron_job_edit_fns.inc';
19 
32 {
33 
34 
40  function __construct()
41  {
42  parent::__construct();
43 
44  }//end constructor
45 
46 
57  public function paintInLineBackend(Cron_Job_Future_Lineage $asset, Backend_Outputter $o, $prefix)
58  {
59  $major = $asset->getAssetInLink('major');
60  $minor = $asset->getAssetInLink('minor');
61 
62  $o->openSection(($asset->id) ? $asset->name : translate('cron_new_future_linking'));
63 
64  $o->openField(translate('delete_link'));
65  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($minor->id, SQ_SC_LINK_SIGNIFICANT, '', TRUE, 'minor');
66  $link_options = Array(0 => '', 'SQ_CRON_JOB_FUTURE_LINEAGE_DELETE_ALL_LINKS' => translate('cron_fl_delete_all_existing_links'));
67  foreach ($links as $link) {
68  $parent = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['majorid'], $link['major_type_code']);
69 
70  // if the link id is null we got to continue
71  // this line has been added to fix a problem when viewing the linking screen of
72  // a LDAP backend User
73  if (empty($link['linkid'])) continue;
74 
75  if (is_null($parent)) continue;
76  $link_options[$link['linkid']] = translate('link').' #'.$link['linkid'].' - '.translate('parent').' : '.$parent->name.' (#'.$parent->id.')';
77  }
78 
79  $default_delete_linkid = $asset->attr('delete_linkid');
80  if ($asset->attr('delete_link_all')) {
81  $default_delete_linkid = 'SQ_CRON_JOB_FUTURE_LINEAGE_DELETE_ALL_LINKS';
82  }
83  combo_box($prefix.'_delete_linkid', $link_options, FALSE, $default_delete_linkid);
84  $o->closeField();
85 
86  $o->openField(translate('new_link_type'));
87  $link_info = $asset->attr('link_info');
88  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
89  $editable_link_types = Array(
90  SQ_LINK_TYPE_1 => link_type_name(SQ_LINK_TYPE_1),
91  SQ_LINK_TYPE_2 => link_type_name(SQ_LINK_TYPE_2),
92  SQ_LINK_NOTICE => link_type_name(SQ_LINK_NOTICE),
93  );
94  $link_type = (isset($link_info['link_type'])) ? $link_info['link_type'] : '';
95  combo_box($prefix.'_link_type', $editable_link_types, FALSE, $link_type);
96  $o->closeField();
97 
98  $o->openField(translate('new_link_parent'));
99  if ($asset->writeAccess('links')) {
100  asset_finder($prefix.'_major_assetid', ((is_null($major)) ? 0 : $major->id), Array(), 'sq_sidenav', FALSE, 'null', Array('clear'));
101  } else {
102  if (is_null($major)) {
103  echo '<a href="'.$major->getBackendHref().'">'.translate('asset_format', $major->name, $major->id).'</a>';
104  } else {
105  echo translate('not_set');
106  }
107  }
108  $o->closeField();
109 
110  $o->openField(translate('at'));
111  $this->_paintWhenBox($asset, $o, $prefix);
112  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
113  $o->note(translate('cron_manager_next_run', $cron_mgr->readableRefreshTime(), $GLOBALS['SQ_SYSTEM']->datetime($cron_mgr->timeOfNextRun())));
114  $o->closeField();
115 
116  $o->closeSection();
117 
118  return TRUE;
119 
120  }//end paintInLineBackend()
121 
122 
134  {
135  if (empty($_POST[$prefix.'_delete_linkid']) && (empty($_POST[$prefix.'_link_type']) || empty($_POST[$prefix.'_major_assetid']['assetid']))) {
136  return FALSE;
137  }
138 
139  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
140  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
141 
142  // Are we going to delete all existing links...? Or maybe just the one that
143  // the user has specified.
144  $delete_linkid = $_POST[$prefix.'_delete_linkid'];
145  if ($delete_linkid == 'SQ_CRON_JOB_FUTURE_LINEAGE_DELETE_ALL_LINKS') {
146  $attribute = Array('name' => 'delete_link_all', 'value' => TRUE);
147  } else {
148  $attribute = Array('name' => 'delete_linkid', 'value' => $delete_linkid);
149  }
150  if (!$asset->setAttrValue($attribute['name'], $attribute['value'])) {
151  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
152  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
153  return FALSE;
154  }
155 
156  if (!empty($_POST[$prefix.'_major_assetid']['assetid'])) {
157  $major = $GLOBALS['SQ_SYSTEM']->am->getAsset($_POST[$prefix.'_major_assetid']['assetid']);
158  if (is_null($major)) {
159  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
160  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
161  return FALSE;
162  }
163  $link_info = $asset->attr('link_info');
164  $link_info['link_type'] = $_POST[$prefix.'_link_type'];
165  if (!$asset->setAttrValue('link_info', $link_info) || !$asset->setAssetInLink($major, 'major')) {
166  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
167  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
168  return FALSE;
169  }
170  }
171 
172  if (!$asset->saveAttributes() || !$asset->setAttrValue('when', $this->_processWhenBox($asset, $o, $prefix))) {
173  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
174  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
175  return FALSE;
176  }
177 
178  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
179  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
180  return TRUE;
181 
182  }//end processInLineBackend()
183 
184 
196  {
197  // section to create a new link
198  $o->openSection(translate('create_linking'));
199 
200  // add the asset finder
201  $o->openField(translate('new_link_parent'));
202  asset_finder($prefix.'_major_assetid');
203  $o->closeField();
204 
205  // type of the link
206  $o->openField(translate('new_link_type'));
207  $editable_link_types = Array(
208  SQ_LINK_TYPE_1 => link_type_name(SQ_LINK_TYPE_1),
209  SQ_LINK_TYPE_2 => link_type_name(SQ_LINK_TYPE_2),
210  SQ_LINK_NOTICE => link_type_name(SQ_LINK_NOTICE),
211  );
212  combo_box($prefix.'_link_type', $editable_link_types, FALSE, SQ_LINK_TYPE_1);
213  $o->closeField();
214 
215  // value of the link
216  $o->openField(translate('use_link_value'));
217  text_box($prefix.'_link_value', '', 20, 20);
218  $o->closeField();
219 
220  // if the minor asset is a shadow asset we will not allow the backend user to create a new link in the future
221  $minor_asset = $asset->getAssetInLink('minor');
222 
223  // check if the minor is a shadow asset or not
224  $id_parts = explode(':', $minor_asset->id);
225 
226  // if it is a shadow asset the user cannot create a new link at a future time using the cron manager
227  if (isset($id_parts[1])) {
228  $o->openField(translate('at'));
229  $o->note(translate('cron_manager_future_not_available_create'));
230  $this->_paintWhenBox($asset, $o, $prefix, '', 'disabled');
231  $o->closeField();
232  } else {
233  // if the minor asset is not a shadow asset
234  // allow the user to set the create link as a future link
235  $o->openField(translate('at'));
236  $o->note(translate('cron_manager_create_now_or_later'));
237  $this->_paintWhenBox($asset, $o, $prefix);
238  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
239  $o->note(translate('cron_manager_next_run', $cron_mgr->readableRefreshTime(), $GLOBALS['SQ_SYSTEM']->datetime($cron_mgr->timeOfNextRun())));
240  $o->closeField();
241  }
242 
243 
244  $o->closeSection();
245 
246  }//end paintInLineBackendCreate()
247 
248 
260  {
261  // if the major_assetid_create is not empty we will link this asset to the major asset
262  if ($major_id_array = array_get_index($_POST, $prefix.'_major_assetid', FALSE)) {
263 
264  $major_id = $major_id_array['assetid'];
265 
266  // if the major is empty throw an error
267  if (empty($major_id)) return;
268 
269  // get the link type
270  $link_type = array_get_index($_POST, $prefix.'_link_type', SQ_LINK_TYPE_1);
271 
272  // get the link value
273  $link_value = array_get_index($_POST, $prefix.'_link_value', '');
274 
275  // get the future time
276  $future_time = Array();
277  $future_time['day'] = array_get_index($_REQUEST, $prefix.'_when_day', '');
278  $future_time['month'] = array_get_index($_REQUEST, $prefix.'_when_month', '');
279  $future_time['year'] = array_get_index($_REQUEST, $prefix.'_when_year', '');
280  $future_time['duration'] = array_get_index($_REQUEST, $prefix.'_when_duration', '');
281  $future_time['period'] = array_get_index($_REQUEST, $prefix.'_when_period', '');
282 
283  // if the future_time[XX] is empty it means that the field were disabled or we are getting a wrong future time
284  $link_now = ($future_time['day'] == '') || ($future_time['month'] == '') || ($future_time['year'] == '') || ($future_time['duration'] == '') |
285  (($future_time['day'] == '--') && ($future_time['month'] == '--') && ($future_time['year'] == '--') && ($future_time['duration'] == '-'));
286 
287  // if no time is set of if the future time is incorrect
288  // we do the linking now
289  if ($link_now) {
290  // we create the link immediately
291  $new_parent = $GLOBALS['SQ_SYSTEM']->am->getAsset($major_id);
292  $value = $link_value;
293  $dependant = FALSE;
294  $exclusive = FALSE;
295  $sort_order = '';
296  $am = $GLOBALS['SQ_SYSTEM']->am;
297 
298  $minor_asset = $asset->getAssetInLink('minor');
299 
300  // this array contains the information of the minor asset id
301  // old parent id and link id and link type wanted type
302  $_assets = Array();
303 
304  // this array will be passed to the hipo job
305  // the $_assets array will be part of this array
306  $hipo_var = Array();
307 
308  // check if the minor is a shadow asset or not
309  $id_parts = explode(':', $minor_asset->id);
310 
311  // if it is a shadow asset get the bridge to get linkid, parents, etc.
312  if (isset($id_parts[1])) {
313  $bridge = $am->getAsset($id_parts[0]);
314  $_links = $bridge->getLinks($minor_asset->id, SQ_SC_LINK_BACKEND_NAV, '', TRUE, 'minor');
315 
316  // of there is no link do nothing
317  if (count($_links) <= 0) return FALSE;
318 
319  // build the $_assets for an shadow asset
320  // The link id is composed with the bridge id and the shadowasset id
321  // parentid contains the old parent id
322  $_assets[$minor_asset->id] = Array(
323  'linkid' => $bridge->id.':'.$minor_asset->id,
324  'link_type' => $link_type,
325  'parentid' => $_links[0]['majorid'],
326  );
327 
328  } else {
329  // the minor is a normal asset so ask the asset manager to get linkid, parents, etc.
330  $_links = $am->getLink($minor_asset->id, NULL, '', TRUE, NULL, 'minor');
331 
332  // parentid contains the old parent id
333  $_assets[$minor_asset->id] = Array(
334  'linkid' => $_links['linkid'],
335  'link_type' => $link_type,
336  'parentid' => $_links['majorid'],
337  );
338  }
339 
340  // check if we can create a link under the new parent
341  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($minor_asset->id);
342  $value = $link_value;
343  $dependant = FALSE;
344  $exclusive = FALSE;
345  $sort_order = 1;
346  $asset->prepareLink($new_parent, 'minor', $link_type, $value, $sort_order, $dependant, $exclusive);
347  $new_parent->prepareLink($asset, 'major', $link_type, $value, $sort_order, $dependant, $exclusive);
348  $can_create = $new_parent->canCreateLink($asset, $link_type, $exclusive);
349 
350  // if we cannot create the link do nothing
351  if ($can_create !== TRUE) {
352  trigger_error($can_create);
353  return FALSE;
354  }
355 
356  $hipo_var = Array(
357  'link_action' => 'create',
358  'to_parent_assetid' => $major_id,
359  'to_parent_pos' => 1,
360  'assets' => $_assets,
361  'link_value' => $value,
362  );
363  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
364  $hh->queueHipo('hipo_job_create_links', $hipo_var);
365 
366  // check for any HIPOs that have been queued
367  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
368 
369  $url = $hh->runQueuedJobs();
370 
371  if (!empty($url)) $o->setRedirect($url);
372 
373  return FALSE;
374  } else {
375  // if a time is set so we need to add a job in the cron manager
376  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
377  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
378  $major = $GLOBALS['SQ_SYSTEM']->am->getAsset($major_id);
379 
380  if (is_null($major)) {
381  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
382  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
383  return FALSE;
384  }
385  $link_info = $asset->attr('link_info');
386  $link_info['link_type'] = $link_type;
387  $link_info['value'] = $link_value;
388  if (!$asset->setAttrValue('link_info', $link_info) || !$asset->setAssetInLink($major, 'major')) {
389  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
390  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
391  return FALSE;
392  }
393 
394  if (!$asset->saveAttributes() || !$asset->setAttrValue('when', $this->_processWhenBox($asset, $o, $prefix))) {
395  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
396  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
397  return FALSE;
398  }
399 
400  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
401  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
402  return TRUE;
403  }
404 
405  }//end if
406 
407  }//end processInLineBackendCreate()
408 
409 
421  {
422  $major = $asset->getAssetInLink('major');
423  $minor = $asset->getAssetInLink('minor');
424 
425  $o->openSection(translate('delete_link'));
426 
427  $o->openField(translate('delete_link'));
428  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($minor->id, SQ_SC_LINK_SIGNIFICANT|SQ_LINK_NOTICE, '', TRUE, 'minor');
429  $link_options = Array(0 => '', 'SQ_CRON_JOB_FUTURE_LINEAGE_DELETE_ALL_LINKS' => translate('cron_fl_delete_all_existing_links'));
430  foreach ($links as $link) {
431  $print_link_type = $this->_printLinkType($link);
432  $parent = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['majorid'], $link['major_type_code']);
433  if (is_null($parent)) continue;
434 
435  // if the link id is null we got to continue
436  // this line has been added to fix a problem when viewing the linking screen of
437  // a LDAP backend User/Shadow assets
438  if (empty($link['linkid'])) continue;
439  if (empty($link['linkid'])) {
440 
441  // if the link id is equal to zero and the minor id exists it is more likely to be a shadow asset
442  if (!empty($minor->id)) {
443  $id_parts = explode(':', $minor->id);
444  if (isset($id_parts[1])) {
445  // it is a shadow asset the link id is the bridge id following with the minor id
446  $link_options[$id_parts[0].':'.$minor->id] = translate('link').' #'.$id_parts[0].':'.$minor->id.' ('.$print_link_type.') - '.translate('parent').' : '.$parent->name.' (#'.$id_parts[0].')';
447  continue;
448  } else {
449  // the minor asset is not a shadow asset but the link is equal to zero
450  // we just ignore that asset
451  continue;
452  }
453  } else {
454  // the minor id is empty, ignore this asset
455  continue;
456  }
457  }
458 
459 
460  $link_options[$link['linkid']] = translate('link').' #'.$link['linkid'].' ('.$print_link_type.') - '.translate('parent').' : '.$parent->name.' (#'.$parent->id.')';
461  }//end foreach
462 
463  $default_delete_linkid = $asset->attr('delete_linkid');
464  if ($asset->attr('delete_link_all')) {
465  $default_delete_linkid = 'SQ_CRON_JOB_FUTURE_LINEAGE_DELETE_ALL_LINKS';
466  }
467  combo_box($prefix.'_delete_linkid', $link_options, FALSE, $default_delete_linkid);
468  $o->closeField();
469 
470  // if the minor asset is a shadow asset we will not allow the backend user to create a new link in the future
471  $minor_asset = $asset->getAssetInLink('minor');
472 
473  // check if the minor is a shadow asset or not
474  $id_parts = explode(':', $minor_asset->id);
475 
476  // if the minor asset is a shadow asset the user cannot create a new link at a future time using the cron manager
477  if (isset($id_parts[1])) {
478  $o->openField(translate('at'));
479  $o->note(translate('cron_manager_future_not_available_delete'));
480  $this->_paintWhenBox($asset, $o, $prefix, '', 'disabled');
481  $o->closeField();
482  } else {
483  // if the minor asset is not a shadow asset
484  // allow the user to set the delete link at a future time
485  $o->openField(translate('at'));
486  $o->note(translate('cron_manager_delete_now_or_later'));
487  $this->_paintWhenBox($asset, $o, $prefix);
488  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
489  $o->note(translate('cron_manager_next_run', $cron_mgr->readableRefreshTime(), $GLOBALS['SQ_SYSTEM']->datetime($cron_mgr->timeOfNextRun())));
490  $o->closeField();
491  }
492  $o->closeSection();
493 
494  return TRUE;
495 
496  }//end paintInLineBackendDelete()
497 
498 
511  {
512  if (empty($_POST[$prefix.'_delete_linkid'])) {
513  return FALSE;
514  }
515 
516  $minor_asset = $asset->getAssetInLink('minor');
517  // link to delete
518  $delete_linkid = $_POST[$prefix.'_delete_linkid'];
519 
520 
521  // get the future time
522  $future_time[] = Array();
523  $future_time['day'] = array_get_index($_REQUEST, $prefix.'_when_day', '');
524  $future_time['month'] = array_get_index($_REQUEST, $prefix.'_when_month', '');
525  $future_time['year'] = array_get_index($_REQUEST, $prefix.'_when_year', '');
526  $future_time['duration'] = array_get_index($_REQUEST, $prefix.'_when_duration', '');
527  $future_time['period'] = array_get_index($_REQUEST, $prefix.'_when_period', '');
528 
529  // if the future_time[XX] is empty it means that the field were disabled or we are getting a wrong future time
530  $link_now = ($future_time['day'] == '') || ($future_time['month'] == '') || ($future_time['year'] == '') || ($future_time['duration'] == '') |
531  (($future_time['day'] == '--') && ($future_time['month'] == '--') && ($future_time['year'] == '--') && ($future_time['duration'] == '-'));
532 
533  // if no time is set of if the future time is incorrect
534  // we delete the link now
535  if ($link_now) {
536  // we delete the link immediately
537 
538  // trash_folder
539  $trash_folder = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('trash_folder');
540 
541  // get the asset manager
542  $am = $GLOBALS['SQ_SYSTEM']->am;
543 
544  // get the hipo herder
545  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
546 
547  // we delete all the links to the minor asset
548  if ($delete_linkid == 'SQ_CRON_JOB_FUTURE_LINEAGE_DELETE_ALL_LINKS') {
549  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($minor_asset->id, SQ_SC_LINK_SIGNIFICANT, '', TRUE, 'minor');
550  $link_options = Array();
551 
552  $_assets[$minor_asset->id] = Array();
553 
554  // loop through all the links and add them to the hipo herder
555  foreach ($links as $link) {
556  // if the linkid is empty it means that it is a wrong link or a link to and bridge and shadow asset
557  if (empty($link['linkid'])) continue;
558 
559  // get the information about the link we want to move to the trash
560  $link_info = $am->getLinkById($link['linkid'], $minor_asset->id, 'minor');
561 
562  // if the link is already in the trash then skip it
563  if ($link_info['majorid'] == $trash_folder->id) {
564  continue;
565  }
566 
567  // this array contains the link information to move to the trash
568  // parentid contains the old parent id
569  $_assets[$minor_asset->id][] = Array(
570  'linkid' => $link_info['linkid'],
571  'link_type' => $link_info['link_type'],
572  'parentid' => $link_info['majorid'],
573  );
574 
575  }//end foreach
576 
577  if (!empty($_assets[$minor_asset->id])) {
578  // gathered links, now give them to the HIPO
579  $hipo_vars = Array(
580  'link_action' => 'move',
581  'to_parent_assetid' => $trash_folder->id,
582  'to_parent_pos' => 1,
583  'assets' => $_assets,
584  );
585 
586 
587  // add the job to the hipo queue
588  $hh->queueHipo('hipo_job_create_links', $hipo_vars);
589 
590  // check for any HIPOs that have been queued
591  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
592 
593  $url = $hh->runQueuedJobs();
594 
595  // if the hipo URL is not empty redirect to the hipo
596  if (!empty($url)) $o->setRedirect($url);
597  }
598 
599  // once all the jobs have been added to the queue
600 
601 
602 
603 
604  } else {
605  // we delete a specific link id
606 
607  // get the information about the link we want to move to the trash
608  $link_info = $am->getLinkById($delete_linkid, $minor_asset->id, 'minor');
609 
610  // Delete notice links first
611  if ($link_info['link_type'] == SQ_LINK_NOTICE) {
612  // if type code is not there, assume we are a shadow link
613  if (is_null($link_info['major_type_code'])) {
614  $GLOBALS['SQ_SYSTEM']->am->deleteShadowAssetLink($link_info['linkid']);
615  } else {
616  $GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($link_info['linkid']);
617  }
618 
619  // I'm OK! FALSE is needed not to throw an error
620  return FALSE;
621  }
622 
623  // if the link is already in the trash then skip it
624  if ($link_info['majorid'] == $trash_folder->id) {
625  return FALSE;
626  }
627 
628  // this array contains the link information to move to the trash
629  // parentid contains the old parent id
630  $_assets[$minor_asset->id] = Array(
631  'linkid' => $link_info['linkid'],
632  'link_type' => $link_info['link_type'],
633  'parentid' => $link_info['majorid'],
634  );
635 
636  // this array will be given to the hipo
637  $hipo_var = Array(
638  'link_action' => 'move',
639  'to_parent_assetid' => $trash_folder->id,
640  'to_parent_pos' => 1,
641  'assets' => $_assets,
642  );
643 
644  $hh->queueHipo('hipo_job_create_links', $hipo_var);
645 
646  // check for any HIPOs that have been queued
647  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
648 
649  $url = $hh->runQueuedJobs();
650 
651  // if the hipo URL is not empty redirect to the hipo
652  if (!empty($url)) $o->setRedirect($url);
653 
654  }//end else
655 
656  return FALSE;
657  } else {
658 
659  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
660  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
661 
662  // Are we going to delete all existing links...? Or maybe just the one that
663  // the user has specified.
664  if ($delete_linkid == 'SQ_CRON_JOB_FUTURE_LINEAGE_DELETE_ALL_LINKS') {
665  $attribute = Array('name' => 'delete_link_all', 'value' => TRUE);
666  } else {
667  $attribute = Array('name' => 'delete_linkid', 'value' => $delete_linkid);
668  }
669  if (!$asset->setAttrValue($attribute['name'], $attribute['value'])) {
670  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
671  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
672  return FALSE;
673  }
674 
675  if (!$asset->saveAttributes() || !$asset->setAttrValue('when', $this->_processWhenBox($asset, $o, $prefix))) {
676  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
677  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
678  return FALSE;
679  }
680 
681  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
682  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
683  return TRUE;
684  }
685 
686  }//end processInLineBackendDelete()
687 
688 
700  {
701  // section to create a new link
702  $o->openSection(translate('create_linking'));
703 
704  // add the asset finder
705  $o->openField(translate('new_link_child'));
706  asset_finder($prefix.'_minor_assetid');
707  $o->closeField();
708 
709  // type of the link
710  $o->openField(translate('new_link_type'));
711  $editable_link_types = Array(
712  SQ_LINK_TYPE_1 => link_type_name(SQ_LINK_TYPE_1),
713  SQ_LINK_TYPE_2 => link_type_name(SQ_LINK_TYPE_2),
714  SQ_LINK_NOTICE => link_type_name(SQ_LINK_NOTICE),
715  );
716  combo_box($prefix.'_link_type', $editable_link_types, FALSE, SQ_LINK_TYPE_1);
717  $o->closeField();
718 
719  // value of the link
720  $o->openField(translate('use_link_value'));
721  text_box($prefix.'_link_value', '', 20, 20);
722  $o->closeField();
723 
724  // if the major asset is a shadow asset we will not allow the backend user to create a new link in the future
725  $major_asset = $asset->getAssetInLink('major');
726 
727  // check if the minor is a shadow asset or not
728  $id_parts = explode(':', $major_asset->id);
729 
730  // if it is a shadow asset the user cannot create a new link at a future time using the cron manager
731  if (isset($id_parts[1])) {
732  $o->openField(translate('at'));
733  $o->note(translate('cron_manager_future_not_available_create'));
734  $this->_paintWhenBox($asset, $o, $prefix, '', 'disabled');
735  $o->closeField();
736  } else {
737  // if the minor asset is not a shadow asset
738  // allow the user to set the create link as a future link
739  $o->openField(translate('at'));
740  $o->note(translate('cron_manager_create_now_or_later'));
741  $this->_paintWhenBox($asset, $o, $prefix);
742  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
743  $o->note(translate('cron_manager_next_run', $cron_mgr->readableRefreshTime(), $GLOBALS['SQ_SYSTEM']->datetime($cron_mgr->timeOfNextRun())));
744  $o->closeField();
745  }
746 
747 
748  $o->closeSection();
749 
750  }//end paintInLineBackendCreateReverse()
751 
752 
764  {
765  // if the major_assetid_create is not empty we will link this asset to the major asset
766  if ($minor_id_array = array_get_index($_POST, $prefix.'_minor_assetid', FALSE)) {
767 
768  $minor_id = $minor_id_array['assetid'];
769 
770  // if the major is empty throw an error
771  if (empty($minor_id)) return;
772 
773  // get the link type
774  $link_type = array_get_index($_POST, $prefix.'_link_type', SQ_LINK_TYPE_1);
775 
776  // get the link value
777  $link_value = array_get_index($_POST, $prefix.'_link_value', '');
778 
779  // get the future time
780  $future_time = Array();
781  $future_time['day'] = array_get_index($_REQUEST, $prefix.'_when_day', '');
782  $future_time['month'] = array_get_index($_REQUEST, $prefix.'_when_month', '');
783  $future_time['year'] = array_get_index($_REQUEST, $prefix.'_when_year', '');
784  $future_time['duration'] = array_get_index($_REQUEST, $prefix.'_when_duration', '');
785  $future_time['period'] = array_get_index($_REQUEST, $prefix.'_when_period', '');
786 
787  // if the future_time[XX] is empty it means that the field were disabled or we are getting a wrong future time
788  $link_now = ($future_time['day'] == '') || ($future_time['month'] == '') || ($future_time['year'] == '') || ($future_time['duration'] == '') |
789  (($future_time['day'] == '--') && ($future_time['month'] == '--') && ($future_time['year'] == '--') && ($future_time['duration'] == '-'));
790 
791  // if no time is set of if the future time is incorrect
792  // we do the linking now
793  if ($link_now) {
794  // we create the link immediately
795  $value = $link_value;
796  $dependant = FALSE;
797  $exclusive = FALSE;
798  $sort_order = '';
799  $am = $GLOBALS['SQ_SYSTEM']->am;
800 
801  $major_asset = $asset->getAssetInLink('major');
802 
803  // this array contains the information of the minor asset id
804  // old parent id and link id and link type wanted type
805  $_assets = Array();
806 
807  // this array will be passed to the hipo job
808  // the $_assets array will be part of this array
809  $hipo_var = Array();
810 
811  // check if the minor is a shadow asset or not
812  $id_parts = explode(':', $major_asset->id);
813 
814  // if it is a shadow asset get the bridge to get linkid, parents, etc.
815  if (isset($id_parts[1])) {
816  $bridge = $am->getAsset($id_parts[0]);
817  $_links = $bridge->getLinks($major_asset->id, SQ_SC_LINK_BACKEND_NAV, '', TRUE, 'major');
818 
819  // of there is no link do nothing
820  if (count($_links) <= 0) return FALSE;
821 
822  // build the $_assets for an shadow asset
823  // The link id is composed with the bridge id and the shadowasset id
824  // parentid contains the old parent id
825  $_assets[$minor_asset->id] = Array(
826  'linkid' => $bridge->id.':'.$minor_asset->id,
827  'link_type' => $link_type,
828  'parentid' => $_links[0]['majorid'],
829  );
830 
831  } else {
832  // the minor is a normal asset so ask the asset manager to get linkid, parents, etc.
833  $_links = $am->getLink($major_asset->id, NULL, '', TRUE, NULL, 'major');
834 
835  // parentid contains the old parent id
836  $_assets[$minor_id] = Array(
837  'linkid' => isset($_links['linkid']) ? $_links['linkid'] : '',
838  'link_type' => $link_type,
839  'parentid' => $major_asset->id,
840  );
841  }
842 
843  // check if we can create a link under the new parent
844  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($minor_id);
845  $value = $link_value;
846  $dependant = FALSE;
847  $exclusive = FALSE;
848  $sort_order = NULL;
849  $asset->prepareLink($major_asset, 'minor', $link_type, $value, $sort_order, $dependant, $exclusive);
850  $major_asset->prepareLink($asset, 'major', $link_type, $value, $sort_order, $dependant, $exclusive);
851  $can_create = $major_asset->canCreateLink($asset, $link_type, $exclusive);
852 
853  // if we cannot create the link do nothing
854  if ($can_create !== TRUE) return FALSE;
855 
856  $hipo_var = Array(
857  'link_action' => 'create',
858  'to_parent_assetid' => $major_asset->id,
859  'to_parent_pos' => 1,
860  'assets' => $_assets,
861  'link_value' => $value,
862  );
863  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
864  $hh->queueHipo('hipo_job_create_links', $hipo_var);
865 
866  // check for any HIPOs that have been queued
867  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
868 
869  $url = $hh->runQueuedJobs();
870 
871  if (!empty($url)) $o->setRedirect($url);
872 
873  return FALSE;
874  } else {
875  // if a time is set so we need to add a job in the cron manager
876  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
877  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
878  $minor_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($minor_id);
879 
880  if (is_null($minor_asset)) {
881  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
882  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
883  return FALSE;
884  }
885  $link_info = $asset->attr('link_info');
886  $link_info['link_type'] = $link_type;
887  $link_info['value'] = $link_value;
888  if (!$asset->setAttrValue('link_info', $link_info) || !$asset->setAssetInLink($minor_asset, 'minor')) {
889  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
890  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
891  return FALSE;
892  }
893 
894  if (!$asset->setAttrValue('reverse_mode', 1)) {
895  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
896  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
897  return FALSE;
898  }
899 
900  if (!$asset->saveAttributes() || !$asset->setAttrValue('when', $this->_processWhenBox($asset, $o, $prefix))) {
901  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
902  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
903  return FALSE;
904  }
905 
906  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
907  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
908  return TRUE;
909  }
910 
911  }//end if
912 
913  }//end processInLineBackendCreateReverse()
914 
915 
927  {
928  $major = $asset->getAssetInLink('major');
929 
930  $o->openSection(translate('delete_link'));
931 
932  $o->openField(translate('delete_link'));
933  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($major->id, SQ_SC_LINK_SIGNIFICANT|SQ_LINK_NOTICE, '', TRUE, 'major');
934  $link_options = Array(0 => '', 'SQ_CRON_JOB_FUTURE_LINEAGE_DELETE_ALL_LINKS' => translate('cron_fl_delete_all_existing_links'));
935  foreach ($links as $link) {
936  $print_link_type = $this->_printLinkType($link);
937  $child = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
938  if (is_null($child)) continue;
939 
940  // if the link id is null we got to continue
941  // this line has been added to fix a problem when viewing the linking screen of
942  // a LDAP backend User/Shadow assets
943  if (empty($link['linkid'])) continue;
944 
945 
946  $link_options[$link['linkid']] = translate('link').' #'.$link['linkid'].' ('.$print_link_type.') - '.translate('child').' : '.$child->name.' (#'.$child->id.')';
947  }//end foreach
948 
949  $default_delete_linkid = $asset->attr('delete_linkid');
950  if ($asset->attr('delete_link_all')) {
951  $default_delete_linkid = 'SQ_CRON_JOB_FUTURE_LINEAGE_DELETE_ALL_LINKS';
952  }
953  combo_box($prefix.'_delete_linkid', $link_options, FALSE, $default_delete_linkid);
954  $o->closeField();
955 
956  // if the minor asset is a shadow asset we will not allow the backend user to create a new link in the future
957  $major_asset = $asset->getAssetInLink('major');
958 
959  // check if the minor is a shadow asset or not
960  $id_parts = explode(':', $major_asset->id);
961 
962  // if the minor asset is a shadow asset the user cannot create a new link at a future time using the cron manager
963  if (isset($id_parts[1])) {
964  $o->openField(translate('at'));
965  $o->note(translate('cron_manager_future_not_available_delete'));
966  $this->_paintWhenBox($asset, $o, $prefix, '', 'disabled');
967  $o->closeField();
968  } else {
969  // if the minor asset is not a shadow asset
970  // allow the user to set the delete link at a future time
971  $o->openField(translate('at'));
972  $o->note(translate('cron_manager_delete_now_or_later'));
973  $this->_paintWhenBox($asset, $o, $prefix);
974  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
975  $o->note(translate('cron_manager_next_run', $cron_mgr->readableRefreshTime(), $GLOBALS['SQ_SYSTEM']->datetime($cron_mgr->timeOfNextRun())));
976  $o->closeField();
977  }
978  $o->closeSection();
979 
980  return TRUE;
981 
982  }//end paintInLineBackendDeleteReverse()
983 
984 
997  {
998  if (empty($_POST[$prefix.'_delete_linkid'])) {
999  return FALSE;
1000  }
1001 
1002  $major_asset = $asset->getAssetInLink('major');
1003  // link to delete
1004  $delete_linkid = $_POST[$prefix.'_delete_linkid'];
1005 
1006 
1007  // get the future time
1008  $future_time[] = Array();
1009  $future_time['day'] = array_get_index($_REQUEST, $prefix.'_when_day', '');
1010  $future_time['month'] = array_get_index($_REQUEST, $prefix.'_when_month', '');
1011  $future_time['year'] = array_get_index($_REQUEST, $prefix.'_when_year', '');
1012  $future_time['duration'] = array_get_index($_REQUEST, $prefix.'_when_duration', '');
1013  $future_time['period'] = array_get_index($_REQUEST, $prefix.'_when_period', '');
1014 
1015  // if the future_time[XX] is empty it means that the field were disabled or we are getting a wrong future time
1016  $link_now = ($future_time['day'] == '') || ($future_time['month'] == '') || ($future_time['year'] == '') || ($future_time['duration'] == '') |
1017  (($future_time['day'] == '--') && ($future_time['month'] == '--') && ($future_time['year'] == '--') && ($future_time['duration'] == '-'));
1018 
1019  // if no time is set of if the future time is incorrect
1020  // we delete the link now
1021  if ($link_now) {
1022  // we delete the link immediately
1023 
1024  // trash_folder
1025  $trash_folder = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('trash_folder');
1026 
1027  // get the asset manager
1028  $am = $GLOBALS['SQ_SYSTEM']->am;
1029 
1030  // get the hipo herder
1031  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
1032 
1033  // we delete all the links to the minor asset
1034  if ($delete_linkid == 'SQ_CRON_JOB_FUTURE_LINEAGE_DELETE_ALL_LINKS') {
1035  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($major_asset->id, SQ_SC_LINK_SIGNIFICANT, '', TRUE, 'major');
1036  $link_options = Array();
1037 
1038 
1039 
1040  // loop through all the links and add them to the hipo herder
1041  foreach ($links as $link) {
1042 
1043  // if the linkid is empty it means that it is a wrong link or a link to and bridge and shadow asset
1044  if (empty($link['linkid'])) continue;
1045 
1046  // get the information about the link we want to move to the trash
1047  $link_info = $am->getLinkById($link['linkid'], $major_asset->id, 'major');
1048 
1049  $_assets[$link_info['minorid']] = Array();
1050  // this array contains the link information to move to the trash
1051  // parentid contains the old parent id
1052  $_assets[$link_info['minorid']][] = Array(
1053  'linkid' => $link_info['linkid'],
1054  'link_type' => $link_info['link_type'],
1055  'parentid' => $major_asset->id,
1056  );
1057 
1058  }//end foreach
1059 
1060  if (!empty($_assets)) {
1061  // gathered links, now give them to the HIPO
1062  $hipo_vars = Array(
1063  'link_action' => 'move',
1064  'to_parent_assetid' => $trash_folder->id,
1065  'to_parent_pos' => 1,
1066  'assets' => $_assets,
1067  );
1068 
1069  // add the job to the hipo queue
1070  $hh->queueHipo('hipo_job_create_links', $hipo_vars);
1071 
1072  // check for any HIPOs that have been queued
1073  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
1074 
1075  $url = $hh->runQueuedJobs();
1076 
1077  // if the hipo URL is not empty redirect to the hipo
1078  if (!empty($url)) $o->setRedirect($url);
1079  }
1080 
1081  // once all the jobs have been added to the queue
1082 
1083 
1084 
1085 
1086  } else {
1087  // we delete a specific link id
1088 
1089  // get the information about the link we want to move to the trash
1090  $link_info = $am->getLinkById($delete_linkid, $major_asset->id, 'major');
1091 
1092  // Delete notice links first
1093  if ($link_info['link_type'] == SQ_LINK_NOTICE) {
1094  // if type code is not there, assume we are a shadow link
1095  if (is_null($link_info['minor_type_code'])) {
1096  $GLOBALS['SQ_SYSTEM']->am->deleteShadowAssetLink($link_info['linkid']);
1097  } else {
1098  $GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($link_info['linkid']);
1099  }
1100 
1101  // I'm OK! FALSE is needed not to throw an error
1102  return FALSE;
1103  }
1104 
1105 
1106  // this array contains the link information to move to the trash
1107  // parentid contains the old parent id
1108  $_assets[$link_info['minorid']] = Array(
1109  'linkid' => $link_info['linkid'],
1110  'link_type' => $link_info['link_type'],
1111  'parentid' => $major_asset->id,
1112  );
1113 
1114  // this array will be given to the hipo
1115  $hipo_var = Array(
1116  'link_action' => 'move',
1117  'to_parent_assetid' => $trash_folder->id,
1118  'to_parent_pos' => 1,
1119  'assets' => $_assets,
1120  );
1121 
1122  $hh->queueHipo('hipo_job_create_links', $hipo_var);
1123 
1124  // check for any HIPOs that have been queued
1125  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
1126 
1127  $url = $hh->runQueuedJobs();
1128 
1129  // if the hipo URL is not empty redirect to the hipo
1130  if (!empty($url)) $o->setRedirect($url);
1131 
1132  }//end else
1133 
1134  return FALSE;
1135  } else {
1136 
1137  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
1138  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
1139 
1140  // Are we going to delete all existing links...? Or maybe just the one that
1141  // the user has specified.
1142  if ($delete_linkid == 'SQ_CRON_JOB_FUTURE_LINEAGE_DELETE_ALL_LINKS') {
1143  $attribute = Array('name' => 'delete_link_all', 'value' => TRUE);
1144  } else {
1145  $attribute = Array('name' => 'delete_linkid', 'value' => $delete_linkid);
1146  }
1147  if (!$asset->setAttrValue($attribute['name'], $attribute['value'])) {
1148  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
1149  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
1150  return FALSE;
1151  }
1152 
1153  if (!$asset->setAttrValue('reverse_mode', 1)) {
1154  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
1155  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
1156  return FALSE;
1157  }
1158 
1159  if (!$asset->saveAttributes() || !$asset->setAttrValue('when', $this->_processWhenBox($asset, $o, $prefix))) {
1160  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
1161  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
1162  return FALSE;
1163  }
1164 
1165  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
1166  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
1167  return TRUE;
1168  }
1169  }//end processInLineBackendDeleteReverse()
1170 
1171 
1180  private function _printLinkType($link_info)
1181  {
1182  $type_return = '';
1183  if (!isset($link_info['link_type'])) {
1184  return $type_return;
1185  }
1186 
1187  switch ($link_info['link_type']) {
1188  case SQ_LINK_TYPE_1:
1189  $type_return = 'TYPE 1';
1190  break;
1191  case SQ_LINK_TYPE_2:
1192  $type_return = 'TYPE 2';
1193  break;
1194  case SQ_LINK_NOTICE:
1195  $type_return = 'NOTICE';
1196  break;
1197  default:
1198  $type_return = '';
1199  }//end switch
1200 
1201  return $type_return;
1202 
1203  }//end _printLinkType()
1204 
1205 
1206 }//end class
1207 ?>