WorkflowTransitionStateDescriptorWidget.php 7.92 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
 * Emilia Romagna Open Innovation
 * Arter
Marcello Pivanti's avatar
Marcello Pivanti committed
6
7
 *
 *
Aster's avatar
Aster committed
8
 * @package    arter\amos\workflow\widgets
Marcello Pivanti's avatar
Marcello Pivanti committed
9
10
11
 * @category   CategoryName
 */

Aster's avatar
Aster committed
12
namespace arter\amos\workflow\widgets;
Marcello Pivanti's avatar
Marcello Pivanti committed
13

Aster's avatar
Aster committed
14
15
16
17
18
19
use arter\amos\core\controllers\CrudController;
use arter\amos\core\forms\ActiveForm;
use arter\amos\core\module\AmosModule;
use arter\amos\core\module\BaseAmosModule;
use arter\amos\core\record\Record;
use arter\amos\core\utilities\WorkflowTransitionWidgetUtility;
Marcello Pivanti's avatar
Marcello Pivanti committed
20
21
22
23
24
25
26
use kartik\base\Widget;
use raoul2000\workflow\base\Status;

/**
 * Class WorkflowTransitionStateDescriptorWidget
 * Renders the widget useful to view the workflow status of a model.
 *
Aster's avatar
Aster committed
27
 * @package arter\amos\core\forms
Marcello Pivanti's avatar
Marcello Pivanti committed
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
 */
class WorkflowTransitionStateDescriptorWidget extends Widget
{
    /**
     * @var string $containerWidgetClass
     */
    public $containerWidgetClass = 'workflow-transition-state-descriptor-widget col-xs-12';

    /**
     * @var string $icon to validate
     */
    public $icon = 'refresh-alt';

    /**
     * @var string $icon validate
     */
    public $iconValidate = 'check-all';

    /**
     * @var string $icon edit
     */
    public $iconEdit = 'edit';

    /**
     * @var array Array of the icon options
     */
    public $iconOptions = '';//['style' => 'font-size: 50px;'];

    /**
     * @var string $buttonLayout
     */
    public $buttonLayout = ''; //"<div id=\"workflow-form-actions\" class=\"pull-right\">{buttonSubmit}</div>";

    /**
     * If the initialMessage is not set the default value is 'CURRENT STATE'
     * @var string $initialMessage
     */
    public $initialMessage;

    /**
     * If the label is not set the default value is 'Not set'
     * @var string $initialLabel
     */
    public $initialLabel;

    /**
     * @var string $classHr
     */
    public $classHr = 'workflow';

    /**
     * @var string $classDivIcon
     */
    public $classDivIcon;

    /**
     * @var string $classDivLabel
     */
    public $classDivLabel;

    /**
     * @var string $classDivMessage
     */
    public $classDivMessage;

    /**
     * @var string $classDivButton
     */
    public $classDivButton;

    /**
     * @var string $customJs
     */
    public $customJs;

    /**
     * If it is not set the default value is 'Are you sure you want to change status?'
     * @var string $dataConfirm
     */
    public $dataConfirm;

    /**
     * @var bool $viewWidgetOnNewRecord If true force to view the widget when the model is in new record state
     */
    public $viewWidgetOnNewRecord = false;

    /**
     * @var Record $model
     */
    private $model;

    /**
     * @var ActiveForm $form
     */
    private $form;

    /**
     * @var string $workflowId
     */
    private $workflowId;

    /**
     * @var array $metadata
     */
    private $metadata;

    /**
     * @var AmosModule $module
     */
    private $module;

    /**
     * @var string $translationCategory
     */
    private $translationCategory;

    /**
     *
     * Set of the permissionSave
     */
    public function init()
    {
        /** @var CrudController $controller */
        $controller = \Yii::$app->controller;
        $moduleName = $controller->module->uniqueId;
        $this->module = \Yii::$app->getModule($moduleName);
        if (!$this->translationCategory) {
            $this->translationCategory = preg_replace('/[^aA-zZ]/i', '', 'amos' . $moduleName);
        }

        parent::init();
    }

    /**
     * @return string
     */
    public function getWorkflowId()
    {
        return $this->workflowId;
    }

