PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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