MetaApiController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use Typemill\Models\WriteYaml;
  6. class MetaApiController extends ContentController
  7. {
  8. # get the standard meta-definitions and the meta-definitions from plugins (same for all sites)
  9. public function getMetaDefinitions(Request $request, Response $response, $args)
  10. {
  11. $metatabs = $this->aggregateMetaDefinitions();
  12. return $response->withJson(array('definitions' => $metatabs, 'errors' => false));
  13. }
  14. # get the standard meta-definitions and the meta-definitions from plugins (same for all sites)
  15. public function aggregateMetaDefinitions()
  16. {
  17. $writeYaml = new writeYaml();
  18. $metatabs = $writeYaml->getYaml('system' . DIRECTORY_SEPARATOR . 'author', 'metatabs.yaml');
  19. # load cached metadefinitions
  20. # check if valid
  21. # if not, refresh cache
  22. # loop through all plugins
  23. foreach($this->settings['plugins'] as $name => $plugin)
  24. {
  25. if($plugin['active'])
  26. {
  27. $pluginSettings = \Typemill\Settings::getObjectSettings('plugins', $name);
  28. if($pluginSettings && isset($pluginSettings['metatabs']))
  29. {
  30. $metatabs = array_merge_recursive($metatabs, $pluginSettings['metatabs']);
  31. }
  32. }
  33. }
  34. return $metatabs;
  35. }
  36. public function getArticleMetaObject(Request $request, Response $response, $args)
  37. {
  38. /* get params from call */
  39. $this->params = $request->getParams();
  40. $this->uri = $request->getUri();
  41. # set structure
  42. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  43. # set item
  44. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  45. $writeYaml = new writeYaml();
  46. $pagemeta = $writeYaml->getPageMeta($this->settings, $this->item);
  47. if(!$pagemeta)
  48. {
  49. # set the status for published and drafted
  50. $this->setPublishStatus();
  51. # set path
  52. $this->setItemPath($this->item->fileType);
  53. # read content from file
  54. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  55. $pagemeta = $writeYaml->getPageMetaDefaults($this->content, $this->settings, $this->item);
  56. }
  57. # get global metadefinitions
  58. $metadefinitions = $this->aggregateMetaDefinitions();
  59. $metadata = [];
  60. foreach($metadefinitions as $tabname => $tab )
  61. {
  62. $metadata[$tabname] = [];
  63. foreach($tab['fields'] as $fieldname => $fielddefinitions)
  64. {
  65. $metadata[$tabname][$fieldname] = isset($pagemeta[$tabname][$fieldname]) ? $pagemeta[$tabname][$fieldname] : null;
  66. }
  67. }
  68. return $response->withJson(array('metadata' => $metadata, 'metadefinitions' => $metadefinitions, 'errors' => false));
  69. }
  70. public function updateArticleMeta(Request $request, Response $response, $args)
  71. {
  72. /* get params from call */
  73. $this->params = $request->getParams();
  74. $this->uri = $request->getUri();
  75. $tab = isset($this->params['tab']) ? $this->params['tab'] : false;
  76. $metaData = isset($this->params['data']) ? $this->params['data'] : false ;
  77. $objectName = 'meta';
  78. $errors = false;
  79. if(!$tab or !$metaData)
  80. {
  81. return $response->withJson($this->errors, 404);
  82. }
  83. # load metadefinitions
  84. $metaDefinitions = $this->aggregateMetaDefinitions();
  85. # create validation object
  86. $validate = $this->getValidator();
  87. # take the user input data and iterate over all fields and values
  88. foreach($metaData as $fieldName => $fieldValue)
  89. {
  90. # get the corresponding field definition from original plugin settings */
  91. $fieldDefinition = isset($metaDefinitions[$tab]['fields'][$fieldName]) ? $metaDefinitions[$tab]['fields'][$fieldName] : false;
  92. if(!$fieldDefinition)
  93. {
  94. $errors[$tab][$fieldName] = 'This field is not defined';
  95. }
  96. else
  97. {
  98. # validate user input for this field
  99. $result = $validate->objectField($fieldName, $fieldValue, $objectName, $fieldDefinition);
  100. if($result !== true)
  101. {
  102. $errors[$tab][$fieldName] = $result[$fieldName][0];
  103. }
  104. }
  105. }
  106. # return validation errors
  107. if($errors){ return $response->withJson(array('errors' => $errors),422); }
  108. # set structure
  109. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  110. # set item
  111. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  112. $writeYaml = new writeYaml();
  113. # get existing metadata for page
  114. $meta = $writeYaml->getYaml($this->settings['contentFolder'], $this->item->pathWithoutType . '.yaml');
  115. # add the new/edited metadata
  116. $meta[$tab] = $metaData;
  117. # store the metadata
  118. $writeYaml->updateYaml($this->settings['contentFolder'], $this->item->pathWithoutType . '.yaml', $meta);
  119. # return with the new metadata
  120. return $response->withJson(array('metadata' => $metaData, 'errors' => false));
  121. }
  122. }
  123. # check models -> writeYaml for getPageMeta and getPageMetaDefaults.