WriteYaml.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Typemill\Models;
  3. use Typemill\Extensions\ParsedownExtension;
  4. class WriteYaml extends Write
  5. {
  6. /**
  7. * Get the a yaml file.
  8. * @param string $fileName is the name of the Yaml Folder.
  9. * @param string $yamlFileName is the name of the Yaml File.
  10. */
  11. public function getYaml($folderName, $yamlFileName)
  12. {
  13. $yaml = $this->getFile($folderName, $yamlFileName);
  14. if($yaml)
  15. {
  16. return \Symfony\Component\Yaml\Yaml::parse($yaml);
  17. }
  18. return false;
  19. }
  20. /**
  21. * Writes a yaml file.
  22. * @param string $fileName is the name of the Yaml Folder.
  23. * @param string $yamlFileName is the name of the Yaml File.
  24. * @param array $contentArray is the content as an array.
  25. */
  26. public function updateYaml($folderName, $yamlFileName, $contentArray)
  27. {
  28. $yaml = \Symfony\Component\Yaml\Yaml::dump($contentArray,6);
  29. if($this->writeFile($folderName, $yamlFileName, $yaml))
  30. {
  31. return true;
  32. }
  33. return false;
  34. }
  35. # used by contentApiController (backend) and pageController (frontend)
  36. public function getPageMeta($settings, $item)
  37. {
  38. $meta = $this->getYaml($settings['contentFolder'], $item->pathWithoutType . '.yaml');
  39. if(!$meta)
  40. {
  41. return false;
  42. }
  43. # compare with meta that are in use right now (e.g. changed theme, disabled plugin)
  44. $metascheme = $this->getYaml('cache', 'metatabs.yaml');
  45. if($metascheme)
  46. {
  47. $meta = $this->whitelistMeta($meta,$metascheme);
  48. }
  49. $meta = $this->addFileTimeToMeta($meta, $item, $settings);
  50. return $meta;
  51. }
  52. # used by contentApiController (backend) and pageController (frontend)
  53. public function getPageMetaDefaults($content, $settings, $item)
  54. {
  55. # initialize parsedown extension
  56. $parsedown = new ParsedownExtension();
  57. # if content is not an array, then transform it
  58. if(!is_array($content))
  59. {
  60. # turn markdown into an array of markdown-blocks
  61. $content = $parsedown->markdownToArrayBlocks($content);
  62. }
  63. $title = false;
  64. # delete markdown from title
  65. if(isset($content[0]))
  66. {
  67. $title = trim($content[0], "# ");
  68. }
  69. $description = false;
  70. # delete markdown from title
  71. if(isset($content[1]))
  72. {
  73. $firstLineArray = $parsedown->text($content[1]);
  74. $description = strip_tags($parsedown->markup($firstLineArray, $item->urlAbs));
  75. $description = substr($description, 0, 300);
  76. $lastSpace = strrpos($description, ' ');
  77. $description = substr($description, 0, $lastSpace);
  78. }
  79. $author = $settings['author'];
  80. if(isset($_SESSION))
  81. {
  82. if(isset($_SESSION['firstname']) && $_SESSION['firstname'] !='' && isset($_SESSION['lastname']) && $_SESSION['lastname'] != '')
  83. {
  84. $author = $_SESSION['firstname'] . ' ' . $_SESSION['lastname'];
  85. }
  86. elseif(isset($_SESSION['user']))
  87. {
  88. $author = $_SESSION['user'];
  89. }
  90. }
  91. # create new meta-file
  92. $meta = [
  93. 'meta' => [
  94. 'title' => $title,
  95. 'description' => $description,
  96. 'author' => $author,
  97. 'created' => date("Y-m-d"),
  98. ]
  99. ];
  100. $this->updateYaml($settings['contentFolder'], $item->pathWithoutType . '.yaml', $meta);
  101. $meta = $this->addFileTimeToMeta($meta, $item, $settings);
  102. return $meta;
  103. }
  104. private function whitelistMeta($meta, $metascheme)
  105. {
  106. # we have only 2 dimensions, so no recursive needed
  107. foreach($meta as $tab => $values)
  108. {
  109. if(!isset($metascheme[$tab]))
  110. {
  111. unset($meta[$tab]);
  112. }
  113. foreach($values as $key => $value)
  114. {
  115. if(!isset($metascheme[$tab][$key]))
  116. {
  117. unset($meta[$tab][$key]);
  118. }
  119. }
  120. }
  121. return $meta;
  122. }
  123. private function addFileTimeToMeta($meta, $item, $settings)
  124. {
  125. $filePath = $settings['contentFolder'] . $item->path;
  126. $fileType = isset($item->fileType) ? $item->fileType : 'md';
  127. # check if url is a folder.
  128. if($item->elementType == 'folder')
  129. {
  130. $filePath = $settings['contentFolder'] . $item->path . DIRECTORY_SEPARATOR . 'index.'. $fileType;
  131. }
  132. # add the modified date for the file
  133. $meta['meta']['modified'] = file_exists($filePath) ? date("Y-m-d",filemtime($filePath)) : false;
  134. return $meta;
  135. }
  136. }