You are here

scald_galleria.module in Scald: Gallery 7.2

Scald Galleria is a player for Scald Gallery.

File

scald_galleria/scald_galleria.module
View source
<?php

/**
 * @file
 * Scald Galleria is a player for Scald Gallery.
 */

/**
 * Implements hook_theme().
 */
function scald_galleria_theme() {
  return array(
    'scald_galleria' => array(
      'variables' => array(
        'atom' => NULL,
        'images' => array(),
        'options' => '',
      ),
      'template' => 'scald_galleria',
    ),
  );
}

/**
 * Implements hook_scald_player().
 */
function scald_galleria_scald_player() {
  return array(
    'galleria' => array(
      'name' => 'Galleria',
      'description' => 'Display image gallery using Galleria, a JavaScript image gallery framework.',
      'type' => array(
        'gallery',
      ),
      'settings' => array(
        'width' => '690',
        'height' => '450',
        'fullscreen_link' => FALSE,
        'thumb_style' => 'thumbnail',
        'normal_style' => 'large',
        'big_style' => 'original',
        'theme' => 'classic',
        'options' => '',
      ),
    ),
  );

  // This code will never be hit, but is necessary to mark the string
  // for translation on localize.d.o
  // @codingStandardsIgnoreStart
  t('Galleria');
  t('Display image gallery using Galleria, a JavaScript image gallery framework.');

  // @codingStandardsIgnoreEnd
}

/**
 * Implements hook_scald_player_settings_form().
 */
function scald_galleria_scald_player_settings_form($form, &$form_state) {
  $form['theme'] = array(
    '#type' => 'textfield',
    '#title' => t('Galleria theme'),
    '#description' => t('Theme that should be used for Galleria display.'),
    '#size' => 40,
    '#default_value' => $form['#scald']['player_settings']['theme'],
  );
  $form['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Galleria width'),
    '#description' => t('Width of Galleria (in px or other explicit css unit). Use 100% for full width gallery.'),
    '#size' => 40,
    '#default_value' => $form['#scald']['player_settings']['width'],
  );
  $form['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Galleria height'),
    '#description' => t('Height of Galleria (in px or other explicit css unit).'),
    '#size' => 40,
    '#default_value' => $form['#scald']['player_settings']['height'],
  );
  $form['fullscreen_link'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable fullscreen link'),
    '#description' => t('A fullscreen link will be visible above the gallery if enabled.'),
    '#default_value' => $form['#scald']['player_settings']['fullscreen_link'],
  );
  $styles = image_styles();
  $image_styles = array(
    'original' => t('Original image'),
  );
  foreach ($styles as $key => $style) {
    $image_styles[$key] = $style['name'];
  }
  $form['thumb_style'] = array(
    '#type' => 'select',
    '#title' => t('Thumbnail style'),
    '#description' => t('Select image style that should be used for thumbnails.'),
    '#options' => $image_styles,
    '#default_value' => $form['#scald']['player_settings']['thumb_style'],
  );
  $form['normal_style'] = array(
    '#type' => 'select',
    '#title' => t('Normal style'),
    '#description' => t('Select image style that should be used for standard images (big images in Galleria).'),
    '#options' => $image_styles,
    '#default_value' => $form['#scald']['player_settings']['normal_style'],
  );
  $form['big_style'] = array(
    '#type' => 'select',
    '#title' => t('Big style'),
    '#description' => t('Select image style that should be used for fullscreen images.'),
    '#options' => $image_styles,
    '#default_value' => $form['#scald']['player_settings']['big_style'],
  );
  $form['options'] = array(
    '#type' => 'textarea',
    '#title' => t('Galleria options'),
    '#description' => t('Additional Galleria <a href="!link" target="_blank">options</a>. Galleria options must be in <a href="http://json.org/" target="_blank">JSON format</a>. Please note that string must be quoted using double quotes. For example: use <code>{"transition": "fade"}</code> instead of <code>{transition: \'fade\'}</code>.', array(
      '!link' => 'http://galleria.io/docs/options/',
    )),
    '#default_value' => isset($form['#scald']['player_settings']['options']) ? $form['#scald']['player_settings']['options'] : '{}',
  );
  return $form;
}

/**
 * Implements hook_form_alter().
 */
function scald_galleria_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'scald_player_settings_form' && $form['#scald']['player'] == 'galleria') {
    $form['#validate'] = array(
      'scald_galleria_scald_player_settings_form_validate',
    );
  }
}

/**
 * Scald player settings form validate handler.
 */
