PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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