diff --git a/lib/Alchemy/Phrasea/Application.php b/lib/Alchemy/Phrasea/Application.php index 5d1398c71a..d8ff6ec9f3 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -128,6 +128,7 @@ use Alchemy\Phrasea\Twig\JSUniqueID; use Alchemy\Phrasea\Twig\Fit; use Alchemy\Phrasea\Twig\Camelize; use Alchemy\Phrasea\Twig\BytesConverter; +use Alchemy\Phrasea\Twig\PhraseanetExtension; use Alchemy\Phrasea\Utilities\CachedTranslator; use FFMpeg\FFMpegServiceProvider; use Monolog\Logger; @@ -645,6 +646,7 @@ class Application extends SilexApplication $twig->addExtension(new Fit()); $twig->addExtension(new Camelize()); $twig->addExtension(new BytesConverter()); + $twig->addExtension(new PhraseanetExtension($app)); $twig->addFilter('serialize', new \Twig_Filter_Function('serialize')); $twig->addFilter('stristr', new \Twig_Filter_Function('stristr')); diff --git a/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php b/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php new file mode 100644 index 0000000000..b751ef0031 --- /dev/null +++ b/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php @@ -0,0 +1,189 @@ +app = $app; + } + + public function getFilters() + { + return array( + ); + } + + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('user_setting', array($this, 'getUserSetting')), + new \Twig_SimpleFunction('record_thumbnail_url', array($this, 'getThumbnailUrl')), + new \Twig_SimpleFunction('record_doctype_icon', array($this, 'getDoctypeIcon'), array( + 'is_safe' => array('html') + )), + new \Twig_SimpleFunction('has_access_subdef', array($this, 'hasAccessSubDefinition')), + new \Twig_SimpleFunction('record_thumbnailgif_url', array($this, 'getThumbnailGifUrl')), + new \Twig_SimpleFunction('granted_on_collection', array($this, 'isGrantedOnCollection')), + new \Twig_SimpleFunction('granted_on_databox', array($this, 'isGrantedOnDatabox')), + new \Twig_SimpleFunction('collection_logo', array($this, 'getCollectionLogo'), array( + 'is_safe' => array('html') + )), + ); + } + + public function isGrantedOnDatabox($databoxId, $rights) + { + if (false === ($this->app['authentication']->getUser() instanceof User)) { + + return false; + } + + $rights = (array) $rights; + foreach ($rights as $right) { + if (false === $this->app['acl']->get($this->app['authentication']->getUser())->has_right_on_sbas($databoxId, $right)) { + + return false; + } + } + + return true; + } + + public function isGrantedOnCollection($baseId, $rights) + { + if (false === ($this->app['authentication']->getUser() instanceof User)) { + + return false; + } + + $rights = (array) $rights; + foreach ($rights as $right) { + if (false === $this->app['acl']->get($this->app['authentication']->getUser())->has_right_on_base($baseId, $right)) { + + return false; + } + } + + return true; + } + + public function getCollectionLogo($baseId) + { + if (false === $this->app['filesystem']->exists(sprintf('%s/config/minilogos/%s', $this->app['root.path'], $baseId))) { + return ''; + } + + return sprintf( + '', + \phrasea::bas_labels($baseId, $this->app), + $baseId + ); + } + + public function hasAccessSubDefinition(RecordInterface $record, $subDefinition) + { + if (false === ($this->app['authentication']->getUser() instanceof User)) { + + return false; + } + + return $this->app['acl']->get($this->app['authentication']->getUser())->has_access_to_subdef($record, $subDefinition); + } + + public function getDoctypeIcon(RecordInterface $record) + { + $src = $title = ''; + if ($record->isStory()) { + $src = '/skins/icons/icon_story.gif'; + $title = $this->app['translator']->trans('reportage'); + + return sprintf('', $src, $title); + } + + switch ($record->getType()) { + case 'image': + $src = '/skins/icons/icon_image.gif'; + $title = $this->app['translator']->trans('image'); + break; + case 'document': + $src = '/skins/icons/icon_document.gif'; + $title = $this->app['translator']->trans('document'); + break; + case 'video': + $src = '/skins/icons/icon_video.gif'; + $title = $this->app['translator']->trans('reportage'); + break; + case 'audio': + $src = '/skins/icons/icon_audio.gif'; + $title = $this->app['translator']->trans('audio'); + break; + case 'flash': + $src = '/skins/icons/icon_flash.gif'; + $title = $this->app['translator']->trans('flash'); + break; + } + + return sprintf('', $src, $title); + } + + public function getThumbnailUrl(RecordInterface $record) + { + return $this->getSubdefUrl($record, 'thumbnail'); + } + + public function getThumbnailGifUrl(RecordInterface $record) + { + return $this->getSubdefUrl($record, 'thumbnailgif'); + } + + public function getSubdefUrl(RecordInterface $record, $subdefName) + { + if ($record instanceof ElasticsearchRecord) { + if ($record->getSubdefs()->containsKey($subdefName)) { + $thumbnail = $record->getSubdefs()->get($subdefName); + if (null !== $path = $thumbnail['path']) { + if (is_string($path) && '' !== $path) { + return $this->app['phraseanet.static-file']->getUrl($path); + } + } + } + } elseif ($record instanceof \record_adapter) { + if (null !== $thumbnail = $record->get_subdef($subdefName)) { + if ('' !== $path = $thumbnail->get_pathfile()) { + return $this->app['phraseanet.static-file']->getUrl($path); + } + } + } + + $path = sprintf('%s/www/skins/icons/substitution/%s.png', + $this->app['root.path'], + str_replace('/', '_', $record->getMimeType()) + ); + + return $this->app['phraseanet.static-file']->getUrl($path); + } + + public function getUserSetting($setting, $default = null) + { + if (false === ($this->app['authentication']->getUser() instanceof User)) { + + return $default; + } + + return $this->app['settings']->getUserSetting($this->app['authentication']->getUser(), $setting, $default); + } + + public function getName() + { + return 'phraseanet'; + } +}