ContentApiController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use Typemill\Models\Folder;
  6. use Typemill\Models\Write;
  7. use Typemill\Extensions\ParsedownExtension;
  8. class ContentApiController extends ContentController
  9. {
  10. public function publishArticle(Request $request, Response $response, $args)
  11. {
  12. # get params from call
  13. $this->params = $request->getParams();
  14. $this->uri = $request->getUri();
  15. # validate input only if raw mode
  16. if($this->params['raw'])
  17. {
  18. if(!$this->validateEditorInput()){ return $response->withJson($this->errors,422); }
  19. }
  20. # set structure
  21. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  22. # set item
  23. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  24. # set the status for published and drafted
  25. $this->setPublishStatus();
  26. # set path
  27. $this->setItemPath($this->item->fileType);
  28. # if raw mode, use the content from request
  29. if($this->params['raw'])
  30. {
  31. $this->content = '# ' . $this->params['title'] . "\r\n\r\n" . $this->params['content'];
  32. }
  33. else
  34. {
  35. # read content from file
  36. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  37. # If it is a draft, then create clean markdown content
  38. if(is_array($this->content))
  39. {
  40. # initialize parsedown extension
  41. $parsedown = new ParsedownExtension();
  42. # turn markdown into an array of markdown-blocks
  43. $this->content = $parsedown->arrayBlocksToMarkdown($this->content);
  44. }
  45. }
  46. # set path for the file (or folder)
  47. $this->setItemPath('md');
  48. # update the file
  49. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $this->content))
  50. {
  51. # update the file
  52. $delete = $this->deleteContentFiles(['txt']);
  53. # update the internal structure
  54. $this->setStructure($draft = true, $cache = false);
  55. # update the public structure
  56. $this->setStructure($draft = false, $cache = false);
  57. return $response->withJson(['success'], 200);
  58. }
  59. else
  60. {
  61. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  62. }
  63. }
  64. public function unpublishArticle(Request $request, Response $response, $args)
  65. {
  66. # get params from call
  67. $this->params = $request->getParams();
  68. $this->uri = $request->getUri();
  69. # set structure
  70. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  71. # set item
  72. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  73. # set the status for published and drafted
  74. $this->setPublishStatus();
  75. # check if draft exists, if not, create one.
  76. if(!$this->item->drafted)
  77. {
  78. # set path for the file (or folder)
  79. $this->setItemPath('md');
  80. # set content of markdown-file
  81. if(!$this->setContent()){ return $response->withJson($this->errors, 404); }
  82. # initialize parsedown extension
  83. $parsedown = new ParsedownExtension();
  84. # turn markdown into an array of markdown-blocks
  85. $contentArray = $parsedown->markdownToArrayBlocks($this->content);
  86. # encode the content into json
  87. $contentJson = json_encode($contentArray);
  88. # set path for the file (or folder)
  89. $this->setItemPath('txt');
  90. /* update the file */
  91. if(!$this->write->writeFile($this->settings['contentFolder'], $this->path, $contentJson))
  92. {
  93. return $response->withJson(['errors' => ['message' => 'Could not create a draft of the page. Please check if the folder is writable']], 404);
  94. }
  95. }
  96. # update the file
  97. $delete = $this->deleteContentFiles(['md']);
  98. if($delete)
  99. {
  100. # update the internal structure
  101. $this->setStructure($draft = true, $cache = false);
  102. # update the live structure
  103. $this->setStructure($draft = false, $cache = false);
  104. return $response->withJson(['success'], 200);
  105. }
  106. else
  107. {
  108. return $response->withJson(['errors' => ['message' => "Could not delete some files. Please check if the files exists and are writable"]], 404);
  109. }
  110. }
  111. public function deleteArticle(Request $request, Response $response, $args)
  112. {
  113. # get params from call
  114. $this->params = $request->getParams();
  115. $this->uri = $request->getUri();
  116. # set url to base path initially
  117. $url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'];
  118. # set structure
  119. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  120. # set item
  121. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  122. if($this->item->elementType == 'file')
  123. {
  124. $delete = $this->deleteContentFiles(['md','txt']);
  125. }
  126. elseif($this->item->elementType == 'folder')
  127. {
  128. $delete = $this->deleteContentFolder();
  129. }
  130. if($delete)
  131. {
  132. # check if it is a subfile or subfolder and set the redirect-url to the parent item
  133. if(count($this->item->keyPathArray) > 1)
  134. {
  135. # get the parent item
  136. $parentItem = Folder::getParentItem($this->structure, $this->item->keyPathArray);
  137. if($parentItem)
  138. {
  139. # an active file has been moved to another folder
  140. $url .= $parentItem->urlRelWoF;
  141. }
  142. }
  143. # update the live structure
  144. $this->setStructure($draft = false, $cache = false);
  145. #update the backend structure
  146. $this->setStructure($draft = true, $cache = false);
  147. return $response->withJson(array('data' => $this->structure, 'errors' => false, 'url' => $url), 200);
  148. }
  149. else
  150. {
  151. return $response->withJson(array('data' => $this->structure, 'errors' => $this->errors), 404);
  152. }
  153. }
  154. public function updateArticle(Request $request, Response $response, $args)
  155. {
  156. # get params from call
  157. $this->params = $request->getParams();
  158. $this->uri = $request->getUri();
  159. # validate input
  160. if(!$this->validateEditorInput()){ return $response->withJson($this->errors,422); }
  161. # set structure
  162. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  163. # set item
  164. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  165. # set path for the file (or folder)
  166. $this->setItemPath('txt');
  167. # merge title with content for complete markdown document
  168. $updatedContent = '# ' . $this->params['title'] . "\r\n\r\n" . $this->params['content'];
  169. # initialize parsedown extension
  170. $parsedown = new ParsedownExtension();
  171. # turn markdown into an array of markdown-blocks
  172. $contentArray = $parsedown->markdownToArrayBlocks($updatedContent);
  173. # encode the content into json
  174. $contentJson = json_encode($contentArray);
  175. /* update the file */
  176. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $contentJson))
  177. {
  178. # update the internal structure
  179. $this->setStructure($draft = true, $cache = false);
  180. return $response->withJson(['success'], 200);
  181. }
  182. else
  183. {
  184. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  185. }
  186. }
  187. public function sortArticle(Request $request, Response $response, $args)
  188. {
  189. # get params from call
  190. $this->params = $request->getParams();
  191. $this->uri = $request->getUri();
  192. # url is only needed, if an active page is moved
  193. $url = false;
  194. # set structure
  195. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
  196. # validate input
  197. if(!$this->validateNavigationSort()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Data not valid. Please refresh the page and try again.', 'url' => $url), 422); }
  198. # get the ids (key path) for item, old folder and new folder
  199. $itemKeyPath = explode('.', $this->params['item_id']);
  200. $parentKeyFrom = explode('.', $this->params['parent_id_from']);
  201. $parentKeyTo = explode('.', $this->params['parent_id_to']);
  202. # get the item from structure
  203. $item = Folder::getItemWithKeyPath($this->structure, $itemKeyPath);
  204. if(!$item){ return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not find this page. Please refresh and try again.', 'url' => $url), 404); }
  205. # if a folder is moved on the first level
  206. if($this->params['parent_id_from'] == 'navi')
  207. {
  208. # create empty and default values so that the logic below still works
  209. $newFolder = new \stdClass();
  210. $newFolder->path = '';
  211. $folderContent = $this->structure;
  212. }
  213. else
  214. {
  215. # get the target folder from structure
  216. $newFolder = Folder::getItemWithKeyPath($this->structure, $parentKeyTo);
  217. # get the content of the target folder
  218. $folderContent = $newFolder->folderContent;
  219. }
  220. # if the item has been moved within the same folder
  221. if($this->params['parent_id_from'] == $this->params['parent_id_to'])
  222. {
  223. # get key of item
  224. $itemKey = end($itemKeyPath);
  225. reset($itemKeyPath);
  226. # delete item from folderContent
  227. unset($folderContent[$itemKey]);
  228. }
  229. elseif($this->params['active'] == 'active')
  230. {
  231. # an active file has been moved to another folder
  232. $url = $this->uri->getBaseUrl() . '/tm/content' . $newFolder->urlRelWoF . '/' . $item->slug;
  233. }
  234. # add item to newFolder
  235. array_splice($folderContent, $this->params['index_new'], 0, array($item));
  236. # initialize index
  237. $index = 0;
  238. # initialise write object
  239. $write = new Write();
  240. # iterate through the whole content of the new folder
  241. $writeError = false;
  242. foreach($folderContent as $folderItem)
  243. {
  244. if(!$write->moveElement($folderItem, $newFolder->path, $index))
  245. {
  246. $writeError = true;
  247. }
  248. $index++;
  249. }
  250. if($writeError){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Something went wrong. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404); }
  251. # update the structure for editor
  252. $this->setStructure($draft = true, $cache = false);
  253. # get item for url and set it active again
  254. if(isset($this->params['url']))
  255. {
  256. $activeItem = Folder::getItemForUrl($this->structure, $this->params['url']);
  257. }
  258. # keep the internal structure for response
  259. $internalStructure = $this->structure;
  260. # update the structure for website
  261. $this->setStructure($draft = false, $cache = false);
  262. return $response->withJson(array('data' => $internalStructure, 'errors' => false, 'url' => $url));
  263. }
  264. public function createArticle(Request $request, Response $response, $args)
  265. {
  266. # get params from call
  267. $this->params = $request->getParams();
  268. $this->uri = $request->getUri();
  269. # url is only needed, if an active page is moved
  270. $url = false;
  271. # set structure
  272. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
  273. # validate input
  274. if(!$this->validateNaviItem()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Special Characters not allowed. Length between 1 and 20 chars.', 'url' => $url), 422); }
  275. # get the ids (key path) for item, old folder and new folder
  276. $folderKeyPath = explode('.', $this->params['folder_id']);
  277. # get the item from structure
  278. $folder = Folder::getItemWithKeyPath($this->structure, $folderKeyPath);
  279. if(!$folder){ return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not find this page. Please refresh and try again.', 'url' => $url), 404); }
  280. # Rename all files within the folder to make sure, that namings and orders are correct
  281. # get the content of the target folder
  282. $folderContent = $folder->folderContent;
  283. # create the name for the new item
  284. $nameParts = Folder::getStringParts($this->params['item_name']);
  285. $name = implode("-", $nameParts);
  286. $slug = $name;
  287. # initialize index
  288. $index = 0;
  289. # initialise write object
  290. $write = new Write();
  291. # iterate through the whole content of the new folder
  292. $writeError = false;
  293. foreach($folderContent as $folderItem)
  294. {
  295. # check, if the same name as new item, then return an error
  296. if($folderItem->slug == $slug)
  297. {
  298. return $response->withJson(array('data' => $this->structure, 'errors' => 'There is already a page with this name. Please choose another name.', 'url' => $url), 404);
  299. }
  300. if(!$write->moveElement($folderItem, $folder->path, $index))
  301. {
  302. $writeError = true;
  303. }
  304. $index++;
  305. }
  306. if($writeError){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Something went wrong. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404); }
  307. # add prefix number to the name
  308. $namePath = $index > 9 ? $index . '-' . $name : '0' . $index . '-' . $name;
  309. $folderPath = 'content' . $folder->path;
  310. # create default content
  311. $content = json_encode(['# Add Title', 'Add Content']);
  312. if($this->params['type'] == 'file')
  313. {
  314. if(!$write->writeFile($folderPath, $namePath . '.txt', $content))
  315. {
  316. return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not create the file. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404);
  317. }
  318. }
  319. elseif($this->params['type'] == 'folder')
  320. {
  321. if(!$write->checkPath($folderPath . DIRECTORY_SEPARATOR . $namePath))
  322. {
  323. return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not create the folder. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404);
  324. }
  325. $write->writeFile($folderPath . DIRECTORY_SEPARATOR . $namePath, 'index.txt', $content);
  326. }
  327. # update the structure for editor
  328. $this->setStructure($draft = true, $cache = false);
  329. # get item for url and set it active again
  330. if(isset($this->params['url']))
  331. {
  332. $activeItem = Folder::getItemForUrl($this->structure, $this->params['url']);
  333. }
  334. # activate this if you want to redirect after creating the page...
  335. # $url = $this->uri->getBaseUrl() . '/tm/content' . $folder->urlRelWoF . '/' . $name;
  336. return $response->withJson(array('data' => $this->structure, 'errors' => false, 'url' => $url));
  337. }
  338. public function createBaseFolder(Request $request, Response $response, $args)
  339. {
  340. # get params from call
  341. $this->params = $request->getParams();
  342. $this->uri = $request->getUri();
  343. # url is only needed, if an active page is moved
  344. $url = false;
  345. # set structure
  346. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
  347. # validate input
  348. #if(!$this->validateBaseFolder()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Special Characters not allowed. Length between 1 and 20 chars.', 'url' => $url), 422); }
  349. # create the name for the new item
  350. $nameParts = Folder::getStringParts($this->params['item_name']);
  351. $name = implode("-", $nameParts);
  352. $slug = $name;
  353. # initialize index
  354. $index = 0;
  355. # initialise write object
  356. $write = new Write();
  357. # iterate through the whole content of the new folder
  358. $writeError = false;
  359. foreach($this->structure as $folder)
  360. {
  361. # check, if the same name as new item, then return an error
  362. if($folder->slug == $slug)
  363. {
  364. return $response->withJson(array('data' => $this->structure, 'errors' => 'There is already a page with this name. Please choose another name.', 'url' => $url), 404);
  365. }
  366. if(!$write->moveElement($folder, '', $index))
  367. {
  368. $writeError = true;
  369. }
  370. $index++;
  371. }
  372. if($writeError){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Something went wrong. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404); }
  373. # add prefix number to the name
  374. $namePath = $index > 9 ? $index . '-' . $name : '0' . $index . '-' . $name;
  375. $folderPath = 'content';
  376. if(!$write->checkPath($folderPath . DIRECTORY_SEPARATOR . $namePath))
  377. {
  378. return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not create the folder. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404);
  379. }
  380. # create default content
  381. $content = json_encode(['# Add Title', 'Add Content']);
  382. $write->writeFile($folderPath . DIRECTORY_SEPARATOR . $namePath, 'index.txt', $content);
  383. # update the structure for editor
  384. $this->setStructure($draft = true, $cache = false);
  385. # get item for url and set it active again
  386. if(isset($this->params['url']))
  387. {
  388. $activeItem = Folder::getItemForUrl($this->structure, $this->params['url']);
  389. }
  390. return $response->withJson(array('data' => $this->structure, 'errors' => false, 'url' => $url));
  391. }
  392. public function getArticleMarkdown(Request $request, Response $response, $args)
  393. {
  394. /* get params from call */
  395. $this->params = $request->getParams();
  396. $this->uri = $request->getUri();
  397. # set structure
  398. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  399. /* set item */
  400. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  401. # set the status for published and drafted
  402. $this->setPublishStatus();
  403. # set path
  404. $this->setItemPath($this->item->fileType);
  405. # read content from file
  406. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  407. $content = $this->content;
  408. if($content == '')
  409. {
  410. $content = [];
  411. }
  412. # if content is not an array, then transform it
  413. if(!is_array($content))
  414. {
  415. # initialize parsedown extension
  416. $parsedown = new ParsedownExtension();
  417. # turn markdown into an array of markdown-blocks
  418. $content = $parsedown->markdownToArrayBlocks($content);
  419. }
  420. # delete markdown from title
  421. if(isset($content[0]))
  422. {
  423. $content[0] = trim($content[0], "# ");
  424. }
  425. return $response->withJson(array('data' => $content, 'errors' => false));
  426. }
  427. public function updateBlock(Request $request, Response $response, $args)
  428. {
  429. /* get params from call */
  430. $this->params = $request->getParams();
  431. $this->uri = $request->getUri();
  432. /* validate input */
  433. if(!$this->validateBlockInput()){ return $response->withJson($this->errors,422); }
  434. # set structure
  435. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  436. /* set item */
  437. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  438. # set the status for published and drafted
  439. $this->setPublishStatus();
  440. # set path
  441. $this->setItemPath($this->item->fileType);
  442. # read content from file
  443. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  444. # make it more clear which content we have
  445. $pageMarkdown = $this->content;
  446. $blockMarkdown = $this->params['markdown'];
  447. # standardize line breaks
  448. $blockMarkdown = str_replace(array("\r\n", "\r"), "\n", $blockMarkdown);
  449. # remove surrounding line breaks
  450. $blockMarkdown = trim($blockMarkdown, "\n");
  451. if($pageMarkdown == '')
  452. {
  453. $pageMarkdown = [];
  454. }
  455. # initialize parsedown extension
  456. $parsedown = new ParsedownExtension();
  457. # if content is not an array, then transform it
  458. if(!is_array($pageMarkdown))
  459. {
  460. # turn markdown into an array of markdown-blocks
  461. $pageMarkdown = $parsedown->markdownToArrayBlocks($pageMarkdown);
  462. }
  463. # if it is a new content-block
  464. if($this->params['block_id'] == 99999)
  465. {
  466. # update the markdown block in the page content
  467. $pageMarkdown[] = $blockMarkdown;
  468. $id = (count($pageMarkdown)-1);
  469. $blockId = 'blox-' . $id;
  470. }
  471. elseif(!isset($pageMarkdown[$this->params['block_id']]))
  472. {
  473. # return error
  474. return $response->withJson(array('data' => false, 'errors' => 'The ID of the content-block is wrong.'), 404);
  475. }
  476. elseif($this->params['block_id'] == 0)
  477. {
  478. # update the markdown block in the page content
  479. $blockMarkdown = trim($blockMarkdown, "# ");
  480. $blockMarkdownTitle = '# ' . $blockMarkdown;
  481. $pageMarkdown[$this->params['block_id']] = $blockMarkdownTitle;
  482. $id = $this->params['block_id'];
  483. $blockId = $this->params['block_id'];
  484. }
  485. else
  486. {
  487. # update the markdown block in the page content
  488. $pageMarkdown[$this->params['block_id']] = $blockMarkdown;
  489. $id = $this->params['block_id'];
  490. $blockId = $this->params['block_id'];
  491. }
  492. # encode the content into json
  493. $pageJson = json_encode($pageMarkdown);
  494. # set path for the file (or folder)
  495. $this->setItemPath('txt');
  496. /* update the file */
  497. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  498. {
  499. # update the internal structure
  500. $this->setStructure($draft = true, $cache = false);
  501. }
  502. else
  503. {
  504. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  505. }
  506. /* set safe mode to escape javascript and html in markdown */
  507. $parsedown->setSafeMode(true);
  508. /* parse markdown-file to content-array, if title parse title. */
  509. if($this->params['block_id'] == 0)
  510. {
  511. $blockArray = $parsedown->text($blockMarkdownTitle);
  512. }
  513. else
  514. {
  515. $blockArray = $parsedown->text($blockMarkdown);
  516. }
  517. /* parse markdown-content-array to content-string */
  518. $blockHTML = $parsedown->markup($blockArray);
  519. return $response->withJson(array('content' => $blockHTML, 'markdown' => $blockMarkdown, 'blockId' => $blockId, 'id' => $id, 'errors' => false));
  520. }
  521. public function deleteBlock(Request $request, Response $response, $args)
  522. {
  523. /* get params from call */
  524. $this->params = $request->getParams();
  525. $this->uri = $request->getUri();
  526. # set structure
  527. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  528. # set item
  529. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  530. # set the status for published and drafted
  531. $this->setPublishStatus();
  532. # set path
  533. $this->setItemPath($this->item->fileType);
  534. # read content from file
  535. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  536. # get content
  537. $this->content;
  538. if($this->content == '')
  539. {
  540. $this->content = [];
  541. }
  542. # initialize parsedown extension
  543. $parsedown = new ParsedownExtension();
  544. # if content is not an array, then transform it
  545. if(!is_array($this->content))
  546. {
  547. # turn markdown into an array of markdown-blocks
  548. $this->content = $parsedown->markdownToArrayBlocks($this->content);
  549. }
  550. # check if id exists
  551. if(!isset($this->content[$this->params['block_id']])){ return $response->withJson(array('data' => false, 'errors' => 'The ID of the content-block is wrong.'), 404); }
  552. # delete the block
  553. unset($this->content[$this->params['block_id']]);
  554. $this->content = array_values($this->content);
  555. # delete markdown from title
  556. if(isset($this->content[0]))
  557. {
  558. $this->content[0] = trim($this->content[0], "# ");
  559. }
  560. # encode the content into json
  561. $pageJson = json_encode($this->content);
  562. # set path for the file (or folder)
  563. $this->setItemPath('txt');
  564. /* update the file */
  565. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  566. {
  567. # update the internal structure
  568. $this->setStructure($draft = true, $cache = false);
  569. }
  570. else
  571. {
  572. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  573. }
  574. return $response->withJson(array('markdown' => $this->content, 'errors' => false));
  575. }
  576. }