ContentBackendController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use Slim\Views\Twig;
  6. use Typemill\Models\Folder;
  7. use Typemill\Extensions\ParsedownExtension;
  8. class ContentBackendController extends ContentController
  9. {
  10. /**
  11. * Show Content for raw editor
  12. *
  13. * @param obj $request the slim request object
  14. * @param obj $response the slim response object
  15. * @return obje $response with redirect to route
  16. */
  17. public function showContent(Request $request, Response $response, $args)
  18. {
  19. # get params from call
  20. $this->uri = $request->getUri();
  21. $this->params = isset($args['params']) ? ['url' => $this->uri->getBasePath() . '/' . $args['params']] : ['url' => $this->uri->getBasePath()];
  22. # set structure
  23. if(!$this->setStructure($draft = true)){ return $this->renderIntern404($response, array( 'navigation' => true, 'content' => $this->errors )); }
  24. # set item
  25. if(!$this->setItem()){ return $this->renderIntern404($response, array( 'navigation' => $this->structure, 'settings' => $this->settings, 'content' => $this->errors )); }
  26. # get the breadcrumb (here we need it only to mark the actual item active in navigation)
  27. $breadcrumb = isset($this->item->keyPathArray) ? Folder::getBreadcrumb($this->structure, $this->item->keyPathArray) : false;
  28. # set the status for published and drafted
  29. $this->setPublishStatus();
  30. # set path
  31. $this->setItemPath($this->item->fileType);
  32. # add the modified date for the file
  33. $this->item->modified = ($this->item->published OR $this->item->drafted) ? filemtime($this->settings['contentFolder'] . $this->path) : false;
  34. # read content from file
  35. if(!$this->setContent()){ return $this->renderIntern404($response, array( 'navigation' => $this->structure, 'settings' => $this->settings, 'content' => $this->errors )); }
  36. $content = $this->content;
  37. $title = false;
  38. # if content is an array, then it is a draft
  39. if(is_array($content))
  40. {
  41. # transform array to markdown
  42. $parsedown = new ParsedownExtension();
  43. $content = $parsedown->arrayBlocksToMarkdown($content);
  44. }
  45. # if there is content
  46. if($content != '')
  47. {
  48. # normalize linebreaks
  49. $content = str_replace(array("\r\n", "\r"), "\n", $content);
  50. $content = trim($content, "\n");
  51. # and strip out title
  52. if($content[0] == '#')
  53. {
  54. $contentParts = explode("\n", $content, 2);
  55. $title = trim($contentParts[0], "# \t\n\r\0\x0B");
  56. $content = trim($contentParts[1]);
  57. }
  58. }
  59. return $this->render($response, 'editor/editor-raw.twig', array('navigation' => $this->structure, 'title' => $title, 'content' => $content, 'item' => $this->item, 'settings' => $this->settings ));
  60. }
  61. /**
  62. * Show Content for raw editor
  63. *
  64. * @param obj $request the slim request object
  65. * @param obj $response the slim response object
  66. * @return obje $response with redirect to route
  67. */
  68. public function showBlox(Request $request, Response $response, $args)
  69. {
  70. # get params from call
  71. $this->uri = $request->getUri();
  72. $this->params = isset($args['params']) ? ['url' => $this->uri->getBasePath() . '/' . $args['params']] : ['url' => $this->uri->getBasePath()];
  73. # set structure
  74. if(!$this->setStructure($draft = true)){ return $this->renderIntern404($response, array( 'navigation' => true, 'content' => $this->errors )); }
  75. # set item
  76. if(!$this->setItem()){ return $this->renderIntern404($response, array( 'navigation' => $this->structure, 'settings' => $this->settings, 'content' => $this->errors )); }
  77. # get the breadcrumb (here we need it only to mark the actual item active in navigation)
  78. $breadcrumb = isset($this->item->keyPathArray) ? Folder::getBreadcrumb($this->structure, $this->item->keyPathArray) : false;
  79. # set the status for published and drafted
  80. $this->setPublishStatus();
  81. # set path
  82. $this->setItemPath($this->item->fileType);
  83. # add the modified date for the file
  84. $this->item->modified = ($this->item->published OR $this->item->drafted) ? filemtime($this->settings['contentFolder'] . $this->path) : false;
  85. # read content from file
  86. if(!$this->setContent()){ return $this->renderIntern404($response, array( 'navigation' => $this->structure, 'settings' => $this->settings, 'content' => $this->errors )); }
  87. $content = $this->content;
  88. if($content == '')
  89. {
  90. $content = [];
  91. }
  92. # initialize parsedown extension
  93. $parsedown = new ParsedownExtension();
  94. # if content is not an array, then transform it
  95. if(!is_array($content))
  96. {
  97. # turn markdown into an array of markdown-blocks
  98. $content = $parsedown->markdownToArrayBlocks($content);
  99. }
  100. foreach($content as $key => $block)
  101. {
  102. /* parse markdown-file to content-array */
  103. $contentArray = $parsedown->text($block);
  104. /* parse markdown-content-array to content-string */
  105. $content[$key] = $parsedown->markup($contentArray);
  106. }
  107. # extract title and delete from content array, array will start at 1 after that.
  108. $title = $content[0];
  109. unset($content[0]);
  110. return $this->render($response, 'editor/editor-blox.twig', array('navigation' => $this->structure, 'title' => $title, 'content' => $content, 'item' => $this->item, 'settings' => $this->settings ));
  111. }
  112. public function showEmpty(Request $request, Response $response, $args)
  113. {
  114. return $this->renderIntern404($response, array( 'settings' => $this->settings ));
  115. }
  116. }