|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* HiPanel core package |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://hipanel.com/ |
|
7
|
|
|
* @package hipanel-core |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hipanel\actions; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class SmartCreateAction. |
|
16
|
|
|
*/ |
|
17
|
|
|
class SmartCreateAction extends SwitchAction |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string View name, used for render of result on GET request |
|
21
|
|
|
*/ |
|
22
|
|
|
public $view; |
|
23
|
|
|
|
|
24
|
|
|
public function run() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->view = $this->view ?: $this->getScenario(); |
|
27
|
|
|
return parent::run(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** {@inheritdoc} */ |
|
31
|
|
|
protected function getDefaultRules() |
|
32
|
|
|
{ |
|
33
|
|
|
return array_merge(parent::getDefaultRules(), [ |
|
34
|
|
|
'GET ajax' => [ |
|
35
|
|
|
'class' => RenderAjaxAction::class, |
|
36
|
|
|
'view' => $this->view, |
|
37
|
|
|
'data' => $this->data, |
|
38
|
|
View Code Duplication |
'params' => function ($action) { |
|
|
|
|
|
|
39
|
|
|
$model = $action->controller->newModel(['scenario' => $action->scenario]); |
|
40
|
|
|
return [ |
|
41
|
|
|
'model' => $model, |
|
42
|
|
|
'models' => [$model], |
|
43
|
|
|
]; |
|
44
|
|
|
}, |
|
45
|
|
|
], |
|
46
|
|
|
'GET' => [ |
|
47
|
|
|
'class' => RenderAction::class, |
|
48
|
|
|
'view' => $this->view, |
|
49
|
|
|
'data' => $this->data, |
|
50
|
|
View Code Duplication |
'params' => function ($action) { |
|
|
|
|
|
|
51
|
|
|
$model = $action->controller->newModel(['scenario' => $action->scenario]); |
|
52
|
|
|
return [ |
|
53
|
|
|
'model' => $model, |
|
54
|
|
|
'models' => [$model], |
|
55
|
|
|
]; |
|
56
|
|
|
}, |
|
57
|
|
|
], |
|
58
|
|
|
'POST' => [ |
|
59
|
|
|
'save' => true, |
|
60
|
|
|
'success' => [ |
|
61
|
|
|
'class' => RedirectAction::class, |
|
62
|
|
View Code Duplication |
'url' => function ($action) { |
|
|
|
|
|
|
63
|
|
|
return $action->collection->count() > 1 |
|
64
|
|
|
? $action->controller->getSearchUrl(['id_in' => $action->collection->ids]) |
|
65
|
|
|
: $action->controller->getActionUrl('view', ['id' => $action->model->id]); |
|
66
|
|
|
}, |
|
67
|
|
|
], |
|
68
|
|
|
'error' => [ |
|
69
|
|
|
'class' => RenderAction::class, |
|
70
|
|
|
'view' => $this->view, |
|
71
|
|
|
'data' => $this->data, |
|
72
|
|
|
'params' => function ($action) { |
|
73
|
|
|
return [ |
|
74
|
|
|
'model' => $action->collection->first, |
|
75
|
|
|
'models' => $action->collection->models, |
|
76
|
|
|
]; |
|
77
|
|
|
}, |
|
78
|
|
|
], |
|
79
|
|
|
], |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.