|
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\storage\storage\DocumentStorage; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Document |
|
14
|
|
|
* @package CloudControl\Cms\storage |
|
15
|
|
|
* @property array fields |
|
16
|
|
|
* @property array bricks |
|
17
|
|
|
* @property array dynamicBricks |
|
18
|
|
|
* @property array content |
|
19
|
|
|
* @property \PDO dbHandle |
|
20
|
|
|
* @property DocumentStorage documentStorage |
|
21
|
|
|
* @property boolean unpublishedChanges |
|
22
|
|
|
*/ |
|
23
|
|
|
class Document |
|
24
|
|
|
{ |
|
25
|
|
|
public $id; |
|
26
|
|
|
public $path; |
|
27
|
|
|
public $title; |
|
28
|
|
|
public $slug; |
|
29
|
|
|
public $type; |
|
30
|
|
|
public $documentType; |
|
31
|
|
|
public $documentTypeSlug; |
|
32
|
|
|
public $state; |
|
33
|
|
|
public $lastModificationDate; |
|
34
|
|
|
public $creationDate; |
|
35
|
|
|
public $lastModifiedBy; |
|
36
|
|
|
protected $fields; |
|
37
|
|
|
protected $bricks; |
|
38
|
|
|
protected $dynamicBricks; |
|
39
|
|
|
protected $content; |
|
40
|
|
|
|
|
41
|
|
|
protected $dbHandle; |
|
42
|
|
|
|
|
43
|
|
|
protected $jsonEncodedFields = array('fields', 'bricks', 'dynamicBricks'); |
|
44
|
|
|
protected $orderableFields = array('title', 'slug', 'type', 'documentType', 'documentTypeSlug', 'state', 'lastModificationDate', 'creationDate', 'lastModifiedBy'); |
|
45
|
|
|
|
|
46
|
|
|
public static $DOCUMENT_STATES = array('published', 'unpublished'); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param $name |
|
50
|
|
|
* @return array|mixed |
|
51
|
|
|
* @throws \Exception |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __get($name) |
|
54
|
|
|
{ |
|
55
|
|
|
if (in_array($name, $this->jsonEncodedFields, true)) { |
|
56
|
|
|
return $this->getJsonEncodedField($name); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($name === 'content') { |
|
60
|
|
|
return $this->getContent(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ($name === 'dbHandle') { |
|
64
|
|
|
throw new \RuntimeException('Trying to get protected property repository.'); |
|
65
|
|
|
} |
|
66
|
|
|
return $this->getPropertyIfExists($name); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $name |
|
71
|
|
|
* @param $value |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __set($name, $value) |
|
74
|
|
|
{ |
|
75
|
|
|
if (in_array($name, $this->jsonEncodedFields, true)) { |
|
76
|
|
|
$this->$name = json_encode($value); |
|
77
|
|
|
} elseif ($name === 'content') { |
|
78
|
|
|
// Dont do anything for now.. |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->$name = $value; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return array |
|
87
|
|
|
* @throws \RuntimeException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getContent() |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->dbHandle === null) { |
|
92
|
|
|
throw new \RuntimeException('Document doesnt have a dbHandle handle. (path: ' . $this->path . ')'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if ($this->content === null) { |
|
96
|
|
|
$docs = $this->documentStorage->getDocumentsWithState($this->path); |
|
97
|
|
|
$this->content = $docs; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->content; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function __toString() |
|
107
|
|
|
{ |
|
108
|
|
|
return 'Document:' . $this->title; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param $name |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
private function getPropertyIfExists($name) |
|
116
|
|
|
{ |
|
117
|
|
|
return isset($this->$name) ? $this->$name : array(''); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param $name |
|
122
|
|
|
* @return mixed |
|
123
|
|
|
*/ |
|
124
|
|
|
private function decodeJsonToFieldContainer($name) |
|
125
|
|
|
{ |
|
126
|
|
|
$stdObj = json_decode($this->$name); |
|
127
|
|
|
$temp = serialize($stdObj); |
|
128
|
|
|
$temp = preg_replace('@^O:8:"stdClass":@', 'O:' . strlen(FieldContainer::class) . ':"' . FieldContainer::class . '":', $temp); |
|
129
|
|
|
return unserialize($temp); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param $name |
|
134
|
|
|
* @return array|mixed |
|
135
|
|
|
*/ |
|
136
|
|
|
private function getJsonEncodedField($name) |
|
137
|
|
|
{ |
|
138
|
|
|
if (isset($this->$name) && is_string($this->$name)) { |
|
139
|
|
|
return $this->decodeJsonToFieldContainer($name); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $this->getPropertyIfExists($name); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
} |