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