1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: VITALYIEGOROV |
5
|
|
|
* Date: 09.12.15 |
6
|
|
|
* Time: 09:57 |
7
|
|
|
*/ |
8
|
|
|
namespace samsoncms\api; |
9
|
|
|
|
10
|
|
|
use samsoncms\api\query\FieldNavigation; |
11
|
|
|
use samsoncms\api\query\MaterialNavigation; |
12
|
|
|
use samsonframework\orm\Condition; |
13
|
|
|
use samsonframework\orm\ConditionInterface; |
14
|
|
|
use samsonframework\orm\QueryInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Material additional fields table. |
18
|
|
|
* @package samsoncms\api |
19
|
|
|
*/ |
20
|
|
|
class FieldsTable |
21
|
|
|
{ |
22
|
|
|
/** @var integer Navigation identifier for table structure */ |
23
|
|
|
protected $navigationID; |
24
|
|
|
|
25
|
|
|
/** @var integer Table parent material identifier */ |
26
|
|
|
protected $materialID; |
27
|
|
|
|
28
|
|
|
/** @var Field[] Collection field instances that correspond table columns */ |
29
|
|
|
protected $fields; |
30
|
|
|
|
31
|
|
|
/** @var QueryInterface Database query interface */ |
32
|
|
|
protected $query; |
33
|
|
|
|
34
|
|
|
/** @var string Locale identifier */ |
35
|
|
|
protected $locale; |
36
|
|
|
|
37
|
|
|
/** @var array Fields table collection */ |
38
|
|
|
protected $collection; |
39
|
|
|
|
40
|
|
|
/** @return array Get field table column names collection */ |
41
|
|
|
public function columns() |
42
|
|
|
{ |
43
|
|
|
return array_column($this->fields, Field::F_IDENTIFIER); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get collection of table column values as array. |
48
|
|
|
* |
49
|
|
|
* @param string $fieldID Additional field identifier |
50
|
|
|
* @return array Collection of table column values as array |
51
|
|
|
*/ |
52
|
|
|
public function values($fieldID) |
53
|
|
|
{ |
54
|
|
|
$return = array(); |
55
|
|
|
if (isset($this->fields[$fieldID])) { |
56
|
|
|
$return = array_column($this->collection, $fieldID); |
57
|
|
|
} |
58
|
|
|
return $return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get field table as multidimensional array. |
63
|
|
|
* |
64
|
|
|
* @return array Field table represented as array |
65
|
|
|
*/ |
66
|
|
|
public function toArray() |
67
|
|
|
{ |
68
|
|
|
return $this->collection; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @return array Collection of table rows(materials) identifiers */ |
72
|
|
|
protected function rowIDs() |
73
|
|
|
{ |
74
|
|
|
// Get collection of nested materials |
75
|
|
|
return $this->query |
76
|
|
|
->entity(Material::ENTITY) |
77
|
|
|
->where(Material::F_DELETION, 1) |
78
|
|
|
->where(Material::F_PRIMARY, (new MaterialNavigation())->idsByRelationID($this->navigationID)) |
|
|
|
|
79
|
|
|
->where(Material::F_PARENT, $this->materialID) |
80
|
|
|
->orderBy(Material::F_PRIORITY) |
81
|
|
|
->fields(Material::F_PRIMARY); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Build correct localized field request for retrieving additional fields records. |
86
|
|
|
* |
87
|
|
|
* @param Field[] $fields Collection of additional fields |
88
|
|
|
* @return Condition Built condition for query |
89
|
|
|
*/ |
90
|
|
|
protected function fieldsCondition($fields) |
91
|
|
|
{ |
92
|
|
|
// Group fields by localization |
93
|
|
|
$localizedColumns = array(); |
94
|
|
|
$notLocalizedColumns = array(); |
95
|
|
|
/** @var Field $field Iterate table columns(fields) */ |
96
|
|
|
foreach ($fields as $field) { |
97
|
|
|
if ($field->localized()) { |
98
|
|
|
$localizedColumns[] = $field->id; |
99
|
|
|
} else { |
100
|
|
|
$notLocalizedColumns[] = $field->id; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Create field condition |
105
|
|
|
$fieldsCondition = new Condition(ConditionInterface::DISJUNCTION); |
106
|
|
|
// Create localized condition |
107
|
|
|
if (sizeof($localizedColumns)) { |
108
|
|
|
$localizedCondition = new Condition(ConditionInterface::CONJUNCTION); |
109
|
|
|
$localizedCondition->add(Field::F_PRIMARY, $localizedColumns) |
110
|
|
|
->add(MaterialField::F_LOCALE, $this->locale); |
111
|
|
|
|
112
|
|
|
// Add this condition to condition group |
113
|
|
|
$fieldsCondition->addCondition($localizedCondition); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Create not localized condition |
117
|
|
|
if (sizeof($notLocalizedColumns)) { |
118
|
|
|
$fieldsCondition->add(Field::F_PRIMARY, $notLocalizedColumns); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $fieldsCondition; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Fill table with data from database. |
126
|
|
|
*/ |
127
|
|
|
protected function load() |
128
|
|
|
{ |
129
|
|
|
// Get table Fields instances |
130
|
|
|
$this->fields = (new FieldNavigation())->byRelationID($this->navigationID); |
|
|
|
|
131
|
|
|
|
132
|
|
|
if (sizeof($rowIDs = $this->rowIDs())) { |
133
|
|
|
/** @var MaterialField $fieldValue Get additional field value instances */ |
134
|
|
|
foreach ($this->query->entity(CMS::MATERIAL_FIELD_RELATION_ENTITY) |
|
|
|
|
135
|
|
|
// Get only needed rows(materials) |
136
|
|
|
->where(Material::F_PRIMARY, $rowIDs) |
|
|
|
|
137
|
|
|
->where(Material::F_DELETION, 1) |
138
|
|
|
// Get correct localizes field condition for columns |
139
|
|
|
->whereCondition($this->fieldsCondition($this->fields)) |
140
|
|
|
->exec() as $fieldValue |
141
|
|
|
) { |
142
|
|
|
/** @var Field $field Try to find Field instance by identifier */ |
143
|
|
|
$field = &$this->fields[$fieldValue[Field::F_PRIMARY]]; |
144
|
|
|
if (isset($field)) { |
145
|
|
|
/** |
146
|
|
|
* Store table row(material) as it primary, store columns(Fields) |
147
|
|
|
* by field primary. Use correct column for value. |
148
|
|
|
*/ |
149
|
|
|
$this->collection[$fieldValue[Material::F_PRIMARY]][$fieldValue[Field::F_PRIMARY]] |
150
|
|
|
= $fieldValue[$field->valueFieldName()]; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* FieldsTable constructor. |
158
|
|
|
* |
159
|
|
|
* @param QueryInterface $query Database query interface |
160
|
|
|
* @param mixed $navigationID Navigation identifier for table structure |
161
|
|
|
* @param integer $materialID Table parent material identifier |
162
|
|
|
* @param string $locale Locale identifier |
163
|
|
|
*/ |
164
|
|
|
public function __construct(QueryInterface $query, $navigationID, $materialID, $locale = '') |
165
|
|
|
{ |
166
|
|
|
$this->query = $query; |
167
|
|
|
$this->navigationID = $navigationID; |
168
|
|
|
$this->materialID = $materialID; |
169
|
|
|
$this->locale = $locale; |
170
|
|
|
|
171
|
|
|
$this->load(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: