TagSlideshow.php 7.34 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 19 20 21 22 23 24 25 26 27 28 29
<?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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
 */
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	public function setPreferences($preferences) {
		if (null == $preferences)
			$this->_preferences = $this->_default_preferences;
		else
			$this->_preferences  = array_merge($this->_default_preferences, 
																				 $preferences);
		return $this;
	}


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


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

		return $this
			->setAlbum($album)
			->setPreferences($preferences)
			->renderSlideShowScripts()
			->renderAlbumMedias();
	}


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

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


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

102
		$script_loader
llaffont's avatar
llaffont committed
103 104
			->addScript(URL_JAVA . 'diaporama/jquery.cycle.all.min')
			->addJQueryReady(sprintf(
105 106 107 108 109 110 111 112 113 114 115 116 117
															 '$(\'%s\').cycle(%s);
                                var container = $(\'%1$s\').parent();
                                container.addClass(\'slideshow\');
										            container.prepend(\'<div class="controls"><a href="#"></a><a href="#"></a></div>\');
                                container.find(\'.controls a:first-child\').click( 
                                                                  function(event){ 
                                                                         event.preventDefault(); 
                                                                         $(\'%1$s\').cycle(\'prev\') } );
                                container.find(\'.controls a + a\').click(
                                                                  function(event){
                                                                         event.preventDefault(); 
                                                                         $(\'%1$s\').cycle(\'next\') } );
                                container.find(\'.controls a\').css(\'top\', (container.parent().height()/3)+\'px\')',
llaffont's avatar
llaffont committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
										 $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>'.
134
									    '<div class="medias">%s</div>'.
llaffont's avatar
llaffont committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
										  '<p></p>'.
										'</div>', 
										$this->_album->getId(),
										$this->_renderAllMedias($medias));
		return $html;
	}

	/**
	 * @param array $medias
	 * @return string
	 */
	protected function _renderAllMedias($medias) {
		$html = '';
		foreach ($medias as $media) 
			$html .= $this->_renderMedia($media);
		return $html;
	}


	/**
	 * @param Class_AlbumRessource $media
	 * @return string
	 */
	protected function _renderMedia($media) {
		$content = $this->view->tagImg($this->view->url(array('controller' => 'bib-numerique',
																													'action' => 'thumbnail',
																													'width' => $media->getAlbum()->getThumbnailWidth(),
																													'id' => $media->getId()),
																										null,
																										true), 
																	 array('style' => sprintf('width: %spx',  
																														$this->_preferences['op_largeur_img']),
																				 'title' => htmlspecialchars($media->getTitre()),
																				 'alt' => htmlspecialchars($media->getDescription())));
		
		$datas = array('titre' => $media->getTitre(), 
									 'content' => $content, 
									 'description' => $media->getDescription());
		if ($media->hasLinkTo()) {
			foreach ($datas as $idx => $data)
				$datas[$idx] = sprintf('<a href="%s">%s</a>', 
															 $media->getLinkTo(),
															 $data);
		} else {
			$datas['content'] = sprintf('<a href="%s" rel="prettyphoto[%s]" title="%s">%s</a>',
																	$this->view->url(array('module' => 'opac',
																												 'controller' => 'bib-numerique',
																												 'action' => 'get-resource',
																												 'id' => $media->getId())),
																	htmlentities($media->getAlbum()->getTitre()),
																	$media->getTitre(),
																	$content);
		}

		

		$content = sprintf('<h2>%s</h2>'.
											   '%s'.
											 '<p>%s</p>',
											 $datas['titre'],
											 $datas['content'], 
											 $datas['description']);

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



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

		$medias = $album->getImages();

		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;
	}
}
?>