1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: jensk |
4
|
|
|
* Date: 13-3-2017 |
5
|
|
|
* Time: 16:24 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace CloudControl\Cms\storage\factories; |
9
|
|
|
|
10
|
|
|
use CloudControl\Cms\storage\entities\Document; |
11
|
|
|
use CloudControl\Cms\storage\factories\documentfactory\Sanitizer; |
12
|
|
|
use CloudControl\Cms\storage\storage\DocumentTypesStorage; |
13
|
|
|
use CloudControl\Cms\util\GlobalFunctions; |
14
|
|
|
use CloudControl\Cms\util\StringUtil; |
15
|
|
|
|
16
|
|
|
class DocumentFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param array $postValues |
20
|
|
|
* @param DocumentTypesStorage $documentTypesStorage |
21
|
|
|
* |
22
|
|
|
* @return \CloudControl\Cms\storage\entities\Document |
23
|
|
|
*/ |
24
|
|
|
public static function createDocumentFromPostValues($postValues, DocumentTypesStorage $documentTypesStorage) |
25
|
|
|
{ |
26
|
|
|
$postValues = GlobalFunctions::utf8Convert($postValues); |
27
|
|
|
$documentType = $documentTypesStorage->getDocumentTypeBySlug($postValues['documentType']); |
28
|
|
|
|
29
|
|
|
$staticBricks = $documentType->bricks; |
30
|
|
|
|
31
|
|
|
$documentObj = self::createInitialDocumentObject($postValues, $documentType); |
32
|
|
|
|
33
|
|
|
$fields = Sanitizer::sanitizeFields($postValues, $documentType); |
34
|
|
|
|
35
|
|
|
$documentObj->fields = $fields; |
36
|
|
|
$documentObj->bricks = array(); |
37
|
|
|
|
38
|
|
|
$documentObj = self::createBrickArrayForDocument($postValues, $documentObj, $staticBricks); |
39
|
|
|
$documentObj = self::createDynamicBrickArrayForDocument($postValues, $documentObj); |
40
|
|
|
|
41
|
|
|
return $documentObj; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $postValues |
46
|
|
|
* @param \stdClass $documentType |
47
|
|
|
* |
48
|
|
|
* @return Document |
49
|
|
|
*/ |
50
|
|
|
private static function createInitialDocumentObject($postValues, $documentType) |
51
|
|
|
{ |
52
|
|
|
$documentObj = new Document(); |
53
|
|
|
$documentObj->title = $postValues['title']; |
54
|
|
|
$documentObj->slug = StringUtil::slugify($postValues['title']); |
55
|
|
|
$documentObj->type = 'document'; |
56
|
|
|
$documentObj->documentType = $documentType->title; |
57
|
|
|
$documentObj->documentTypeSlug = $documentType->slug; |
58
|
|
|
$documentObj->state = isset($postValues['state']) ? 'published' : 'unpublished'; |
59
|
|
|
$documentObj->lastModificationDate = time(); |
60
|
|
|
$documentObj->creationDate = isset($postValues['creationDate']) ? (int)$postValues['creationDate'] : time(); |
61
|
|
|
$documentObj->lastModifiedBy = $_SESSION['cloudcontrol']->username; |
62
|
|
|
|
63
|
|
|
return $documentObj; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $postValues |
68
|
|
|
* @param Document $documentObj |
69
|
|
|
* @param array $staticBricks |
70
|
|
|
* |
71
|
|
|
* @return Document |
72
|
|
|
*/ |
73
|
|
|
private static function createBrickArrayForDocument($postValues, $documentObj, $staticBricks) |
74
|
|
|
{ |
75
|
|
|
if (isset($postValues['bricks'])) { |
76
|
|
|
foreach ($postValues['bricks'] as $brickSlug => $brick) { |
77
|
|
|
// Find the current bricktype and check if its multiple |
78
|
|
|
list($staticBrick, $multiple) = self::getStaticBrickAndSetMultiple($staticBricks, $brickSlug); |
79
|
|
|
|
80
|
|
|
if ($multiple) { |
81
|
|
|
$documentObj = self::addMultipleBricks($documentObj, $brick, $staticBrick, $brickSlug); |
82
|
|
|
} else { |
83
|
|
|
$documentObj = self::addSingleBrick($documentObj, $brick, $brickSlug); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
return $documentObj; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $postValues |
92
|
|
|
* @param Document $documentObj |
93
|
|
|
* |
94
|
|
|
* @return Document |
95
|
|
|
*/ |
96
|
|
|
private static function createDynamicBrickArrayForDocument($postValues, $documentObj) |
97
|
|
|
{ |
98
|
|
|
$documentObj->dynamicBricks = array(); |
99
|
|
|
if (isset($postValues['dynamicBricks'])) { |
100
|
|
|
foreach ($postValues['dynamicBricks'] as $brickTypeSlug => $brick) { |
101
|
|
|
foreach ($brick as $brickContent) { |
102
|
|
|
$brickObj = new \stdClass(); |
103
|
|
|
$brickObj->type = $brickTypeSlug; |
104
|
|
|
$brickObj->fields = Sanitizer::sanitizeBrickContent($brickContent); |
105
|
|
|
$dynamicBricks = $documentObj->dynamicBricks; |
106
|
|
|
$dynamicBricks[] = $brickObj; |
107
|
|
|
$documentObj->dynamicBricks = $dynamicBricks; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
return $documentObj; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $staticBricks |
116
|
|
|
* @param $brickSlug |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
private static function getStaticBrickAndSetMultiple($staticBricks, $brickSlug) |
121
|
|
|
{ |
122
|
|
|
$staticBrick = null; |
123
|
|
|
$multiple = false; |
124
|
|
|
foreach ($staticBricks as $staticBrick) { |
125
|
|
|
if ($staticBrick->slug === $brickSlug) { |
126
|
|
|
$multiple = $staticBrick->multiple; |
127
|
|
|
break; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return array($staticBrick, $multiple); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $staticBrick |
136
|
|
|
* @param $brickInstance |
137
|
|
|
* |
138
|
|
|
* @return \stdClass |
139
|
|
|
*/ |
140
|
|
|
private static function createBrick($staticBrick, $brickInstance) |
141
|
|
|
{ |
142
|
|
|
$brickObj = new \stdClass(); |
143
|
|
|
$brickObj->fields = new \stdClass(); |
144
|
|
|
$brickObj->type = $staticBrick->brickSlug; |
145
|
|
|
|
146
|
|
|
foreach ($brickInstance['fields'] as $fieldName => $fieldValues) { |
147
|
|
|
$purifier = Sanitizer::getPurifier(); |
148
|
|
|
foreach ($fieldValues as $fieldKey => $value) { |
149
|
|
|
$fieldValues[$fieldKey] = $purifier->purify($value); |
150
|
|
|
} |
151
|
|
|
$brickObj->fields->$fieldName = $fieldValues; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $brickObj; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $documentObj |
159
|
|
|
* @param $brick |
160
|
|
|
* @param $staticBrick |
161
|
|
|
* @param $brickSlug |
162
|
|
|
* |
163
|
|
|
* @return mixed |
164
|
|
|
*/ |
165
|
|
|
private static function addMultipleBricks($documentObj, $brick, $staticBrick, $brickSlug) |
166
|
|
|
{ |
167
|
|
|
$brickArray = array(); |
168
|
|
|
foreach ($brick as $brickInstance) { |
169
|
|
|
$brickObj = self::createBrick($staticBrick, $brickInstance); |
170
|
|
|
$brickArray[] = $brickObj; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$bricks = $documentObj->bricks; |
174
|
|
|
$bricks[$brickSlug] = $brickArray; |
175
|
|
|
$documentObj->bricks = $bricks; |
176
|
|
|
|
177
|
|
|
return $documentObj; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param $documentObj |
182
|
|
|
* @param $brick |
183
|
|
|
* @param $brickSlug |
184
|
|
|
* |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
|
|
private static function addSingleBrick($documentObj, $brick, $brickSlug) |
188
|
|
|
{ |
189
|
|
|
$bricks = $documentObj->bricks; |
190
|
|
|
|
191
|
|
|
$purifier = Sanitizer::getPurifier(); |
192
|
|
|
foreach ($brick['fields'] as $fieldKey => $values) { |
193
|
|
|
foreach ($values as $valueKey => $value) { |
194
|
|
|
$values[$valueKey] = $purifier->purify($value); |
195
|
|
|
} |
196
|
|
|
$brick['fields'][$fieldKey] = $values; |
197
|
|
|
} |
198
|
|
|
$bricks[$brickSlug] = $brick; |
199
|
|
|
|
200
|
|
|
$documentObj->bricks = $bricks; |
201
|
|
|
return $documentObj; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
|
206
|
|
|
|
207
|
|
|
|
208
|
|
|
|
209
|
|
|
|
210
|
|
|
|
211
|
|
|
} |