Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
soap_api_workflow_service.inc
1 <?php
18 require_once SQ_PACKAGES_PATH.'/web_services/api/soap_api/soap_api.inc';
19 
31 class Soap_Api_Workflow_Service extends Soap_Api
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
55  public function getFunctionList()
56  {
57  return Array(
58  'StartWorkflow' => '1',
59  'SafeEditAsset' => '1',
60  'CancelWorkflow' => '1',
61  'CompleteWorkflow' => '1',
62  'SetWorkflowSchema' => '1',
63  'ApproveAssetInWorkflow' => '1',
64  );
65 
66  }//end getFunctionList()
67 
68 
82  function StartWorkflow($request)
83  {
84  $request_info = (Array) $request;
85  // We gotta assume that when someone call this function, they know what status the asset is in, and what status it is going to.
86  // We are going to do the changes, but there will be no confirmation for the caller to be warned what is going to happen.
87 
88  $assetid = array_get_index($request_info, 'AssetID', '');
89  if (!empty($assetid)) {
90  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
91  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
92  $asset_status = $asset->status;
93  if (!in_array($asset_status, Array (SQ_STATUS_UNDER_CONSTRUCTION, SQ_STATUS_LIVE, SQ_STATUS_EDITING))) {
94  throw new SoapFault('Server', 'Can Not Start Workflow When Asset Is In '.get_status_description($asset_status).' Status');
95  }//end if
96 
97  $wm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
98  $current_schemas = $wm->getSchemas($assetid, TRUE);
99  if (empty($current_schemas)) {
100  throw new SoapFault('Server', 'There Is No Workflow Schemas Applied On This Asset');
101  }//end if
102 
103  // We are starting workflow by change the status of the asset
104  $running_vars = Array (
105  'assetid' => $assetid,
106  'new_status' => self::getNextStatusStartWorkflow($asset_status),
107  'dependants_only' => TRUE,
108  );
109 
110  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
111  $errors = $hh->freestyleHipo('hipo_job_edit_status', $running_vars);
112  if (empty($errors)) {
113  return Array (
114  'StartWorkflowResult' => TRUE,
115  );
116  } else {
117  throw new SoapFault('Server', 'Unable To Start Workflow');
118  }//end else
119 
120  } else {
121  throw new SoapFault('Server', 'Asset ID Is Not Valid. Please Provide A Valid Asset ID');
122  }//end else
123 
124  }//end StartWorkflow()
125 
126 
133  private static function getNextStatusStartWorkflow($status)
134  {
135  $next_statuses = Array (
136  SQ_STATUS_UNDER_CONSTRUCTION => SQ_STATUS_PENDING_APPROVAL,
137  SQ_STATUS_LIVE => SQ_STATUS_LIVE_APPROVAL,
138  SQ_STATUS_EDITING => SQ_STATUS_EDITING_APPROVAL,
139  );
140  if (isset($next_statuses)) {
141  return ($next_statuses[$status]);
142  } else {
143  throw new SoapFault('Server', 'There are no status to advance to from status '.$status);
144  }//end else
145 
146  }//end getNextStatusStartWorkflow()
147 
148 
155  private static function getNextStatusCancelWorkflow($status)
156  {
157  $next_statuses = Array (
158  SQ_STATUS_PENDING_APPROVAL => SQ_STATUS_UNDER_CONSTRUCTION,
159  SQ_STATUS_APPROVED => SQ_STATUS_UNDER_CONSTRUCTION,
160  SQ_STATUS_LIVE_APPROVAL => SQ_STATUS_EDITING,
161  SQ_STATUS_EDITING => SQ_STATUS_LIVE,
162  SQ_STATUS_EDITING_APPROVAL => SQ_STATUS_EDITING
163  );
164  if (isset($next_statuses[$status])) {
165  return ($next_statuses[$status]);
166  } else {
167  throw new SoapFault('Server', 'There are no status to advance to from status '.$status);
168  }//end else
169 
170  }//end getNextStatusStartWorkflow()
171 
172 
179  private static function getNextStatusCompleteWorkflow($status)
180  {
181  $next_statuses = Array (
182  SQ_STATUS_APPROVED => SQ_STATUS_LIVE,
183  SQ_STATUS_LIVE_APPROVAL => SQ_STATUS_LIVE,
184  SQ_STATUS_EDITING_APPROVED => SQ_STATUS_LIVE,
185  );
186  if (isset($next_statuses[$status])) {
187  return ($next_statuses[$status]);
188  } else {
189  throw new SoapFault('Server', 'There are no status to advance to from status '.$status);
190  }//end else
191 
192  }//end getNextStatusStartWorkflow()
193 
194 
201  private static function getNextStatusApproveAssetInWorkflow($status)
202  {
203  $next_statuses = Array (
204  SQ_STATUS_EDITING => SQ_STATUS_EDITING_APPROVED,
205  SQ_STATUS_UNDER_CONSTRUCTION=> SQ_STATUS_APPROVED,
206  SQ_STATUS_PENDING_APPROVAL => SQ_STATUS_APPROVED,
207  SQ_STATUS_LIVE_APPROVAL => SQ_STATUS_LIVE_APPROVAL,
208  );
209 
210  if (isset($next_statuses[$status])) {
211  return ($next_statuses[$status]);
212  } else {
213  throw new SoapFault('Server', 'There are no status to advance to from status '.$status);
214  }//end else
215 
216  }//end getNextStatusApproveAssetInWorkflow()
217 
232  function CancelWorkflow($request)
233  {
234  $request_info = (Array) $request;
235  $assetid = array_get_index($request_info, 'AssetID', '');
236  if (!empty($assetid)) {
237  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
238 
239  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
240  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
241  $asset_status = $asset->status;
242  if (!in_array($asset_status, Array (SQ_STATUS_PENDING_APPROVAL, SQ_STATUS_LIVE_APPROVAL, SQ_STATUS_EDITING, SQ_STATUS_APPROVED, SQ_STATUS_EDITING_APPROVAL))) {
243  throw new SoapFault('Server', 'Can Not Cancel Workflow When Asset Is In '.get_status_description($asset_status).' Status');
244  }//end if
245 
246  $wm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
247  $current_schemas = $wm->getSchemas($assetid, TRUE, ($asset_status == SQ_STATUS_EDITING) ? FALSE : TRUE);
248  if (empty($current_schemas)) {
249  throw new SoapFault('Server', 'There is no workflow schemas applied on this asset');
250  }//end if
251 
252  // We are starting workflow by changing the status of the asset
253  $running_vars = Array (
254  'assetid' => $assetid,
255  'new_status' => self::getNextStatusCancelWorkflow($asset_status),
256  'dependants_only' => TRUE,
257  );
258 
259  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
260  $errors = $hh->freestyleHipo('hipo_job_edit_status', $running_vars);
261  if (empty($errors)) {
262  return Array (
263  'CancelWorkflowResult' => TRUE,
264  );
265  } else {
266  throw new SoapFault('Server', 'Unable To Cancel Workflow');
267  }//end else
268 
269  } else {
270  throw new SoapFault('Server', 'Asset ID is not valid. Please provide a valid Asset ID');
271  }//end else
272 
273  }//end CancelWorkflow()
274 
275 
289  function CompleteWorkflow($request)
290  {
291  $request_info = (Array) $request;
292  $assetid = array_get_index($request_info, 'AssetID', '');
293 
294  if (!empty($assetid)) {
295  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
296  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
297  $asset_status = $asset->status;
298  if (!in_array($asset_status, Array (SQ_STATUS_APPROVED, SQ_STATUS_EDITING_APPROVED, SQ_STATUS_LIVE_APPROVAL))) {
299  throw new SoapFault('Server', 'Can Not Complete Workflow When Asset Is In '.get_status_description($asset_status).' Status');
300  }//end if
301 
302  $wm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
303  $current_schemas = $wm->getSchemas($assetid, TRUE, TRUE);
304  // When the asset is in SQ_STATUS_APPROVED, there is no running workflow
305  if (empty($current_schemas) && !in_array($asset_status, Array(SQ_STATUS_APPROVED, SQ_STATUS_EDITING_APPROVED))) {
306  throw new SoapFault('Server', 'There is no workflow schemas applied on this asset');
307  }//end if
308 
309  $new_status = self::getNextStatusCompleteWorkflow($asset_status);
310 
311  // We are starting workflow by change the status of the asset
312  $running_vars = Array (
313  'assetid' => $assetid,
314  'new_status' => $new_status,
315  'dependants_only' => TRUE,
316  );
317 
318  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
319  $errors = $hh->freestyleHipo('hipo_job_edit_status', $running_vars);
320  if (empty($errors)) {
321  return Array (
322  'CompleteWorkflowResult' => TRUE,
323  );
324  } else {
325  throw new SoapFault('Server', 'Unable To Complete Workflow');
326  }//end else
327 
328  } else {
329  throw new SoapFault('Server', 'Asset ID Is Not Valid. Please Provide A Valid Asset ID');
330  }//end else
331 
332  }//end CompleteWorkflow()
333 
334 
348  function SafeEditAsset($request)
349  {
350  $request_info = (Array) $request;
351  $assetid = array_get_index($request_info, 'AssetID', '');
352 
353  if (!empty($assetid)) {
354  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
355  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
356  $asset_status = $asset->status;
357  if (!in_array($asset_status, Array (SQ_STATUS_LIVE))) {
358  throw new SoapFault('Server', 'Can Not Bring Asset To Safe Edit When Asset Is In '.get_status_description($asset_status).' Status');
359  }//end if
360 
361  $wm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
362  $current_schemas = $wm->getSchemas($assetid, TRUE);
363  if (empty($current_schemas)) {
364  throw new SoapFault('Server', 'There Is No Workflow Schemas Applied On This Asset');
365  }//end if
366 
367  // We are starting workflow by change the status of the asset
368  $running_vars = Array (
369  'assetid' => $assetid,
370  'new_status' => SQ_STATUS_EDITING,
371  'dependants_only' => TRUE,
372  );
373 
374  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
375  $errors = $hh->freestyleHipo('hipo_job_edit_status', $running_vars);
376  if (empty($errors)) {
377  return Array (
378  'SafeEditAssetResult' => TRUE,
379  );
380  } else {
381  throw new SoapFault('Server', 'Unable To Bring Asset To Safe Edit mode');
382  }//end else
383 
384  } else {
385  throw new SoapFault('Server', 'Asset ID Is Not Valid. Please Provide A Valid Asset ID');
386  }//end else
387 
388  }//end SafeEditAsset()
389 
403  function ApproveAssetInWorkflow($request)
404  {
405  // This one is a very hard one, gotta be very careful
406  $request_info = (Array) $request;
407  $assetid = array_get_index($request_info, 'AssetID', '');
408  $workflow_message = array_get_index($request_info, 'WorkflowMessage', '');
409 
410  if (!empty($assetid)) {
411  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
412  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
413 
414  $available_statuses = $asset->getAvailableStatii();
415  $asset_status = $asset->status;
416 
417  if (!in_array($asset_status, Array (SQ_STATUS_EDITING, SQ_STATUS_UNDER_CONSTRUCTION, SQ_STATUS_APPROVED, SQ_STATUS_LIVE_APPROVAL, SQ_STATUS_PENDING_APPROVAL, SQ_STATUS_EDITING_APPROVAL))) {
418  throw new SoapFault('Server', 'Can Not Approve Workflow When Asset Is In '.get_status_description($asset_status).' Status');
419  }//end if
420 
421  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
422  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
423  $current_userid = $GLOBALS['SQ_SYSTEM']->currentUserId();
424 
425  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
426  $running_schemas = $wfm->getSchemas($assetid, TRUE, TRUE);
427  $userid = $GLOBALS['SQ_SYSTEM']->currentUserId();
428 
429  if (empty($running_schemas) && $asset_status != SQ_STATUS_UNDER_CONSTRUCTION) {
430  throw new SoapFault('Server', 'There is no workflow schemas running on this asset');
431  }//end if
432 
433  if (in_array($asset_status, Array ( SQ_STATUS_PENDING_APPROVAL, SQ_STATUS_EDITING_APPROVAL))) {
434  // check whether we should go to pending approval next or pending approved next
435  // OR to editing approval or editing approved
436 
437  // the current user can approve this asset to keep the workflow going
438  $wf_complete = $wfm->testPublish($asset->id, $userid);
439 
440  // we also need to check what if all the required metadata fields are completed
441  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
442  $publishers = $wfm->whoCanPublish($asset->id);
443  if ($mm->allowsMetadata($asset->id) && !$GLOBALS['SQ_SYSTEM']->am->isDependant($asset->id)) {
444  $m_complete = $mm->requiredFieldsComplete($asset->id);
445 
446  if ($wf_complete && $m_complete) {
447  if ($asset_status == SQ_STATUS_PENDING_APPROVAL) {
448  $new_status = SQ_STATUS_APPROVED;
449  } else if ($asset_status == SQ_STATUS_EDITING_APPROVAL) {
450  $new_status = SQ_STATUS_EDITING_APPROVED;
451  }//end else
452  $statii[SQ_STATUS_APPROVED] = translate('status_change_approve');
453  } else if (in_array($userid, $publishers)) {
454  if ($asset_status == SQ_STATUS_PENDING_APPROVAL) {
455  $new_status = SQ_STATUS_PENDING_APPROVAL;
456  } else if ($asset_status == SQ_STATUS_EDITING_APPROVAL) {
457  $new_status = SQ_STATUS_EDITING_APPROVAL;
458  }//end else
459  }//end else
460  }//end if
461  } else {
462  $new_status = self::getNextStatusApproveAssetInWorkflow($asset_status);
463  }//end else
464 
465  // are comments required?
466  $require_comment = $wfm->requiresComment($asset->id, $current_userid);
467 
468  if ($require_comment && empty($workflow_message)) {
469  throw new SoapFault('Server', 'This approval step requires a comment and no comment have been provided');
470  }//end if
471 
472  if (!empty($running_schemas) && !($require_comment && empty($workflow_message))) {
473  // Lets do the status processing
474  // We are escalating workflow by change the status of the asset
475 
476  $running_vars = Array (
477  'assetid' => $assetid,
478  'new_status' => $new_status,
479  'dependants_only' => TRUE,
480  );
481  $errors = $hh->freestyleHipo('hipo_job_edit_status', $running_vars);
482 
483  // Then send a message
484  if (!empty($workflow_message)) {
485  $ms = $GLOBALS['SQ_SYSTEM']->getMessagingService();
486  $msg_reps = Array (
487  'message' => $workflow_message,
488  );
489  $message = $ms->newMessage(Array(), 'asset.workflow.userlog', $msg_reps);
490  $message->parameters['assetid'] = $asset->id;
491  $message->parameters['version'] = substr($asset->version, 0, strrpos($asset->version, '.'));
492  $message->send();
493  $updated = TRUE;
494  }//end if
495 
496  if (empty($errors)) {
497  return Array (
498  'ApproveAssetInWorkflowResult' => TRUE,
499  );
500  } else {
501  throw new SoapFault('Server', 'Unable To Complete Workflow');
502  }//end else
503 
504  }//end if
505 
506  } else {
507  throw new SoapFault('Server', 'Asset ID Is Not Valid. Please Provide A Valid Asset ID');
508  }//end else
509 
510  }//end ApproveAssetInWorkflow()
511 
512 
519  function SetWorkflowSchema($request)
520  {
521  $request_info = (Array) $request;
522 
523  $assetid = array_get_index($request_info, 'AssetID', '');
524  $schemaid = array_get_index($request_info, 'SchemaID', '');
525  $grant = self::array_get_index_restricted_value($request_info, 'Grant', ''); // Apply, Deny, Revoke = 1, 0, -1
526  $grant_level = Array (
527  'Apply' => '1',
528  'Deny' => '0',
529  'Revoke' => '-1',
530  );
531  $grant_val = $grant_level[$grant]; // We are not going check if isset index, the WSDL should check the input values for $grant
532  $auto_cascade = self::getBooleanIndex($request_info, 'AutoCascadeToNewChildren', TRUE);
533  $cascade = self::getBooleanIndex($request_info, 'Cascade', TRUE);
534 
535  $dependants = $GLOBALS['SQ_SYSTEM']->am->getDependantChildren($assetid);
536  $has_dependants = !empty($dependants);
537 
538  if (!empty($assetid) && !empty($schemaid)) {
539  $schema = $GLOBALS['SQ_SYSTEM']->am->getAsset($schemaid);
540  if (!($schema instanceof Workflow_Schema)) {
541  throw new SoapFault('Server', 'The provided schemaid does not belong to a workflow schema asset');
542  }//end if
543 
544  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
545  $running_vars = Array (
546  'schema_changes' => Array (
547  Array (
548  'assetids' => Array ( $assetid),
549  'granted' => $grant_val,
550  'schemaid' => $schemaid,
551  'cascades' => $auto_cascade,
552  'previous_access' => NULL,
553  'dependants_only' => (!($cascade && $auto_cascade) && $has_dependants) || (!$has_dependants && !$cascade),
554  ),
555  ),
556  );
557 
558  $errors = $hh->freestyleHipo('hipo_job_edit_workflow_schemas', $running_vars);
559  if (empty($errors)) {
560  return Array (
561  'SetWorkflowSchemaResult' => TRUE,
562  );
563  } else {
564  $message = '';
565  if (isset($errors[0]['message'])) $message = $errors[0]['message'];
566  throw new SoapFault('Server', 'Unable to set Workflow Schema on Asset. '.$message);
567  }//end else
568  } else {
569  throw new SoapFault('Server', 'Asset ID or Schema ID is not valid. Please provide a valid AssetID and SchemaID');
570  }//end else
571 
572  }//end SetWorkflowSchema()
573 
574 
581  public static function getComplexElements($func_list=Array())
582  {
583  $obj_optional = self::getArgType('AssetObject', 0, 1);
584  $grant_type = self::getArgType('Grant', 1, 1);
585 
586  $complex_types = Array (
587  'StartWorkflow' => Array (
588  'StartWorkflow' => Array (
589  'AssetID' => self::$string_non_optional,
590  ),
591  'StartWorkflowResponse' => Array (
592  'StartWorkflowResult' => self::$boolean_optional,
593  ),
594  ),
595  'SafeEditAsset' => Array (
596  'SafeEditAsset' => Array (
597  'AssetID' => self::$string_non_optional,
598  ),
599  'SafeEditAssetResponse' => Array (
600  'SafeEditAssetResult' => self::$boolean_optional,
601  ),
602  ),
603  'CancelWorkflow' => Array (
604  'CancelWorkflow' => Array (
605  'AssetID' => self::$string_non_optional,
606  ),
607  'CancelWorkflowResponse' => Array (
608  'CancelWorkflowResult' => self::$boolean_optional,
609  ),
610  ),
611  'CompleteWorkflow' => Array (
612  'CompleteWorkflow' => Array (
613  'AssetID' => self::$string_non_optional,
614  ),
615  'CompleteWorkflowResponse' => Array (
616  'CompleteWorkflowResult' => self::$boolean_optional,
617  ),
618  ),
619  'SetWorkflowSchema' => Array (
620  'SetWorkflowSchema' => Array (
621  'AssetID' => self::$string_non_optional,
622  'SchemaID' => self::$string_non_optional,
623  'Grant' => $grant_type,
624  'AutoCascadeToNewChildren' => self::$boolean_optional,
625  'Cascade' => self::$boolean_optional,
626  ),
627  'SetWorkflowSchemaResponse' => Array (
628  'SetWorkflowSchemaResult' => self::$boolean_optional,
629  ),
630  ),
631  'ApproveAssetInWorkflow' => Array (
632  'ApproveAssetInWorkflow' => Array (
633  'AssetID' => self::$string_non_optional,
634  'WorkflowMessage' => self::$string_optional,
635  ),
636  'ApproveAssetInWorkflowResponse' => Array (
637  'ApproveAssetInWorkflowResult' => self::$string_optional,
638  ),
639  ),
640  );
641 
642  $complex_types_available = parent::getComplexElements($complex_types);
643 
644  return $complex_types_available;
645 
646 
647  }//end getComplexTypes();
648 
649 
656  public static function getSimpleRestrictedTypes($func_list=Array())
657  {
658  $simple_restricted_types = Array (
659  'SetWorkflowSchema' => Array (
660  'Grant' => Array (
661  'restriction_base' => 'string',
662  'enumeration' => Array (
663  'Apply', 'Deny', 'Revoke',
664  ),
665  ),
666  ),
667  );
668 
669  $simple_restricted_types_available = parent::processSimpleRestrictedTypes($simple_restricted_types);
670 
671  return $simple_restricted_types_available;
672 
673  }//end getSimpleRestrictedTypes()
674 
675 
676 }//end class
677 ?>