= Html::a('Start the Wizflow one more time !!',['wizflow','nav'=>'start']) ?>
21 |You have successfully created your Wizflow powered application.
13 | 14 | 15 |44 | * [ 45 | * 'current' => [ 46 | * 'attrib1' => 'value1', 47 | * //... 48 | * 'status' => 'Wizflow/step', 49 | * ], 50 | * 'path' => [ 51 | * 'Wizflow/step' => [ 52 | * 'attrib1' => 'value1', 53 | * //... 54 | * 'status' => 'Wizflow/step', 55 | * ], 56 | * //etc ... 57 | * ] 58 | * ] 59 | *60 | */ 61 | private $_wiz = [ 62 | 'current' => null, 63 | 'path' => [] 64 | ]; 65 | /** 66 | * Loads existing wizard data if available. 67 | * 68 | * @see \yii\base\Object::init() 69 | */ 70 | public function init() 71 | { 72 | if( empty($this->skeyName) || ! is_string($this->skeyName)) { 73 | throw new InvalidConfigException('Parameter "skeyName" must be a non-empty string'); 74 | } 75 | 76 | if( empty($this->workflowSourceName) || ! is_string($this->workflowSourceName)) { 77 | throw new InvalidConfigException('Parameter "workflowSourceName" must be a non-empty string'); 78 | } 79 | 80 | $this->_ws = Yii::$app->get($this->getWorkflowSourceName()); 81 | 82 | $wiz = Yii::$app->session->get($this->skeyName,null); 83 | if($wiz !== null) { 84 | $this->_wiz = $wiz; 85 | } 86 | } 87 | /** 88 | * Returns the name workflow source component to use. 89 | * Note that the component must be available and registred in the Yii::$app container. 90 | * 91 | * @return string The name of the workflow source component 92 | */ 93 | public function getWorkflowSourceName() 94 | { 95 | return $this->workflowSourceName; 96 | } 97 | 98 | /** 99 | * Returns the workflow source component used by the manager 100 | * @return IWorkflowSource the workflow source component instance 101 | */ 102 | public function getWorkflowSource() 103 | { 104 | return $this->_ws; 105 | } 106 | /** 107 | * Creates and returns the form model instance for the status attribute. 108 | * Note that the attribute array passed as argument must include the status value. 109 | * 110 | * The instance is created using the 'model' metadata value associated with the 'status' 111 | * attribute value. The metadata 'model' must be an array suitable to invoke Yii::createObject(). 112 | * 113 | * Once created, the SimpleWorkflowBehavior is attached to the model. 114 | * 115 | * @param array $attributes list of attributes 116 | * @throws Exception 117 | * @return Model 118 | */ 119 | private function createInstance($attributes) 120 | { 121 | if( isset($attributes['status']) == false ) { 122 | throw new Exception('missing attribute "status"'); 123 | } 124 | 125 | $status = $this->workflowSource->getStatus($attributes['status']); 126 | $config = array_merge( 127 | $status->getMetadata('model'), 128 | $attributes 129 | ); 130 | $instance = Yii::createObject($config); 131 | $instance->attachBehavior( 132 | $this->workflowBehaviorName, 133 | $this->workflowBehavior 134 | ); 135 | 136 | return $instance; 137 | } 138 | /** 139 | * Save the wizard data into the current session 140 | */ 141 | public function save() 142 | { 143 | Yii::$app->session->set($this->skeyName,$this->_wiz); 144 | } 145 | /** 146 | * @return boolean TRUE if the wizard process as started (i.e. a current step is available) 147 | * FALSE otherwise. 148 | */ 149 | public function hasCurrentStep() 150 | { 151 | return $this->_wiz['current'] != null; 152 | } 153 | /** 154 | * Updates the current step with the model passed as argument. 155 | * 156 | * @param Model $model 157 | * @throws Exception 158 | */ 159 | public function updateCurrentStep($model) 160 | { 161 | if( $this->hasCurrentStep() == false ) { 162 | throw new Exception('wizard has no current step'); 163 | } 164 | $this->_wiz['current'] = $model->getAttributes(); 165 | } 166 | /** 167 | * Returns the model associated with the current wizard step. 168 | * 169 | * @return Model|null the current stemp model instance or NULL if no current step is defined. 170 | */ 171 | public function getCurrentStep() 172 | { 173 | $currentStep = null; 174 | if( $this->hasCurrentStep() ) { 175 | $currentStep = $this->createInstance($this->_wiz['current']); 176 | } 177 | return $currentStep; 178 | } 179 | 180 | /** 181 | * Returns an array containing all steps models that have been done until now. 182 | * The current step is not included in the returned array. 183 | * 184 | * @return array list of steps that have been performed until now 185 | */ 186 | public function getPath() 187 | { 188 | $path = []; 189 | foreach ($this->_wiz['path'] as $stepConfig) { 190 | $stepModel = $this->createInstance($stepConfig); 191 | $path[] = $stepModel; 192 | } 193 | return $path; 194 | } 195 | /** 196 | * Add the current step to the path and creates the next step form which becomes the new current step. 197 | * 198 | * @return Model the new current step 199 | */ 200 | public function getNextStep() 201 | { 202 | $nextStep = null; 203 | if ( $this->hasCurrentStep()) { 204 | // add current step to the path 205 | 206 | $currentStep = $this->getCurrentStep(); 207 | $this->_wiz['path'][$currentStep->getWorkflowStatus()->getId()] = $this->_wiz['current']; 208 | $this->_wiz['current'] = null; 209 | 210 | // find the next step 211 | 212 | $nextStatuses = $currentStep->getNextStatuses(true, true); 213 | $nextStep = null; 214 | if( count($nextStatuses) != 0) { 215 | foreach($nextStatuses as $info) { 216 | if( $info['isValid']) { 217 | // create the next step Form instance 218 | $nextStep = $this->createInstance(['status' => $info['status']->getId()]); 219 | 220 | // save it as the current step 221 | $this->_wiz['current'] = $nextStep->getAttributes(); 222 | 223 | break; 224 | } 225 | } 226 | } 227 | } 228 | return $nextStep; 229 | } 230 | /** 231 | * Replace the current step, with the last step of the path and returns it. 232 | * 233 | * @return Model the new current step 234 | */ 235 | public function getPreviousStep() 236 | { 237 | $prevStep = null; 238 | if( count($this->_wiz['path']) != 0) { 239 | 240 | $config = array_pop($this->_wiz['path']); 241 | 242 | // replace current step 243 | $this->_wiz['current'] = $config; 244 | 245 | // create instance 246 | $prevStep = $this->createInstance($config); 247 | } 248 | return $prevStep; 249 | } 250 | /** 251 | * Initiate the wizard. 252 | * This method reset the existing wizard path and returns the model for the first step. 253 | * 254 | * @return Model the model for the first step 255 | */ 256 | public function start() 257 | { 258 | $workflow = $this->workflowSource->getWorkflow($this->workflowBehavior['defaultWorkflowId']); 259 | 260 | $status = $workflow->getInitialStatus(); 261 | $config = $status->getMetadata('model'); 262 | $config['status'] = $status->getId(); 263 | 264 | $firstStep = $this->createInstance($config); 265 | 266 | $this->_wiz['current'] = $firstStep->getAttributes(); 267 | $this->_wiz['path'] = []; 268 | 269 | return $firstStep; 270 | } 271 | /** 272 | * Clean up session data that may have been stored by this wizard. 273 | */ 274 | public function destroy() 275 | { 276 | Yii::$app->session->remove($this->skeyName); 277 | } 278 | } 279 | -------------------------------------------------------------------------------- /src/WizflowModelInterface.php: -------------------------------------------------------------------------------- 1 | 2 |