ContentController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Views\Twig;
  4. use Slim\Http\Request;
  5. use Slim\Http\Response;
  6. use Typemill\Models\Folder;
  7. use Typemill\Models\WriteYaml;
  8. use \Symfony\Component\Yaml\Yaml;
  9. use Typemill\Models\Helpers;
  10. class ContentController extends Controller
  11. {
  12. /**
  13. * Show Content
  14. *
  15. * @param obj $request the slim request object
  16. * @param obj $response the slim response object
  17. * @return obje $response with redirect to route
  18. */
  19. public function showContent(Request $request, Response $response, $args)
  20. {
  21. $settings = $this->c->get('settings');
  22. $pathToContent = $settings['rootPath'] . $settings['contentFolder'];
  23. $uri = $request->getUri();
  24. /* scan the content of the folder */
  25. $structure = Folder::scanFolder($pathToContent);
  26. /* if there is no content, render an empty page */
  27. if(count($structure) == 0)
  28. {
  29. return $this->render($response, 'content/content.twig', array( 'navigation' => true, 'content' => 'Nothing found in content folder.' ));
  30. }
  31. /* create an array of object with the whole content of the folder */
  32. $structure = Folder::getFolderContentDetails($structure, $uri->getBaseUrl(), $uri->getBasePath());
  33. /* if there is no structure at all, the content folder is probably empty */
  34. if(!$structure)
  35. {
  36. return $this->render($response, 'content/content.twig', array( 'navigation' => true, 'content' => 'Nothing found in content folder.' ));
  37. }
  38. /* if it is the startpage */
  39. if(empty($args))
  40. {
  41. /* check, if there is an index-file in the root of the content folder */
  42. $contentMD = file_exists($pathToContent . DIRECTORY_SEPARATOR . 'index.md') ? file_get_contents($pathToContent . DIRECTORY_SEPARATOR . 'index.md') : NULL;
  43. }
  44. else
  45. {
  46. /* get the request url */
  47. $urlRel = $uri->getBasePath() . '/' . $args['params'];
  48. /* find the url in the content-item-tree and return the item-object for the file */
  49. $item = Folder::getItemForUrl($structure, $urlRel);
  50. /* if there is still no item, return a 404-page */
  51. if(!$item)
  52. {
  53. return $this->render404($response, array( 'navigation' => $structure, 'settings' => $settings, 'base_url' => $base_url ));
  54. }
  55. /* add the paging to the item */
  56. $item = Folder::getPagingForItem($structure, $item);
  57. /* check if url is a folder. If so, check if there is an index-file in that folder */
  58. if($item->elementType == 'folder' && $item->index)
  59. {
  60. $filePath = $pathToContent . $item->path . DIRECTORY_SEPARATOR . 'index.md';
  61. }
  62. elseif($item->elementType == 'file')
  63. {
  64. $filePath = $pathToContent . $item->path;
  65. }
  66. /* add the modified date for the file */
  67. $item->modified = isset($filePath) ? filemtime($filePath) : false;
  68. /* read the content of the file */
  69. $contentMD = isset($filePath) ? file_get_contents($filePath) : false;
  70. }
  71. $title = false;
  72. $content = $contentMD;
  73. if($contentMD[0] == '#')
  74. {
  75. $contentParts = explode("\r\n", $contentMD, 2);
  76. $title = trim($contentParts[0], "# \t\n\r\0\x0B");
  77. $content = trim($contentParts[1]);
  78. }
  79. return $this->render($response, 'content/content.twig', array('navigation' => $structure, 'title' => $title, 'content' => $content, 'item' => $item, 'settings' => $settings ));
  80. }
  81. }