PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/sly/Controller/Contentmeta.php

https://bitbucket.org/phoenixh/sallycms-backend
PHP | 331 lines | 241 code | 61 blank | 29 comment | 25 complexity | 730d17efb29b167fe15dc9f8baa4eea0 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2014, webvariants GmbH & Co. KG, http://www.webvariants.de
  4. *
  5. * This file is released under the terms of the MIT license. You can find the
  6. * complete text in the attached LICENSE file or online at:
  7. *
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. class sly_Controller_Contentmeta extends sly_Controller_Content_Base {
  11. public function indexAction() {
  12. $this->init();
  13. if ($this->header() !== true) return;
  14. $post = $this->getRequest()->post;
  15. $container = $this->getContainer();
  16. $userService = $container['sly-service-user'];
  17. $artService = $container['sly-service-article'];
  18. $page = $this->getRequest()->get('p_revisions', 'int', 0);
  19. $perPage = 20;
  20. $revisions = $artService->findAllRevisions($this->article->getId(), $this->article->getClang(), $page * $perPage, $perPage);
  21. $this->render('content/meta/index.phtml', array(
  22. 'article' => $this->article,
  23. 'user' => $userService->getCurrentUser(),
  24. 'clangB' => $post->get('clang_b', 'int'),
  25. 'revisions' => $revisions,
  26. 'userService' => $userService,
  27. 'perPage' => $perPage,
  28. 'total' => $artService->countRevisions($this->article)
  29. ), false);
  30. }
  31. protected function getPageName() {
  32. return 'contentmeta';
  33. }
  34. public function checkPermission($action) {
  35. $hasPermission = parent::checkPermission($action);
  36. $request = $this->getRequest();
  37. if ($action === 'deleterevision') {
  38. $user = $this->getCurrentUser();
  39. $articleId = $request->request('article_id', 'int', 0);
  40. $hasPermission = sly_Backend_Authorisation_Util::canEditArticle($user, $articleId);
  41. }
  42. if ($request->isMethod('POST')) {
  43. sly_Util_Csrf::checkToken();
  44. }
  45. return $hasPermission;
  46. }
  47. public function processmetaformAction() {
  48. $this->init();
  49. $post = $this->getRequest()->post;
  50. try {
  51. // save metadata
  52. if ($post->has('save_meta')) {
  53. return $this->saveMeta();
  54. }
  55. // make article the startarticle
  56. elseif ($post->has('to_startarticle') && $this->canConvertToStartArticle()) {
  57. return $this->convertToStartArticle();
  58. }
  59. // copy content to another language
  60. elseif ($post->has('copy_content')) {
  61. return $this->copyContent();
  62. }
  63. // move article to other category
  64. elseif ($post->has('move_article')) {
  65. return $this->moveArticle();
  66. }
  67. elseif ($post->has('copy_article')) {
  68. return $this->copyArticle();
  69. }
  70. elseif ($post->has('move_category')) {
  71. return $this->moveCategory();
  72. }
  73. }
  74. catch (Exception $e) {
  75. $this->getFlashMessage()->appendWarning($e->getMessage());
  76. }
  77. $this->indexAction();
  78. }
  79. public function deleterevisionAction() {
  80. $this->init();
  81. $flash = $this->getFlashMessage();
  82. try {
  83. $this->getContainer()->getArticleService()->purgeArticleRevision($this->article);
  84. $flash->appendInfo(t('article_revision_deleted'));
  85. } catch (Exception $e) {
  86. $flash->appendWarning(t('cannont_delete_article_revision'));
  87. }
  88. $this->redirectToArticle();
  89. }
  90. private function saveMeta() {
  91. $flash = $this->getContainer()->getFlashMessage();
  92. // notify system
  93. $flash->appendInfo(t('metadata_updated'));
  94. $this->getContainer()->getDispatcher()->notify('SLY_ART_META_UPDATED', $this->article, array(
  95. 'id' => $this->article->getId(), // deprecated
  96. 'clang' => $this->article->getClang() // deprecated
  97. ));
  98. return $this->redirectToArticle();
  99. }
  100. private function convertToStartArticle() {
  101. $flash = $this->getContainer()->getFlashMessage();
  102. $service = $this->getContainer()->getArticleService();
  103. try {
  104. $service->convertToStartArticle($this->article->getId());
  105. $flash->appendInfo(t('article_converted_to_startarticle'));
  106. }
  107. catch (sly_Exception $e) {
  108. $flash->appendWarning(t('cannot_convert_to_startarticle').': '.$e->getMessage());
  109. }
  110. return $this->redirectToArticle();
  111. }
  112. private function copyContent() {
  113. $request = $this->getRequest();
  114. $srcClang = $request->post('clang', 'int', 0);
  115. $dstClangs = array_unique($request->postArray('clang_b', 'int'));
  116. $user = $this->getCurrentUser();
  117. $infos = array();
  118. $errs = array();
  119. $articleService = $this->getContainer()->getArticleService();
  120. if (empty($dstClangs)) {
  121. throw new sly_Authorisation_Exception(t('no_language_selected'));
  122. }
  123. if (!sly_Util_Language::hasPermissionOnLanguage($user, $srcClang)) {
  124. $lang = sly_Util_Language::findById($srcClang);
  125. throw new sly_Authorisation_Exception(t('you_have_no_access_to_this_language', sly_translate($lang->getName())));
  126. }
  127. foreach ($dstClangs as $targetClang) {
  128. if (!sly_Util_Language::hasPermissionOnLanguage($user, $targetClang)) {
  129. $lang = sly_Util_Language::findById($targetClang);
  130. $errs[$targetClang] = t('you_have_no_access_to_this_language', sly_translate($lang->getName()));
  131. continue;
  132. }
  133. if (!$this->canCopyContent($targetClang)) {
  134. $errs[$targetClang] = t('no_rights_to_this_function');
  135. continue;
  136. }
  137. try {
  138. if ($targetClang === $this->article->getClang()) {
  139. continue;
  140. }
  141. $target = $articleService->findByPK($this->article->getId(), $targetClang);
  142. $target = $articleService->touch($target);
  143. $articleService->copyContent($this->article, $target, $user);
  144. $infos[$targetClang] = t('article_content_copied');
  145. }
  146. catch (sly_Exception $e) {
  147. $errs[$targetClang] = t('cannot_copy_article_content').': '.$e->getMessage();
  148. }
  149. }
  150. // only prepend language names if there were more than one language
  151. if (count($dstClangs) > 1) {
  152. foreach ($infos as $clang => $msg) {
  153. $lang = sly_Util_Language::findById($clang);
  154. $infos[$clang] = sly_translate($lang->getName()).': '.$msg;
  155. }
  156. foreach ($errs as $clang => $msg) {
  157. $lang = sly_Util_Language::findById($clang);
  158. $errs[$clang] = sly_translate($lang->getName()).': '.$msg;
  159. }
  160. }
  161. $flash = $this->getFlashMessage();
  162. foreach ($infos as $msg) {
  163. $flash->appendInfo($msg);
  164. }
  165. foreach ($errs as $msg) {
  166. $flash->appendWarning($msg);
  167. }
  168. return $this->redirectToArticle();
  169. }
  170. private function moveArticle() {
  171. $target = $this->getRequest()->post('category_id_new', 'int', 0);
  172. $flash = $this->getFlashMessage();
  173. $service = $this->getContainer()->getArticleService();
  174. if ($this->canMoveArticle()) {
  175. try {
  176. $service->move($this->article->getId(), $target);
  177. $flash->appendInfo(t('article_moved'));
  178. }
  179. catch (sly_Exception $e) {
  180. $flash->appendWarning(t('cannot_move_article').': '.$e->getMessage());
  181. }
  182. }
  183. else {
  184. $flash->appendWarning(t('no_rights_to_this_function'));
  185. }
  186. return $this->redirectToArticle();
  187. }
  188. private function copyArticle() {
  189. $target = $this->getRequest()->post('category_copy_id_new', 'int', 0);
  190. $flash = $this->getFlashMessage();
  191. $service = $this->getContainer()->getArticleService();
  192. if ($this->canCopyArticle($target)) {
  193. try {
  194. $this->article = $service->copy($this->article->getId(), $target);
  195. $flash->appendInfo(t('article_copied'));
  196. }
  197. catch (sly_Exception $e) {
  198. $flash->appendWarning(t('cannot_copy_article').': '.$e->getMessage());
  199. }
  200. }
  201. else {
  202. $flash->appendWarning(t('no_rights_to_this_function'));
  203. }
  204. return $this->redirectToArticle();
  205. }
  206. private function moveCategory() {
  207. $target = $this->getRequest()->post('category_id_new', 'int');
  208. $user = $this->getCurrentUser();
  209. $flash = $this->getFlashMessage();
  210. $service = $this->getContainer()->getCategoryService();
  211. if ($this->canMoveCategory() && \sly_Backend_Authorisation_Util::canEditArticle($user, $target)) {
  212. try {
  213. $service->move($this->article->getCategoryId(), $target);
  214. $flash->appendInfo(t('category_moved'));
  215. }
  216. catch (sly_Exception $e) {
  217. $flash->appendWarning(t('cannot_move_category').': '.$e->getMessage());
  218. }
  219. }
  220. else {
  221. $flash->appendWarning(t('no_rights_to_this_function'));
  222. }
  223. return $this->redirectToArticle();
  224. }
  225. /**
  226. * @return boolean
  227. */
  228. protected function canMoveArticle() {
  229. if ($this->article->isStartArticle()) return false;
  230. $user = $this->getCurrentUser();
  231. return $user->isAdmin() || $user->hasRight('article', 'move', 0) || $user->hasRight('article', 'move', $this->article->getId());
  232. }
  233. /**
  234. * @return boolean
  235. */
  236. protected function canConvertToStartArticle() {
  237. $user = $this->getCurrentUser();
  238. return sly_Backend_Authorisation_Util::canEditArticle($user, $this->article->getCategoryId());
  239. }
  240. /**
  241. * @return boolean
  242. */
  243. protected function canCopyContent($clang_b) {
  244. $user = $this->getCurrentUser();
  245. $editok = sly_Backend_Authorisation_Util::canEditContent($user, $this->article->getId());
  246. $clangok = sly_Util_Language::hasPermissionOnLanguage($user, $clang_b);
  247. return $editok && $clangok;
  248. }
  249. /**
  250. * @return boolean
  251. */
  252. protected function canCopyArticle($target) {
  253. $user = $this->getCurrentUser();
  254. return sly_Backend_Authorisation_Util::canEditArticle($user, $target);
  255. }
  256. /**
  257. * @return boolean
  258. */
  259. protected function canMoveCategory() {
  260. if (!$this->article->isStartArticle()) return false;
  261. $user = $this->getCurrentUser();
  262. return $user->isAdmin() || $user->hasRight('article', 'move', sly_Authorisation_ArticleListProvider::ALL) || $user->hasRight('article', 'move', $this->article->getId());
  263. }
  264. protected function redirectToArticle() {
  265. $artID = $this->article->getId();
  266. $clang = $this->article->getClang();
  267. $params = array('article_id' => $artID, 'clang' => $clang);
  268. return $this->redirectResponse($params);
  269. }
  270. }