MetaApiController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. $pluginSettings = \Typemill\Settings::getObjectSettings('plugins', $name);
  26. if($pluginSettings && isset($pluginSettings['metatabs']))
  27. {
  28. $metatabs = array_merge_recursive($metatabs, $pluginSettings['metatabs']);
  29. }
  30. }
  31. return $metatabs;
  32. }
  33. public function getArticleMetaObject(Request $request, Response $response, $args)
  34. {
  35. /* get params from call */
  36. $this->params = $request->getParams();
  37. $this->uri = $request->getUri();
  38. # set structure
  39. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  40. # set item
  41. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  42. $writeYaml = new writeYaml();
  43. $pagemeta = $writeYaml->getPageMeta($this->settings, $this->item);
  44. if(!$pagemeta)
  45. {
  46. # set the status for published and drafted
  47. $this->setPublishStatus();
  48. # set path
  49. $this->setItemPath($this->item->fileType);
  50. # read content from file
  51. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  52. $pagemeta = $writeYaml->getPageMetaDefaults($this->content, $this->settings, $this->item);
  53. }
  54. # get global metadefinitions
  55. $metadefinitions = $this->aggregateMetaDefinitions();
  56. $metadata = [];
  57. foreach($metadefinitions as $tabname => $tab )
  58. {
  59. $metadata[$tabname] = [];
  60. foreach($tab['fields'] as $fieldname => $fielddefinitions)
  61. {
  62. $metadata[$tabname][$fieldname] = isset($pagemeta[$tabname][$fieldname]) ? $pagemeta[$tabname][$fieldname] : null;
  63. }
  64. }
  65. return $response->withJson(array('metadata' => $metadata, 'metadefinitions' => $metadefinitions, 'errors' => false));
  66. }
  67. public function updateArticleMeta(Request $request, Response $response, $args)
  68. {
  69. /* get params from call */
  70. $this->params = $request->getParams();
  71. $this->uri = $request->getUri();
  72. $tab = isset($this->params['tab']) ? $this->params['tab'] : false;
  73. $metaData = isset($this->params['data']) ? $this->params['data'] : false ;
  74. $objectName = 'meta';
  75. $errors = false;
  76. if(!$tab or !$metaData)
  77. {
  78. return $response->withJson($this->errors, 404);
  79. }
  80. # load metadefinitions
  81. $metaDefinitions = $this->aggregateMetaDefinitions();
  82. # create validation object
  83. $validate = $this->getValidator();
  84. # take the user input data and iterate over all fields and values
  85. foreach($metaData as $fieldName => $fieldValue)
  86. {
  87. # get the corresponding field definition from original plugin settings */
  88. $fieldDefinition = isset($metaDefinitions[$tab]['fields'][$fieldName]) ? $metaDefinitions[$tab]['fields'][$fieldName] : false;
  89. if(!$fieldDefinition)
  90. {
  91. $errors[$tab][$fieldName] = 'This field is not defined';
  92. }
  93. else
  94. {
  95. # validate user input for this field
  96. $result = $validate->objectField($fieldName, $fieldValue, $objectName, $fieldDefinition);
  97. if($result !== true)
  98. {
  99. $errors[$tab][$fieldName] = $result[$fieldName][0];
  100. }
  101. }
  102. }
  103. # return validation errors
  104. if($errors){ return $response->withJson(array('errors' => $errors),422); }
  105. # set structure
  106. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  107. # set item
  108. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  109. $writeYaml = new writeYaml();
  110. # get existing metadata for page
  111. $meta = $writeYaml->getYaml($this->settings['contentFolder'], $this->item->pathWithoutType . '.yaml');
  112. # add the new/edited metadata
  113. $meta[$tab] = $metaData;
  114. # store the metadata
  115. $writeYaml->updateYaml($this->settings['contentFolder'], $this->item->pathWithoutType . '.yaml', $meta);
  116. # return with the new metadata
  117. return $response->withJson(array('metadata' => $metaData, 'errors' => false));
  118. }
  119. }