<?php
/**
 * Plugin Name: NextGEN Album Browser
 * Plugin URI: http://www.doof.me.uk
 * Description: Show all NGG albums on a single page.
 * Version: 0.1
 * Author: Nick Gasson
 * License: GPL2
 */

/* Copyright 2010  Nick Gasson <nick@nickg.me.uk>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2, as 
   published by the Free Software Foundation.
   
   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

function ngg_album_browse_one($nggdb) {
   global $wp_query;
   $album_id = $wp_query->query_vars['album'];
      
   $album = $nggdb->find_album($album_id);

   $back_link = remove_query_arg(array('gallery', 'album'));

   $result = "<a href=\"{$back_link}\">Back to albums</a>\n";
   
   $result .= "<h2>{$album->name}</h2>\n";
   $result .= "<p>{$album->albumdesc}</p>\n";
   
   if (isset($wp_query->query_vars['gallery'])) {
      $gallery_id = $wp_query->query_vars['gallery'];
      $gallery = $nggdb->find_gallery($gallery_id);

      $result .= "<h3>{$gallery->title}</h3>\n";
      $result .= "<p>{$gallery->galdesc}</p>\n";

      $result .= nggShowGallery($gallery_id, '');

      $result .= "<h3>Other Galleries</h3>\n";
   }
   
   $result .= nggCreateAlbum($album->gallery_ids, 'extend', $album);

   return $result;
}

function ngg_album_browse_all($nggdb, $order) {
   $albums = $nggdb->find_all_album();

   if ($order == 'random') {
      shuffle($albums);
   }
   
   $result = '';
   foreach ($albums as $a) {
      $result .= "<h2>{$a->name}</h2>\n";
      $result .= nggCreateAlbum($a->galleries, 'compact', $a);
      $result .= '<div class="ngg-clear"></div><br/>';
   }

   return $result;
}

function ngg_album_browse_shortcode($atts) {
   $nggdb = $GLOBALS['nggdb'];

   extract(shortcode_atts(array('order' => ''), $atts));

   global $wp_query;
   if (isset($wp_query->query_vars['album'])) {
      return ngg_album_browse_one($nggdb);
   }
   else {
      return ngg_album_browse_all($nggdb, $order);
   }   
}

add_shortcode('albumbrowser', 'ngg_album_browse_shortcode');

?>
