ContentBackendController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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()->withUserInfo('');
  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 information for homepage
  25. $this->setHomepage();
  26. # set item
  27. if(!$this->setItem()){ return $this->renderIntern404($response, array( 'navigation' => $this->structure, 'settings' => $this->settings, 'content' => $this->errors )); }
  28. # get the breadcrumb (here we need it only to mark the actual item active in navigation)
  29. $breadcrumb = isset($this->item->keyPathArray) ? Folder::getBreadcrumb($this->structure, $this->item->keyPathArray) : false;
  30. # set the status for published and drafted
  31. $this->setPublishStatus();
  32. # set path
  33. $this->setItemPath($this->item->fileType);
  34. # add the modified date for the file
  35. $this->item->modified = ($this->item->published OR $this->item->drafted) ? filemtime($this->settings['contentFolder'] . $this->path) : false;
  36. # read content from file
  37. if(!$this->setContent()){ return $this->renderIntern404($response, array( 'navigation' => $this->structure, 'settings' => $this->settings, 'content' => $this->errors )); }
  38. $content = $this->content;
  39. $title = false;
  40. # if content is an array, then it is a draft
  41. if(is_array($content))
  42. {
  43. # transform array to markdown
  44. $parsedown = new ParsedownExtension();
  45. $content = $parsedown->arrayBlocksToMarkdown($content);
  46. }
  47. # if there is content
  48. if($content != '')
  49. {
  50. # normalize linebreaks
  51. $content = str_replace(array("\r\n", "\r"), "\n", $content);
  52. $content = trim($content, "\n");
  53. # and strip out title
  54. if($content[0] == '#')
  55. {
  56. $contentParts = explode("\n", $content, 2);
  57. $title = trim($contentParts[0], "# \t\n\r\0\x0B");
  58. $content = trim($contentParts[1]);
  59. }
  60. }
  61. return $this->render($response, 'editor/editor-raw.twig', array('navigation' => $this->structure, 'homepage' => $this->homepage, 'title' => $title, 'content' => $content, 'item' => $this->item, 'settings' => $this->settings ));
  62. }
  63. /**
  64. * Show Content for blox editor
  65. *
  66. * @param obj $request the slim request object
  67. * @param obj $response the slim response object
  68. * @return obje $response with redirect to route
  69. */
  70. public function showBlox(Request $request, Response $response, $args)
  71. {
  72. # get params from call
  73. $this->uri = $request->getUri()->withUserInfo('');
  74. $this->params = isset($args['params']) ? ['url' => $this->uri->getBasePath() . '/' . $args['params']] : ['url' => $this->uri->getBasePath()];
  75. # set structure
  76. if(!$this->setStructure($draft = true)){ return $this->renderIntern404($response, array( 'navigation' => true, 'content' => $this->errors )); }
  77. # set information for homepage
  78. $this->setHomepage();
  79. # set item
  80. if(!$this->setItem()){ return $this->renderIntern404($response, array( 'navigation' => $this->structure, 'settings' => $this->settings, 'content' => $this->errors )); }
  81. # set the status for published and drafted
  82. $this->setPublishStatus();
  83. # set path
  84. $this->setItemPath($this->item->fileType);
  85. # add the modified date for the file
  86. $this->item->modified = ($this->item->published OR $this->item->drafted) ? filemtime($this->settings['contentFolder'] . $this->path) : false;
  87. # read content from file
  88. if(!$this->setContent()){ return $this->renderIntern404($response, array( 'navigation' => $this->structure, 'settings' => $this->settings, 'content' => $this->errors )); }
  89. $content = $this->content;
  90. if($content == '')
  91. {
  92. $content = [];
  93. }
  94. # initialize parsedown extension
  95. $parsedown = new ParsedownExtension();
  96. # to fix footnote-logic in parsedown, set visual mode to true
  97. $parsedown->setVisualMode();
  98. # if content is not an array, then transform it
  99. if(!is_array($content))
  100. {
  101. # turn markdown into an array of markdown-blocks
  102. $content = $parsedown->markdownToArrayBlocks($content);
  103. }
  104. # needed for ToC links
  105. $relurl = '/tm/content/' . $this->settings['editor'] . '/' . $this->item->urlRel;
  106. foreach($content as $key => $block)
  107. {
  108. /* parse markdown-file to content-array */
  109. $contentArray = $parsedown->text($block);
  110. /* parse markdown-content-array to content-string */
  111. $content[$key] = $parsedown->markup($contentArray, $relurl);
  112. }
  113. # extract title and delete from content array, array will start at 1 after that.
  114. $title = '# add title';
  115. if(isset($content[0]))
  116. {
  117. $title = $content[0];
  118. unset($content[0]);
  119. }
  120. return $this->render($response, 'editor/editor-blox.twig', array('navigation' => $this->structure, 'homepage' => $this->homepage, 'title' => $title, 'content' => $content, 'item' => $this->item, 'settings' => $this->settings ));
  121. }
  122. public function showEmpty(Request $request, Response $response, $args)
  123. {
  124. return $this->renderIntern404($response, array( 'settings' => $this->settings ));
  125. }
  126. }