PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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