MetaApiController.php 12 KB

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