TagSlideshow.php 7.7 KB
Newer Older
llaffont's avatar
llaffont committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
 * AFI-OPAC 2.0 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * AFI-OPAC 2.0 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with AFI-OPAC 2.0; if not, write to the Free Software
Laurent's avatar
Laurent committed
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
llaffont's avatar
llaffont committed
20 21 22 23 24 25 26 27 28 29
 */
class ZendAfi_View_Helper_TagSlideshow extends Zend_View_Helper_HtmlElement {
	protected $_default_preferences = array(
																					'op_largeur_img' => 400,
																					'op_hauteur_boite' => 400,
																					'nb_aff' => 0);
	protected $_preferences;
	protected $_album;


30 31 32
	public static function getTransitionDefinitions() {
		return array('fade' => 'Transparence',
								 'shuffle' => 'Mélange',
33 34 35 36
								 'scrollHorz' => 'Défilement horizontal',
								 'scrollVert' => 'Défilement vertical',
								 'curtainX' => 'Rideau horizontal',
								 'curtainY' => 'Rideau vertical');
37 38 39
	}


llaffont's avatar
llaffont committed
40 41 42 43
	public function setPreferences($preferences) {
		if (null == $preferences)
			$this->_preferences = $this->_default_preferences;
		else
Laurent's avatar
Laurent committed
44
			$this->_preferences  = array_merge($this->_default_preferences,
llaffont's avatar
llaffont committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
																				 $preferences);
		return $this;
	}


	public function setAlbum($album) {
		$this->_album = $album;
		return $this;
	}


	public function tagSlideshow($album, $preferences = null) {
		if (!$album)
			return '';

Laurent's avatar
Laurent committed
60 61 62
		if (!is_array($preferences))
			$preferences = [];

llaffont's avatar
llaffont committed
63 64
		return $this
			->setAlbum($album)
Laurent's avatar
Laurent committed
65 66
			->setPreferences(array_merge(['op_hauteur_boite' => $album->getOpHauteurBoite()],
																	 $preferences))
llaffont's avatar
llaffont committed
67 68 69 70 71 72 73 74 75
			->renderSlideShowScripts()
			->renderAlbumMedias();
	}


	public function renderSlideShowScripts() {
		if (!$this->_album)
			return $this;

76
		return $this->renderSlideShowScriptsOn(Class_ScriptLoader::getInstance(),
llaffont's avatar
llaffont committed
77 78 79
																					 sprintf('div.slideshow-%d .medias',
																									 $this->_album->getId()),
																					 array('width' => $this->_preferences['op_largeur_img'],
80 81 82
																								 'height' => $this->_preferences['op_hauteur_boite'],
																								 'fit' => true,
																								 'aspect' => true));
llaffont's avatar
llaffont committed
83 84 85 86 87 88 89
	}


