|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* User: Jens |
|
4
|
|
|
* Date: 17-2-2017 |
|
5
|
|
|
* Time: 13:00 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace CloudControl\Cms\storage\entities; |
|
9
|
|
|
|
|
10
|
|
|
use CloudControl\Cms\components\CmsComponent; |
|
11
|
|
|
use CloudControl\Cms\storage\storage\DocumentStorage; |
|
12
|
|
|
use CloudControl\Cms\util\DocumentSorter; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Document |
|
16
|
|
|
* @package CloudControl\Cms\storage |
|
17
|
|
|
* @property array fields |
|
18
|
|
|
* @property array bricks |
|
19
|
|
|
* @property array dynamicBricks |
|
20
|
|
|
* @property array content |
|
21
|
|
|
* @property \PDO dbHandle |
|
22
|
|
|
* @property DocumentStorage documentStorage |
|
23
|
|
|
* @property boolean unpublishedChanges |
|
24
|
|
|
* @property \stdClass $documentContent Used in ApiComponent |
|
25
|
|
|
*/ |
|
26
|
|
|
class Document |
|
27
|
|
|
{ |
|
28
|
|
|
public $id; |
|
29
|
|
|
public $path; |
|
30
|
|
|
public $title; |
|
31
|
|
|
public $slug; |
|
32
|
|
|
public $type; |
|
33
|
|
|
public $documentType; |
|
34
|
|
|
public $documentTypeSlug; |
|
35
|
|
|
public $state; |
|
36
|
|
|
public $lastModificationDate; |
|
37
|
|
|
public $creationDate; |
|
38
|
|
|
public $lastModifiedBy; |
|
39
|
|
|
public $publicationDate; |
|
40
|
|
|
public $unpublishedChanges; |
|
41
|
|
|
protected $documentStorage; |
|
42
|
|
|
protected $fields; |
|
43
|
|
|
protected $bricks; |
|
44
|
|
|
protected $dynamicBricks; |
|
45
|
|
|
protected $content; |
|
46
|
|
|
|
|
47
|
|
|
protected $order = 'ASC'; |
|
48
|
|
|
protected $orderByField; |
|
49
|
|
|
|
|
50
|
|
|
protected $dbHandle; |
|
51
|
|
|
|
|
52
|
|
|
private static $JSON_ENCODED_FIELDS = array('fields', 'bricks', 'dynamicBricks'); |
|
53
|
|
|
|
|
54
|
|
|
public static $DOCUMENT_STATES = array('published', 'unpublished'); |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param $name |
|
58
|
|
|
* @return array|mixed |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __get($name) |
|
62
|
|
|
{ |
|
63
|
|
|
if (in_array($name, self::$JSON_ENCODED_FIELDS, true)) { |
|
64
|
|
|
return $this->getJsonEncodedField($name); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($name === 'content') { |
|
68
|
|
|
return $this->getContent(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($name === 'dbHandle') { |
|
72
|
|
|
throw new \RuntimeException('Trying to get protected property repository.'); |
|
73
|
|
|
} |
|
74
|
|
|
return $this->getPropertyIfExists($name); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $name |
|
79
|
|
|
* @param $value |
|
80
|
|
|
*/ |
|
81
|
|
|
public function __set($name, $value) |
|
82
|
|
|
{ |
|
83
|
|
|
if (in_array($name, self::$JSON_ENCODED_FIELDS, true)) { |
|
84
|
|
|
$this->$name = json_encode($value); |
|
85
|
|
|
} elseif ($name === 'content') { |
|
86
|
|
|
// Dont do anything for now.. |
|
87
|
|
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->$name = $value; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return array |
|
95
|
|
|
* @throws \Exception |
|
96
|
|
|
* @throws \RuntimeException |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getContent() |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->dbHandle === null) { |
|
101
|
|
|
throw new \RuntimeException('Document doesnt have a dbHandle handle. (path: ' . $this->path . ')'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ($this->content === null) { |
|
105
|
|
|
$slug = substr($this->path, 1); |
|
106
|
|
|
if (CmsComponent::isCmsLoggedIn()) { |
|
107
|
|
|
$docs = $this->documentStorage->getDocumentsWithState($this->path); |
|
108
|
|
|
} else { |
|
109
|
|
|
$docs = $this->documentStorage->getDocumentsBySlug($slug); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if ($this->orderByField !== null) { |
|
113
|
|
|
$docs = $this->orderContentByField($docs); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$this->content = $docs; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this->content; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function orderByField($fieldName, $order = 'ASC') |
|
123
|
|
|
{ |
|
124
|
|
|
$this->orderByField = $fieldName; |
|
125
|
|
|
$this->order = strtoupper($order) === 'ASC' ? 'ASC' : 'DESC'; |
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public function __toString() |
|
133
|
|
|
{ |
|
134
|
|
|
return 'Document:' . $this->title; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param $name |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
|
|
private function getPropertyIfExists($name) |
|
142
|
|
|
{ |
|
143
|
|
|
return isset($this->$name) ? $this->$name : array(''); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param $name |
|
148
|
|
|
* @return mixed |
|
149
|
|
|
*/ |
|
150
|
|
|
private function decodeJsonToFieldContainer($name) |
|
151
|
|
|
{ |
|
152
|
|
|
$stdObj = json_decode($this->$name); |
|
153
|
|
|
$temp = serialize($stdObj); |
|
154
|
|
|
$temp = preg_replace('@^O:8:"stdClass":@', |
|
155
|
|
|
'O:' . strlen(FieldContainer::class) . ':"' . FieldContainer::class . '":', $temp); |
|
156
|
|
|
return unserialize($temp); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param $name |
|
161
|
|
|
* @return array|mixed |
|
162
|
|
|
*/ |
|
163
|
|
|
private function getJsonEncodedField($name) |
|
164
|
|
|
{ |
|
165
|
|
|
if (isset($this->$name) && is_string($this->$name)) { |
|
166
|
|
|
return $this->decodeJsonToFieldContainer($name); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $this->getPropertyIfExists($name); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param array $docs |
|
174
|
|
|
* @return array |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function orderContentByField($docs) |
|
177
|
|
|
{ |
|
178
|
|
|
return DocumentSorter::sortDocumentsByField($docs, $this->orderByField, $this->order); |
|
179
|
|
|
} |
|
180
|
|
|
} |