PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/sally/backend/lib/Controller/Mediapool.php

https://bitbucket.org/mediastuttgart/sallycms-0.6
PHP | 398 lines | 290 code | 91 blank | 17 comment | 40 complexity | 0bcc52867fd585e9dd7bf077709b1202 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_Mediapool extends sly_Controller_Backend implements sly_Controller_Interface {
  11. protected $warning;
  12. protected $info;
  13. protected $category;
  14. protected $selectBox;
  15. protected $categories;
  16. protected $action;
  17. private $init = false;
  18. protected function init($action = '') {
  19. if ($this->init) return;
  20. $this->init = true;
  21. // load our i18n stuff
  22. sly_Core::getI18N()->appendFile(SLY_SALLYFOLDER.'/backend/lang/pages/mediapool/');
  23. $this->info = sly_request('info', 'string');
  24. $this->warning = sly_request('warning', 'string');
  25. $this->args = sly_requestArray('args', 'string');
  26. $this->categories = array();
  27. $this->action = $action;
  28. // init category filter
  29. if (isset($this->args['categories'])) {
  30. $cats = array_map('intval', explode('|', $this->args['categories']));
  31. $this->categories = array_unique($cats);
  32. }
  33. $this->getCurrentCategory();
  34. // build navigation
  35. $layout = sly_Core::getLayout();
  36. $nav = $layout->getNavigation();
  37. $page = $nav->find('mediapool');
  38. $cur = sly_Core::getCurrentControllerName();
  39. $subline = array(
  40. array('mediapool', t('media_list')),
  41. array('mediapool_upload', t('upload_file'))
  42. );
  43. if ($this->isMediaAdmin()) {
  44. $subline[] = array('mediapool_structure', t('categories'));
  45. $subline[] = array('mediapool_sync', t('sync_files'));
  46. }
  47. foreach ($subline as $item) {
  48. $sp = $page->addSubpage($item[0], $item[1]);
  49. if (!empty($this->args)) {
  50. $sp->setExtraParams(array('args' => $this->args));
  51. // ignore the extra params when detecting the current page
  52. if ($cur === $item[0]) $sp->forceStatus(true);
  53. }
  54. }
  55. $page = sly_Core::dispatcher()->filter('SLY_MEDIAPOOL_MENU', $page);
  56. $layout->showNavigation(false);
  57. $layout->pageHeader(t('media_list'), $page);
  58. $layout->setBodyAttr('class', 'sly-popup sly-mediapool');
  59. print $this->render('mediapool/javascript.phtml');
  60. }
  61. protected function getArgumentString($separator = '&amp;') {
  62. $args = array();
  63. foreach ($this->args as $name => $value) {
  64. $args['args['.$name.']'] = $value;
  65. }
  66. return http_build_query($args, '', $separator);
  67. }
  68. protected function getCurrentCategory() {
  69. if ($this->category === null) {
  70. $category = sly_request('rex_file_category', 'int', -1);
  71. $service = sly_Service_Factory::getMediaCategoryService();
  72. if ($category == -1) {
  73. $category = sly_Util_Session::get('media[rex_file_category]', 'int');
  74. }
  75. // respect category filter
  76. if (!empty($this->categories) && !in_array($category, $this->categories)) {
  77. $category = reset($this->categories);
  78. }
  79. $category = $service->findById($category);
  80. $category = $category ? $category->getId() : 0;
  81. sly_util_Session::set('media[rex_file_category]', $category);
  82. $this->category = $category;
  83. }
  84. return $this->category;
  85. }
  86. protected function getOpenerLink(sly_Model_Medium $file) {
  87. $callback = sly_request('callback', 'string');
  88. $link = '';
  89. if (!empty($callback)) {
  90. $filename = $file->getFilename();
  91. $title = $file->getTitle();
  92. $link = '<a href="#" data-filename="'.sly_html($filename).'" data-title="'.sly_html($title).'">'.t('apply_file').'</a>';
  93. }
  94. return $link;
  95. }
  96. protected function getFiles() {
  97. $cat = $this->getCurrentCategory();
  98. $where = 'f.category_id = '.$cat;
  99. $where = sly_Core::dispatcher()->filter('SLY_MEDIA_LIST_QUERY', $where, array('category_id' => $cat));
  100. $where = '('.$where.')';
  101. if (isset($this->args['types'])) {
  102. $types = explode('|', preg_replace('#[^a-z0-9/+.-|]#i', '', $this->args['types']));
  103. if (!empty($types)) {
  104. $where .= ' AND filetype IN ("'.implode('","', $types).'")';
  105. }
  106. }
  107. $db = sly_DB_Persistence::getInstance();
  108. $prefix = sly_Core::getTablePrefix();
  109. $query = 'SELECT f.id FROM '.$prefix.'file f LEFT JOIN '.$prefix.'file_category c ON f.category_id = c.id WHERE '.$where.' ORDER BY f.updatedate DESC';
  110. $files = array();
  111. $db->query($query);
  112. foreach ($db as $row) {
  113. $files[$row['id']] = sly_Util_Medium::findById($row['id']);
  114. }
  115. return $files;
  116. }
  117. public function indexAction() {
  118. $this->init('index');
  119. $files = $this->getFiles();
  120. print $this->render('mediapool/toolbar.phtml');
  121. if (empty($files)) {
  122. print sly_Helper_Message::info(t('no_media_found'));
  123. }
  124. else {
  125. print $this->render('mediapool/index.phtml', compact('files'));
  126. }
  127. }
  128. public function batchAction() {
  129. $this->init('batch');
  130. if (!empty($_POST['delete'])) {
  131. return $this->deleteAction();
  132. }
  133. return $this->moveAction();
  134. }
  135. public function moveAction() {
  136. $this->init('move');
  137. if (!$this->isMediaAdmin()) {
  138. return $this->indexAction();
  139. }
  140. $media = sly_postArray('selectedmedia', 'int', array());
  141. if (empty($media)) {
  142. $this->warning = t('no_files_selected');
  143. return $this->indexAction();
  144. }
  145. $service = sly_Service_Factory::getMediumService();
  146. foreach ($media as $mediumID) {
  147. $medium = sly_Util_Medium::findById($mediumID);
  148. if (!$medium) continue;
  149. $medium->setCategoryId($this->category);
  150. $service->update($medium);
  151. }
  152. // refresh asset cache in case permissions have changed
  153. $this->revalidate();
  154. $this->info = t('selected_files_moved');
  155. $this->indexAction();
  156. }
  157. public function deleteAction() {
  158. $this->init('delete');
  159. if (!$this->isMediaAdmin()) {
  160. return $this->indexAction();
  161. }
  162. $files = sly_postArray('selectedmedia', 'int', array());
  163. if (empty($files)) {
  164. $this->warning = t('no_files_selected');
  165. return $this->indexAction();
  166. }
  167. foreach ($files as $fileID) {
  168. $media = sly_Util_Medium::findById($fileID);
  169. if ($media) {
  170. $retval = $this->deleteMedia($media);
  171. }
  172. else {
  173. $this->warning[] = t('file_not_found', $fileID);
  174. }
  175. }
  176. $this->indexAction();
  177. }
  178. protected function deleteMedia(sly_Model_Medium $medium) {
  179. $filename = $medium->getFileName();
  180. $user = sly_Util_User::getCurrentUser();
  181. // TODO: Is $this->isMediaAdmin() redundant? The user rights are already checked in delete()...
  182. if ($this->isMediaAdmin() || $user->hasRight('mediacategory', 'access', $medium->getCategoryId())) {
  183. $usages = $this->isInUse($medium);
  184. if ($usages === false) {
  185. $service = sly_Service_Factory::getMediumService();
  186. try {
  187. $service->delete($medium->getId());
  188. $this->revalidate();
  189. $this->info[] = t('medium_deleted');
  190. }
  191. catch (sly_Exception $e) {
  192. $this->warning[] = $e->getMessage();
  193. }
  194. }
  195. else {
  196. $tmp = array();
  197. $tmp[] = t('file_delete_error_1', $filename).' '.t('file_delete_error_2').'<br />';
  198. $tmp[] = '<ul>';
  199. foreach ($usages as $usage) {
  200. if (!empty($usage['link'])) {
  201. $tmp[] = '<li><a href="javascript:openPage(\''.sly_html($usage['link']).'\')">'.sly_html($usage['title']).'</a></li>';
  202. }
  203. else {
  204. $tmp[] = '<li>'.sly_html($usage['title']).'</li>';
  205. }
  206. }
  207. $tmp[] = '</ul>';
  208. $this->warning[] = implode("\n", $tmp);
  209. }
  210. }
  211. else {
  212. $this->warning[] = t('no_permission');
  213. }
  214. }
  215. public function checkPermission($action) {
  216. $user = sly_Util_User::getCurrentUser();
  217. if (is_null($user)) return false;
  218. return $user->hasStructureRight() || $user->hasRight('pages', 'mediapool');
  219. }
  220. protected function isMediaAdmin() {
  221. $user = sly_Util_User::getCurrentUser();
  222. return $user->isAdmin() || $user->hasRight('mediacategory', 'access', sly_Authorisation_ListProvider::ALL);
  223. }
  224. protected function canAccessFile(sly_Model_Medium $medium) {
  225. return $this->canAccessCategory($medium->getCategoryId());
  226. }
  227. protected function canAccessCategory($cat) {
  228. $user = sly_Util_User::getCurrentUser();
  229. return $this->isMediaAdmin() || $user->hasRight('mediacategory', 'access', intval($cat));
  230. }
  231. protected function getCategorySelect() {
  232. $user = sly_Util_User::getCurrentUser();
  233. if ($this->selectBox === null) {
  234. $this->selectBox = sly_Form_Helper::getMediaCategorySelect('rex_file_category', null, $user);
  235. $this->selectBox->setLabel(t('categories'));
  236. $this->selectBox->setMultiple(false);
  237. $this->selectBox->setAttribute('value', $this->getCurrentCategory());
  238. // filter categories if args[categories] is set
  239. if (isset($this->args['categories'])) {
  240. $cats = array_map('intval', explode('|', $this->args['categories']));
  241. $cats = array_unique($cats);
  242. if (!empty($cats)) {
  243. $values = array_keys($this->selectBox->getValues());
  244. foreach ($values as $catID) {
  245. if (!in_array($catID, $cats)) $this->selectBox->removeValue($catID);
  246. }
  247. }
  248. }
  249. }
  250. return $this->selectBox;
  251. }
  252. protected function getDimensions($width, $height, $maxWidth, $maxHeight) {
  253. if ($width > $maxWidth) {
  254. $factor = (float) $maxWidth / $width;
  255. $width = $maxWidth;
  256. $height *= $factor;
  257. }
  258. if ($height > $maxHeight) {
  259. $factor = (float) $maxHeight / $height;
  260. $height = $maxHeight;
  261. $width *= $factor;
  262. }
  263. return array(ceil($width), ceil($height));
  264. }
  265. protected function isDocType(sly_Model_Medium $medium) {
  266. static $docTypes = array(
  267. 'bmp', 'css', 'doc', 'docx', 'eps', 'gif', 'gz', 'jpg', 'mov', 'mp3',
  268. 'ogg', 'pdf', 'png', 'ppt', 'pptx','pps', 'ppsx', 'rar', 'rtf', 'swf',
  269. 'tar', 'tif', 'txt', 'wma', 'xls', 'xlsx', 'zip'
  270. );
  271. return in_array($medium->getExtension(), $docTypes);
  272. }
  273. protected function isImage(sly_Model_Medium $medium) {
  274. static $exts = array('gif', 'jpeg', 'jpg', 'png', 'bmp', 'tif', 'tiff', 'webp');
  275. return in_array($medium->getExtension(), $exts);
  276. }
  277. protected function isInUse(sly_Model_Medium $medium) {
  278. $sql = sly_DB_Persistence::getInstance();
  279. $filename = addslashes($medium->getFilename());
  280. $prefix = sly_Core::getTablePrefix();
  281. $query =
  282. 'SELECT s.article_id, s.clang FROM '.$prefix.'slice_value sv, '.$prefix.'article_slice s, '.$prefix.'article a '.
  283. 'WHERE sv.slice_id = s.slice_id AND a.id = s.article_id AND a.clang = s.clang '.
  284. 'AND value LIKE "%'.$filename.'%" GROUP BY s.article_id, s.clang';
  285. $res = array();
  286. $usages = array();
  287. $sql->query($query);
  288. foreach ($sql as $row) $res[] = $row;
  289. foreach ($res as $row) {
  290. $article = sly_Util_Article::findById($row['article_id'], $row['clang']);
  291. $usages[] = array(
  292. 'title' => $article->getName(),
  293. 'type' => 'sly-article',
  294. 'link' => 'index.php?page=content&article_id='.$row['article_id'].'&mode=edit&clang='.$row['clang']
  295. );
  296. }
  297. $usages = sly_Core::dispatcher()->filter('SLY_MEDIA_USAGES', $usages, array(
  298. 'filename' => $medium->getFilename(),
  299. 'media' => $medium
  300. ));
  301. return empty($usages) ? false : $usages;
  302. }
  303. protected function revalidate() {
  304. // re-validate asset cache
  305. sly_Service_Factory::getAssetService()->validateCache();
  306. }
  307. }