PageRenderTime 101ms CodeModel.GetById 23ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/sly/Controller/Contentmeta.php

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