|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by jensk on 17-3-2017. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace library\storage\storage; |
|
7
|
|
|
|
|
8
|
|
|
use library\storage\Document; |
|
9
|
|
|
use library\storage\factories\DocumentFactory; |
|
10
|
|
|
|
|
11
|
|
|
class DocumentStorage extends AbstractStorage |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Get documents |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $state |
|
17
|
|
|
* |
|
18
|
|
|
* @return array |
|
19
|
|
|
* @throws \Exception |
|
20
|
|
|
*/ |
|
21
|
|
|
public function getDocuments($state = 'published') |
|
22
|
|
|
{ |
|
23
|
|
|
if (!in_array($state, Document::$DOCUMENT_STATES)) { |
|
24
|
|
|
throw new \Exception('Unsupported document state: ' . $state); |
|
25
|
|
|
} |
|
26
|
|
|
return $this->repository->getDocuments($state); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getDocumentsWithState($folderPath = '/') |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->repository->getDocumentsWithState($folderPath); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return int |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getTotalDocumentCount() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->repository->getTotalDocumentCount(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $slug |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $state |
|
46
|
|
|
* |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
* @throws \Exception |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getDocumentBySlug($slug, $state = 'published') |
|
51
|
|
|
{ |
|
52
|
|
|
if (!in_array($state, Document::$DOCUMENT_STATES)) { |
|
53
|
|
|
throw new \Exception('Unsupported document state: ' . $state); |
|
54
|
|
|
} |
|
55
|
|
|
$path = '/' . $slug; |
|
56
|
|
|
|
|
57
|
|
|
return $this->repository->getDocumentByPath($path, $state); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param $postValues |
|
62
|
|
|
* @param $state |
|
63
|
|
|
* |
|
64
|
|
|
* @throws \Exception |
|
65
|
|
|
*/ |
|
66
|
|
|
public function saveDocument($postValues, $state = 'unpublished') |
|
67
|
|
|
{ |
|
68
|
|
|
if (!in_array($state, Document::$DOCUMENT_STATES)) { |
|
69
|
|
|
throw new \Exception('Unsupported document state: ' . $state); |
|
70
|
|
|
} |
|
71
|
|
|
$oldPath = '/' . $postValues['path']; |
|
72
|
|
|
|
|
73
|
|
|
$container = $this->getDocumentContainerByPath($oldPath); |
|
74
|
|
|
$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, new DocumentTypesStorage($this->repository)); |
|
75
|
|
|
if ($container->path === '/') { |
|
76
|
|
|
$newPath = $container->path . $documentObject->slug; |
|
77
|
|
|
} else { |
|
78
|
|
|
$newPath = $container->path . '/' . $documentObject->slug; |
|
79
|
|
|
} |
|
80
|
|
|
$documentObject->path = $newPath; |
|
81
|
|
|
$this->repository->saveDocument($documentObject, $state); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param $postValues |
|
86
|
|
|
* @param string $state |
|
87
|
|
|
*/ |
|
88
|
|
|
public function addDocument($postValues, $state = 'unpublished') |
|
89
|
|
|
{ |
|
90
|
|
|
$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, new DocumentTypesStorage($this->repository)); |
|
91
|
|
|
if ($postValues['path'] === '/') { |
|
92
|
|
|
$documentObject->path = $postValues['path'] . $documentObject->slug; |
|
93
|
|
|
} else { |
|
94
|
|
|
$documentObject->path = $postValues['path'] . '/' . $documentObject->slug; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->repository->saveDocument($documentObject, $state); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param $slug |
|
102
|
|
|
*/ |
|
103
|
|
|
public function deleteDocumentBySlug($slug) |
|
104
|
|
|
{ |
|
105
|
|
|
$path = '/' . $slug; |
|
106
|
|
|
$this->repository->deleteDocumentByPath($path); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns the folder containing the document |
|
111
|
|
|
* |
|
112
|
|
|
* @param $path |
|
113
|
|
|
* |
|
114
|
|
|
* @return bool|\library\storage\Document |
|
115
|
|
|
* @throws \Exception |
|
116
|
|
|
*/ |
|
117
|
|
|
private function getDocumentContainerByPath($path) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->repository->getDocumentContainerByPath($path); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getPublishedDocumentsNoFolders() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->repository->getPublishedDocumentsNoFolders(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
} |