WizardPlayAction.php 4.41 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
1
<?php
Aster's avatar
Aster committed
2

Marcello Pivanti's avatar
Marcello Pivanti committed
3
/**
Aster's avatar
Aster committed
4
5
 * Lombardia Informatica S.p.A.
 * OPEN 2.0
Marcello Pivanti's avatar
Marcello Pivanti committed
6
 *
Aster's avatar
Aster committed
7
 * @see http://example.com Developers'community
8
9
 * @license GPLv3
 * @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
Marcello Pivanti's avatar
Marcello Pivanti committed
10
 *
Aster's avatar
Aster committed
11
 * @package    elitedivision\amos\wizflow
Marcello Pivanti's avatar
Marcello Pivanti committed
12
 * @category   CategoryName
Aster's avatar
Aster committed
13
 * @author     Lombardia Informatica S.p.A.
Marcello Pivanti's avatar
Marcello Pivanti committed
14
15
 */

Aster's avatar
Aster committed
16
namespace elitedivision\amos\wizflow;
Marcello Pivanti's avatar
Marcello Pivanti committed
17
18
19
20
21
22
23
24
25

use Yii;
use yii\base\Model;
use yii\helpers\BaseFileHelper;
use yii\validators\FileValidator;
use yii\web\UploadedFile;

/**
 * Class WizardPlayAction
Aster's avatar
Aster committed
26
 * @package elitedivision\amos\wizflow
Marcello Pivanti's avatar
Marcello Pivanti committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
 */
class WizardPlayAction extends \yii\base\Action
{
    public $wizardManagerName = 'wizflowManager';
    public $url = ['wizflow'];
    public $urlFinish = ['finish'];

    /**
     * @var string $tmpStorePath This is the tmp directory where the temp files are stored.
     */
    public $tmpStorePath = '';

    /**
     * @param string $nav
     * @return string
     */
    public function run($nav = 'start')
    {
        $wizard = Yii::$app->get($this->wizardManagerName);

        if ($nav == 'prev') {
            $model = $wizard->getPreviousStep();
            if ($model == null) {
                Yii::$app->controller->redirect(array_merge($this->url, ['nav' => 'start']));
            }
        } elseif ($nav == 'start') {
            $model = $wizard->start();
        } else {
            /** @var Model $model */
            $model = $wizard->getCurrentStep();
            if ($model == null) {
                $this->controller->redirect($this->urlFinish);
            } else {

                $oldModel = $model;
                if ($model->load(Yii::$app->request->post())) {

                    $validators = $model->validators;
                    /** @var FileValidator FileValidator */
                    foreach ($validators as $validator) {
                        if (is_a($validator, FileValidator::className(), true)) {
                            foreach ($validator->attributes as $attribute) {
                                $uploadedFile = UploadedFile::getInstance($model, $attribute);
                                if (empty($uploadedFile)) {
                                    $model->$attribute = $oldModel->$attribute;
                                } else {
                                    $model->$attribute = $this->saveTmpfile($uploadedFile);
                                }
                            }

                        }
                    }

                    if ($model->validate()) {
                        // current step has been completed : save it and get next step
                        $wizard->updateCurrentStep($model);
                        $model = $wizard->getNextStep();
                        if ($model == null) {
                            //we reached the last step : save it and go tp the finish page
                            $wizard->save();
                            $this->controller->redirect($this->urlFinish);
                        }
                    }
                    else {
                        $wizard->updateCurrentStep($model);
                    }
                } else {
                    if (empty($model)) {
                        $model = $wizard->getNextStep();
                        if ($model == null) {
                            $this->controller->redirect($this->urlFinish);
                        }
                    }
                }

            }
        }
        if ($model != null) {
            $viewname = $model->getWorkflowStatus()->getMetadata('view');
            $wizard->save();
            return $this->controller->render($viewname, [
                'model' => $model,
                'path' => $wizard->getPath()
            ]);
        }
    }

    /**
     * Store the file in a secure position
     *
     * @param UploadedFile $uploadedFile
     * @return UploadedFile
     */
    private function saveTmpfile($uploadedFile)
    {
        $storePath = $this->tmpStorePath;
        if (!strlen($storePath)) {
            $storePath = Yii::getAlias('@runtime') . DIRECTORY_SEPARATOR . 'wizflow';
        }

        if (!is_dir($storePath)) {
            BaseFileHelper::createDirectory($storePath, 0775, true);
        }
        $newTempfile = $storePath . DIRECTORY_SEPARATOR . basename($uploadedFile->tempName);
        if (copy($uploadedFile->tempName, $newTempfile)) {
            $uploadedFile->tempName = $newTempfile;
        }
        return $uploadedFile;
    }
}