|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by jensk on 26-3-2018. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace CloudControl\Cms\components; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
use CloudControl\Cms\cc\Application; |
|
10
|
|
|
use CloudControl\Cms\components\api\Response; |
|
11
|
|
|
use CloudControl\Cms\search\CharacterFilter; |
|
12
|
|
|
use CloudControl\Cms\search\results\SearchSuggestion; |
|
13
|
|
|
use CloudControl\Cms\search\Search; |
|
14
|
|
|
use CloudControl\Cms\search\Tokenizer; |
|
15
|
|
|
use CloudControl\Cms\storage\entities\Document; |
|
16
|
|
|
use CloudControl\Cms\storage\Storage; |
|
17
|
|
|
use CloudControl\Cms\storage\storage\DocumentStorage; |
|
18
|
|
|
|
|
19
|
|
|
class ApiComponent extends CachableBaseComponent |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Response |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $response; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Storage $storage |
|
28
|
|
|
*/ |
|
29
|
|
|
public function run(Storage $storage) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::run($storage); |
|
32
|
|
|
header('Content-Type: application/json'); |
|
33
|
|
|
header('Access-Control-Allow-Origin: *'); |
|
34
|
|
|
$this->response = $this->getResponse(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return Response |
|
39
|
|
|
*/ |
|
40
|
|
|
private function getResponse() |
|
41
|
|
|
{ |
|
42
|
|
|
try { |
|
43
|
|
|
if (isset($_GET['id'])) { |
|
44
|
|
|
return $this->getSingleDocumentResponse(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (isset($_GET['q'])) { |
|
48
|
|
|
return $this->getSearchDocumentsResponse(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this->getRootDocumentsResponse(); |
|
52
|
|
|
} catch (\Exception $e) { |
|
53
|
|
|
$error = $e->getFile() . ':' . $e->getLine() . ' ' . $e->getMessage(); |
|
54
|
|
|
return new Response(array(), false, $error); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return Document |
|
60
|
|
|
*/ |
|
61
|
|
|
private function getDocumentById() |
|
62
|
|
|
{ |
|
63
|
|
|
$id = intval($_GET['id']); |
|
64
|
|
|
$db = $this->storage->getRepository()->getContentRepository()->getContentDbHandle(); |
|
65
|
|
|
$stmt = $this->getPDOStatement($db, $this->getDocumentByIdSql($db, $id)); |
|
66
|
|
|
return $stmt->fetchObject(Document::class); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param \PDO $db |
|
71
|
|
|
* @param int $id |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
private function getDocumentByIdSql($db, $id) |
|
75
|
|
|
{ |
|
76
|
|
|
return 'SELECT * |
|
77
|
|
|
FROM documents_published |
|
78
|
|
|
WHERE id = ' . $db->quote($id) . ' |
|
79
|
|
|
'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param \PDO $db |
|
84
|
|
|
* @param string $sql |
|
85
|
|
|
* @return \PDOStatement |
|
86
|
|
|
*/ |
|
87
|
|
|
private function getPDOStatement($db, $sql) |
|
88
|
|
|
{ |
|
89
|
|
|
$stmt = $db->query($sql); |
|
90
|
|
|
if ($stmt === false) { |
|
91
|
|
|
$errorInfo = $db->errorInfo(); |
|
92
|
|
|
$errorMsg = $errorInfo[2]; |
|
93
|
|
|
throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
94
|
|
|
} |
|
95
|
|
|
return $stmt; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return Response |
|
100
|
|
|
*/ |
|
101
|
|
|
private function getSingleDocumentResponse() |
|
102
|
|
|
{ |
|
103
|
|
|
$document = $this->getDocumentById(); |
|
104
|
|
|
|
|
105
|
|
|
if ($document === false) { |
|
|
|
|
|
|
106
|
|
|
return new Response(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($document->type === 'folder') { |
|
110
|
|
|
return $this->getFolderResponse($document); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$documentContent = $this->getDocumentContent($document); |
|
114
|
|
|
$document->documentContent = $documentContent; |
|
115
|
|
|
|
|
116
|
|
|
return new Response($document); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param Application $application |
|
121
|
|
|
*/ |
|
122
|
|
|
public function render($application = null) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->renderedContent = $this->response; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return Response |
|
129
|
|
|
* @throws \Exception |
|
130
|
|
|
*/ |
|
131
|
|
|
private function getRootDocumentsResponse() |
|
132
|
|
|
{ |
|
133
|
|
|
$documents = $this->storage->getDocuments()->getDocuments('published'); |
|
134
|
|
|
return new Response($documents); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return Response |
|
139
|
|
|
* @throws \Exception |
|
140
|
|
|
*/ |
|
141
|
|
|
private function getSearchDocumentsResponse() |
|
142
|
|
|
{ |
|
143
|
|
|
$rawResults = $this->getRawResults(); |
|
144
|
|
|
$results = array(); |
|
145
|
|
|
$suggestions = array(); |
|
146
|
|
|
foreach ($rawResults as $rawResult) { |
|
147
|
|
|
if ($rawResult instanceof SearchSuggestion) { |
|
148
|
|
|
$suggestions[] = $rawResults; |
|
149
|
|
|
continue; |
|
150
|
|
|
} |
|
151
|
|
|
$result = $rawResult->getDocument(); |
|
152
|
|
|
$result->searchInfo = $rawResult; |
|
153
|
|
|
$results[] = $result; |
|
154
|
|
|
} |
|
155
|
|
|
$response = new Response($results); |
|
156
|
|
|
$response->searchSuggestions = $suggestions; |
|
157
|
|
|
return $response; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param Document $document |
|
162
|
|
|
* @return \stdClass |
|
163
|
|
|
*/ |
|
164
|
|
|
private function getDocumentContent($document) |
|
165
|
|
|
{ |
|
166
|
|
|
$documentContent = new \stdClass(); |
|
167
|
|
|
$documentContent->fields = $document->fields; |
|
168
|
|
|
$documentContent->bricks = $document->bricks; |
|
169
|
|
|
$documentContent->dynamicBricks = $document->dynamicBricks; |
|
170
|
|
|
return $documentContent; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return array |
|
175
|
|
|
* @throws \Exception |
|
176
|
|
|
*/ |
|
177
|
|
|
private function getRawResults() |
|
178
|
|
|
{ |
|
179
|
|
|
$filteredQuery = new CharacterFilter($_GET['q']); |
|
180
|
|
|
$tokenizer = new Tokenizer($filteredQuery); |
|
181
|
|
|
$search = new Search($this->storage); |
|
182
|
|
|
$rawResults = $search->getDocumentsForTokenizer($tokenizer); |
|
183
|
|
|
return $rawResults; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param Document $document |
|
188
|
|
|
* @return Response |
|
189
|
|
|
*/ |
|
190
|
|
|
private function getFolderResponse($document) |
|
191
|
|
|
{ |
|
192
|
|
|
$document->dbHandle = $this->storage->getContentDbHandle(); |
|
193
|
|
|
$document->documentStorage = new DocumentStorage($this->storage->getRepository()); |
|
194
|
|
|
$document->getContent(); |
|
195
|
|
|
$response = new Response($document->getContent()); |
|
196
|
|
|
$response->folder = $document->path; |
|
197
|
|
|
return $response; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
} |