From fcea03a8002c0caa82700f30dbb95643267be347 Mon Sep 17 00:00:00 2001
From: Jan Dittberner <jan@dittberner.info>
Date: Sat, 11 Jul 2009 11:53:43 +0000
Subject: [PATCH] make themes configurable (fixes #50)

 * gallery.ini:
  - remove theme specific configuration
  - add default theme name
 * includes/galleryfunctions.php:
  - implement theme initialization
  - configure common parameters from theme
 * includes/theme.class.php:
  - implement a simple theme class
 * index.php:
  - assign $gallery to template
 * themes/default_horizontal/theme.ini:
  - add theme specific configuration
 * themes/default_horizontal/theme.php:
  - use $content['gallery'] instead of undefined $gallery
---
 gallery.ini                         |  3 +-
 includes/galleryfunctions.php       |  7 ++-
 includes/theme.class.php            | 70 +++++++++++++++++++++++++++++
 index.php                           |  3 +-
 themes/default_horizontal/theme.ini |  4 ++
 themes/default_horizontal/theme.php |  2 +-
 6 files changed, 83 insertions(+), 6 deletions(-)
 create mode 100644 includes/theme.class.php
 create mode 100644 themes/default_horizontal/theme.ini

diff --git a/gallery.ini b/gallery.ini
index ada09a5..9d8cff3 100644
--- a/gallery.ini
+++ b/gallery.ini
@@ -1,3 +1,2 @@
 logfile=gallery.log
-previewwidth=311
-thumbheight=67
+defaulttheme=default_horizontal
diff --git a/includes/galleryfunctions.php b/includes/galleryfunctions.php
index 1421c84..2ce398c 100644
--- a/includes/galleryfunctions.php
+++ b/includes/galleryfunctions.php
@@ -45,15 +45,18 @@ if (array_key_exists('logfile', $configuration)) {
   ini_set('error_log', $configuration['logfile']);
 }
 
+require_once('theme.class.php');
+$theme = new Theme($configuration['defaulttheme']);
+
 /**
  * Breite der Vorschaubilder.
  */
-$previewwidth = $configuration['previewwidth'];
+$previewwidth = $theme->previewsize;
 
 /**
  * Höhe der Thumbnailbilder.
  */
-$thumbheight = $configuration['thumbheight'];
+$thumbheight = $theme->thumbsize;
 
 /**
  * Prüft, ob eine Galerie mit dem übergebenen Namen existiert.
diff --git a/includes/theme.class.php b/includes/theme.class.php
new file mode 100644
index 0000000..b8ccdfe
--- /dev/null
+++ b/includes/theme.class.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * @file defines a theme class for ScrollingJQueryGallery
+ *
+ * @author Jan Dittberner <jan@dittberner.info>
+ *
+ * @version $Id$
+ *
+ * Copyright (c) 2009 Jan Dittberner
+ * Jan Dittberner IT-Consulting & -Solutions
+ * Cottbuser Str. 1, D-01129 Dresden
+ *
+ * This file is part of the ScrollingJQueryGallery component of the
+ * gnuviech-server.de Websitetools
+ *
+ * ScrollingJQueryGallery is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * ScrollingJQueryGallery 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with ScrollingJQueryGallery.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Theme class.
+ */
+class Theme {
+  /**
+   * Theme type 'horizontal' or 'vertical'
+   * @var string
+   */
+  var $themetype;
+
+  /**
+   * Size of preview images. Used for calculating preview image sizes.
+   * @var int
+   */
+  var $previewsize;
+
+  /**
+   * Size of thumbnail images. Depending on the theme type this is
+   * used for calculating the width (vertical themes) or height
+   * (horizontal themes) of the thumbnal images.
+   * @var int
+   */
+  var $thumbsize;
+
+  /**
+   * Constructor for themes. Expects a theme name and initializes the
+   * internal state of the instance from the 'theme.ini' file in the
+   * named theme's directory.
+   * @param string $name the directory name of the theme
+   */
+  function __construct($name) {
+    $themeconfig = parse_ini_file(
+      realpath(implode(DIRECTORY_SEPARATOR,
+                       array('themes', $name, 'theme.ini'))));
+    $this->themetype = $themeconfig['themetype'];
+    $this->previewsize = intval($themeconfig['previewsize']);
+    $this->thumbsize = intval($themeconfig['thumbsize']);
+  }
+}
+?>
\ No newline at end of file
diff --git a/index.php b/index.php
index 23f6c27..4fe31eb 100644
--- a/index.php
+++ b/index.php
@@ -49,8 +49,8 @@ $thumbinfo = getThumbNailInfo($gallery);
 
 require('includes/template.class.php');
 
-
 $template = new Template();
+
 $scripts = array('js/jquery.js',
                  'js/jquery.colorBlend.js',
                  'js/jquery.lightbox.js',
@@ -87,6 +87,7 @@ $template->assign('firstpreview', getFirstPreview($thumbinfo));
 $template->assign('firstdescription', getFirstDescription($thumbinfo));
 $template->assign('lang', 'de');
 $template->assign('themepath', 'themes/default_horizontal');
+$template->assign('gallery', $gallery);
 
 $template->display('themes/default_horizontal/theme.php');
 
diff --git a/themes/default_horizontal/theme.ini b/themes/default_horizontal/theme.ini
new file mode 100644
index 0000000..87c98c1
--- /dev/null
+++ b/themes/default_horizontal/theme.ini
@@ -0,0 +1,4 @@
+themename=Horizontal default theme
+themetype=horizontal
+previewsize=311
+thumbsize=67
diff --git a/themes/default_horizontal/theme.php b/themes/default_horizontal/theme.php
index 7b5dd95..322d155 100644
--- a/themes/default_horizontal/theme.php
+++ b/themes/default_horizontal/theme.php
@@ -22,7 +22,7 @@
       <ul id="menu"><?php
  foreach ($content['gallerylinks'] as $data) {
    printf('<li class="menu%s"><a class="menu" href="%s">%s </a></li>',
-          ($data['gallery'] == $gallery) ? ' active' : '',
+          ($data['gallery'] == $content['gallery']) ? ' active' : '',
           $data['url'], $data['label']);
  } ?></ul>
       <div id="content_container">