WorkflowConfigTransition.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @file
  4. * Contains workflow\includes\Entity\WorkflowConfigTransition.
  5. * Contains workflow\includes\Entity\WorkflowConfigTransitionController.
  6. */
  7. /**
  8. * Implements a configurated Transition.
  9. */
  10. class WorkflowConfigTransition extends Entity {
  11. // Transition data.
  12. public $tid = 0;
  13. // public $old_sid = 0;
  14. // public $new_sid = 0;
  15. public $sid = 0; // @todo D8: remove $sid, use $new_sid. (requires conversion of Views displays.)
  16. public $target_sid = 0;
  17. public $roles = array();
  18. // Extra fields.
  19. public $wid = 0;
  20. // The following must explicitely defined, and not be public, to avoid errors when exporting with json_encode().
  21. protected $workflow = NULL;
  22. /**
  23. * Entity class functions.
  24. */
  25. /*
  26. // Implementing clone needs a list of tid-less transitions, and a conversion
  27. // of sids for both States and ConfigTransitions.
  28. // public function __clone() {}
  29. */
  30. public function __construct(array $values = array(), $entityType = NULL) {
  31. // Please be aware that $entity_type and $entityType are different things!
  32. return parent::__construct($values, $entityType = 'WorkflowConfigTransition');
  33. }
  34. /**
  35. * Permanently deletes the entity.
  36. */
  37. public function delete() {
  38. // Notify any interested modules before we delete, in case there's data needed.
  39. // @todo D8: this can be replaced by a hook_entity_delete(?)
  40. module_invoke_all('workflow', 'transition delete', $this->tid, NULL, NULL, FALSE);
  41. return parent::delete();
  42. }
  43. protected function defaultLabel() {
  44. return $this->label;
  45. }
  46. protected function defaultUri() {
  47. return array('path' => 'admin/config/workflow/workflow/manage/' . $this->wid . '/transitions/');
  48. }
  49. /**
  50. * Property functions.
  51. */
  52. /**
  53. * Returns the Workflow object of this State.
  54. *
  55. * @param Workflow $workflow
  56. * An optional workflow object. Can be used as a setter.
  57. *
  58. * @return Workflow
  59. * Workflow object.
  60. */
  61. public function setWorkflow($workflow) {
  62. $this->wid = $workflow->wid;
  63. $this->workflow = $workflow;
  64. }
  65. public function getWorkflow() {
  66. if (isset($this->workflow)) {
  67. return $this->workflow;
  68. }
  69. return workflow_load_single($this->wid);
  70. }
  71. public function getOldState() {
  72. return workflow_state_load_single($this->sid);
  73. }
  74. public function getNewState() {
  75. return workflow_state_load_single($this->target_sid);
  76. }
  77. /**
  78. * Verifies if the given transition is allowed.
  79. *
  80. * - In settings;
  81. * - In permissions;
  82. * - By permission hooks, implemented by other modules.
  83. *
  84. * @return bool
  85. * TRUE if OK, else FALSE.
  86. */
  87. public function isAllowed($user_roles) {
  88. if ($user_roles == 'ALL') {
  89. // Superuser.
  90. return TRUE;
  91. }
  92. elseif ($user_roles) {
  93. return array_intersect($user_roles, $this->roles) == TRUE;
  94. }
  95. return TRUE;
  96. }
  97. /**
  98. * Generate a machine name for a transition.
  99. */
  100. public static function machineName($start_name, $end_name) {
  101. $new_name = sprintf("%s_to_%s", $start_name, $end_name);
  102. // Special case: replace parens in creation state transition names.
  103. $new_name = str_replace("(creation)", "_creation", $new_name);
  104. return $new_name;
  105. }
  106. public function save() {
  107. parent::save();
  108. // Ensure Workflow is marked overridden.
  109. $workflow = $this->getWorkflow();
  110. if ($workflow->status == ENTITY_IN_CODE) {
  111. $workflow->status = ENTITY_OVERRIDDEN;
  112. $workflow->save();
  113. }
  114. }
  115. }
  116. /**
  117. * Implements a controller class for WorkflowConfigTransition.
  118. *
  119. * The 'true' controller class is 'Workflow'.
  120. */
  121. class WorkflowConfigTransitionController extends EntityAPIController {
  122. /**
  123. * Overrides DrupalDefaultEntityController::cacheGet().
  124. *
  125. * Override default function, due to core issue #1572466.
  126. */
  127. protected function cacheGet($ids, $conditions = array()) {
  128. // Load any available entities from the internal cache.
  129. if ($ids === FALSE && !$conditions) {
  130. return $this->entityCache;
  131. }
  132. return parent::cacheGet($ids, $conditions);
  133. }
  134. public function save($entity, DatabaseTransaction $transaction = NULL) {
  135. $workflow = $entity->getWorkflow();
  136. // To avoid double posting, check if this transition already exist.
  137. if (empty($entity->tid)) {
  138. if ($workflow) {
  139. $config_transitions = $workflow->getTransitionsBySidTargetSid($entity->sid, $entity->target_sid);
  140. $config_transition = reset($config_transitions);
  141. if ($config_transition) {
  142. $entity->tid = $config_transition->tid;
  143. }
  144. }
  145. }
  146. // Create the machine_name. This can be used to rebuild/revert the Feature in a target system.
  147. if (empty($entity->name)) {
  148. $entity->name = $entity->sid . '_' . $entity->target_sid;
  149. }
  150. $return = parent::save($entity, $transaction);
  151. if ($return) {
  152. // Save in current workflow for the remainder of this page request.
  153. // Keep in sync with Workflow::getTransitions() !
  154. $workflow = $entity->getWorkflow();
  155. if ($workflow) {
  156. $workflow->transitions[$entity->tid] = $entity;
  157. // $workflow->sortTransitions();
  158. }
  159. }
  160. // Reset the cache for the affected workflow, to force reload upon next page_load.
  161. workflow_reset_cache($entity->wid);
  162. return $return;
  163. }
  164. }