TagSlideshow.php 8.64 KB
Newer Older
llaffont's avatar
llaffont committed
1 2 3 4
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
Laurent's avatar
Laurent committed
5
 * BOKEH is free software; you can redistribute it and/or modify
llaffont's avatar
llaffont committed
6 7 8 9 10 11
 * 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).
 *
Laurent's avatar
Laurent committed
12
 * BOKEH is distributed in the hope that it will be useful,
llaffont's avatar
llaffont committed
13 14 15 16 17
 * 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
Laurent's avatar
Laurent committed
18
 * along with BOKEH; 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
 */
Ghislain Loas's avatar
Ghislain Loas committed
21
class ZendAfi_View_Helper_TagSlideshow extends ZendAfi_View_Helper_BaseHelper {
Patrick Barroca's avatar
Patrick Barroca committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  protected $_default_preferences = array(
                                          'op_largeur_img' => 400,
                                          'op_hauteur_boite' => 400,
                                          'nb_aff' => 0);
  protected $_preferences;
  protected $_album;


  public static function getTransitionDefinitions() {
    return array('fade' => 'Transparence',
                 'shuffle' => 'Mélange',
                 'scrollHorz' => 'Défilement horizontal',
                 'scrollVert' => 'Défilement vertical',
                 'curtainX' => 'Rideau horizontal',
                 'curtainY' => 'Rideau vertical');
  }


  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 '';

    if (!is_array($preferences))
      $preferences = [];

    return $this
      ->setAlbum($album)
      ->setPreferences(array_merge(['op_hauteur_boite' => $album->getOpHauteurBoite()],
                                   $preferences))
      ->renderSlideShowScripts()
      ->renderAlbumMedias();
  }


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

    return $this->renderSlideShowScriptsOn(Class_ScriptLoader::getInstance(),
                                           sprintf('div.slideshow-%d .medias',
                                                   $this->_album->getId()),
                                           array('width' => $this->_preferences['op_largeur_img'],
                                                 'height' => $this->_preferences['op_hauteur_boite'],
                                                 'fit' => true,
                                                 'aspect' => true));
  }


  /**
   * @param Class_Album $album
   * @return ZendAfi_View_Helper_Accueil_BibNumerique
   */
  public function renderSlideShowScriptsOn($script_loader, $selector, $options=null) {
91 92 93 94 95 96
    $cycle_options = ['cleartypeNoBg' => true,
                      'pause' => 1,
                      'fx' => 'fade',
                      'animIn' => ['opacity' =>  1],
                      'animOut' => ['opacity' =>  0]];

Patrick Barroca's avatar
Patrick Barroca committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110
    if (array_isset('op_transition', $this->_preferences)
        && in_array($this->_preferences['op_transition'], array_keys(self::getTransitionDefinitions())))
      $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);

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



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

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

    $html = sprintf('<div class="slideshow slideshow-%d">'.
                      '<h2></h2>'.
                      '<div class="medias">%s</div>'.
                      '<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) {
    $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);

    $content = $this->view->tagImg($url_media,
                                   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 {
191 192 193 194 195 196
      $datas['content'] = $this->view->tagAnchor($this->view->url(['module' => 'opac',
                                                  'controller' => 'bib-numerique',
                                                  'action' => 'get-resource',
                                                                   'id' => $media->getId()]),
                                                 $content,
                                                 ['title' => $this->_('Accéder à la ressource "%s" dans un nouvel onglet', $media->getTitre())]);
Patrick Barroca's avatar
Patrick Barroca committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    }



    $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;
  }
llaffont's avatar
llaffont committed
229 230
}
?>