function scald_galleria_scald_player_settings_form_validate($form, &$form_state) {
  if (!preg_match('/^[a-zA-Z0-9%]+$/', $form_state['values']['width'])) {
    form_set_error('width', t('Incorrect width format.'));
  }
  if (!preg_match('/^[a-zA-Z0-9%]+$/', $form_state['values']['height'])) {
    form_set_error('height', t('Incorrect height format.'));
  }
  if (!empty($form_state['values']['options']) && !json_decode($form_state['values']['options'])) {
    form_set_error('options', t('Incorrect options format.'));
  }
}

/**
 * Implements hook_scald_prerender().
 */
function scald_galleria_scald_prerender($atom, $context, $options, $mode) {
  if ($mode == 'player') {
    if (!($items = field_get_items('scald_atom', $atom, 'gallery_items'))) {
      return;
    }

    // Galleria display settings.
    $players = scald_players();
    $player = $players['galleria'];
    $config = scald_context_config_load($context);
    $settings = isset($config->player[$atom->type]['settings']) ? $config->player[$atom->type]['settings'] : array();
    if (isset($player['settings'])) {

      // Add default settings.
      $settings += $player['settings'];
    }
    $normal_style = $settings['normal_style'];
    $thumb_style = $settings['thumb_style'];
    $big_style = $settings['big_style'];
    if (empty($config->data['width'])) {
      $width = $settings['width'];
    }
    else {
      $width = $config->data['width'];
    }
    if (is_numeric($width)) {
      $width .= 'px';
    }
    if (empty($config->data['height'])) {
      $height = $settings['height'];
    }
    else {
      $height = $config->data['height'];
    }
    if (is_numeric($height)) {
      $height .= 'px';
    }
    $galleria_opts = array(
      'width' => $width,
      'height' => $height,
      'theme' => $settings['theme'],
      'options' => $settings['options'],
      'fullscreen_link' => $settings['fullscreen_link'],
    );

    // Load URLs to thumb, normal and fullscreen images.
    $images = array();
    foreach ($items as $item) {
      if (!($item_atom = scald_atom_load($item['sid']))) {
        continue;
      }
      $thumbnail = field_get_items('scald_atom', $item_atom, 'scald_thumbnail');
      if (!empty($thumbnail[0]['fid'])) {
        $file = file_load($thumbnail[0]['fid']);
        if ($file) {
          $image = array(
            'thumb' => $thumb_style == 'original' ? file_create_url($file->uri) : image_style_url($thumb_style, $file->uri),
            'normal' => $normal_style == 'original' ? file_create_url($file->uri) : image_style_url($normal_style, $file->uri),
            'big' => $big_style == 'original' ? file_create_url($file->uri) : image_style_url($big_style, $file->uri),
            'title' => check_plain($item_atom->title),
          );
          if (!empty($thumbnail[0]['alt'])) {
            $image['alt_attr'] = check_plain($thumbnail[0]['alt']);
          }
          if (!empty($thumbnail[0]['title'])) {
            $image['title_attr'] = check_plain($thumbnail[0]['title']);
          }
          $field = field_get_items('scald_atom', $item_atom, 'scald_authors');
          if (!empty($field)) {
            $authors = field_view_value('scald_atom', $item_atom, 'scald_authors', $field[0], array(
              'type' => 'taxonomy_term_reference_plain',
            ));
            $image['authors'] = $authors['#markup'];
          }
          if (isset($atom->data['items'])) {
            $item_data = $atom->data['items'][$item['sid']];
            if (!empty($item_data['title_overriden'])) {
              $image['title'] = $item_data['title'];
            }
            $image['description'] = $item_data['description'];
          }

          // Hardcode for some video providers. However the $atom->file_source
          // should be used.
          if ($item_atom->provider == 'scald_youtube') {
            $image['iframe'] = 'https://www.youtube.com/embed/' . $item_atom->base_id;
          }
          elseif ($item_atom->provider == 'scald_vimeo') {
            $image['iframe'] = 'https://player.vimeo.com/video/' . $item_atom->base_id;
          }
          elseif ($item_atom->provider == 'scald_dailymotion') {
            $image['iframe'] = 'https://www.dailymotion.com/embed/video/' . $item_atom->base_id;
          }
          elseif ($item_atom->type == 'video') {
            $image['iframe'] = file_create_url($item_atom->file_source);
          }
          $images[] = $image;
        }
      }
    }
    $galleria_id = 'scald-gallery-' . $atom->sid;
    $options = empty($galleria_opts['options']) ? '' : ', ' . $galleria_opts['options'];

    // Prevents multiple loading of the same theme.
    static $processed_themes = array();
    $inline_js = '';
    if (!in_array($galleria_opts['theme'], $processed_themes)) {
      $processed_themes[] = $galleria_opts['theme'];
      $inline_js = "Galleria.loadTheme('" . url(scald_galleria_get_library_file($galleria_opts['theme']), array(
        'language' => (object) array(
          'language' => LANGUAGE_NONE,
        ),
      )) . "');";
    }
    $inline_js .= "if (jQuery('." . $galleria_id . "').length > 0) { Galleria.run('." . $galleria_id . "'" . $options . "); }";
    $atom->rendered->player = array(
      '#theme' => 'scald_galleria',
      '#atom' => $atom,
      '#images' => $images,
      '#options' => $galleria_opts,
      '#attached' => array(
        'js' => array(
          scald_galleria_get_library_file() => array(),
          $inline_js => array(
            'type' => 'inline',
            'scope' => 'footer',
          ),
          // @codingStandardsIgnoreStart
          drupal_get_path('module', 'scald_galleria') . '/scald_galleria.js',
        ),
      ),
    );
    if (!empty($settings['fullscreen_link'])) {
      $fullscreen_toggle_js = drupal_get_path('module', 'scald_galleria') . '/scald_galleria_fullscreen_toggle.js';
      $atom->rendered->player['#attached']['js'][$fullscreen_toggle_js] = array();
    }
  }
}

