ContentBackendController.php 6.1 KB

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