ContentBackendController.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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
  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->render404($response, array( 'navigation' => true, 'content' => $this->errors )); }
  24. # set item
  25. if(!$this->setItem()){ return $this->render404($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->render404($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.twig', array('navigation' => $this->structure, 'title' => $title, 'content' => $content, 'item' => $this->item, 'settings' => $this->settings ));
  60. }
  61. }