MetaApiController.php 9.5 KB

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