    /**
     * @param string $workflowId
     */
    public function setWorkflowId($workflowId)
    {
        $this->workflowId = $workflowId;
    }

    /**
     * @return array
     */
    public function getMetadata()
    {
        return $this->metadata;
    }

    /**
     * @param array $metadata
     */
    public function setMetadata($metadata)
    {
        $this->metadata = $metadata;
    }

    /**
     * @return string
     */
    public function getTranslationCategory()
    {
        return $this->translationCategory;
    }

    /**
     * @param string $translationCategory
     */
    public function setTranslationCategory($translationCategory)
    {
        $this->translationCategory = $translationCategory;
    }

    /**
     * @return Record
     */
    public function getModel()
    {
        return $this->model;
    }

    /**
     * @param Record $model
     */
    public function setModel($model)
    {
        $this->model = $model;
    }

    /**
     * @return ActiveForm
     */
    public function getForm()
    {
        return $this->form;
    }

    /**
     * @param ActiveForm $form
     */
    public function setForm($form)
    {
        $this->form = $form;
    }

    /**
     * @inheritdoc
     */
    public function run()
    {
        if (isset(\Yii::$app->params['hideWorkflowTransitionWidget']) && \Yii::$app->params['hideWorkflowTransitionWidget']) {
            return '';
        }
        $content = '';
        $module = ($this->module);
        if ($this->model->hasWorkflowStatus()) {
            $status = $module::t($this->translationCategory, WorkflowTransitionWidgetUtility::getLabelStatus($this->model));
            $js = "
                $('form').on('submit', function (e) {
                    var buttonId = $('[clicked=true]').attr('id');
                    if (!buttonId) {
                        buttonId = '" . $this->model->getWorkflowStatus()->getId() . "'
                    }
                    $('#workflow-status_id').val(buttonId);
                });
                
                $('[type=submit]').on('click', function(){
                    $('[type=submit]').removeAttr('clicked');
                    $(this).attr('clicked', true);
                });
                "
                . ((isset($this->customJs)) ? $this->customJs : "");

            $this->getView()->registerJs($js, \yii\web\View::POS_READY);

            $content = '<div class="' . $this->containerWidgetClass . '">'
                . '<div' . (isset($this->classDivMessage) ? ' class="' . $this->classDivMessage . '"' : '') . '>'
                . '<span>' . ((isset($status) && (strlen($status) > 0)) ? $status : (isset($this->initialLabel) ? $this->initialLabel : BaseAmosModule::t('amoscore', 'Not set'))) . '</span>'
                . '</div>';
            $content .= '</div>';
        } elseif ($this->viewWidgetOnNewRecord && $this->model->isNewRecord && $this->model->{$this->model->statusAttribute}) {
            /**
             * This piece of code is used for print widget when the model is new and not yet saved.
             * It print the status of the model if it is set.
             */
            $modelStatus = $this->model->{$this->model->statusAttribute};
            $status = $module::t($this->translationCategory, WorkflowTransitionWidgetUtility::getLabelStatus($this->model, $modelStatus));
            $content = '
            <div class="' . $this->containerWidgetClass . '">
                    <div' . (isset($this->classDivMessage) ? ' class="' . $this->classDivMessage . '"' : '') . '>
                        <span>' . ((isset($status) && (strlen($status) > 0)) ? $status : (isset($this->initialLabel) ? $this->initialLabel : BaseAmosModule::t('amoscore', 'Not set'))) . '</span>
                    </div>
            </div>
            ';
        }
        return $content;
    }

    /**
     * This method return the workflow status icon if is set in the metadata field.
     * @param string $key
     * @return string
     * @throws \yii\base\InvalidConfigException
     */
    private function getIconStatus($key = '')
    {
        if (!$key) {
            $key = $this->model->getWorkflowStatus()->getId();
        }
        /** @var Status $state */
        $state = $this->model->getWorkflowSource()->getStatus($key, $this->model);
        $icon = '';
        if ($state) {
            $metadati = $state->getMetaData();
            if (isset($metadati['icon'])) {
                $icon = $metadati['icon'];
            }
        }
        return $icon;
    }
}