From 2fec0aef7e37c6e372939ac44ce8b6bd2f8aa869 Mon Sep 17 00:00:00 2001
From: David Tomaschik <david@systemoverlord.com>
Date: Tue, 15 Mar 2011 10:16:32 -0400
Subject: [PATCH] Issue #1093308: Add support for importing presentations in Galleria.

---
 galleria-container.tpl.php   |    2 +-
 galleria.module              |    3 +-
 galleria.theme.inc           |    4 +-
 presentation_galleria.info   |    6 ++
 presentation_galleria.module |  170 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 180 insertions(+), 5 deletions(-)
 create mode 100644 presentation_galleria.info
 create mode 100644 presentation_galleria.module

diff --git a/galleria-container.tpl.php b/galleria-container.tpl.php
index 5a416ea..5c3243d 100644
--- a/galleria-container.tpl.php
+++ b/galleria-container.tpl.php
@@ -17,4 +17,4 @@
       <?php endforeach; ?>
       </ul>
   </div>
-<?php endif; ?>
\ No newline at end of file
+<?php endif; ?>
diff --git a/galleria.module b/galleria.module
index 7304f43..c4fd885 100644
--- a/galleria.module
+++ b/galleria.module
@@ -91,7 +91,6 @@ function galleria_field_formatter_settings_summary($field, $instance, $view_mode
  * Prepares a renderable array of images and adds the neccessary JS and CSS
  */
 function galleria_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
- 
   $slides = array();
   
   // stores data for all images in $slides
@@ -221,4 +220,4 @@ function galleria_theme() {
   );
 
   return $styles;
-}
\ No newline at end of file
+}
diff --git a/galleria.theme.inc b/galleria.theme.inc
index 8615bf1..8651367 100644
--- a/galleria.theme.inc
+++ b/galleria.theme.inc
@@ -93,7 +93,7 @@ function template_preprocess_galleria_insert_image(&$variables) {
  */
 function template_preprocess_galleria_container(&$variables) {
   // get the path of the public files dir
-  $basepath = '/' . variable_get('file_public_path', '/sites/default/files') . '/';
+  $basepath = variable_get('file_public_path', '/sites/default/files') . '/';
  
   // get the subdir if image files are stored in one
   $filepath = substr($variables['images'][0]['#item']['uri'], 9);
@@ -104,4 +104,4 @@ function template_preprocess_galleria_container(&$variables) {
   // don't add a 2nd trailing slash if no subdir was configured 
   $path .= (!empty($filepath)) ? $filepath . '/' : '';
   $variables['path'] = $path;
-}
\ No newline at end of file
+}
diff --git a/presentation_galleria.info b/presentation_galleria.info
new file mode 100644
index 0000000..f657f32
--- /dev/null
+++ b/presentation_galleria.info
@@ -0,0 +1,6 @@
+name = Presentation Galleria
+description = Uses the ImageMagick module to convert a PDF presentation and the Galleria module to display it.
+core = 7.x
+
+dependencies[] = imagemagick
+dependencies[] = galleria
diff --git a/presentation_galleria.module b/presentation_galleria.module
new file mode 100644
index 0000000..98f2347
--- /dev/null
+++ b/presentation_galleria.module
@@ -0,0 +1,170 @@
+<?php
+
+/**
+ * Implementation of hook_field_formatter_info().
+ * 
+ * Adds the Galleria format option within the manage display form of
+ * of a file field.
+ */
+function presentation_galleria_field_formatter_info() {
+  $formatters = array(
+    'presentation_galleria' => array(
+      'label' => t('Presentation Galleria'),
+      'field types' => array('file'),
+      'settings' => array(
+        'galleria_zoom_style' => 'presentation_galleria_zoom',
+        'galleria_thumb_style' => 'presentation_galleria_thumb', 
+        'galleria_player_theme' => ''
+      ),
+    ),
+  );
+  return $formatters;
+}
+
+/**
+ * Implementation of hook_field_formatter_settings_form().
+ * 
+ * Wraps standard galleria settings.
+ */
+function presentation_galleria_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
+  return galleria_field_formatter_settings_form($field, $instance, $view_mode, $form, $form_state);
+}
+
+/**
+ * Implementation of hook_field_formatter_settings_summary().
+ * 
+ * Displays the summary of the set options of a Galleria formatted image field
+ */
+function presentation_galleria_field_formatter_settings_summary($field, $instance, $view_mode) {
+  return galleria_field_formatter_settings_summary($field, $instance, $view_mode);
+}
+
+/**
+ * Implementation of hook_field_formatter_view().
+ * 
+ * Prepares a renderable array of images and adds the neccessary JS and CSS
+ */
+function presentation_galleria_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $item, $display) {
+ 
+  $slides = array();
+
+  /* Convert and get list */  
+  $items = _presentation_galleria_convert($item[0]);
+  
+  // stores data for all images in $slides
+  foreach ($items as $delta => $item) {
+    $slides[$delta] = array(
+      '#theme' => 'galleria_image_formatter_item',
+      '#item' => $item,
+      '#node' => $entity,
+      '#field' => $field,
+      '#galleria_zoom_style' => $display['settings']['galleria_zoom_style'],
+      '#galleria_thumb_style' => $display['settings']['galleria_thumb_style'],
+    );
+  }
+  
+  // if there are images init the galleria
+  if (count($slides) > 0) {
+    galleria_includes();
+   
+    $element = array(
+      '#theme' => 'galleria_container',
+      '#images' => $slides,
+    );
+    
+    return $element;
+  }
+  else {
+    // no images to show, therefore cancel display
+    return;
+  }
+}
+
+/**
+ * Implementation of hook_image_default_styles().
+ * 
+ * Provides default image style presets that can be overridden by site administrators.
+ */
+function presentation_galleria_image_default_styles() {
+  $styles = array();
+
+  // image preset for the big image
+  $styles['presentation_galleria_zoom'] = array(
+    'effects' => array(
+      array(
+        'name' => 'image_scale',
+        'data' => array('width' => 640, 'height' => 480, 'upscale' => 1),
+        'weight' => 0,
+      ),
+    )
+  );
+  
+  // image preset for thumbnails
+  $styles['presentation_galleria_thumb'] = array(
+    'effects' => array(
+      array(
+        'name' => 'image_scale',
+        'data' => array('width' => 64, 'height' => 48, 'upscale' => 1),
+        'weight' => 0,
+      ),
+    )
+  );
+
+  return $styles;
+}
+
+/* 
+ * Convert presentation
+ */
+function _presentation_galleria_convert($source){
+  /* Get source path */
+  $srcfile = drupal_realpath($source['uri']);
+  $scheme = file_uri_scheme($source['uri']);
+  $target = file_uri_target($source['uri']);
+  $tarinfo = pathinfo($target);
+  $tardir = $tarinfo['dirname']; /* URI directory */
+  
+  /* Figure out destination */
+  $path = pathinfo($srcfile);
+  $destdir = $path['dirname'].DIRECTORY_SEPARATOR.$path['filename'];
+  if(!file_exists($destdir)){
+    // Does not exist, convert
+    if(!mkdir($destdir)){
+      // Could not make directory
+      trigger_error("Unable to create directory $destdir.",E_USER_ERROR);
+      return array();
+    }
+    
+    /* Make destination path */
+    $dest = $destdir.DIRECTORY_SEPARATOR.$path['filename']."-%04d.png";
+    
+    /* Actually convert */
+    if(!_presentation_galleria_convert_real($srcfile,$dest)){
+      trigger_error("Unable to convert presentation.",E_USER_ERROR);
+      return array();
+    }
+  }
+  
+  /* Get list of files */
+  $list = array();
+  $files = scandir($destdir);
+  foreach($files as $file){
+    if($file=="." || $file=="..")
+      continue;
+    $list[]=array(
+      'uri'=>$scheme."://".$tardir."/".$path['filename']."/".$file,
+      'filename'=>$file,
+      'title'=>$file
+    );
+  }
+  
+  return $list;
+}
+
+/* 
+ * Really convert 
+ */
+function _presentation_galleria_convert_real($source,$dest){
+  $command = escapeshellarg($source).' '.escapeshellarg($dest);
+  return _imagemagick_convert_exec($command,$output,$errors);
+}
-- 
1.7.1

