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