PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/sally/backend/lib/Controller/Contentmeta.php

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