MetaApiController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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\WriteMeta;
  7. use Typemill\Models\Folder;
  8. class MetaApiController extends ContentController
  9. {
  10. # get the standard meta-definitions and the meta-definitions from plugins (same for all sites)
  11. public function getMetaDefinitions(Request $request, Response $response, $args)
  12. {
  13. $metatabs = $this->aggregateMetaDefinitions();
  14. return $response->withJson(array('definitions' => $metatabs, 'errors' => false));
  15. }
  16. # get the standard meta-definitions and the meta-definitions from plugins (same for all sites)
  17. public function aggregateMetaDefinitions($folder = null)
  18. {
  19. $writeYaml = new writeYaml();
  20. $metatabs = $writeYaml->getYaml('system' . DIRECTORY_SEPARATOR . 'author', 'metatabs.yaml');
  21. # add radio buttons to choose posts or pages for folder.
  22. if($folder)
  23. {
  24. $metatabs['meta']['fields']['contains'] = [
  25. 'type' => 'radio',
  26. 'label' => 'This folder contains:',
  27. 'options' => ['pages' => 'PAGES (sort in navigation with drag & drop)', 'posts' => 'POSTS (sorted by publish date, for news or blogs)'],
  28. 'class' => 'medium'
  29. ];
  30. }
  31. # loop through all plugins
  32. foreach($this->settings['plugins'] as $name => $plugin)
  33. {
  34. if($plugin['active'])
  35. {
  36. $pluginSettings = \Typemill\Settings::getObjectSettings('plugins', $name);
  37. if($pluginSettings && isset($pluginSettings['metatabs']))
  38. {
  39. $metatabs = array_merge_recursive($metatabs, $pluginSettings['metatabs']);
  40. }
  41. }
  42. }
  43. # add the meta from theme settings here
  44. $themeSettings = \Typemill\Settings::getObjectSettings('themes', $this->settings['theme']);
  45. if($themeSettings && isset($themeSettings['metatabs']))
  46. {
  47. $metatabs = array_merge_recursive($metatabs, $themeSettings['metatabs']);
  48. }
  49. return $metatabs;
  50. }
  51. public function getArticleMetaObject(Request $request, Response $response, $args)
  52. {
  53. /* get params from call */
  54. $this->params = $request->getParams();
  55. $this->uri = $request->getUri();
  56. # set structure
  57. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  58. # set item
  59. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  60. $writeMeta = new writeMeta();
  61. $pagemeta = $writeMeta->getPageMeta($this->settings, $this->item);
  62. if(!$pagemeta)
  63. {
  64. # set the status for published and drafted
  65. $this->setPublishStatus();
  66. # set path
  67. $this->setItemPath($this->item->fileType);
  68. # read content from file
  69. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  70. $pagemeta = $writeMeta->getPageMetaBlank($this->content, $this->settings, $this->item);
  71. }
  72. # if item is a folder
  73. if($this->item->elementType == "folder" && isset($this->item->contains))
  74. {
  75. $pagemeta['meta']['contains'] = isset($pagemeta['meta']['contains']) ? $pagemeta['meta']['contains'] : $this->item->contains;
  76. # get global metadefinitions
  77. $metadefinitions = $this->aggregateMetaDefinitions($folder = true);
  78. }
  79. else
  80. {
  81. # get global metadefinitions
  82. $metadefinitions = $this->aggregateMetaDefinitions();
  83. }
  84. $metadata = [];
  85. $metascheme = [];
  86. foreach($metadefinitions as $tabname => $tab )
  87. {
  88. $metadata[$tabname] = [];
  89. foreach($tab['fields'] as $fieldname => $fielddefinitions)
  90. {
  91. $metascheme[$tabname][$fieldname] = true;
  92. $metadata[$tabname][$fieldname] = isset($pagemeta[$tabname][$fieldname]) ? $pagemeta[$tabname][$fieldname] : null;
  93. }
  94. }
  95. # store the metascheme in cache for frontend
  96. $writeMeta->updateYaml('cache', 'metatabs.yaml', $metascheme);
  97. return $response->withJson(array('metadata' => $metadata, 'metadefinitions' => $metadefinitions, 'item' => $this->item, 'errors' => false));
  98. }
  99. public function updateArticleMeta(Request $request, Response $response, $args)
  100. {
  101. # get params from call
  102. $this->params = $request->getParams();
  103. $this->uri = $request->getUri();
  104. $tab = isset($this->params['tab']) ? $this->params['tab'] : false;
  105. $metaInput = isset($this->params['data']) ? $this->params['data'] : false ;
  106. $objectName = 'meta';
  107. $errors = false;
  108. if(!$tab or !$metaInput)
  109. {
  110. return $response->withJson($this->errors, 404);
  111. }
  112. # set structure
  113. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  114. # set item
  115. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  116. # if item is a folder
  117. if($this->item->elementType == "folder")
  118. {
  119. $pagemeta['meta']['contains'] = isset($pagemeta['meta']['contains']) ? $pagemeta['meta']['contains'] : $this->item->contains;
  120. # get global metadefinitions
  121. $metaDefinitions = $this->aggregateMetaDefinitions($folder = true);
  122. }
  123. else
  124. {
  125. # get global metadefinitions
  126. $metaDefinitions = $this->aggregateMetaDefinitions();
  127. }
  128. # create validation object
  129. $validate = $this->getValidator();
  130. # take the user input data and iterate over all fields and values
  131. foreach($metaInput as $fieldName => $fieldValue)
  132. {
  133. # get the corresponding field definition from original plugin settings */
  134. $fieldDefinition = isset($metaDefinitions[$tab]['fields'][$fieldName]) ? $metaDefinitions[$tab]['fields'][$fieldName] : false;
  135. if(!$fieldDefinition)
  136. {
  137. $errors[$tab][$fieldName] = 'This field is not defined';
  138. }
  139. else
  140. {
  141. # validate user input for this field
  142. $result = $validate->objectField($fieldName, $fieldValue, $objectName, $fieldDefinition);
  143. if($result !== true)
  144. {
  145. $errors[$tab][$fieldName] = $result[$fieldName][0];
  146. }
  147. }
  148. }
  149. # return validation errors
  150. if($errors){ return $response->withJson(array('errors' => $errors),422); }
  151. $writeMeta = new writeMeta();
  152. # get existing metadata for page
  153. $metaPage = $writeMeta->getYaml($this->settings['contentFolder'], $this->item->pathWithoutType . '.yaml');
  154. # get extended structure
  155. $extended = $writeMeta->getYaml('cache', 'structure-extended.yaml');
  156. # flag for changed structure
  157. $structure = false;
  158. if($tab == 'meta')
  159. {
  160. # if manual date has been modified
  161. if( $this->hasChanged($metaInput, $metaPage['meta'], 'manualdate'))
  162. {
  163. # update the time
  164. $metaInput['time'] = date('H-i-s', time());
  165. # if it is a post, then rename the post
  166. if($this->item->elementType == "file" && strlen($this->item->order) == 12)
  167. {
  168. # create file-prefix with date
  169. $datetime = $metaInput['manualdate'] . '-' . $metaInput['time'];
  170. $datetime = implode(explode('-', $datetime));
  171. $datetime = substr($datetime,0,12);
  172. # create the new filename
  173. $pathWithoutFile = str_replace($this->item->originalName, "", $this->item->path);
  174. $newPathWithoutType = $pathWithoutFile . $datetime . '-' . $this->item->slug;
  175. $writeMeta->renamePost($this->item->pathWithoutType, $newPathWithoutType);
  176. # recreate the draft structure
  177. $this->setStructure($draft = true, $cache = false);
  178. # update item
  179. $this->setItem();
  180. }
  181. }
  182. # if folder has changed and contains pages instead of posts or posts instead of pages
  183. if($this->item->elementType == "folder" && isset($metaInput['contains']) && $this->hasChanged($metaInput, $metaPage['meta'], 'contains'))
  184. {
  185. $structure = true;
  186. if($metaInput['contains'] == "posts")
  187. {
  188. $writeMeta->transformPagesToPosts($this->item);
  189. }
  190. if($metaInput['contains'] == "pages")
  191. {
  192. $writeMeta->transformPostsToPages($this->item);
  193. }
  194. }
  195. # normalize the meta-input
  196. $metaInput['navtitle'] = (isset($metaInput['navtitle']) && $metaInput['navtitle'] !== null )? $metaInput['navtitle'] : '';
  197. $metaInput['hide'] = (isset($metaInput['hide']) && $metaInput['hide'] !== null) ? $metaInput['hide'] : false;
  198. # input values are empty but entry in structure exists
  199. if(!$metaInput['hide'] && $metaInput['navtitle'] == "" && isset($extended[$this->item->urlRelWoF]))
  200. {
  201. # delete the entry in the structure
  202. unset($extended[$this->item->urlRelWoF]);
  203. $structure = true;
  204. }
  205. elseif(
  206. # check if navtitle or hide-value has been changed
  207. ($this->hasChanged($metaInput, $metaPage['meta'], 'navtitle'))
  208. OR
  209. ($this->hasChanged($metaInput, $metaPage['meta'], 'hide'))
  210. )
  211. {
  212. # add new file data. Also makes sure that the value is set.
  213. $extended[$this->item->urlRelWoF] = ['hide' => $metaInput['hide'], 'navtitle' => $metaInput['navtitle']];
  214. $structure = true;
  215. }
  216. }
  217. # add the new/edited metadata
  218. $metaPage[$tab] = $metaInput;
  219. # store the metadata
  220. $writeMeta->updateYaml($this->settings['contentFolder'], $this->item->pathWithoutType . '.yaml', $metaPage);
  221. if($structure)
  222. {
  223. # store the extended file
  224. $writeMeta->updateYaml('cache', 'structure-extended.yaml', $extended);
  225. # recreate the draft structure
  226. $this->setStructure($draft = true, $cache = false);
  227. # update item
  228. $this->setItem();
  229. # set item in navigation active again
  230. $activeItem = Folder::getItemForUrl($this->structure, $this->item->urlRel, $this->uri->getBaseUrl());
  231. # send new structure to frontend
  232. $structure = $this->structure;
  233. }
  234. # return with the new metadata
  235. return $response->withJson(array('metadata' => $metaInput, 'structure' => $structure, 'item' => $this->item, 'errors' => false));
  236. }
  237. protected function hasChanged($input, $page, $field)
  238. {
  239. if(isset($input[$field]) && isset($page[$field]) && $input[$field] == $page[$field])
  240. {
  241. return false;
  242. }
  243. if(!isset($input[$field]) && !isset($input[$field]))
  244. {
  245. return false;
  246. }
  247. return true;
  248. }
  249. }