/**
 * Preprocess function for scald_galleria.tpl.php.
 */
function scald_galleria_preprocess_scald_galleria(&$vars) {

  // Prevents multiple loading of the same atom.
  static $atom_processed = array();
  if (!empty($atom_processed[$vars['atom']->sid])) {
    $vars['items'] = $atom_processed[$vars['atom']->sid]['items'];
    $vars['galleria_id'] = $atom_processed[$vars['atom']->sid]['id'];
    return;
  }

  // Prepare items.
  $items = array();
  foreach ($vars['images'] as $image) {
    $attributes = array(
      'data-title' => $image['title'],
      'data-big' => $image['big'],
    );
    if (isset($image['authors'])) {
      $attributes['data-authors'] = $image['authors'];
    }
    if (isset($image['description'])) {
      $attributes['data-description'] = $image['description'];
    }
    if (isset($image['iframe'])) {
      $attributes['class'] = 'iframe';
    }
    drupal_alter('scald_galleria_item', $image, $attributes, $vars);
    $thumb = theme('image', array(
      'alt' => isset($image['alt_attr']) ? $image['alt_attr'] : '',
      'title' => isset($image['title_attr']) ? $image['title_attr'] : '',
      'path' => $image['thumb'],
      'attributes' => $attributes,
    ));
    $items[] = l($thumb, isset($image['iframe']) ? $image['iframe'] : $image['normal'], array(
      'html' => TRUE,
    ));
  }
  $vars['items'] = $items;
  $vars['galleria_id'] = 'scald-gallery-' . $vars['atom']->sid;

  // Keep the loaded data of the atom in the static variable.
  $atom_processed[$vars['atom']->sid] = array(
    'id' => $vars['galleria_id'],
    'items' => $items,
  );
}

/**
 * Gets Galleria js file (based on galleria.module).
 *
 * @param string $theme
 *    Defines theme that should be loaded. Main Galleria JS will be
 *    returned if set to NULL.
 */
function scald_galleria_get_library_file($theme = NULL) {
  $cache_suffix = $theme ? "_{$theme}" : '';
  $cache = cache_get('scald_galleria_lib_file' . $cache_suffix);
  if ($cache !== FALSE && file_exists($cache->data)) {
    return $cache->data;
  }

  // Search for library file.
  $libpath = libraries_get_path('galleria');

  // Seach for minimized files first.
  // Sort the found files to use the newest version if there's more than one.
  $minpath = $normalpath = $libpath;
  $minpath .= $theme ? "/themes/{$theme}/galleria.{$theme}*.min.js" : '/galleria-*.min.js';
  $normalpath .= $theme ? "/themes/{$theme}/galleria.{$theme}*.js" : '/galleria-*.js';
  $js = glob($minpath);
  if ($js === FALSE || count($js) == 0) {
    $js = glob($normalpath);
  }
  if (count($js) > 0) {
    rsort($js);
    cache_set('scald_galleria_lib_file' . $cache_suffix, $js[0]);
    return $js[0];
  }
  else {

    // Could not find JavaScript library.
    return FALSE;
  }
}