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 $fieldID Additional field identifier |
49
|
|
|
* @return array Collection of table column values as array |
50
|
|
|
*/ |
51
|
|
|
public function values($fieldID) |
52
|
|
|
{ |
53
|
|
|
$return = array(); |
54
|
|
|
if (isset($this->fields[$fieldID])) { |
55
|
|
|
$return = array_column($this->collection, $fieldID); |
56
|
|
|
} |
57
|
|
|
return $return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function entities($fieldID, $entityIdentifier = Material::ENTITY) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$return = array(); |
63
|
|
|
$field = &$this->fields[$fieldID]; |
64
|
|
|
if (isset($field) && $field->Type) { |
65
|
|
|
$return = array_column($this->collection, $fieldID); |
66
|
|
|
} |
67
|
|
|
return $return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get field table as multidimensional array. |
72
|
|
|
* |
73
|
|
|
* @return array Field table represented as array |
74
|
|
|
*/ |
75
|
|
|
public function toArray() |
76
|
|
|
{ |
77
|
|
|
return $this->collection; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** @return array Collection of table rows(materials) identifiers */ |
81
|
|
|
protected function rowIDs() |
82
|
|
|
{ |
83
|
|
|
// Get collection of nested materials |
84
|
|
|
return $this->query |
85
|
|
|
->entity(Material::ENTITY) |
86
|
|
|
->where(Material::F_DELETION, 1) |
87
|
|
|
->where(Material::F_PRIMARY, (new MaterialNavigation())->idsByRelationID($this->navigationID)) |
|
|
|
|
88
|
|
|
->where(Material::F_PARENT, $this->materialID) |
89
|
|
|
->orderBy(Material::F_PRIORITY) |
90
|
|
|
->fields(Material::F_PRIMARY); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Build correct localized field request for retrieving additional fields records. |
95
|
|
|
* |
96
|
|
|
* @param Field[] $fields Collection of additional fields |
97
|
|
|
* @return Condition Built condition for query |
98
|
|
|
*/ |
99
|
|
|
protected function fieldsCondition($fields) |
100
|
|
|
{ |
101
|
|
|
// Group fields by localization |
102
|
|
|
$localizedColumns = array(); |
103
|
|
|
$notLocalizedColumns = array(); |
104
|
|
|
/** @var Field $field Iterate table columns(fields) */ |
105
|
|
|
foreach ($fields as $field) { |
106
|
|
|
if ($field->localized()) { |
107
|
|
|
$localizedColumns[] = $field->id; |
108
|
|
|
} else { |
109
|
|
|
$notLocalizedColumns[] = $field->id; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Create field condition |
114
|
|
|
$fieldsCondition = new Condition(ConditionInterface::DISJUNCTION); |
115
|
|
|
// Create localized condition |
116
|
|
|
if (sizeof($localizedColumns)) { |
117
|
|
|
$localizedCondition = new Condition(ConditionInterface::CONJUNCTION); |
118
|
|
|
$localizedCondition->add(Field::F_PRIMARY, $localizedColumns) |
119
|
|
|
->add(MaterialField::F_LOCALE, $this->locale); |
120
|
|
|
|
121
|
|
|
// Add this condition to condition group |
122
|
|
|
$fieldsCondition->addCondition($localizedCondition); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Create not localized condition |
126
|
|
|
if (sizeof($notLocalizedColumns)) { |
127
|
|
|
$fieldsCondition->add(Field::F_PRIMARY, $notLocalizedColumns); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $fieldsCondition; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Fill table with data from database. |
135
|
|
|
*/ |
136
|
|
|
protected function load() |
137
|
|
|
{ |
138
|
|
|
// Get table Fields instances |
139
|
|
|
$this->fields = (new FieldNavigation())->byRelationID($this->navigationID); |
|
|
|
|
140
|
|
|
|
141
|
|
|
if (sizeof($rowIDs = $this->rowIDs())) { |
142
|
|
|
/** @var MaterialField $fieldValue Get additional field value instances */ |
143
|
|
|
foreach ($this->query->entity(CMS::MATERIAL_FIELD_RELATION_ENTITY) |
|
|
|
|
144
|
|
|
// Get only needed rows(materials) |
145
|
|
|
->where(Material::F_PRIMARY, $rowIDs) |
|
|
|
|
146
|
|
|
->where(Material::F_DELETION, 1) |
147
|
|
|
// Get correct localizes field condition for columns |
148
|
|
|
->whereCondition($this->fieldsCondition($this->fields)) |
149
|
|
|
->exec() as $fieldValue |
150
|
|
|
) { |
151
|
|
|
/** @var Field $field Try to find Field instance by identifier */ |
152
|
|
|
$field = &$this->fields[$fieldValue[Field::F_PRIMARY]]; |
153
|
|
|
if (isset($field)) { |
154
|
|
|
/** |
155
|
|
|
* Store table row(material) as it primary, store columns(Fields) |
156
|
|
|
* by field primary. Use correct column for value. |
157
|
|
|
*/ |
158
|
|
|
$this->collection[$fieldValue[Material::F_PRIMARY]][$fieldValue[Field::F_PRIMARY]] |
159
|
|
|
= $fieldValue[$field->valueFieldName()]; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* FieldsTable constructor. |
167
|
|
|
* |
168
|
|
|
* @param QueryInterface $query Database query interface |
169
|
|
|
* @param integer $navigationID Navigation identifier for table structure |
170
|
|
|
* @param integer $materialID Table parent material identifier |
171
|
|
|
* @param string $locale Locale identifier |
172
|
|
|
*/ |
173
|
|
|
public function __construct(QueryInterface $query, $navigationID, $materialID, $locale = DEFAULT_LOCALE) |
174
|
|
|
{ |
175
|
|
|
$this->query = $query; |
176
|
|
|
$this->navigationID = $navigationID; |
177
|
|
|
$this->materialID = $materialID; |
178
|
|
|
$this->locale = $locale; |
179
|
|
|
|
180
|
|
|
$this->load(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.