Advertisement
Guest User

Untitled

a guest
May 24th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. // the model /////////////////////////////////////////////////////////////////////
  2.  
  3. class Post extends \yii\db\ActiveRecord
  4. {
  5. /**
  6. * @inheritdoc
  7. */
  8. public static function tableName()
  9. {
  10. return 'item';
  11. }
  12.  
  13. /**
  14. * @inheritdoc
  15. */
  16. public function rules()
  17. {
  18. return [
  19. // The RelatedWorkflowValidator is in charge of validating status_ex transition
  20. // depending on transition occuring on the primary workflow (status attribute)
  21.  
  22. ['status_ex',RelatedWorkflowValidator::className()],
  23. ];
  24. }
  25. /**
  26. * (non-PHPdoc)
  27. * @see \yii\base\Component::behaviors()
  28. */
  29. public function behaviors()
  30. {
  31. return [
  32. // Primary workflow
  33. 'w1' =>[
  34. 'class' => \raoul2000\workflow\base\SimpleWorkflowBehavior::className(),
  35. 'statusAttribute' => 'status',
  36. 'defaultWorkflowId' => 'PostWorkflow1'
  37. ],
  38. // Secondary workflow
  39. 'w2' => [
  40. 'class' => \raoul2000\workflow\base\SimpleWorkflowBehavior::className(),
  41. 'statusAttribute' => 'status_ex',
  42. 'defaultWorkflowId' => 'PostWorkflow2'
  43. ]
  44. ];
  45. }
  46. }
  47.  
  48. // PostWorkflow1 definition /////////////////////////////////////////////////////////////////////
  49.  
  50. class PostWorkflow1 implements IWorkflowDefinitionProvider
  51. {
  52. public function getDefinition() {
  53. return [
  54. 'initialStatusId' => 'draft',
  55. 'status' => [
  56. 'draft' => [
  57. 'transition' => ['correction']
  58. ],
  59. 'correction' => [
  60. 'transition' => ['draft','ready']
  61. ],
  62. 'ready' => [
  63. 'transition' => ['draft', 'correction', 'published']
  64. ],
  65. 'published' => [
  66. 'transition' => ['ready', 'archived']
  67. ],
  68. 'archived' => [
  69. 'transition' => ['ready']
  70. ]
  71. ]
  72. ];
  73. }
  74. }
  75.  
  76. // PostWorkflow2 definition /////////////////////////////////////////////////////////////////////
  77.  
  78. class PostWorkflow2 implements IWorkflowDefinitionProvider
  79. {
  80. public function getDefinition() {
  81. return [
  82. 'initialStatusId' => 'success',
  83. 'status' => [
  84. 'success' => [
  85. 'transition' => ['onHold']
  86. ],
  87. 'onHold' => [
  88. 'transition' => ['success']
  89. ],
  90. ]
  91. ];
  92. }
  93. }
  94.  
  95. // The Standalone Validator class /////////////////////////////////////////////////////////////////////
  96.  
  97. use yii\validators\Validator;
  98. use raoul2000\workflow\validation\WorkflowScenario;
  99.  
  100. class RelatedWorkflowValidator extends Validator
  101. {
  102.  
  103. private $constraint;
  104.  
  105. /**
  106. * Set constraint array where keys are scenario for the secondary workflow, and
  107. * values are array of scenario for the primary workflow that allow secondary transition.
  108. * If value is TRUE, the secondary transition is allowed.
  109. *
  110. * @see \yii\validators\Validator::init()
  111. */
  112. public function init()
  113. {
  114. parent::init();
  115.  
  116. $this->constraint = [
  117. // model can enter into secondary workflow no matter what is the primary workflow transition
  118. WorkflowScenario::enterWorkflow('Post4Workflow2') => true,
  119.  
  120. WorkflowScenario::changeStatus('Post4Workflow2/success', 'Post4Workflow2/onHold') => [
  121. WorkflowScenario::changeStatus('Post4Workflow1/draft', 'Post4Workflow1/correction'),
  122. WorkflowScenario::changeStatus('Post4Workflow1/ready', 'Post4Workflow1/draft')
  123. ],
  124.  
  125. WorkflowScenario::changeStatus('Post4Workflow2/onHold', 'Post4Workflow2/success') => [
  126. WorkflowScenario::changeStatus('Post4Workflow1/correction', 'Post4Workflow1/ready')
  127. ]
  128. ];
  129. }
  130. /**
  131. * Perform validation.
  132. * WARNING : primary and secondary status attribute names are hard coded (to change if needed)
  133. *
  134. * @see \yii\validators\Validator::validateAttribute()
  135. */
  136. public function validateAttribute($model, $attribute)
  137. {
  138.  
  139. $w2scenarios=null;
  140. $w1scenarios=null;
  141. try {
  142.  
  143. list(,$w2scenarios) = $model->getBehavior('w2')->_createTransitionItems($model->status_ex, true, false);
  144. if( $w2scenarios == null) {
  145. return; // no pending transition for w2 : validation succeeds
  146. }
  147. } catch (Exception $e) {
  148. $this->addError($attribute, 'invalid transition on secondary workflow');
  149. return;
  150. }
  151.  
  152. try {
  153. list(,$w1scenarios) = $model->getBehavior('w1')->_createTransitionItems($model->status, true, false);
  154. } catch (Exception $e) {
  155. $this->addError($attribute, 'invalid transition on primary workflow');
  156. return;
  157. }
  158.  
  159. foreach ($w2scenarios as $w2sc) {
  160. if (isset( $this->constraint[$w2sc])) {
  161. if( $this->constraint[$w2sc] === true) {
  162. return;
  163. }elseif($w1scenarios === null && in_array($w1scenarios, $this->constraint[$w2sc])) {
  164. return;
  165. }else {
  166. foreach ($w1scenarios as $w1sc) {
  167. if( in_array($w1sc,$this->constraint[$w2sc])) {
  168. return;
  169. }
  170. }
  171. }
  172. }
  173. }
  174.  
  175. // the constraint array does not contain secondary AND related primary transition
  176. // for the current model.
  177.  
  178. $msgInfo= implode(', ',$w2scenarios);
  179. $this->addError($attribute, 'constraint failed : '. $msgInfo);
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement