You are here

scald_gallery.module in Scald: Gallery 7

Same filename and directory in other branches
  1. 7.2 scald_gallery.module

Scald Gallery is a Scald Atom Provider for image galleries.

File

scald_gallery.module
View source
<?php

/**
 * @file
 * Scald Gallery is a Scald Atom Provider for image galleries.
 */

/**
 * Implements hook_theme().
 */
function scald_gallery_theme() {
  return array(
    'scald_gallery' => array(
      'variables' => array(
        'atom' => NULL,
        'images' => array(),
        'galleria_options' => array(
          'width' => '690',
          'height' => '450',
          'theme' => 'classic',
        ),
      ),
      'template' => 'scald_gallery',
    ),
  );
}

/**
 * Implements hook_scald_atom_providers().
 */
function scald_gallery_scald_atom_providers() {
  return array(
    'gallery' => 'Gallery items upload',
  );

  // This code will never be hit, but is necessary to mark the string
  // for translation on localize.d.o
  t('Gallery items upload');
}

/**
 * Implements hook_scald_player().
 */
function scald_gallery_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',
        'thumb_style' => 'thumbnail',
        'normal_style' => 'large',
        'big_style' => 'original',
        'theme' => 'classic',
      ),
    ),
  );

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

/**
 * Implements hook_scald_player_settings_form().
 */
function scald_gallery_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. Defaults to "classic".'),
    '#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.'),
    '#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.'),
    '#size' => 40,
    '#default_value' => $form['#scald']['player_settings']['height'],
  );
  $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'],
  );
  return $form;
}

/**
 * Implements hook_scald_add_form().
 */
function scald_gallery_scald_add_form(&$form, &$form_state) {
  $form['gallery'] = array(
    '#type' => 'plupload',
    '#upload_validators' => array(
      'file_validate_extensions' => array(
        'jpg jpeg png gif',
      ),
    ),
  );
}

/**
 * Implements hook_scald_add_form_fill().
 */
function scald_gallery_scald_add_form_fill(&$atom, $form, $form_state) {
  $gallery_items = _scald_gallery_save_items($form_state);
  $atom->gallery_items[LANGUAGE_NONE] = array_map(function ($item) {
    return array(
      'sid' => $item->sid,
    );
  }, $gallery_items);
  $atom->scald_thumbnail[LANGUAGE_NONE][0] = (array) $gallery_items[0]->scald_thumbnail[LANGUAGE_NONE][0];
}

/**
 * Implements hook_scald_fetch().
 */
function scald_gallery_scald_fetch($atom, $type) {
  if ($type == 'atom') {
    $file = $atom->scald_thumbnail[LANGUAGE_NONE][0]['uri'];
    if (file_exists($file)) {
      $atom->file_source = $atom->thumbnail_source = $file;
    }
  }
}

/**
 * Implements hook_scald_prerender().
 */
function scald_gallery_scald_prerender($atom, $context, $options, $mode) {
  if ($mode == 'player') {

    // Galleria display settings.
    $config = scald_context_config_load($context);
    $normal_style = $config->player[$atom->type]['settings']['normal_style'];
    $thumb_style = $config->player[$atom->type]['settings']['thumb_style'];
    $big_style = $config->player[$atom->type]['settings']['big_style'];
    $galleria_opts = array(
      'width' => $config->player[$atom->type]['settings']['width'],
      'height' => $config->player[$atom->type]['settings']['height'],
      'theme' => $config->player[$atom->type]['settings']['theme'],
    );

    // Load URLs to thumb, normal and fullscreen images.
    $images = array();
    $items = field_get_items('scald_atom', $atom, 'gallery_items');
    foreach ($items as $item) {
      $item_atom = scald_atom_load($item['sid']);
      if ($atom) {
        $thumbnail = field_get_items('scald_atom', $item_atom, 'scald_thumbnail');
        if (!empty($thumbnail[0]['fid'])) {
          $file = file_load($thumbnail[0]['fid']);
          if ($file) {
            $images[] = 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),
            );
          }
        }
      }
    }
    $atom->rendered->player = theme('scald_gallery', array(
      'atom' => $atom,
      'images' => $images,
      'galleria_options' => $galleria_opts,
    ));

    // We are not caching since we include JS in preprocess.
    $atom->rendered->nocache = TRUE;
  }
}

/**
 * Preprocess function for scald_gallery.tpl.php.
 */
function scald_gallery_preprocess_scald_gallery(&$vars) {

  // Prepare items.
  $items = array();
  foreach ($vars['images'] as $image) {
    $thumb = theme('image', array(
      'path' => $image['thumb'],
      'attributes' => array(
        'data-title' => $image['title'],
        'data-big' => $image['big'],
      ),
    ));
    $items[] = l($thumb, $image['normal'], array(
      'html' => TRUE,
    ));
  }
  $vars['items'] = $items;

  // Init galleria.
  $vars['galleria_id'] = 'scald-gallery-' . $vars['atom']->sid;
  $galleria_path = libraries_get_path('galleria');
  $galleria_js_path = scald_gallery_get_library_file();
  drupal_add_js($galleria_js_path);
  $theme_js_path = scald_gallery_get_library_file($vars['galleria_options']['theme']);
  drupal_add_js($theme_js_path);
  $inline_js = "Galleria.run('#" . $vars['galleria_id'] . "');";
  drupal_add_js($inline_js, array(
    'type' => 'inline',
    'scope' => 'footer',
  ));
}

/**
 * Saves files uploaded with Plupload as Drupal file entites.
 */
function _scald_gallery_save_items($form_state) {
  $files = array();
  $images = array();
  $dest = variable_get('scald_gallery_upload_loc', 'public://atoms/galleries');
  if (!file_prepare_directory($dest, FILE_CREATE_DIRECTORY || FILE_MODIFY_PERMISSIONS)) {
    drupal_set_message(t('Gallery items could not be uploaded since we were unable to write to the destination directory.', 'error'));
    return $files;
  }
  if (!empty($form_state['values']['gallery'])) {

    // Save uploaded files as core file entites first.
    foreach ($form_state['values']['gallery'] as $i => $file) {
      $source = $file['tmppath'];
      $destination = file_stream_wrapper_uri_normalize($dest . '/' . $file['name']);
      $destination = file_unmanaged_move($source, $destination, FILE_EXISTS_RENAME);
      $file = plupload_file_uri_to_object($destination);
      $file->status = 0;
      file_save($file);
      $files[] = $file;
    }

    // Now create scald_image items.
    foreach ($files as $file) {
      $atom = new stdClass();
      $atom->title = $file->filename;
      $atom->base_id = $file->fid;
      $atom->scald_thumbnail[LANGUAGE_NONE][0] = (array) $file;
      $atom->type = 'image';
      $atom->provider = 'scald_image';
      scald_atom_save($atom);
      $atoms[] = $atom;
    }
  }
  return $atoms;
}

/**
 * Gets Galleria js file (based on galleria.module).
 *
 * @param $theme Defines theme that should be loaded. Main Galleria JS will be
 *   returned if set to NULL.
 */
function scald_gallery_get_library_file($theme = NULL) {
  $cache_suffix = $theme ? "_{$theme}" : '';
  $cache = cache_get('scald_gallery_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_gallery_galleria_lib_file' . $cache_suffix, $js[0]);
    return $js[0];
  }
  else {

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