ContentBackendController.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use Slim\Views\Twig;
  6. use Typemill\Extensions\ParsedownExtension;
  7. class ContentBackendController extends ContentController
  8. {
  9. /**
  10. * Show Content
  11. *
  12. * @param obj $request the slim request object
  13. * @param obj $response the slim response object
  14. * @return obje $response with redirect to route
  15. */
  16. public function showContent(Request $request, Response $response, $args)
  17. {
  18. # get params from call
  19. $this->uri = $request->getUri();
  20. $this->params = isset($args['params']) ? ['url' => $this->uri->getBasePath() . '/' . $args['params']] : ['url' => $this->uri->getBasePath()];
  21. # set structure
  22. if(!$this->setStructure($draft = true)){ return $this->render404($response, array( 'navigation' => true, 'content' => $this->errors )); }
  23. # set item
  24. if(!$this->setItem()){ return $this->render404($response, array( 'navigation' => $this->structure, 'settings' => $this->settings, 'content' => $this->errors )); }
  25. # set the status for published and drafted
  26. $this->setPublishStatus();
  27. # set path
  28. $this->setItemPath($this->item->fileType);
  29. # add the modified date for the file
  30. $this->item->modified = ($this->item->published OR $this->item->drafted) ? filemtime($this->settings['contentFolder'] . $this->path) : false;
  31. # read content from file
  32. if(!$this->setContent()){ return $this->render404($response, array( 'navigation' => $this->structure, 'settings' => $this->settings, 'content' => $this->errors )); }
  33. $content = $this->content;
  34. $title = false;
  35. # if content is an array, then it is a draft
  36. if(is_array($content))
  37. {
  38. # transform array to markdown
  39. $parsedown = new ParsedownExtension();
  40. $content = $parsedown->arrayBlocksToMarkdown($content);
  41. }
  42. # if there is content
  43. if($content != '')
  44. {
  45. # normalize linebreaks
  46. $content = str_replace(array("\r\n", "\r"), "\n", $content);
  47. $content = trim($content, "\n");
  48. # and strip out title
  49. if($content[0] == '#')
  50. {
  51. $contentParts = explode("\n", $content, 2);
  52. $title = trim($contentParts[0], "# \t\n\r\0\x0B");
  53. $content = trim($contentParts[1]);
  54. }
  55. }
  56. return $this->render($response, 'content/content.twig', array('navigation' => $this->structure, 'title' => $title, 'content' => $content, 'item' => $this->item, 'settings' => $this->settings ));
  57. }
  58. }