|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Admin\Property\Input\Selectize; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
// From 'charcoal-admin' |
|
8
|
|
|
use Charcoal\Admin\Property\Input\SelectizeInput; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Relation Input Selectize |
|
12
|
|
|
*/ |
|
13
|
|
|
class RelationInput extends SelectizeInput |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* The target object type to build the choices from. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $targetObjectType; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Check used to parse multi Choice map against the obj properties. |
|
24
|
|
|
* |
|
25
|
|
|
* @var boolean |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $isChoiceObjMapFinalized = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Retrieve the target object type to build the choices from. |
|
31
|
|
|
* |
|
32
|
|
|
* @throws InvalidArgumentException If the target object type was not previously set. |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function targetObjectType() |
|
36
|
|
|
{ |
|
37
|
|
|
if ($this->targetObjectType === null) { |
|
38
|
|
|
$resolved = false; |
|
|
|
|
|
|
39
|
|
|
if (!$this->resolveTargetObjectType()) { |
|
40
|
|
|
throw new InvalidArgumentException( |
|
41
|
|
|
'Target object type could not be properly determined.' |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $this->targetObjectType; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Resolve the target object type from multiple sources & contexts |
|
51
|
|
|
* |
|
52
|
|
|
* @return boolean |
|
53
|
|
|
*/ |
|
54
|
|
|
private function resolveTargetObjectType() |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
$resolved = false; |
|
57
|
|
|
$formData = $this->viewController()->formData(); |
|
58
|
|
|
$param = isset($_GET['target_object_type']) ? $_GET['target_object_type'] : false; |
|
59
|
|
|
|
|
60
|
|
|
// Resolving through formData should be the most common occurence for this property input |
|
61
|
|
|
if (isset($formData['target_object_type']) && |
|
62
|
|
|
is_string($formData['target_object_type']) && |
|
63
|
|
|
!empty($formData['target_object_type']) |
|
64
|
|
|
) { |
|
65
|
|
|
$this->targetObjectType = $formData['target_object_type']; |
|
66
|
|
|
$resolved = true; |
|
67
|
|
|
// Resolve through URL params |
|
68
|
|
|
} elseif (is_string($param) && !empty($param)) { |
|
69
|
|
|
$this->targetObjectType = $param; |
|
70
|
|
|
$resolved = true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $resolved; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Retrieve the object-to-choice data map. |
|
78
|
|
|
* |
|
79
|
|
|
* @return array Returns a data map to abide. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function choiceObjMap() |
|
82
|
|
|
{ |
|
83
|
|
|
if ($this->choiceObjMap === null) { |
|
84
|
|
|
$map = $this->defaultChoiceObjMap(); |
|
85
|
|
|
|
|
86
|
|
|
$model = $this->modelFactory()->get($this->targetObjectType()); |
|
87
|
|
|
$objProperties = $model->properties(); |
|
88
|
|
|
|
|
89
|
|
|
if ($objProperties instanceof \Iterator) { |
|
90
|
|
|
$objProperties = iterator_to_array($objProperties); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
foreach ($map as &$mapProp) { |
|
94
|
|
|
$props = explode(':', $mapProp); |
|
95
|
|
|
foreach ($props as $p) { |
|
96
|
|
|
if (isset($objProperties[$p])) { |
|
97
|
|
|
$mapProp = $p; |
|
98
|
|
|
break; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$this->choiceObjMap = $map; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this->choiceObjMap; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.