MetaApiController.php 10 KB

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