PageController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace System\Controllers;
  3. use System\Models\Folder;
  4. use System\Models\Cache;
  5. use System\Models\Helpers;
  6. class PageController extends Controller
  7. {
  8. public function index($request, $response, $args)
  9. {
  10. /* Initiate Variables */
  11. $structure = false;
  12. $contentHTML = false;
  13. $item = false;
  14. $breadcrumb = false;
  15. $description = '';
  16. $settings = $this->c->get('settings');
  17. $pathToContent = $settings['rootPath'] . $settings['contentFolder'];
  18. $cache = new Cache();
  19. $uri = $request->getUri();
  20. $base_url = $uri->getBaseUrl();
  21. if($cache->validate())
  22. {
  23. $structure = $this->getCachedStructure($cache);
  24. $cached = true;
  25. }
  26. else
  27. {
  28. $structure = $this->getFreshStructure($pathToContent, $cache, $uri);
  29. $cached = false;
  30. if(!$structure)
  31. {
  32. $content = '<h1>No Content</h1><p>Your content folder is empty.</p>';
  33. $this->c->view->render($response, '/index.twig', [ 'content' => $content ]);
  34. }
  35. }
  36. /* if the user is on startpage */
  37. if(empty($args))
  38. {
  39. /* check, if there is an index-file in the root of the content folder */
  40. $contentMD = file_exists($pathToContent . DIRECTORY_SEPARATOR . 'index.md') ? file_get_contents($pathToContent . DIRECTORY_SEPARATOR . 'index.md') : NULL;
  41. }
  42. else
  43. {
  44. /* get the request url */
  45. $urlRel = $uri->getBasePath() . '/' . $args['params'];
  46. /* find the url in the content-item-tree and return the item-object for the file */
  47. $item = Folder::getItemForUrl($structure, $urlRel);
  48. if(!$item && $cached)
  49. {
  50. $structure = $this->getFreshStructure($pathToContent, $cache, $uri);
  51. $item = Folder::getItemForUrl($structure, $urlRel);
  52. }
  53. if(!$item){ return $this->render404($response, array( 'navigation' => $structure, 'settings' => $settings, 'base_url' => $base_url )); }
  54. /* get breadcrumb for page */
  55. $breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
  56. /* add the paging to the item */
  57. $item = Folder::getPagingForItem($structure, $item);
  58. /* check if url is a folder. If so, check if there is an index-file for the folder */
  59. if($item->elementType == 'folder' && $item->index)
  60. {
  61. $filePath = $pathToContent . $item->path . DIRECTORY_SEPARATOR . 'index.md';
  62. }
  63. elseif($item->elementType == 'file')
  64. {
  65. $filePath = $pathToContent . $item->path;
  66. }
  67. /* read the content of the file */
  68. $contentMD = isset($filePath) ? file_get_contents($filePath) : false;
  69. }
  70. /* initialize parsedown */
  71. $Parsedown = new \ParsedownExtra();
  72. /* parse markdown-file to html-string */
  73. $contentHTML = $Parsedown->text($contentMD);
  74. $description = substr(strip_tags($contentHTML),0,150);
  75. $description = trim(preg_replace('/\s+/', ' ', $description));
  76. /*
  77. $timer['topiccontroller']=microtime(true);
  78. $timer['end topiccontroller']=microtime(true);
  79. Helpers::printTimer($timer);
  80. */
  81. $route = empty($args) && $settings['startpage'] ? '/cover.twig' : '/index.twig';
  82. $this->c->view->render($response, $route, array('navigation' => $structure, 'content' => $contentHTML, 'item' => $item, 'breadcrumb' => $breadcrumb, 'settings' => $settings, 'description' => $description, 'base_url' => $base_url ));
  83. }
  84. protected function getCachedStructure($cache)
  85. {
  86. return $cache->getData('structure');
  87. }
  88. protected function getFreshStructure($pathToContent, $cache, $uri)
  89. {
  90. /* scan the content of the folder */
  91. $structure = Folder::scanFolder($pathToContent);
  92. /* if there is no content, render an empty page */
  93. if(count($structure) == 0)
  94. {
  95. return false;
  96. }
  97. /* create an array of object with the whole content of the folder */
  98. $structure = Folder::getFolderContentDetails($structure, $uri->getBaseUrl(), $uri->getBasePath());
  99. /* cache navigation */
  100. $cache->refresh($structure, 'structure');
  101. return $structure;
  102. }
  103. }
  104. ?>