	/**
	 * @param Class_Album $album
	 * @return ZendAfi_View_Helper_Accueil_BibNumerique
	 */
90
	public function renderSlideShowScriptsOn($script_loader, $selector, $options=null) {
Laurent's avatar
Laurent committed
91
		$cycle_options = array('pause' => 1,
92 93 94 95
													 'fx' => 'fade',
													 'animIn' => array('opacity' =>  1),
													 'animOut' => array('opacity' =>  0),
													 );
96
		if (array_isset('op_transition', $this->_preferences)
97
				&& in_array($this->_preferences['op_transition'], array_keys(self::getTransitionDefinitions())))
llaffont's avatar
llaffont committed
98 99 100 101 102 103 104 105
			$cycle_options['fx'] = $this->_preferences['op_transition'];

		if (array_isset('op_timeout', $this->_preferences))
			$cycle_options['timeout'] = 1000 * $this->_preferences['op_timeout'];

		if ($options)
			$cycle_options = array_merge($cycle_options, $options);

106
		$script_loader
llaffont's avatar
llaffont committed
107 108
			->addScript(URL_JAVA . 'diaporama/jquery.cycle.all.min')
			->addJQueryReady(sprintf(
109 110 111 112
															 '$(\'%s\').cycle(%s);
                                var container = $(\'%1$s\').parent();
                                container.addClass(\'slideshow\');
										            container.prepend(\'<div class="controls"><a href="#"></a><a href="#"></a></div>\');
Laurent's avatar
Laurent committed
113 114 115
                                container.find(\'.controls a:first-child\').click(
                                                                  function(event){
                                                                         event.preventDefault();
116 117 118
                                                                         $(\'%1$s\').cycle(\'prev\') } );
                                container.find(\'.controls a + a\').click(
                                                                  function(event){
Laurent's avatar
Laurent committed
119
                                                                         event.preventDefault();
120 121
                                                                         $(\'%1$s\').cycle(\'next\') } );
                                container.find(\'.controls a\').css(\'top\', (container.parent().height()/3)+\'px\')',
llaffont's avatar
llaffont committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
										 $selector,
										 json_encode($cycle_options)))
			->loadPrettyPhoto();
		return $this;
	}



	public function renderAlbumMedias() {
		if (!$this->_album)
			return '';

		$medias = $this->_getMedias($this->_album);

		$html = sprintf('<div class="slideshow slideshow-%d">'.
										  '<h2></h2>'.
138
									    '<div class="medias">%s</div>'.
llaffont's avatar
llaffont committed
139
										  '<p></p>'.
Laurent's avatar
Laurent committed
140
										'</div>',
llaffont's avatar
llaffont committed
141 142 143 144 145 146 147 148 149 150 151
										$this->_album->getId(),
										$this->_renderAllMedias($medias));
		return $html;
	}

	/**
	 * @param array $medias
	 * @return string
	 */
	protected function _renderAllMedias($medias) {
		$html = '';
Laurent's avatar
Laurent committed
152
		foreach ($medias as $media)
llaffont's avatar
llaffont committed
153 154 155 156 157 158 159 160 161 162
			$html .= $this->_renderMedia($media);
		return $html;
	}


	/**
	 * @param Class_AlbumRessource $media
	 * @return string
	 */
	protected function _renderMedia($media) {
163 164 165 166 167 168 169 170 171 172 173 174 175
		$params = array('width' => (int)$media->getAlbum()->getThumbnailWidth(),
										'id' => (int)$media->getId());

		if ($media->isThumbnailExistsForParams($params))
			$url_media = $media->getThumbnailUrlForParams($params);
		else
			$url_media = $this->view->url(array('controller' => 'bib-numerique',
																					'action' => 'thumbnail',
																					'width' => $media->getAlbum()->getThumbnailWidth(),
																					'id' => $media->getId()),
																		null,
																		true);

Laurent's avatar
Laurent committed
176 177
		$content = $this->view->tagImg($url_media,
																	 array('style' => sprintf('width: %spx',
178 179 180
																														$this->_preferences['op_largeur_img']),
																				 'title' => htmlspecialchars($media->getTitre()),
																				 'alt' => htmlspecialchars($media->getDescription())));
Laurent's avatar
Laurent committed
181 182 183

		$datas = array('titre' => $media->getTitre(),
									 'content' => $content,
llaffont's avatar
llaffont committed
184 185 186
									 'description' => $media->getDescription());
		if ($media->hasLinkTo()) {
			foreach ($datas as $idx => $data)
Laurent's avatar
Laurent committed
187
				$datas[$idx] = sprintf('<a href="%s">%s</a>',
llaffont's avatar
llaffont committed
188 189 190
															 $media->getLinkTo(),
															 $data);
		} else {
191
			$datas['content'] = sprintf('<a href="%s" data-prettyphoto="true" data-rel="prettyphoto[%s]" title="%s">%s</a>',
llaffont's avatar
llaffont committed
192 193 194
																	$this->view->url(array('module' => 'opac',
																												 'controller' => 'bib-numerique',
																												 'action' => 'get-resource',
195
																												 'id' => $media->getId())),
llaffont's avatar
llaffont committed
196 197 198 199 200
																	htmlentities($media->getAlbum()->getTitre()),
																	$media->getTitre(),
																	$content);
		}

Laurent's avatar
Laurent committed
201

llaffont's avatar
llaffont committed
202 203 204 205 206

		$content = sprintf('<h2>%s</h2>'.
											   '%s'.
											 '<p>%s</p>',
											 $datas['titre'],
Laurent's avatar
Laurent committed
207
											 $datas['content'],
llaffont's avatar
llaffont committed
208 209 210 211 212 213 214 215 216 217 218
											 $datas['description']);

		return sprintf('<div>%s</div>', $content);
	}



	public function _getMedias($album) {
		if (null === $album)
			return array();

219
		$medias = $album->getImages();
llaffont's avatar
llaffont committed
220 221 222 223 224 225 226 227 228 229 230 231 232

		if (array_key_exists('order', $this->_preferences)
				&& Class_Systeme_ModulesAccueil_BibliothequeNumerique::ORDER_RANDOM == $this->_preferences['order']) {
			shuffle($medias);
		}

		if (0 < ($limit = (int)$this->_preferences['nb_aff']))
			$medias = array_slice($medias, 0, $limit);

		return $medias;
	}
}
?>