MetaApiController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use Typemill\Models\WriteYaml;
  6. use Typemill\Models\Folder;
  7. class MetaApiController extends ContentController
  8. {
  9. # get the standard meta-definitions and the meta-definitions from plugins (same for all sites)
  10. public function getMetaDefinitions(Request $request, Response $response, $args)
  11. {
  12. $metatabs = $this->aggregateMetaDefinitions();
  13. return $response->withJson(array('definitions' => $metatabs, 'errors' => false));
  14. }
  15. # get the standard meta-definitions and the meta-definitions from plugins (same for all sites)
  16. public function aggregateMetaDefinitions()
  17. {
  18. $writeYaml = new writeYaml();
  19. $metatabs = $writeYaml->getYaml('system' . DIRECTORY_SEPARATOR . 'author', 'metatabs.yaml');
  20. # loop through all plugins
  21. foreach($this->settings['plugins'] as $name => $plugin)
  22. {
  23. if($plugin['active'])
  24. {
  25. $pluginSettings = \Typemill\Settings::getObjectSettings('plugins', $name);
  26. if($pluginSettings && isset($pluginSettings['metatabs']))
  27. {
  28. $metatabs = array_merge_recursive($metatabs, $pluginSettings['metatabs']);
  29. }
  30. }
  31. }
  32. # add the meta from theme settings here
  33. $themeSettings = \Typemill\Settings::getObjectSettings('themes', $this->settings['theme']);
  34. if($themeSettings && isset($themeSettings['metatabs']))
  35. {
  36. $metatabs = array_merge_recursive($metatabs, $themeSettings['metatabs']);
  37. }
  38. return $metatabs;
  39. }
  40. public function getArticleMetaObject(Request $request, Response $response, $args)
  41. {
  42. /* get params from call */
  43. $this->params = $request->getParams();
  44. $this->uri = $request->getUri();
  45. # set structure
  46. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  47. # set item
  48. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  49. $writeYaml = new writeYaml();
  50. $pagemeta = $writeYaml->getPageMeta($this->settings, $this->item);
  51. if(!$pagemeta)
  52. {
  53. # set the status for published and drafted
  54. $this->setPublishStatus();
  55. # set path
  56. $this->setItemPath($this->item->fileType);
  57. # read content from file
  58. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  59. $pagemeta = $writeYaml->getPageMetaDefaults($this->content, $this->settings, $this->item);
  60. }
  61. # get global metadefinitions
  62. $metadefinitions = $this->aggregateMetaDefinitions();
  63. $metadata = [];
  64. $metascheme = [];
  65. foreach($metadefinitions as $tabname => $tab )
  66. {
  67. $metadata[$tabname] = [];
  68. foreach($tab['fields'] as $fieldname => $fielddefinitions)
  69. {
  70. $metascheme[$tabname][$fieldname] = true;
  71. $metadata[$tabname][$fieldname] = isset($pagemeta[$tabname][$fieldname]) ? $pagemeta[$tabname][$fieldname] : null;
  72. }
  73. }
  74. # store the metascheme in cache for frontend
  75. $writeYaml->updateYaml('cache', 'metatabs.yaml', $metascheme);
  76. return $response->withJson(array('metadata' => $metadata, 'metadefinitions' => $metadefinitions, 'errors' => false));
  77. }
  78. public function updateArticleMeta(Request $request, Response $response, $args)
  79. {
  80. # get params from call
  81. $this->params = $request->getParams();
  82. $this->uri = $request->getUri();
  83. $tab = isset($this->params['tab']) ? $this->params['tab'] : false;
  84. $metaInput = isset($this->params['data']) ? $this->params['data'] : false ;
  85. $objectName = 'meta';
  86. $errors = false;
  87. if(!$tab or !$metaInput)
  88. {
  89. return $response->withJson($this->errors, 404);
  90. }
  91. # load metadefinitions
  92. $metaDefinitions = $this->aggregateMetaDefinitions();
  93. # create validation object
  94. $validate = $this->getValidator();
  95. # take the user input data and iterate over all fields and values
  96. foreach($metaInput as $fieldName => $fieldValue)
  97. {
  98. # get the corresponding field definition from original plugin settings */
  99. $fieldDefinition = isset($metaDefinitions[$tab]['fields'][$fieldName]) ? $metaDefinitions[$tab]['fields'][$fieldName] : false;
  100. if(!$fieldDefinition)
  101. {
  102. $errors[$tab][$fieldName] = 'This field is not defined';
  103. }
  104. else
  105. {
  106. # validate user input for this field
  107. $result = $validate->objectField($fieldName, $fieldValue, $objectName, $fieldDefinition);
  108. if($result !== true)
  109. {
  110. $errors[$tab][$fieldName] = $result[$fieldName][0];
  111. }
  112. }
  113. }
  114. # return validation errors
  115. if($errors){ return $response->withJson(array('errors' => $errors),422); }
  116. # set structure
  117. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  118. # set item
  119. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  120. $writeYaml = new writeYaml();
  121. # get existing metadata for page
  122. $metaPage = $writeYaml->getYaml($this->settings['contentFolder'], $this->item->pathWithoutType . '.yaml');
  123. # get extended structure
  124. $extended = $writeYaml->getYaml('cache', 'structure-extended.yaml');
  125. # flag for changed structure
  126. $structure = false;
  127. if($tab == 'meta')
  128. {
  129. # normalize the meta-input
  130. $metaInput['navtitle'] = (isset($metaInput['navtitle']) && $metaInput['navtitle'] !== null )? $metaInput['navtitle'] : '';
  131. $metaInput['hide'] = (isset($metaInput['hide']) && $metaInput['hide'] !== null) ? $metaInput['hide'] : false;
  132. # input values are empty but entry in structure exists
  133. if(!$metaInput['hide'] && $metaInput['navtitle'] == "" && isset($extended[$this->item->urlRelWoF]))
  134. {
  135. # delete the entry in the structure
  136. unset($extended[$this->item->urlRelWoF]);
  137. $structure = true;
  138. }
  139. # check if navtitle or hide-value has been changed
  140. elseif(
  141. ($metaPage['meta']['navtitle'] != $metaInput['navtitle'])
  142. OR
  143. ($metaPage['meta']['hide'] != $metaInput['hide'])
  144. )
  145. {
  146. # add new file data. Also makes sure that the value is set.
  147. $extended[$this->item->urlRelWoF] = ['hide' => $metaInput['hide'], 'navtitle' => $metaInput['navtitle']];
  148. $structure = true;
  149. }
  150. if($structure)
  151. {
  152. # store the file
  153. $writeYaml->updateYaml('cache', 'structure-extended.yaml', $extended);
  154. # recreate the draft structure
  155. $this->setStructure($draft = true, $cache = false);
  156. # set item in navigation active again
  157. $activeItem = Folder::getItemForUrl($this->structure, $this->item->urlRel, $this->uri->getBaseUrl());
  158. # send new structure to frontend
  159. $structure = $this->structure;
  160. }
  161. }
  162. # add the new/edited metadata
  163. $meta[$tab] = $metaInput;
  164. # store the metadata
  165. $writeYaml->updateYaml($this->settings['contentFolder'], $this->item->pathWithoutType . '.yaml', $meta);
  166. # return with the new metadata
  167. return $response->withJson(array('metadata' => $metaInput, 'structure' => $structure, 'errors' => false));
  168. }
  169. }
  170. # check models -> writeYaml for getPageMeta and getPageMetaDefaults.