ContentApiController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use Typemill\Extensions\ParsedownExtension;
  6. class ContentApiController extends ContentController
  7. {
  8. public function publishArticle(Request $request, Response $response, $args)
  9. {
  10. # get params from call
  11. $this->params = $request->getParams();
  12. $this->uri = $request->getUri();
  13. # validate input
  14. if(!$this->validateEditorInput()){ return $response->withJson($this->errors,422); }
  15. # set structure
  16. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  17. # set item
  18. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  19. # set the status for published and drafted
  20. $this->setPublishStatus();
  21. # set path for the file (or folder)
  22. $this->setItemPath('md');
  23. # merge title with content for complete markdown document
  24. $updatedContent = '# ' . $this->params['title'] . "\r\n\r\n" . $this->params['content'];
  25. # update the file
  26. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $updatedContent))
  27. {
  28. # update the file
  29. $delete = $this->deleteContentFiles(['txt']);
  30. # update the structure
  31. $this->setStructure($draft = false, $cache = false);
  32. return $response->withJson(['success'], 200);
  33. }
  34. else
  35. {
  36. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  37. }
  38. }
  39. public function unpublishArticle(Request $request, Response $response, $args)
  40. {
  41. # get params from call
  42. $this->params = $request->getParams();
  43. $this->uri = $request->getUri();
  44. # set structure
  45. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  46. # set item
  47. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  48. # set the status for published and drafted
  49. $this->setPublishStatus();
  50. # check if draft exists, if not, create one.
  51. if(!$this->item->drafted)
  52. {
  53. # set path for the file (or folder)
  54. $this->setItemPath('md');
  55. # set content of markdown-file
  56. if(!$this->setContent()){ return $response->withJson($this->errors, 404); }
  57. # initialize parsedown extension
  58. $parsedown = new ParsedownExtension();
  59. # turn markdown into an array of markdown-blocks
  60. $contentArray = $parsedown->markdownToArrayBlocks($this->content);
  61. # encode the content into json
  62. $contentJson = json_encode($contentArray);
  63. # set path for the file (or folder)
  64. $this->setItemPath('txt');
  65. /* update the file */
  66. if(!$this->write->writeFile($this->settings['contentFolder'], $this->path, $contentJson))
  67. {
  68. return $response->withJson(['errors' => ['message' => 'Could not create a draft of the page. Please check if the folder is writable']], 404);
  69. }
  70. }
  71. # update the file
  72. $delete = $this->deleteContentFiles(['md']);
  73. if($delete)
  74. {
  75. # update the live structure
  76. $this->setStructure($draft = false, $cache = false);
  77. return $response->withJson(['success'], 200);
  78. }
  79. else
  80. {
  81. return $response->withJson(['errors' => ['message' => "Could not delete some files. Please check if the files exists and are writable"]], 404);
  82. }
  83. }
  84. public function deleteArticle(Request $request, Response $response, $args)
  85. {
  86. # get params from call
  87. $this->params = $request->getParams();
  88. $this->uri = $request->getUri();
  89. # set structure
  90. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  91. # set item
  92. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  93. # update the file
  94. $delete = $this->deleteContentFiles(['md','txt']);
  95. if($delete)
  96. {
  97. # update the live structure
  98. $this->setStructure($draft = false, $cache = false);
  99. #update the backend structure
  100. $this->setStructure($draft = true, $cache = false);
  101. return $response->withJson(['success'], 200);
  102. }
  103. else
  104. {
  105. return $response->withJson(['errors' => ['message' => "Could not delete some files. Please check if the files exists and are writable"]], 404);
  106. }
  107. }
  108. public function updateArticle(Request $request, Response $response, $args)
  109. {
  110. # get params from call
  111. $this->params = $request->getParams();
  112. $this->uri = $request->getUri();
  113. # validate input
  114. if(!$this->validateEditorInput()){ return $response->withJson($this->errors,422); }
  115. # set structure
  116. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  117. # set item
  118. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  119. # set path for the file (or folder)
  120. $this->setItemPath('txt');
  121. # merge title with content for complete markdown document
  122. $updatedContent = '# ' . $this->params['title'] . "\r\n\r\n" . $this->params['content'];
  123. # initialize parsedown extension
  124. $parsedown = new ParsedownExtension();
  125. # turn markdown into an array of markdown-blocks
  126. $contentArray = $parsedown->markdownToArrayBlocks($updatedContent);
  127. # encode the content into json
  128. $contentJson = json_encode($contentArray);
  129. /* update the file */
  130. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $contentJson))
  131. {
  132. return $response->withJson(['success'], 200);
  133. }
  134. else
  135. {
  136. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  137. }
  138. }
  139. public function createBlock(Request $request, Response $response, $args)
  140. {
  141. /* get params from call */
  142. $this->params = $request->getParams();
  143. $this->uri = $request->getUri();
  144. /* validate input */
  145. if(!$this->validateInput()){ return $response->withJson($this->errors,422); }
  146. /* set structure */
  147. if(!$this->setStructure()){ return $response->withJson($this->errors, 404); }
  148. /* set item */
  149. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  150. /* set path */
  151. $this->setItemPath();
  152. /* get markdown-file */
  153. if(!$this->setMarkdownFile()){ return $response->withJson($this->errors, 404); }
  154. /* get txt-file with content array */
  155. $contentArray = NULL;
  156. /*
  157. create a txt-file with parsedown-array.
  158. you will have .md and .txt file.
  159. scan folder with option to show drafts.
  160. but what is with structure? We use the cached structure, do not forget!!!
  161. if there is a draft, replace the md file with txt-file.
  162. display content: you have to check if md or txt. if txt, then directly open the txt-file.
  163. in here set markdown-file or
  164. set txt-file.
  165. if publish, render txt-content, replace markdown-file, delete txt-file
  166. */
  167. /* initialize pagedown */
  168. /* turn input into array */
  169. /* add input to contentArray */
  170. /* store updated contentArray */
  171. /* transform input to html */
  172. /* send html to client */
  173. }
  174. }