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

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

https://bitbucket.org/SallyCMS/trunk
PHP | 324 lines | 240 code | 69 blank | 15 comment | 32 complexity | 8f63dd729875b91402e671f4021b3c1e 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. abstract class sly_Controller_Mediapool_Base extends sly_Controller_Backend implements sly_Controller_Interface {
  11. protected $args;
  12. protected $category;
  13. protected $selectBox;
  14. protected $categories;
  15. private $init = false;
  16. public function __construct() {
  17. // load our i18n stuff
  18. sly_Core::getI18N()->appendFile(SLY_SALLYFOLDER.'/backend/lang/pages/mediapool/');
  19. }
  20. protected function init() {
  21. if ($this->init) return;
  22. $this->init = true;
  23. $request = $this->getRequest();
  24. $this->args = $request->requestArray('args', 'string');
  25. $this->categories = array();
  26. // init category filter
  27. if (isset($this->args['categories'])) {
  28. $cats = array_map('intval', explode('|', $this->args['categories']));
  29. $this->categories = array_unique($cats);
  30. }
  31. $this->getCurrentCategory();
  32. // build navigation
  33. $layout = sly_Core::getLayout();
  34. $nav = $layout->getNavigation();
  35. $page = $nav->find('mediapool');
  36. if ($page) {
  37. $cur = sly_Core::getCurrentControllerName();
  38. $subline = array(
  39. array('mediapool', t('media_list')),
  40. array('mediapool_upload', t('upload_file'))
  41. );
  42. if ($this->isMediaAdmin()) {
  43. $subline[] = array('mediapool_structure', t('categories'));
  44. $subline[] = array('mediapool_sync', t('sync_files'));
  45. }
  46. foreach ($subline as $item) {
  47. $sp = $page->addSubpage($item[0], $item[1]);
  48. if (!empty($this->args)) {
  49. $sp->setExtraParams(array('args' => $this->args));
  50. // ignore the extra params when detecting the current page
  51. if ($cur === $item[0]) $sp->forceStatus(true);
  52. }
  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. $this->render('mediapool/javascript.phtml', array(), false);
  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. $request = $this->getRequest();
  71. $category = $request->request('category', 'int', -1);
  72. $service = sly_Service_Factory::getMediaCategoryService();
  73. $session = sly_Core::getSession();
  74. if ($category === -1) {
  75. $category = $session->get('sly-media-category', 'int', 0);
  76. }
  77. // respect category filter
  78. if (!empty($this->categories) && !in_array($category, $this->categories)) {
  79. $category = reset($this->categories);
  80. }
  81. $category = $service->findById($category);
  82. $category = $category ? $category->getId() : 0;
  83. $session->set('sly-media-category', $category);
  84. $this->category = $category;
  85. }
  86. return $this->category;
  87. }
  88. protected function getOpenerLink(sly_Model_Medium $file) {
  89. $request = $this->getRequest();
  90. $callback = $request->request('callback', 'string');
  91. $link = '';
  92. if (!empty($callback)) {
  93. $filename = $file->getFilename();
  94. $title = $file->getTitle();
  95. $link = '<a href="#" data-filename="'.sly_html($filename).'" data-title="'.sly_html($title).'">'.t('apply_file').'</a>';
  96. }
  97. return $link;
  98. }
  99. protected function getFiles() {
  100. $cat = $this->getCurrentCategory();
  101. $where = 'f.category_id = '.$cat;
  102. $where = sly_Core::dispatcher()->filter('SLY_MEDIA_LIST_QUERY', $where, array('category_id' => $cat));
  103. $where = '('.$where.')';
  104. if (isset($this->args['types'])) {
  105. $types = explode('|', preg_replace('#[^a-z0-9/+.-|]#i', '', $this->args['types']));
  106. if (!empty($types)) {
  107. $where .= ' AND filetype IN ("'.implode('","', $types).'")';
  108. }
  109. }
  110. $db = sly_DB_Persistence::getInstance();
  111. $prefix = sly_Core::getTablePrefix();
  112. $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';
  113. $files = array();
  114. $db->query($query);
  115. foreach ($db as $row) {
  116. $files[$row['id']] = sly_Util_Medium::findById($row['id']);
  117. }
  118. return $files;
  119. }
  120. protected function deleteMedium(sly_Model_Medium $medium, sly_Util_FlashMessage $msg, $revalidate = true) {
  121. $filename = $medium->getFileName();
  122. $user = sly_Util_User::getCurrentUser();
  123. $service = sly_Service_Factory::getMediumService();
  124. if ($this->canAccessCategory($medium->getCategoryId())) {
  125. $usages = $this->isInUse($medium);
  126. if ($usages === false) {
  127. try {
  128. $service->deleteByMedium($medium);
  129. if ($revalidate) $this->revalidate();
  130. $msg->appendInfo($filename.': '.t('medium_deleted'));
  131. }
  132. catch (sly_Exception $e) {
  133. $msg->appendWarning($filename.': '.$e->getMessage());
  134. }
  135. }
  136. else {
  137. $tmp = array();
  138. $tmp[] = t('file_is_in_use', $filename);
  139. $tmp[] = '<ul>';
  140. foreach ($usages as $usage) {
  141. $title = sly_html($usage['title']);
  142. if (!empty($usage['link'])) {
  143. $tmp[] = '<li><a href="javascript:openPage('.json_encode($usage['link']).')">'.$title.'</a></li>';
  144. }
  145. else {
  146. $tmp[] = '<li>'.$title.'</li>';
  147. }
  148. }
  149. $tmp[] = '</ul>';
  150. $flash->appendWarning(implode("\n", $tmp));
  151. }
  152. }
  153. else {
  154. $msg->appendWarning($filename.': '.t('no_permission'));
  155. }
  156. }
  157. public function checkPermission($action) {
  158. $user = sly_Util_User::getCurrentUser();
  159. if (!$user || (!$user->isAdmin() && !$user->hasRight('pages', 'mediapool'))) {
  160. return false;
  161. }
  162. if ($action === 'batch') {
  163. sly_Util_Csrf::checkToken();
  164. }
  165. return true;
  166. }
  167. protected function isMediaAdmin() {
  168. $user = sly_Util_User::getCurrentUser();
  169. return $user->isAdmin() || $user->hasRight('mediacategory', 'access', sly_Authorisation_ListProvider::ALL);
  170. }
  171. protected function canAccessFile(sly_Model_Medium $medium) {
  172. return $this->canAccessCategory($medium->getCategoryId());
  173. }
  174. protected function canAccessCategory($cat) {
  175. $user = sly_Util_User::getCurrentUser();
  176. return $this->isMediaAdmin() || $user->hasRight('mediacategory', 'access', intval($cat));
  177. }
  178. protected function getCategorySelect() {
  179. $user = sly_Util_User::getCurrentUser();
  180. if ($this->selectBox === null) {
  181. $this->selectBox = sly_Form_Helper::getMediaCategorySelect('category', null, $user);
  182. $this->selectBox->setLabel(t('categories'));
  183. $this->selectBox->setMultiple(false);
  184. $this->selectBox->setAttribute('value', $this->getCurrentCategory());
  185. // filter categories if args[categories] is set
  186. if (isset($this->args['categories'])) {
  187. $cats = array_map('intval', explode('|', $this->args['categories']));
  188. $cats = array_unique($cats);
  189. if (!empty($cats)) {
  190. $values = array_keys($this->selectBox->getValues());
  191. foreach ($values as $catID) {
  192. if (!in_array($catID, $cats)) $this->selectBox->removeValue($catID);
  193. }
  194. }
  195. }
  196. }
  197. return $this->selectBox;
  198. }
  199. protected function getDimensions($width, $height, $maxWidth, $maxHeight) {
  200. if ($width > $maxWidth) {
  201. $factor = (float) $maxWidth / $width;
  202. $width = $maxWidth;
  203. $height *= $factor;
  204. }
  205. if ($height > $maxHeight) {
  206. $factor = (float) $maxHeight / $height;
  207. $height = $maxHeight;
  208. $width *= $factor;
  209. }
  210. return array(ceil($width), ceil($height));
  211. }
  212. protected function isDocType(sly_Model_Medium $medium) {
  213. static $docTypes = array(
  214. 'bmp', 'css', 'doc', 'docx', 'eps', 'gif', 'gz', 'jpg', 'mov', 'mp3',
  215. 'ogg', 'pdf', 'png', 'ppt', 'pptx','pps', 'ppsx', 'rar', 'rtf', 'swf',
  216. 'tar', 'tif', 'txt', 'wma', 'xls', 'xlsx', 'zip'
  217. );
  218. return in_array($medium->getExtension(), $docTypes);
  219. }
  220. protected function isImage(sly_Model_Medium $medium) {
  221. static $exts = array('gif', 'jpeg', 'jpg', 'png', 'bmp', 'tif', 'tiff', 'webp');
  222. return in_array($medium->getExtension(), $exts);
  223. }
  224. protected function isInUse(sly_Model_Medium $medium) {
  225. $sql = sly_DB_Persistence::getInstance();
  226. $filename = addslashes($medium->getFilename());
  227. $prefix = sly_Core::getTablePrefix();
  228. $query =
  229. 'SELECT s.article_id, s.clang FROM '.$prefix.'slice sv, '.$prefix.'article_slice s, '.$prefix.'article a '.
  230. 'WHERE sv.id = s.slice_id AND a.id = s.article_id AND a.clang = s.clang '.
  231. 'AND serialized_values LIKE "%'.$filename.'%" GROUP BY s.article_id, s.clang';
  232. $res = array();
  233. $usages = array();
  234. $router = $this->getContainer()->getApplication()->getRouter();
  235. $sql->query($query);
  236. foreach ($sql as $row) $res[] = $row;
  237. foreach ($res as $row) {
  238. $article = sly_Util_Article::findById($row['article_id'], $row['clang']);
  239. $usages[] = array(
  240. 'title' => $article->getName(),
  241. 'type' => 'sly-article',
  242. 'link' => $router->getPlainUrl('content', null, array('article_id' => $row['article_id'], 'clang' => $row['clang']))
  243. );
  244. }
  245. $usages = sly_Core::dispatcher()->filter('SLY_MEDIA_USAGES', $usages, array(
  246. 'filename' => $medium->getFilename(),
  247. 'media' => $medium
  248. ));
  249. return empty($usages) ? false : $usages;
  250. }
  251. protected function revalidate() {
  252. // re-validate asset cache
  253. sly_Service_Factory::getAssetService()->validateCache();
  254. }
  255. }