Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
workflow_bundle.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset.inc';
18 require_once SQ_CORE_PACKAGE_PATH.'/interfaces/bridge/bridge.inc';
19 
35 class Workflow_Bundle extends Asset implements Bridge
36 {
37 
38 
45  function __construct($assetid=0)
46  {
47  parent::__construct($assetid);
48 
49  }//end constructor
50 
51 
60  function _getName($short_name=FALSE)
61  {
62  return $this->attr('name');
63 
64  }//end _getName()
65 
66 
74  public function _getAllowedLinks()
75  {
76  return Array(
77  SQ_LINK_NOTICE => Array(
78  'asset' => Array(
79  'card' => 'M',
80  'exclusive' => FALSE,
81  ),
82  ),
83  );
84 
85  }//end _getAllowedLinks()
86 
87 
104  public function getAsset($shadowid, $type_code='', $mute_errors=FALSE)
105  {
106  return NULL;
107 
108  }//end getAsset()
109 
110 
132  public function getLinks($assetid, $link_types, $type_code='', $strict_type_code=TRUE, $side_of_link='major', $sort_by=NULL)
133  {
134  return Array();
135 
136  }//end getLinks()
137 
138 
152  public function getParents($assetid, $type_code='', $strict_type_code=TRUE)
153  {
154  return Array();
155 
156  }//end getParents()
157 
158 
185  public function getChildren($assetid, $type_code='', $strict_type_code=TRUE, $dependant=NULL, $sort_by=NULL)
186  {
187  return Array();
188 
189  }//end getChildren()
190 
191 
205  public function getLineageFromURL($assetid, $protocol, $url)
206  {
207  return Array();
208 
209  }//end getLineageFromURL()
210 
211 
232  public function countLinks($assetid, $side_of_link='major', $link_types=0, $type_code='', $strict_type_code=TRUE, $ignore_linkid=0)
233  {
234  return 0;
235 
236  }//end countLinks()
237 
238 
248  public function deleteAssetLink($linkid, $moving=FALSE)
249  {
250  return FALSE;
251 
252  }//end deleteAssetLink()
253 
254 
264  function getAssetMapLinks()
265  {
266  $links_query = $GLOBALS['SQ_SYSTEM']->am->generateGetLinksQuery($this->id, SQ_LINK_NOTICE, 'asset', FALSE);
267  if (empty($links_query)) return Array();
268 
269  $db_links = Array();
270 
271  try {
272  $query = MatrixDAL::preparePdoQuery(implode(' ', $links_query['sql_array']));
273  foreach ($links_query['bind_vars'] as $bind_var => $bind_value) {
274  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
275  }
276  $db_links = MatrixDAL::executePdoAssoc($query);
277  } catch (Exception $e) {
278  throw new Exception('Unable to get links for asset: '.$assetid.' due to database error: '.$e->getMessage());
279  }
280 
281  // Grab the assets from the links query so we can get their info all at once
282  $bundled_assets = Array();
283  $linkids = Array();
284  foreach ($db_links as $db_link) {
285  $bundled_assets[] = $db_link['minorid'];
286  $linkids[$db_link['minorid']] = $db_link['linkid'];
287  }
288 
289  $asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($bundled_assets);
290  $sort_order = 1;
291 
292  $links = Array();
293  foreach ($asset_info as $bundled_assetid => $info) {
294  $links[] = Array(
295  'majorid' => $this->id,
296  'minorid' => $bundled_assetid,
297  'assetid' => $bundled_assetid,
298  'type_code' => $info['type_code'],
299  'linkid' => $linkids[$bundled_assetid],
300 
301  'name' => $info['name'],
302  'short_name' => $info['short_name'],
303  'status' => $info['status'],
304  'link_type' => SQ_LINK_TYPE_2,
305  'sort_order' => $sort_order,
306 
307  'url' => '',
308  'path' => '',
309  'num_kids' => -1,
310  'accessible' => 1,
311  'is_dependant' => 0,
312  );
313 
314  $sort_order++;
315  }
316 
317  return $links;
318 
319  }//end getAssetMapLinks()
320 
321 
342  public function getAssetMapAssetInfo($assetid)
343  {
344  $map_info = Array();
345  if ((string)$assetid === (string)$this->id) {
346  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_NOTICE);
347  foreach ($links as $link) {
348  $linked_assetid = $links['minorid'];
349  $asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($linked_assetid);
350  $map_info[] = Array(
351  'assetid' => $linked_assetid,
352  'name' => $asset_info[$linked_assetid]['name'],
353  'short_name' => $asset_info[$linked_assetid]['short_name'],
354  'version' => $asset_info[$linked_assetid]['version'],
355  'status' => $asset_info[$linked_assetid]['status'],
356  'type_code' => $asset_info[$linked_assetid]['type_code'],
357  'num_kids' => NULL,
358  'accessible' => TRUE,
359  'url' => NULL,
360  'web_path' => NULL,
361  );
362  }
363  }
364 
365  return $map_info;
366 
367  }//end getAssetMapAssetInfo()
368 
369 
390  public function prepareLink(Asset $asset, $side_of_link, &$link_type, &$value, &$sort_order, &$dependant, &$exclusive)
391  {
392  $changed = FALSE;
393 
394  if ($side_of_link === 'major') {
395  $link_type = SQ_LINK_NOTICE;
396  $changed = TRUE;
397  }
398 
399  return $changed;
400 
401  }//end prepareLink()
402 
403 
413  public function getStatus()
414  {
415  if (!isset($this->_tmp['status_object'])) {
416  // work out the name of our status file
417  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
418  $status_code = get_bit_names('SQ_STATUS_', $this->status);
419  $status_code = 'asset_status_bundle_'.strtolower($status_code);
420  require_once dirname(__FILE__).'/asset_status/'.$status_code.'.inc';
421  $this->_tmp['status_object'] = new $status_code($this);
422  }
423  return $this->_tmp['status_object'];
424 
425  }//end getStatus()
426 
427 
444  public function onAssetStatusUpdate(Asset $broadcaster, $event_vars)
445  {
446  $old_status = (int)$event_vars['old_status'];
447  $new_status = (int)$event_vars['new_status'];
448 
449  if ((($old_status & SQ_SC_STATUS_PENDING) !== 0) && (($new_status & SQ_SC_STATUS_ALL_APPROVED) !== 0)) {
450  if ((int)$this->status !== SQ_STATUS_PENDING_APPROVAL) return;
451 
452  $hipo_herder = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
453 
454  if (array_key_exists('completing_workflow', $this->_tmp) === FALSE) {
455  require_once SQ_FUDGE_PATH.'/db_extras/db_extras.inc';
456 
457  $links_query = $GLOBALS['SQ_SYSTEM']->am->generateGetLinksQuery($this->id, SQ_LINK_NOTICE, 'asset', FALSE);
458  if (empty($links_query)) return Array();
459 
460  // Modify the links query so that it only selects bundled assets
461  // in Pending Approval or Safe Edit Pending Approval statuses
462  $links_query['sql_array']['where'] .= ' AND minorid IN (SELECT assetid FROM sq_ast WHERE '.db_extras_bitand(MatrixDAL::getDbType(), 'status', ':all_status').' > 0)';
463  $links_query['bind_vars']['all_status'] = SQ_SC_STATUS_PENDING;
464 
465  try {
466  $query = MatrixDAL::preparePdoQuery(implode(' ', $links_query['sql_array']));
467  foreach ($links_query['bind_vars'] as $bind_var => $bind_value) {
468  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
469  }
470  $db_links = MatrixDAL::executePdoAssoc($query);
471  } catch (Exception $e) {
472  throw new Exception('Unable to get links for asset: '.$assetid.' due to database error: '.$e->getMessage());
473  }
474 
475  // If we have no assets in Pending Approval statuses, there is
476  // nothing left with a workflow. Proceed.
477  if (count($db_links) === 0) {
478  $this->_tmp['completing_workflow'] = 1;
479 
480  $run_level = $GLOBALS['SQ_SYSTEM']->getRunLevel();
481  $GLOBALS['SQ_SYSTEM']->setRunLevel($run_level ^ SQ_SECURITY_STATUS_INTEGRITY);
482  $vars = Array(
483  'assetid' => $this->id,
484  'new_status' => SQ_STATUS_APPROVED,
485  'dependants_only' => TRUE,
486  );
487  $errors = $hipo_herder->freestyleHipo('hipo_job_edit_status', $vars);
488  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
489  }
490 
491  }
492 
493  } else if ((($old_status & (SQ_SC_STATUS_PENDING | SQ_SC_STATUS_ALL_APPROVED)) !== 0) && (($new_status & SQ_SC_STATUS_CAN_APPROVE) !== 0)) {
494 
495  if ((($this->status & (SQ_STATUS_PENDING_APPROVAL | SQ_STATUS_APPROVED)) === 0)) {
496  return;
497  }
498 
499  $hipo_herder = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
500 
501  $run_level = $GLOBALS['SQ_SYSTEM']->getRunLevel();
502  $GLOBALS['SQ_SYSTEM']->setRunLevel($run_level ^ SQ_SECURITY_STATUS_INTEGRITY);
503  $vars = Array(
504  'bundle_assetid' => $this->id,
505  'new_status' => SQ_STATUS_UNDER_CONSTRUCTION,
506  );
507  $hipo_herder->freestyleHipo('hipo_job_edit_bundled_assets_status', $vars);
508  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
509 
510  } else if (((int)$new_status === SQ_STATUS_LIVE) && ((int)$old_status !== SQ_STATUS_EDITING)) {
511  // Transition to Live (but not cancelling a Safe Edit)
512  // Cannot transition from Approving straight to Live...we can go from
513  // Approved, or without workflow we can go from Editing
514  if ((int)$this->status === SQ_STATUS_PENDING_APPROVAL) return;
515 
516  $hipo_herder = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
517 
518  if (array_key_exists('completing_workflow', $this->_tmp) === FALSE) {
519  require_once SQ_FUDGE_PATH.'/db_extras/db_extras.inc';
520 
521  $links_query = $GLOBALS['SQ_SYSTEM']->am->generateGetLinksQuery($this->id, SQ_LINK_NOTICE, 'asset', FALSE);
522  if (empty($links_query)) return Array();
523 
524  // Modify the links query so that it only selects bundled assets
525  // NOT in Live status
526  $links_query['sql_array']['where'] .= ' AND minorid IN (SELECT assetid FROM sq_ast WHERE '.db_extras_bitand(MatrixDAL::getDbType(), 'status', ':all_status').' = 0)';
527  $links_query['bind_vars']['all_status'] = SQ_STATUS_LIVE;
528 
529  try {
530  $query = MatrixDAL::preparePdoQuery(implode(' ', $links_query['sql_array']));
531  foreach ($links_query['bind_vars'] as $bind_var => $bind_value) {
532  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
533  }
534  $db_links = MatrixDAL::executePdoAssoc($query);
535  } catch (Exception $e) {
536  throw new Exception('Unable to get links for asset: '.$assetid.' due to database error: '.$e->getMessage());
537  }
538 
539  // If we have no assets NOT in Live status, so we should go live
540  if (count($db_links) === 0) {
541  $run_level = $GLOBALS['SQ_SYSTEM']->getRunLevel();
542  $GLOBALS['SQ_SYSTEM']->setRunLevel($run_level ^ SQ_SECURITY_STATUS_INTEGRITY);
543  $vars = Array(
544  'assetid' => $this->id,
545  'new_status' => SQ_STATUS_LIVE,
546  'dependants_only' => TRUE,
547  );
548  $hipo_herder->freestyleHipo('hipo_job_edit_status', $vars);
549  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
550  }
551 
552  }
553 
554  }
555 
556  return;
557 
558  }//end onAssetStatusUpdate()
559 
560 
570  public function readAccess(Array $assetids=Array())
571  {
572  if (!$this->id) return TRUE;
573  if (!$GLOBALS['SQ_SYSTEM']->runLevelEnables(SQ_SECURITY_PERMISSIONS)) {
574  return TRUE;
575  }
576 
577  /*
578  * We need to provide Read Permission to the Bundle to ensure that they
579  * can view Bundle details and advance Workflow if available.
580  */
581  $edit_fns = $this->getEditFns();
582  if ($edit_fns->showIfCanApprove($this)) {
583  return TRUE;
584  }
585 
586  return $this->_checkPermissionAccess(SQ_PERMISSION_READ, $assetids);
587 
588  }//end readAccess()
589 
590 
601  public function canCreateLink(Asset $minor, $link_type, $exclusive)
602  {
603  if (!$this->id) return translate('asset_not_created');
604 
605  // Ensure that Shadow Assets cannot be linked to a Bundle
606  $minor_asset_id_parts = explode(':', $minor->id);
607  if (isset($minor_asset_id_parts[1])) {
608  return FALSE;
609  }
610 
611  return parent::canCreateLink($minor, $link_type, $exclusive);
612 
613  }//end canCreateLink()
614 
615 
635  public function createLink(Asset $minor, $link_type, $value='', $sort_order=NULL, $dependant='0', $exclusive='0', $moving=FALSE)
636  {
637  if (!$this->id) return 0;
638 
639  // Ensure that Shadow Assets cannot be linked to a Bundle
640  $minor_asset_id_parts = explode(':', $minor->id);
641  if (isset($minor_asset_id_parts[1])) {
642  return 0;
643  }
644 
645  return parent::createLink($minor, $link_type, $value, $sort_order, $dependant, $exclusive, $moving);
646 
647  }//end createLink()
648 
649 
657  public function getAllBundleStreams()
658  {
659  $bundled_asset_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_NOTICE);
660  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
661 
662  $all_schemas = Array();
663  foreach ($bundled_asset_links as $linkid => $bundled_asset_link) {
664  $bundled_assetid = $bundled_asset_link['minorid'];
665  $bundled_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($bundled_assetid);
666 
667  if ($bundled_asset->adminAccess() === TRUE) {
668  $all_schemas = array_merge($all_schemas, $wfm->getSchemas($bundled_assetid, TRUE));
669  }
670  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($bundled_asset);
671  unset($bundled_asset);
672  }
673 
674  $all_schemas = array_unique($all_schemas);
675  $all_streams = Array();
676 
677  foreach ($all_schemas as $schemaid) {
678  $all_streams += $wfm->getStreams($schemaid, TRUE);
679  }
680 
681  return $all_streams;
682 
683  }//end getAllBundleStreams()
684 
685 
693  {
694  $bundled_asset_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_NOTICE);
695  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
696 
697  $all_streams = array_keys($this->getAllBundleStreams());
698  foreach ($bundled_asset_links as $linkid => $bundled_asset_link) {
699  $bundled_assetid = $bundled_asset_link['minorid'];
700  $schemas = $wfm->getSchemas($bundled_assetid, TRUE);
701  foreach ($schemas as $schemaid) {
702  $all_streams = array_intersect($all_streams, $wfm->getBypassableStreams($bundled_assetid, $schemaid));
703  }
704  }
705 
706  return $all_streams;
707 
708  }//end getAllBypassableBundleStreams()
709 
710 
716  public function isDefaultStreamBypassable()
717  {
718  $bundled_asset_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_NOTICE);
719  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
720 
721  foreach ($bundled_asset_links as $linkid => $bundled_asset_link) {
722  $bundled_assetid = $bundled_asset_link['minorid'];
723  if ($wfm->testPublish($bundled_assetid, $GLOBALS['SQ_SYSTEM']->user->id, '') === FALSE) {
724  return FALSE;
725  }
726  }
727 
728  return TRUE;
729 
730  }//end isDefaultStreamBypassable()
731 
732 
733 }//end class
734 
735 ?>