@@ -19,227 +19,227 @@ discard block |
||
19 | 19 | |
20 | 20 | class Repository |
21 | 21 | { |
22 | - protected $storagePath; |
|
23 | - |
|
24 | - protected $fileBasedSubsets = array('sitemap', 'applicationComponents', 'documentTypes', 'bricks', 'imageSet', 'images', 'files', 'users'); |
|
25 | - |
|
26 | - protected $sitemap; |
|
27 | - protected $sitemapChanges = false; |
|
28 | - |
|
29 | - protected $applicationComponents; |
|
30 | - protected $applicationComponentsChanges = false; |
|
31 | - |
|
32 | - protected $documentTypes; |
|
33 | - protected $documentTypesChanges = false; |
|
34 | - |
|
35 | - protected $bricks; |
|
36 | - protected $bricksChanges = false; |
|
37 | - |
|
38 | - protected $imageSet; |
|
39 | - protected $imageSetChanges = false; |
|
40 | - |
|
41 | - protected $images; |
|
42 | - protected $imagesChanges = false; |
|
43 | - |
|
44 | - protected $files; |
|
45 | - protected $filesChanges = false; |
|
46 | - |
|
47 | - protected $users; |
|
48 | - protected $usersChanges = false; |
|
49 | - |
|
50 | - protected $contentDbHandle; |
|
51 | - |
|
52 | - /** |
|
53 | - * Repository constructor. |
|
54 | - * @param $storagePath |
|
55 | - * @throws \Exception |
|
56 | - */ |
|
57 | - public function __construct($storagePath) |
|
58 | - { |
|
59 | - $storagePath = realpath($storagePath); |
|
60 | - if (is_dir($storagePath) && $storagePath !== false) { |
|
61 | - $this->storagePath = $storagePath; |
|
62 | - } else { |
|
63 | - throw new \Exception('Repository not yet initialized.'); |
|
64 | - } |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Creates the folder in which to create all storage related files |
|
69 | - * |
|
70 | - * @param $storagePath |
|
71 | - * @return bool |
|
72 | - */ |
|
73 | - public static function create($storagePath) |
|
74 | - { |
|
75 | - return mkdir($storagePath); |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Initiates default storage |
|
80 | - * @throws \Exception |
|
81 | - */ |
|
82 | - public function init() |
|
83 | - { |
|
84 | - $storageDefaultPath = realpath('../library/cc/install/_storage.json'); |
|
85 | - $contentSqlPath = realpath('../library/cc/install/content.sql'); |
|
86 | - |
|
87 | - $this->initConfigStorage($storageDefaultPath); |
|
88 | - $this->initContentDb($contentSqlPath); |
|
89 | - |
|
90 | - $this->save(); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Load filebased subset and return it's contents |
|
95 | - * |
|
96 | - * @param $name |
|
97 | - * @return mixed|string |
|
98 | - * @throws \Exception |
|
99 | - */ |
|
100 | - public function __get($name) |
|
101 | - { |
|
102 | - if (isset($this->$name)) { |
|
103 | - if (in_array($name, $this->fileBasedSubsets)) { |
|
104 | - return $this->$name; |
|
105 | - } else { |
|
106 | - throw new \Exception('Trying to get undefined property from Repository: ' . $name); |
|
107 | - } |
|
108 | - } else { |
|
109 | - if (in_array($name, $this->fileBasedSubsets)) { |
|
110 | - return $this->loadSubset($name); |
|
111 | - } else { |
|
112 | - throw new \Exception('Trying to get undefined property from Repository: ' . $name); |
|
113 | - } |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Set filebased subset contents |
|
119 | - * @param $name |
|
120 | - * @param $value |
|
121 | - * @throws \Exception |
|
122 | - */ |
|
123 | - public function __set($name, $value) |
|
124 | - { |
|
125 | - if (in_array($name, $this->fileBasedSubsets)) { |
|
126 | - $this->$name = $value; |
|
127 | - $changes = $name . 'Changes'; |
|
128 | - $this->$changes = true; |
|
129 | - } else { |
|
130 | - throw new \Exception('Trying to persist unknown subset in repository: ' . $name . ' <br /><pre>' . print_r($value, true) . '</pre>'); |
|
131 | - } |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Persist all subsets |
|
136 | - */ |
|
137 | - public function save() |
|
138 | - { |
|
139 | - $this->sitemapChanges ? $this->saveSubset('sitemap') : null; |
|
140 | - $this->applicationComponentsChanges ? $this->saveSubset('applicationComponents') : null; |
|
141 | - $this->documentTypesChanges ? $this->saveSubset('documentTypes') : null; |
|
142 | - $this->bricksChanges ? $this->saveSubset('bricks') : null; |
|
143 | - $this->imageSetChanges ? $this->saveSubset('imageSet') : null; |
|
144 | - $this->imagesChanges ? $this->saveSubset('images') : null; |
|
145 | - $this->filesChanges ? $this->saveSubset('files') : null; |
|
146 | - $this->usersChanges ? $this->saveSubset('users') : null; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Persist subset to disk |
|
151 | - * @param $subset |
|
152 | - */ |
|
153 | - protected function saveSubset($subset) |
|
154 | - { |
|
155 | - $json = json_encode($this->$subset); |
|
156 | - $subsetStoragePath = $this->storagePath . DIRECTORY_SEPARATOR . $subset . '.json'; |
|
157 | - file_put_contents($subsetStoragePath, $json); |
|
158 | - $changes = $subset . 'Changes'; |
|
159 | - $this->$changes = false; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Load subset from disk |
|
164 | - * @param $subset |
|
165 | - * @return mixed|string |
|
166 | - */ |
|
167 | - protected function loadSubset($subset) |
|
168 | - { |
|
169 | - $subsetStoragePath = $this->storagePath . DIRECTORY_SEPARATOR . $subset . '.json'; |
|
170 | - $json = file_get_contents($subsetStoragePath); |
|
171 | - $json = json_decode($json); |
|
172 | - $this->$subset = $json; |
|
173 | - return $json; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * @param $contentSqlPath |
|
178 | - */ |
|
179 | - protected function initContentDb($contentSqlPath) |
|
180 | - { |
|
181 | - $db = $this->getContentDbHandle(); |
|
182 | - $sql = file_get_contents($contentSqlPath); |
|
183 | - $db->exec($sql); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * @param $storageDefaultPath |
|
188 | - */ |
|
189 | - protected function initConfigStorage($storageDefaultPath) |
|
190 | - { |
|
191 | - $json = file_get_contents($storageDefaultPath); |
|
192 | - $json = json_decode($json); |
|
193 | - $this->sitemap = $json->sitemap; |
|
194 | - $this->sitemapChanges = true; |
|
195 | - $this->applicationComponents = $json->applicationComponents; |
|
196 | - $this->applicationComponentsChanges = true; |
|
197 | - $this->documentTypes = $json->documentTypes; |
|
198 | - $this->documentTypesChanges = true; |
|
199 | - $this->bricks = $json->bricks; |
|
200 | - $this->bricksChanges = true; |
|
201 | - $this->imageSet = $json->imageSet; |
|
202 | - $this->imageSetChanges = true; |
|
203 | - $this->images = $json->images; |
|
204 | - $this->imagesChanges = true; |
|
205 | - $this->files = $json->files; |
|
206 | - $this->filesChanges = true; |
|
207 | - $this->users = $json->users; |
|
208 | - $this->usersChanges = true; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @return \PDO |
|
213 | - */ |
|
214 | - protected function getContentDbHandle() |
|
215 | - { |
|
216 | - if ($this->contentDbHandle === null) { |
|
217 | - $this->contentDbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'content.db'); |
|
218 | - } |
|
219 | - return $this->contentDbHandle; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Get all documents |
|
224 | - * @return array |
|
225 | - */ |
|
226 | - public function getDocuments() |
|
227 | - { |
|
228 | - return $this->getDocumentsByPath('/'); |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Get all documents and folders in a certain path |
|
233 | - * @param $folderPath |
|
234 | - * @return array |
|
235 | - * @throws \Exception |
|
236 | - */ |
|
237 | - public function getDocumentsByPath($folderPath) |
|
238 | - { |
|
239 | - $db = $this->getContentDbHandle(); |
|
240 | - $folderPathWithWildcard = $folderPath . '%'; |
|
241 | - |
|
242 | - $stmt = $this->getDbStatement(' |
|
22 | + protected $storagePath; |
|
23 | + |
|
24 | + protected $fileBasedSubsets = array('sitemap', 'applicationComponents', 'documentTypes', 'bricks', 'imageSet', 'images', 'files', 'users'); |
|
25 | + |
|
26 | + protected $sitemap; |
|
27 | + protected $sitemapChanges = false; |
|
28 | + |
|
29 | + protected $applicationComponents; |
|
30 | + protected $applicationComponentsChanges = false; |
|
31 | + |
|
32 | + protected $documentTypes; |
|
33 | + protected $documentTypesChanges = false; |
|
34 | + |
|
35 | + protected $bricks; |
|
36 | + protected $bricksChanges = false; |
|
37 | + |
|
38 | + protected $imageSet; |
|
39 | + protected $imageSetChanges = false; |
|
40 | + |
|
41 | + protected $images; |
|
42 | + protected $imagesChanges = false; |
|
43 | + |
|
44 | + protected $files; |
|
45 | + protected $filesChanges = false; |
|
46 | + |
|
47 | + protected $users; |
|
48 | + protected $usersChanges = false; |
|
49 | + |
|
50 | + protected $contentDbHandle; |
|
51 | + |
|
52 | + /** |
|
53 | + * Repository constructor. |
|
54 | + * @param $storagePath |
|
55 | + * @throws \Exception |
|
56 | + */ |
|
57 | + public function __construct($storagePath) |
|
58 | + { |
|
59 | + $storagePath = realpath($storagePath); |
|
60 | + if (is_dir($storagePath) && $storagePath !== false) { |
|
61 | + $this->storagePath = $storagePath; |
|
62 | + } else { |
|
63 | + throw new \Exception('Repository not yet initialized.'); |
|
64 | + } |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Creates the folder in which to create all storage related files |
|
69 | + * |
|
70 | + * @param $storagePath |
|
71 | + * @return bool |
|
72 | + */ |
|
73 | + public static function create($storagePath) |
|
74 | + { |
|
75 | + return mkdir($storagePath); |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Initiates default storage |
|
80 | + * @throws \Exception |
|
81 | + */ |
|
82 | + public function init() |
|
83 | + { |
|
84 | + $storageDefaultPath = realpath('../library/cc/install/_storage.json'); |
|
85 | + $contentSqlPath = realpath('../library/cc/install/content.sql'); |
|
86 | + |
|
87 | + $this->initConfigStorage($storageDefaultPath); |
|
88 | + $this->initContentDb($contentSqlPath); |
|
89 | + |
|
90 | + $this->save(); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Load filebased subset and return it's contents |
|
95 | + * |
|
96 | + * @param $name |
|
97 | + * @return mixed|string |
|
98 | + * @throws \Exception |
|
99 | + */ |
|
100 | + public function __get($name) |
|
101 | + { |
|
102 | + if (isset($this->$name)) { |
|
103 | + if (in_array($name, $this->fileBasedSubsets)) { |
|
104 | + return $this->$name; |
|
105 | + } else { |
|
106 | + throw new \Exception('Trying to get undefined property from Repository: ' . $name); |
|
107 | + } |
|
108 | + } else { |
|
109 | + if (in_array($name, $this->fileBasedSubsets)) { |
|
110 | + return $this->loadSubset($name); |
|
111 | + } else { |
|
112 | + throw new \Exception('Trying to get undefined property from Repository: ' . $name); |
|
113 | + } |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Set filebased subset contents |
|
119 | + * @param $name |
|
120 | + * @param $value |
|
121 | + * @throws \Exception |
|
122 | + */ |
|
123 | + public function __set($name, $value) |
|
124 | + { |
|
125 | + if (in_array($name, $this->fileBasedSubsets)) { |
|
126 | + $this->$name = $value; |
|
127 | + $changes = $name . 'Changes'; |
|
128 | + $this->$changes = true; |
|
129 | + } else { |
|
130 | + throw new \Exception('Trying to persist unknown subset in repository: ' . $name . ' <br /><pre>' . print_r($value, true) . '</pre>'); |
|
131 | + } |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Persist all subsets |
|
136 | + */ |
|
137 | + public function save() |
|
138 | + { |
|
139 | + $this->sitemapChanges ? $this->saveSubset('sitemap') : null; |
|
140 | + $this->applicationComponentsChanges ? $this->saveSubset('applicationComponents') : null; |
|
141 | + $this->documentTypesChanges ? $this->saveSubset('documentTypes') : null; |
|
142 | + $this->bricksChanges ? $this->saveSubset('bricks') : null; |
|
143 | + $this->imageSetChanges ? $this->saveSubset('imageSet') : null; |
|
144 | + $this->imagesChanges ? $this->saveSubset('images') : null; |
|
145 | + $this->filesChanges ? $this->saveSubset('files') : null; |
|
146 | + $this->usersChanges ? $this->saveSubset('users') : null; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Persist subset to disk |
|
151 | + * @param $subset |
|
152 | + */ |
|
153 | + protected function saveSubset($subset) |
|
154 | + { |
|
155 | + $json = json_encode($this->$subset); |
|
156 | + $subsetStoragePath = $this->storagePath . DIRECTORY_SEPARATOR . $subset . '.json'; |
|
157 | + file_put_contents($subsetStoragePath, $json); |
|
158 | + $changes = $subset . 'Changes'; |
|
159 | + $this->$changes = false; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Load subset from disk |
|
164 | + * @param $subset |
|
165 | + * @return mixed|string |
|
166 | + */ |
|
167 | + protected function loadSubset($subset) |
|
168 | + { |
|
169 | + $subsetStoragePath = $this->storagePath . DIRECTORY_SEPARATOR . $subset . '.json'; |
|
170 | + $json = file_get_contents($subsetStoragePath); |
|
171 | + $json = json_decode($json); |
|
172 | + $this->$subset = $json; |
|
173 | + return $json; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * @param $contentSqlPath |
|
178 | + */ |
|
179 | + protected function initContentDb($contentSqlPath) |
|
180 | + { |
|
181 | + $db = $this->getContentDbHandle(); |
|
182 | + $sql = file_get_contents($contentSqlPath); |
|
183 | + $db->exec($sql); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * @param $storageDefaultPath |
|
188 | + */ |
|
189 | + protected function initConfigStorage($storageDefaultPath) |
|
190 | + { |
|
191 | + $json = file_get_contents($storageDefaultPath); |
|
192 | + $json = json_decode($json); |
|
193 | + $this->sitemap = $json->sitemap; |
|
194 | + $this->sitemapChanges = true; |
|
195 | + $this->applicationComponents = $json->applicationComponents; |
|
196 | + $this->applicationComponentsChanges = true; |
|
197 | + $this->documentTypes = $json->documentTypes; |
|
198 | + $this->documentTypesChanges = true; |
|
199 | + $this->bricks = $json->bricks; |
|
200 | + $this->bricksChanges = true; |
|
201 | + $this->imageSet = $json->imageSet; |
|
202 | + $this->imageSetChanges = true; |
|
203 | + $this->images = $json->images; |
|
204 | + $this->imagesChanges = true; |
|
205 | + $this->files = $json->files; |
|
206 | + $this->filesChanges = true; |
|
207 | + $this->users = $json->users; |
|
208 | + $this->usersChanges = true; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @return \PDO |
|
213 | + */ |
|
214 | + protected function getContentDbHandle() |
|
215 | + { |
|
216 | + if ($this->contentDbHandle === null) { |
|
217 | + $this->contentDbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'content.db'); |
|
218 | + } |
|
219 | + return $this->contentDbHandle; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Get all documents |
|
224 | + * @return array |
|
225 | + */ |
|
226 | + public function getDocuments() |
|
227 | + { |
|
228 | + return $this->getDocumentsByPath('/'); |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Get all documents and folders in a certain path |
|
233 | + * @param $folderPath |
|
234 | + * @return array |
|
235 | + * @throws \Exception |
|
236 | + */ |
|
237 | + public function getDocumentsByPath($folderPath) |
|
238 | + { |
|
239 | + $db = $this->getContentDbHandle(); |
|
240 | + $folderPathWithWildcard = $folderPath . '%'; |
|
241 | + |
|
242 | + $stmt = $this->getDbStatement(' |
|
243 | 243 | SELECT * |
244 | 244 | FROM documents |
245 | 245 | WHERE `path` LIKE ' . $db->quote($folderPathWithWildcard) . ' |
@@ -248,119 +248,119 @@ discard block |
||
248 | 248 | ORDER BY `type` DESC, `path` ASC |
249 | 249 | '); |
250 | 250 | |
251 | - $documents = $stmt->fetchAll(\PDO::FETCH_CLASS, '\library\storage\Document'); |
|
252 | - foreach ($documents as $key => $document) { |
|
253 | - if ($document->type === 'folder') { |
|
254 | - $document->dbHandle = $db; |
|
255 | - $documents[$key] = $document; |
|
256 | - } |
|
257 | - } |
|
258 | - return $documents; |
|
259 | - } |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * @param $path |
|
264 | - * @return bool|Document |
|
265 | - */ |
|
266 | - public function getDocumentContainerByPath($path) |
|
267 | - { |
|
268 | - $document = $this->getDocumentByPath($path); |
|
269 | - if ($document === false) { |
|
270 | - return false; |
|
271 | - } |
|
272 | - $slugLength = strlen($document->slug); |
|
273 | - $containerPath = substr($path, 0, -$slugLength); |
|
274 | - if ($containerPath === '/') { |
|
275 | - return $this->getRootFolder(); |
|
276 | - } |
|
277 | - $containerFolder = $this->getDocumentByPath($containerPath); |
|
278 | - return $containerFolder; |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * @param $path |
|
283 | - * @return bool|Document |
|
284 | - */ |
|
285 | - public function getDocumentByPath($path) |
|
286 | - { |
|
287 | - $db = $this->getContentDbHandle(); |
|
288 | - $document = $this->fetchDocument(' |
|
251 | + $documents = $stmt->fetchAll(\PDO::FETCH_CLASS, '\library\storage\Document'); |
|
252 | + foreach ($documents as $key => $document) { |
|
253 | + if ($document->type === 'folder') { |
|
254 | + $document->dbHandle = $db; |
|
255 | + $documents[$key] = $document; |
|
256 | + } |
|
257 | + } |
|
258 | + return $documents; |
|
259 | + } |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * @param $path |
|
264 | + * @return bool|Document |
|
265 | + */ |
|
266 | + public function getDocumentContainerByPath($path) |
|
267 | + { |
|
268 | + $document = $this->getDocumentByPath($path); |
|
269 | + if ($document === false) { |
|
270 | + return false; |
|
271 | + } |
|
272 | + $slugLength = strlen($document->slug); |
|
273 | + $containerPath = substr($path, 0, -$slugLength); |
|
274 | + if ($containerPath === '/') { |
|
275 | + return $this->getRootFolder(); |
|
276 | + } |
|
277 | + $containerFolder = $this->getDocumentByPath($containerPath); |
|
278 | + return $containerFolder; |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * @param $path |
|
283 | + * @return bool|Document |
|
284 | + */ |
|
285 | + public function getDocumentByPath($path) |
|
286 | + { |
|
287 | + $db = $this->getContentDbHandle(); |
|
288 | + $document = $this->fetchDocument(' |
|
289 | 289 | SELECT * |
290 | 290 | FROM documents |
291 | 291 | WHERE path = ' . $db->quote($path) . ' |
292 | 292 | '); |
293 | - if ($document instanceof Document && $document->type === 'folder') { |
|
294 | - $document->dbHandle = $db; |
|
295 | - } |
|
296 | - return $document; |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * Return the results of the query as array of Documents |
|
301 | - * @param $sql |
|
302 | - * @return array |
|
303 | - * @throws \Exception |
|
304 | - */ |
|
305 | - protected function fetchAllDocuments($sql) |
|
306 | - { |
|
307 | - $stmt = $this->getDbStatement($sql); |
|
308 | - return $stmt->fetchAll(\PDO::FETCH_CLASS, '\library\storage\Document'); |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Return the result of the query as Document |
|
313 | - * @param $sql |
|
314 | - * @return mixed |
|
315 | - * @throws \Exception |
|
316 | - */ |
|
317 | - protected function fetchDocument($sql) |
|
318 | - { |
|
319 | - $stmt = $this->getDbStatement($sql); |
|
320 | - return $stmt->fetchObject('\library\storage\Document'); |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * Prepare the sql statement |
|
325 | - * @param $sql |
|
326 | - * @return \PDOStatement |
|
327 | - * @throws \Exception |
|
328 | - */ |
|
329 | - protected function getDbStatement($sql) |
|
330 | - { |
|
331 | - $db = $this->getContentDbHandle(); |
|
332 | - $stmt = $db->query($sql); |
|
333 | - if ($stmt === false) { |
|
334 | - $errorInfo = $db->errorInfo(); |
|
335 | - $errorMsg = $errorInfo[2]; |
|
336 | - throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
337 | - } |
|
338 | - return $stmt; |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * Create a (non-existent) root folder Document and return it |
|
343 | - * @return Document |
|
344 | - */ |
|
345 | - protected function getRootFolder() |
|
346 | - { |
|
347 | - $rootFolder = new Document(); |
|
348 | - $rootFolder->path = '/'; |
|
349 | - $rootFolder->type = 'folder'; |
|
350 | - return $rootFolder; |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * Save the document to the database |
|
355 | - * @param Document $documentObject |
|
356 | - * @return bool |
|
357 | - * @throws \Exception |
|
358 | - * @internal param $path |
|
359 | - */ |
|
360 | - public function saveDocument($documentObject) |
|
361 | - { |
|
362 | - $db = $this->getContentDbHandle(); |
|
363 | - $stmt = $this->getDbStatement(' |
|
293 | + if ($document instanceof Document && $document->type === 'folder') { |
|
294 | + $document->dbHandle = $db; |
|
295 | + } |
|
296 | + return $document; |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * Return the results of the query as array of Documents |
|
301 | + * @param $sql |
|
302 | + * @return array |
|
303 | + * @throws \Exception |
|
304 | + */ |
|
305 | + protected function fetchAllDocuments($sql) |
|
306 | + { |
|
307 | + $stmt = $this->getDbStatement($sql); |
|
308 | + return $stmt->fetchAll(\PDO::FETCH_CLASS, '\library\storage\Document'); |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Return the result of the query as Document |
|
313 | + * @param $sql |
|
314 | + * @return mixed |
|
315 | + * @throws \Exception |
|
316 | + */ |
|
317 | + protected function fetchDocument($sql) |
|
318 | + { |
|
319 | + $stmt = $this->getDbStatement($sql); |
|
320 | + return $stmt->fetchObject('\library\storage\Document'); |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * Prepare the sql statement |
|
325 | + * @param $sql |
|
326 | + * @return \PDOStatement |
|
327 | + * @throws \Exception |
|
328 | + */ |
|
329 | + protected function getDbStatement($sql) |
|
330 | + { |
|
331 | + $db = $this->getContentDbHandle(); |
|
332 | + $stmt = $db->query($sql); |
|
333 | + if ($stmt === false) { |
|
334 | + $errorInfo = $db->errorInfo(); |
|
335 | + $errorMsg = $errorInfo[2]; |
|
336 | + throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
337 | + } |
|
338 | + return $stmt; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * Create a (non-existent) root folder Document and return it |
|
343 | + * @return Document |
|
344 | + */ |
|
345 | + protected function getRootFolder() |
|
346 | + { |
|
347 | + $rootFolder = new Document(); |
|
348 | + $rootFolder->path = '/'; |
|
349 | + $rootFolder->type = 'folder'; |
|
350 | + return $rootFolder; |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * Save the document to the database |
|
355 | + * @param Document $documentObject |
|
356 | + * @return bool |
|
357 | + * @throws \Exception |
|
358 | + * @internal param $path |
|
359 | + */ |
|
360 | + public function saveDocument($documentObject) |
|
361 | + { |
|
362 | + $db = $this->getContentDbHandle(); |
|
363 | + $stmt = $this->getDbStatement(' |
|
364 | 364 | INSERT OR REPLACE INTO documents (`path`,`title`,`slug`,`type`,`documentType`,`documentTypeSlug`,`state`,`lastModificationDate`,`creationDate`,`lastModifiedBy`,`fields`,`bricks`,`dynamicBricks`) |
365 | 365 | VALUES( |
366 | 366 | ' . $db->quote($documentObject->path) . ', |
@@ -378,37 +378,37 @@ discard block |
||
378 | 378 | ' . $db->quote(json_encode($documentObject->dynamicBricks)) . ' |
379 | 379 | ) |
380 | 380 | '); |
381 | - $result = $stmt->execute(); |
|
382 | - return $result; |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Delete the document from the database |
|
387 | - * If it's a folder, also delete it's contents |
|
388 | - * @param $path |
|
389 | - * @throws \Exception |
|
390 | - */ |
|
391 | - public function deleteDocumentByPath($path) |
|
392 | - { |
|
393 | - $db = $this->getContentDbHandle(); |
|
394 | - $documentToDelete = $this->getDocumentByPath($path); |
|
395 | - if ($documentToDelete instanceof Document) { |
|
396 | - if ($documentToDelete->type == 'document') { |
|
397 | - $stmt = $this->getDbStatement(' |
|
381 | + $result = $stmt->execute(); |
|
382 | + return $result; |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Delete the document from the database |
|
387 | + * If it's a folder, also delete it's contents |
|
388 | + * @param $path |
|
389 | + * @throws \Exception |
|
390 | + */ |
|
391 | + public function deleteDocumentByPath($path) |
|
392 | + { |
|
393 | + $db = $this->getContentDbHandle(); |
|
394 | + $documentToDelete = $this->getDocumentByPath($path); |
|
395 | + if ($documentToDelete instanceof Document) { |
|
396 | + if ($documentToDelete->type == 'document') { |
|
397 | + $stmt = $this->getDbStatement(' |
|
398 | 398 | DELETE FROM documents |
399 | 399 | WHERE path = ' . $db->quote($path) . ' |
400 | 400 | '); |
401 | - $stmt->execute(); |
|
402 | - } elseif ($documentToDelete->type == 'folder') { |
|
403 | - $folderPathWithWildcard = $path . '%'; |
|
404 | - $stmt = $this->getDbStatement(' |
|
401 | + $stmt->execute(); |
|
402 | + } elseif ($documentToDelete->type == 'folder') { |
|
403 | + $folderPathWithWildcard = $path . '%'; |
|
404 | + $stmt = $this->getDbStatement(' |
|
405 | 405 | DELETE FROM documents |
406 | 406 | WHERE (path LIKE ' . $db->quote($folderPathWithWildcard) . ' |
407 | 407 | AND substr(`path`, ' . (strlen($path) + 1) . ', 1) = "/") |
408 | 408 | OR path = ' . $db->quote($path) . ' |
409 | 409 | '); |
410 | - $stmt->execute(); |
|
411 | - } |
|
412 | - } |
|
413 | - } |
|
410 | + $stmt->execute(); |
|
411 | + } |
|
412 | + } |
|
413 | + } |
|
414 | 414 | } |
415 | 415 | \ No newline at end of file |