|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\properties\handlers; |
|
4
|
|
|
|
|
5
|
|
|
abstract class AbstractHandler |
|
6
|
|
|
{ |
|
7
|
|
|
/** @var \app\models\PropertyHandler $propertyHandler */ |
|
8
|
|
|
protected $propertyHandler; |
|
9
|
|
|
/** @var \yii\base\Widget $widgetClass */ |
|
10
|
|
|
protected $widgetClass = null; |
|
11
|
|
|
protected $additionalRenderData = []; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param \app\models\PropertyHandler $propertyHandler |
|
15
|
|
|
*/ |
|
16
|
|
|
function __construct(\app\models\PropertyHandler $propertyHandler) |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
$this->propertyHandler = $propertyHandler; |
|
19
|
|
|
|
|
20
|
|
|
$widgetClass = !empty($this->widgetClass) ? $this->widgetClass : get_called_class().'Widget'; |
|
21
|
|
|
if (class_exists($widgetClass) && is_subclass_of($widgetClass, '\app\properties\handlers\AbstractHandlerWidget')) { |
|
|
|
|
|
|
22
|
|
|
$this->widgetClass = $widgetClass; |
|
|
|
|
|
|
23
|
|
|
} else { |
|
24
|
|
|
$this->widgetClass = null; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$this->init(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Initialize instance |
|
32
|
|
|
*/ |
|
33
|
|
|
public function init() |
|
34
|
|
|
{ |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param \app\models\Property $property |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
|
|
public function changePropertyType(\app\models\Property &$property) |
|
42
|
|
|
{ |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $property |
|
48
|
|
|
* @param $model |
|
49
|
|
|
* @param $values |
|
50
|
|
|
* @param $form |
|
51
|
|
|
* @param $renderType |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function render($property, $model, $values, $form, $renderType) |
|
55
|
|
|
{ |
|
56
|
|
|
if (!empty($this->widgetClass)) { |
|
57
|
|
|
$widgetClass = $this->widgetClass; |
|
58
|
|
|
$renderType = isset($this->propertyHandler->$renderType) ? $this->propertyHandler->$renderType : $this->propertyHandler->frontend_render_view; |
|
59
|
|
|
return $widgetClass::widget([ |
|
60
|
|
|
'values' => $values, |
|
61
|
|
|
'form' => $form, |
|
62
|
|
|
'model' => $model, |
|
63
|
|
|
'property_key' => $property->key, |
|
64
|
|
|
'property_id' => $property->id, |
|
65
|
|
|
'label' => $property->name, |
|
66
|
|
|
'multiple' => $property->multiple, |
|
67
|
|
|
'viewFile' => $renderType, |
|
68
|
|
|
'additional' => $this->additionalRenderData, |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return ''; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param \app\models\Property $property |
|
77
|
|
|
* @param string $formProperties |
|
78
|
|
|
* @param array $values |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function processValues(\app\models\Property $property, $formProperties = '', $values = []) |
|
82
|
|
|
{ |
|
83
|
|
|
return $values; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string|null $action Method of handler |
|
88
|
|
|
* @param array $params |
|
89
|
|
|
* @return mixed|string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function runAction($action = null, $params = []) |
|
92
|
|
|
{ |
|
93
|
|
|
if (preg_match('#^[a-z0-9\\-_]+$#', $action) && strpos($action, '--') === false && trim($action, '-') === $action) { |
|
94
|
|
|
$methodName = 'action' . str_replace(' ', '', ucwords(implode(' ', explode('-', $action)))); |
|
95
|
|
|
if (method_exists($this, $methodName)) { |
|
96
|
|
|
$method = new \ReflectionMethod($this, $methodName); |
|
97
|
|
|
if ($method->isPublic() && $method->getName() === $methodName) { |
|
|
|
|
|
|
98
|
|
|
return call_user_func([$this, $methodName], $params); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return ''; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
?> |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.