Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

nggfunctions.php

By: a guest on Oct 16th, 2012  |  syntax: PHP  |  size: 46.27 KB  |  views: 100  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }
  4.  
  5. /**
  6.  * Return a script for the Imagerotator flash slideshow. Can be used in any template with <?php echo nggShowSlideshow($galleryID, $width, $height) ?>
  7.  * Require the script swfobject.js in the header or footer
  8.  *
  9.  * @access public
  10.  * @param integer $galleryID ID of the gallery
  11.  * @param integer $irWidth Width of the flash container
  12.  * @param integer $irHeight Height of the flash container
  13.  * @return the content
  14.  */
  15. function nggShowSlideshow($galleryID, $width, $height) {
  16.    
  17.     require_once (dirname (__FILE__).'/lib/swfobject.php');
  18.  
  19.     $ngg_options = nggGallery::get_option('ngg_options');
  20.  
  21.     // remove media file from RSS feed
  22.     if ( is_feed() ) {
  23.         $out = '[' . nggGallery::i18n($ngg_options['galTextSlide']) . ']';
  24.         return $out;
  25.     }
  26.  
  27.     //Redirect all calls to the JavaScript slideshow if wanted
  28.     if ( $ngg_options['enableIR'] !== '1' || nggGallery::detect_mobile_phone() === true )
  29.         return nggShow_JS_Slideshow($galleryID, $width, $height);
  30.    
  31.     // If the Imagerotator didn't exist, skip the output
  32.     if ( NGGALLERY_IREXIST == false )
  33.         return;
  34.        
  35.     if (empty($width) ) $width  = (int) $ngg_options['irWidth'];
  36.     if (empty($height)) $height = (int) $ngg_options['irHeight'];
  37.     // Doesn't work fine with zero
  38.     $ngg_options['irRotatetime'] = ($ngg_options['irRotatetime'] == 0) ? 5 : $ngg_options['irRotatetime'];
  39.     // init the flash output
  40.     $swfobject = new swfobject( $ngg_options['irURL'] , 'so' . $galleryID, $width, $height, '7.0.0', 'false');
  41.  
  42.     $swfobject->message = '<p>'. __('The <a href="http://www.macromedia.com/go/getflashplayer">Flash Player</a> and <a href="http://www.mozilla.com/firefox/">a browser with Javascript support</a> are needed.', 'nggallery').'</p>';
  43.     $swfobject->add_params('wmode', 'opaque');
  44.     $swfobject->add_params('allowfullscreen', 'true');
  45.     $swfobject->add_params('bgcolor', $ngg_options['irScreencolor'], 'FFFFFF', 'string', '#');
  46.     $swfobject->add_attributes('styleclass', 'slideshow');
  47.     $swfobject->add_attributes('name', 'so' . $galleryID);
  48.  
  49.     // adding the flash parameter  
  50.     $swfobject->add_flashvars( 'file', urlencode ( trailingslashit ( home_url() ) . 'index.php?callback=imagerotator&gid=' . $galleryID ) );
  51.     $swfobject->add_flashvars( 'shuffle', $ngg_options['irShuffle'], 'true', 'bool');
  52.     // option has oposite meaning : true should switch to next image
  53.     $swfobject->add_flashvars( 'linkfromdisplay', !$ngg_options['irLinkfromdisplay'], 'false', 'bool');
  54.     $swfobject->add_flashvars( 'shownavigation', $ngg_options['irShownavigation'], 'true', 'bool');
  55.     $swfobject->add_flashvars( 'showicons', $ngg_options['irShowicons'], 'true', 'bool');
  56.     $swfobject->add_flashvars( 'kenburns', $ngg_options['irKenburns'], 'false', 'bool');
  57.     $swfobject->add_flashvars( 'overstretch', $ngg_options['irOverstretch'], 'false', 'string');
  58.     $swfobject->add_flashvars( 'rotatetime', $ngg_options['irRotatetime'], 5, 'int');
  59.     $swfobject->add_flashvars( 'transition', $ngg_options['irTransition'], 'random', 'string');
  60.     $swfobject->add_flashvars( 'backcolor', $ngg_options['irBackcolor'], 'FFFFFF', 'string', '0x');
  61.     $swfobject->add_flashvars( 'frontcolor', $ngg_options['irFrontcolor'], '000000', 'string', '0x');
  62.     $swfobject->add_flashvars( 'lightcolor', $ngg_options['irLightcolor'], '000000', 'string', '0x');
  63.     $swfobject->add_flashvars( 'screencolor', $ngg_options['irScreencolor'], '000000', 'string', '0x');
  64.     if ($ngg_options['irWatermark'])
  65.         $swfobject->add_flashvars( 'logo', $ngg_options['wmPath'], '', 'string');
  66.     $swfobject->add_flashvars( 'audio', $ngg_options['irAudio'], '', 'string');
  67.     $swfobject->add_flashvars( 'width', $width, '260');
  68.     $swfobject->add_flashvars( 'height', $height, '320');  
  69.     // create the output
  70.     $out  = '<div class="slideshow">' . $swfobject->output() . '</div>';
  71.     // add now the script code
  72.     $out .= "\n".'<script type="text/javascript" defer="defer">';
  73.     // load script via jQuery afterwards
  74.     // $out .= "\n".'jQuery.getScript( "' . esc_js( includes_url('js/swfobject.js') ) . '", function() {} );';
  75.     if ($ngg_options['irXHTMLvalid']) $out .= "\n".'<!--';
  76.     if ($ngg_options['irXHTMLvalid']) $out .= "\n".'//<![CDATA[';
  77.     $out .= $swfobject->javascript();
  78.     if ($ngg_options['irXHTMLvalid']) $out .= "\n".'//]]>';
  79.     if ($ngg_options['irXHTMLvalid']) $out .= "\n".'-->';
  80.     $out .= "\n".'</script>';
  81.  
  82.     $out = apply_filters('ngg_show_slideshow_content', $out, $galleryID, $width, $height);
  83.            
  84.     return $out;    
  85. }
  86.  
  87. /**
  88.  * Return a script for the jQuery based slideshow. Can be used in any template with <?php echo nggShow_JS_Slideshow($galleryID, $width, $height) ?>
  89.  * Require the script jquery.cycle.all.js
  90.  *
  91.  * @since 1.6.0
  92.  * @access public
  93.  * @param integer $galleryID ID of the gallery
  94.  * @param integer $width Width of the slideshow container
  95.  * @param integer $height Height of the slideshow container
  96.  * @param string $class Classname of the div container
  97.  * @return the content
  98.  */
  99. function nggShow_JS_Slideshow($galleryID, $width, $height, $class = 'ngg-slideshow') {
  100.        
  101.     global $slideCounter;
  102.    
  103.     $ngg_options = nggGallery::get_option('ngg_options');
  104.    
  105.     // we need to know the current page id
  106.     $current_page = (get_the_ID() == false) ? rand(5, 15) : get_the_ID();
  107.         // look for a other slideshow instance
  108.         if ( !isset($slideCounter) ) $slideCounter = 1;
  109.     // create unique anchor
  110.     $anchor = 'ngg-slideshow-' . $galleryID . '-' . $current_page . '-' . $slideCounter++;
  111.    
  112.     if (empty($width) ) $width  = (int) $ngg_options['irWidth'];
  113.     if (empty($height)) $height = (int) $ngg_options['irHeight'];
  114.    
  115.     //filter to resize images for mobile browser
  116.     list($width, $height) = apply_filters('ngg_slideshow_size', array( $width, $height ) );
  117.    
  118.     $width  = (int) $width;
  119.     $height = (int) $height;
  120.            
  121.     $out  = '<div id="' . $anchor . '" class="' . $class . '" style="height:' . $height . 'px;width:' . $width . 'px;">';
  122.     $out .= "\n". '<div id="' . $anchor . '-loader" class="ngg-slideshow-loader" style="height:' . $height . 'px;width:' . $width . 'px;">';
  123.     $out .= "\n". '<img src="'. NGGALLERY_URLPATH . 'images/loader.gif" alt="" />';
  124.     $out .= "\n". '</div>';
  125.     $out .= '</div>'."\n";
  126.         $out .= '<div id="ngg-js-slideshow-caption"></div>'; //MOD: create an element for our caption / alt text
  127.     $out .= "\n".'<script type="text/javascript" defer="defer">';
  128.     $out .= "\n" . 'jQuery(document).ready(function(){ ' . "\n" . 'jQuery("#' . $anchor . '").nggSlideshow( {' .
  129.             'id: '      . $galleryID    . ',' .
  130.             'fx:"'      . $ngg_options['slideFx'] . '",' .
  131.             'width:'    . $width        . ',' .
  132.             'height:'   . $height       . ',' .
  133.             'domain: "' . trailingslashit ( home_url() ) . '",' .
  134.             'timeout:'  . $ngg_options['irRotatetime'] * 1000 .
  135.             '});' . "\n" . '});';
  136.     $out .= "\n".'</script>';
  137.  
  138.     return $out;
  139. }
  140.  
  141. /**
  142.  * nggShowGallery() - return a gallery  
  143.  *
  144.  * @access public
  145.  * @param int | string ID or slug from a gallery
  146.  * @param string $template (optional) name for a template file, look for gallery-$template
  147.  * @param int $images (optional) number of images per page
  148.  * @return the content
  149.  */
  150. function nggShowGallery( $galleryID, $template = '', $images = false ) {
  151.    
  152.     global $nggRewrite;
  153.  
  154.     $ngg_options = nggGallery::get_option('ngg_options');
  155.    
  156.     //Set sort order value, if not used (upgrade issue)
  157.     $ngg_options['galSort'] = ($ngg_options['galSort']) ? $ngg_options['galSort'] : 'pid';
  158.     $ngg_options['galSortDir'] = ($ngg_options['galSortDir'] == 'DESC') ? 'DESC' : 'ASC';
  159.    
  160.     // get gallery values
  161.     //TODO: Use pagination limits here to reduce memory needs
  162.     $picturelist = nggdb::get_gallery($galleryID, $ngg_options['galSort'], $ngg_options['galSortDir']);
  163.  
  164.     if ( !$picturelist )
  165.         return __('[Gallery not found]','nggallery');
  166.  
  167.     // If we have we slug instead the id, we should extract the ID from the first image
  168.     if ( !is_numeric($galleryID) ) {
  169.         $first_image = current($picturelist);
  170.         $galleryID = intval($first_image->gid);
  171.     }
  172.  
  173.     // $_GET from wp_query
  174.     $show    = get_query_var('show');
  175.     $pid     = get_query_var('pid');
  176.     $pageid  = get_query_var('pageid');
  177.    
  178.     // set $show if slideshow first
  179.     if ( empty( $show ) AND ($ngg_options['galShowOrder'] == 'slide')) {
  180.         if ( is_home() )
  181.             $pageid = get_the_ID();
  182.        
  183.         $show = 'slide';
  184.     }
  185.  
  186.     // filter to call up the imagebrowser instead of the gallery
  187.     // use in your theme : add_action( 'ngg_show_imagebrowser_first', create_function('', 'return true;') );
  188.     if ( apply_filters('ngg_show_imagebrowser_first', false, $galleryID ) && $show != 'thumbnails' )  {
  189.         $out = nggShowImageBrowser( $galleryID, $template );
  190.         return $out;
  191.     }
  192.  
  193.     // go on only on this page
  194.     if ( !is_home() || $pageid == get_the_ID() ) {
  195.            
  196.         // 1st look for ImageBrowser link
  197.         if ( !empty($pid) && $ngg_options['galImgBrowser'] && ($template != 'carousel') )  {
  198.             $out = nggShowImageBrowser( $galleryID, $template );
  199.             return $out;
  200.         }
  201.        
  202.         // 2nd look for slideshow
  203.         if ( $show == 'slide' ) {
  204.             $args['show'] = "gallery";
  205.             $out  = '<div class="ngg-galleryoverview">';
  206.             $out .= '<div class="slideshowlink"><a class="slideshowlink" href="' . $nggRewrite->get_permalink($args) . '">'.nggGallery::i18n($ngg_options['galTextGallery']).'</a></div>';
  207.             $out .= nggShowSlideshow($galleryID, $ngg_options['irWidth'], $ngg_options['irHeight']);
  208.             $out .= '</div>'."\n";
  209.             $out .= '<div class="ngg-clear"></div>'."\n";
  210.             return $out;
  211.         }
  212.     }
  213.  
  214.     // get all picture with this galleryid
  215.     if ( is_array($picturelist) )
  216.         $out = nggCreateGallery($picturelist, $galleryID, $template, $images);
  217.    
  218.     $out = apply_filters('ngg_show_gallery_content', $out, intval($galleryID));
  219.     return $out;
  220. }
  221.  
  222. /**
  223.  * Build a gallery output
  224.  *
  225.  * @access internal
  226.  * @param array $picturelist
  227.  * @param bool $galleryID, if you supply a gallery ID, you can add a slideshow link
  228.  * @param string $template (optional) name for a template file, look for gallery-$template
  229.  * @param int $images (optional) number of images per page
  230.  * @return the content
  231.  */
  232. function nggCreateGallery($picturelist, $galleryID = false, $template = '', $images = false) {
  233.     global $nggRewrite;
  234.  
  235.     require_once (dirname (__FILE__) . '/lib/media-rss.php');
  236.    
  237.     $ngg_options = nggGallery::get_option('ngg_options');
  238.  
  239.     //the shortcode parameter will override global settings, TODO: rewrite this to a class
  240.     $ngg_options['galImages'] = ( $images === false ) ? $ngg_options['galImages'] : (int) $images;  
  241.    
  242.     $current_pid = false;
  243.        
  244.     // $_GET from wp_query
  245.     $nggpage  = get_query_var('nggpage');
  246.     $pageid   = get_query_var('pageid');
  247.     $pid      = get_query_var('pid');
  248.    
  249.     // in case of permalinks the pid is a slug, we need the id
  250.     if( !is_numeric($pid) && !empty($pid) ) {
  251.         $picture = nggdb::find_image($pid);        
  252.         $pid = $picture->pid;
  253.     }  
  254.    
  255.     // we need to know the current page id
  256.     $current_page = (get_the_ID() == false) ? 0 : get_the_ID();
  257.  
  258.     if ( !is_array($picturelist) )
  259.         $picturelist = array($picturelist);
  260.    
  261.     // Populate galleries values from the first image          
  262.     $first_image = current($picturelist);
  263.     $gallery = new stdclass;
  264.     $gallery->ID = (int) $galleryID;
  265.     $gallery->show_slideshow = false;
  266.     $gallery->show_piclens = false;
  267.     $gallery->name = stripslashes ( $first_image->name  );
  268.     $gallery->title = stripslashes( $first_image->title );
  269.     $gallery->description = html_entity_decode(stripslashes( $first_image->galdesc));
  270.     $gallery->pageid = $first_image->pageid;
  271.     $gallery->anchor = 'ngg-gallery-' . $galleryID . '-' . $current_page;
  272.     reset($picturelist);
  273.  
  274.     $maxElement  = $ngg_options['galImages'];
  275.     $thumbwidth  = $ngg_options['thumbwidth'];
  276.     $thumbheight = $ngg_options['thumbheight'];    
  277.    
  278.     // fixed width if needed
  279.     $gallery->columns    = intval($ngg_options['galColumns']);
  280.     $gallery->imagewidth = ($gallery->columns > 0) ? 'style="width:' . floor(100/$gallery->columns) . '%;"' : '';
  281.    
  282.     // obsolete in V1.4.0, but kept for compat reason
  283.         // pre set thumbnail size, from the option, later we look for meta data.
  284.     $thumbsize = ($ngg_options['thumbfix']) ? $thumbsize = 'width="' . $thumbwidth . '" height="'.$thumbheight . '"' : '';
  285.    
  286.     // show slideshow link
  287.     if ($galleryID) {
  288.         if ($ngg_options['galShowSlide']) {
  289.             $gallery->show_slideshow = true;
  290.             $gallery->slideshow_link = $nggRewrite->get_permalink(array ( 'show' => 'slide') );
  291.             $gallery->slideshow_link_text = nggGallery::i18n($ngg_options['galTextSlide']);
  292.         }
  293.        
  294.         if ($ngg_options['usePicLens']) {
  295.             $gallery->show_piclens = true;
  296.             $gallery->piclens_link = "javascript:PicLensLite.start({feedUrl:'" . htmlspecialchars( nggMediaRss::get_gallery_mrss_url($gallery->ID) ) . "'});";
  297.         }
  298.     }
  299.  
  300.     // check for page navigation
  301.     if ($maxElement > 0) {
  302.        
  303.         if ( !is_home() || $pageid == $current_page )
  304.             $page = ( !empty( $nggpage ) ) ? (int) $nggpage : 1;
  305.         else
  306.             $page = 1;
  307.          
  308.         $start = $offset = ( $page - 1 ) * $maxElement;
  309.        
  310.         $total = count($picturelist);
  311.  
  312.                 //we can work with display:hidden for some javascript effects
  313.         if (!$ngg_options['galHiddenImg']){
  314.                 // remove the element if we didn't start at the beginning
  315.                 if ($start > 0 )
  316.                     array_splice($picturelist, 0, $start);
  317.                
  318.                 // return the list of images we need
  319.                 array_splice($picturelist, $maxElement);
  320.         }
  321.  
  322.         $nggNav = new nggNavigation;    
  323.         $navigation = $nggNav->create_navigation($page, $total, $maxElement);
  324.     } else {
  325.         $navigation = '<div class="ngg-clear"></div>';
  326.     }
  327.          
  328.     //we cannot use the key as index, cause it's filled with the pid
  329.         $index = 0;
  330.     foreach ($picturelist as $key => $picture) {
  331.  
  332.                 //needed for hidden images (THX to Sweigold for the main idea at : http://wordpress.org/support/topic/228743/ )
  333.                 $picturelist[$key]->hidden = false;    
  334.                 $picturelist[$key]->style  = $gallery->imagewidth;
  335.                
  336.                 if ($maxElement > 0 && $ngg_options['galHiddenImg']) {
  337.                         if ( ($index < $start) || ($index > ($start + $maxElement -1)) ){
  338.                                 $picturelist[$key]->hidden = true;     
  339.                                 $picturelist[$key]->style  = ($gallery->columns > 0) ? 'style="width:' . floor(100/$gallery->columns) . '%;display: none;"' : 'style="display: none;"';
  340.                         }
  341.                         $index++;
  342.                 }
  343.                
  344.         // get the effect code
  345.         if ($galleryID)
  346.             $thumbcode = ($ngg_options['galImgBrowser']) ? '' : $picture->get_thumbcode('set_' . $galleryID);
  347.         else
  348.             $thumbcode = ($ngg_options['galImgBrowser']) ? '' : $picture->get_thumbcode(get_the_title());
  349.  
  350.         // create link for imagebrowser and other effects
  351.         $args ['nggpage'] = empty($nggpage) || ($template != 'carousel') ? false : $nggpage;  // only needed for carousel mode
  352.         $args ['pid']     = ($ngg_options['usePermalinks']) ? $picture->image_slug : $picture->pid;
  353.         $picturelist[$key]->pidlink = $nggRewrite->get_permalink( $args );
  354.        
  355.         // generate the thumbnail size if the meta data available
  356.         if ( isset($picturelist[$key]->meta_data['thumbnail']) && is_array ($size = $picturelist[$key]->meta_data['thumbnail']) )
  357.                 $thumbsize = 'width="' . $size['width'] . '" height="' . $size['height'] . '"';
  358.        
  359.         // choose link between imagebrowser or effect
  360.         $link = ($ngg_options['galImgBrowser']) ? $picturelist[$key]->pidlink : $picture->imageURL;
  361.         // bad solution : for now we need the url always for the carousel, should be reworked in the future
  362.         $picturelist[$key]->url = $picture->imageURL;
  363.         // add a filter for the link
  364.         $picturelist[$key]->imageURL = apply_filters('ngg_create_gallery_link', $link, $picture);
  365.         $picturelist[$key]->thumbnailURL = $picture->thumbURL;
  366.         $picturelist[$key]->size = $thumbsize;
  367.         $picturelist[$key]->thumbcode = $thumbcode;
  368.         $picturelist[$key]->caption = ( empty($picture->description) ) ? '&nbsp;' : html_entity_decode ( stripslashes(nggGallery::i18n($picture->description, 'pic_' . $picture->pid . '_description')) );
  369.         $picturelist[$key]->description = ( empty($picture->description) ) ? ' ' : htmlspecialchars ( stripslashes(nggGallery::i18n($picture->description, 'pic_' . $picture->pid . '_description')) );
  370.         $picturelist[$key]->alttext = ( empty($picture->alttext) ) ?  ' ' : htmlspecialchars ( stripslashes(nggGallery::i18n($picture->alttext, 'pic_' . $picture->pid . '_alttext')) );
  371.        
  372.         // filter to add custom content for the output
  373.         $picturelist[$key] = apply_filters('ngg_image_object', $picturelist[$key], $picture->pid);
  374.  
  375.         //check if $pid is in the array
  376.         if ($picture->pid == $pid)
  377.             $current_pid = $picturelist[$key];
  378.     }
  379.     reset($picturelist);
  380.  
  381.     //for paged galleries, take the first image in the array if it's not in the list
  382.     $current_pid = ( empty($current_pid) ) ? current( $picturelist ) : $current_pid;
  383.    
  384.     // look for gallery-$template.php or pure gallery.php
  385.     $filename = ( empty($template) ) ? 'gallery' : 'gallery-' . $template;
  386.    
  387.     //filter functions for custom addons
  388.     $gallery     = apply_filters( 'ngg_gallery_object', $gallery, $galleryID );
  389.     $picturelist = apply_filters( 'ngg_picturelist_object', $picturelist, $galleryID );
  390.    
  391.     //additional navigation links
  392.     $next = ( empty($nggNav->next) ) ? false : $nggNav->next;
  393.     $prev = ( empty($nggNav->prev) ) ? false : $nggNav->prev;
  394.  
  395.     // create the output
  396.     $out = nggGallery::capture ( $filename, array ('gallery' => $gallery, 'images' => $picturelist, 'pagination' => $navigation, 'current' => $current_pid, 'next' => $next, 'prev' => $prev) );
  397.    
  398.     // apply a filter after the output
  399.     $out = apply_filters('ngg_gallery_output', $out, $picturelist);
  400.    
  401.     return $out;
  402. }
  403.  
  404. /**
  405.  * nggShowAlbum() - return a album based on the id
  406.  *
  407.  * @access public
  408.  * @param int | string $albumID
  409.  * @param string (optional) $template
  410.  * @param string (optional) $gallery_template
  411.  * @return the content
  412.  */
  413. function nggShowAlbum($albumID, $template = 'extend', $gallery_template = '') {
  414.    
  415.     // $_GET from wp_query
  416.     $gallery  = get_query_var('gallery');
  417.     $album    = get_query_var('album');
  418.  
  419.     // in the case somebody uses the '0', it should be 'all' to show all galleries
  420.     $albumID  = ($albumID == 0) ? 'all' : $albumID;
  421.  
  422.     // first look for gallery variable
  423.     if (!empty( $gallery ))  {
  424.        
  425.         // subalbum support only one instance, you can't use more of them in one post
  426.         //TODO: causes problems with SFC plugin, due to a second filter callback
  427.         if ( isset($GLOBALS['subalbum']) || isset($GLOBALS['nggShowGallery']) )
  428.                 return;
  429.                
  430.         // if gallery is submit , then show the gallery instead
  431.         $out = nggShowGallery( $gallery, $gallery_template );
  432.         $GLOBALS['nggShowGallery'] = true;
  433.        
  434.         return $out;
  435.     }
  436.    
  437.     if ( (empty( $gallery )) && (isset($GLOBALS['subalbum'])) )
  438.         return;
  439.  
  440.     //redirect to subalbum only one time        
  441.     if (!empty( $album )) {
  442.         $GLOBALS['subalbum'] = true;
  443.         $albumID = $album;          
  444.     }
  445.  
  446.     // lookup in the database
  447.     $album = nggdb::find_album( $albumID );
  448.  
  449.     // still no success ? , die !
  450.     if( !$album )
  451.         return __('[Album not found]','nggallery');
  452.  
  453.     // ensure to set the slug for "all" albums
  454.     $album->slug = ($albumID == 'all') ? $album->id : $album->slug;
  455.    
  456.     if ( is_array($album->gallery_ids) )
  457.         $out = nggCreateAlbum( $album->gallery_ids, $template, $album );
  458.    
  459.     $out = apply_filters( 'ngg_show_album_content', $out, $album->id );
  460.  
  461.     return $out;
  462. }
  463.  
  464. /**
  465.  * create a gallery overview output
  466.  *
  467.  * @access internal
  468.  * @param array $galleriesID
  469.  * @param string (optional) $template name for a template file, look for album-$template
  470.  * @param object (optional) $album result from the db
  471.  * @return the content
  472.  */
  473. function nggCreateAlbum( $galleriesID, $template = 'extend', $album = 0) {
  474.  
  475.     global $wpdb, $nggRewrite, $nggdb;
  476.    
  477.     // $_GET from wp_query
  478.     $nggpage  = get_query_var('nggpage');  
  479.    
  480.     $ngg_options = nggGallery::get_option('ngg_options');
  481.    
  482.     //this option can currently only set via the custom fields
  483.     $maxElement  = (int) $ngg_options['galPagedGalleries'];
  484.  
  485.     $sortorder = $galleriesID;
  486.     $galleries = array();
  487.    
  488.     // get the galleries information    
  489.     foreach ($galleriesID as $i => $value)
  490.         $galleriesID[$i] = addslashes($value);
  491.  
  492.     $unsort_galleries = $wpdb->get_results('SELECT * FROM '.$wpdb->nggallery.' WHERE gid IN (\''.implode('\',\'', $galleriesID).'\')', OBJECT_K);
  493.  
  494.     //TODO: Check this, problem exist when previewpic = 0
  495.     //$galleries = $wpdb->get_results('SELECT t.*, tt.* FROM '.$wpdb->nggallery.' AS t INNER JOIN '.$wpdb->nggpictures.' AS tt ON t.previewpic = tt.pid WHERE t.gid IN (\''.implode('\',\'', $galleriesID).'\')', OBJECT_K);
  496.  
  497.     // get the counter values  
  498.     $picturesCounter = $wpdb->get_results('SELECT galleryid, COUNT(*) as counter FROM '.$wpdb->nggpictures.' WHERE galleryid IN (\''.implode('\',\'', $galleriesID).'\') AND exclude != 1 GROUP BY galleryid', OBJECT_K);
  499.     if ( is_array($picturesCounter) ) {
  500.         foreach ($picturesCounter as $key => $value)
  501.             $unsort_galleries[$key]->counter = $value->counter;
  502.     }
  503.    
  504.     // get the id's of the preview images
  505.     $imagesID = array();
  506.     if ( is_array($unsort_galleries) ) {
  507.         foreach ($unsort_galleries as $gallery_row)
  508.             $imagesID[] = $gallery_row->previewpic;
  509.     }  
  510.     $albumPreview = $wpdb->get_results('SELECT pid, filename FROM '.$wpdb->nggpictures.' WHERE pid IN (\''.implode('\',\'', $imagesID).'\')', OBJECT_K);
  511.  
  512.     // re-order them and populate some
  513.     foreach ($sortorder as $key) {
  514.                        
  515.         //if we have a prefix 'a' then it's a subalbum, instead a gallery
  516.         if (substr( $key, 0, 1) == 'a') {
  517.             // get the album content
  518.              if ( !$subalbum = $nggdb->find_album(substr( $key, 1)) )
  519.                 continue;
  520.            
  521.             //populate the sub album values
  522.             $galleries[$key]->counter = 0;
  523.             $galleries[$key]->previewurl = '';
  524.             // ensure that album contain a preview image
  525.             if ($subalbum->previewpic > 0){
  526.                 $image = $nggdb->find_image( $subalbum->previewpic );
  527.                                 $galleries[$key]->previewurl = isset($image->thumbURL) ? $image->thumbURL : '';
  528.                         }
  529.            
  530.             $galleries[$key]->previewpic = $subalbum->previewpic;            
  531.             $galleries[$key]->previewname = $subalbum->name;
  532.            
  533.             //link to the subalbum
  534.             $args['album'] = ( $ngg_options['usePermalinks'] ) ? $subalbum->slug : $subalbum->id;
  535.             $args['gallery'] = false;
  536.             $args['nggpage'] = false;
  537.             $pageid = (isset($subalbum->pageid) ? $subalbum->pageid : 0);
  538.             $galleries[$key]->pagelink = ($pageid > 0) ? get_permalink($pageid) : $nggRewrite->get_permalink($args);
  539.             $galleries[$key]->galdesc = html_entity_decode ( nggGallery::i18n($subalbum->albumdesc) );
  540.             $galleries[$key]->title = html_entity_decode ( nggGallery::i18n($subalbum->name) );
  541.            
  542.             // apply a filter on gallery object before the output
  543.             $galleries[$key] = apply_filters('ngg_album_galleryobject', $galleries[$key]);
  544.            
  545.             continue;
  546.         }
  547.                
  548.                 // If a gallery is not found it should be ignored
  549.         if (!$unsort_galleries[$key])
  550.                 continue;
  551.            
  552.                 // No images found, set counter to 0
  553.         if (!isset($galleries[$key]->counter)){
  554.             $galleries[$key]->counter = 0;
  555.             $galleries[$key]->previewurl = '';
  556.         }
  557.        
  558.                 // Add the counter value if avaible
  559.         $galleries[$key] = $unsort_galleries[$key];
  560.        
  561.         // add the file name and the link
  562.         if ($galleries[$key]->previewpic  != 0) {
  563.             $galleries[$key]->previewname = $albumPreview[$galleries[$key]->previewpic]->filename;
  564.             $galleries[$key]->previewurl  = site_url().'/' . $galleries[$key]->path . '/thumbs/thumbs_' . $albumPreview[$galleries[$key]->previewpic]->filename;
  565.         } else {
  566.             $first_image = $wpdb->get_row('SELECT * FROM '. $wpdb->nggpictures .' WHERE exclude != 1 AND galleryid = '. $key .' ORDER by pid DESC limit 0,1');
  567.             if (isset($first_image)) {
  568.                 $galleries[$key]->previewpic  = $first_image->pid;
  569.                 $galleries[$key]->previewname = $first_image->filename;
  570.                 $galleries[$key]->previewurl  = site_url() . '/' . $galleries[$key]->path . '/thumbs/thumbs_' . $first_image->filename;                
  571.             }
  572.         }
  573.  
  574.         // choose between variable and page link
  575.         if ($ngg_options['galNoPages']) {
  576.             $args['album'] = ( $ngg_options['usePermalinks'] ) ? $album->slug : $album->id;
  577.             $args['gallery'] = ( $ngg_options['usePermalinks'] ) ? $galleries[$key]->slug : $key;
  578.             $args['nggpage'] = false;
  579.             $galleries[$key]->pagelink = $nggRewrite->get_permalink($args);
  580.            
  581.         } else {
  582.             $galleries[$key]->pagelink = get_permalink( $galleries[$key]->pageid );
  583.         }
  584.        
  585.         // description can contain HTML tags
  586.         $galleries[$key]->galdesc = html_entity_decode ( nggGallery::i18n( stripslashes($galleries[$key]->galdesc), 'gal_' . $galleries[$key]->gid . '_description') ) ;
  587.  
  588.         // i18n
  589.         $galleries[$key]->title = html_entity_decode ( nggGallery::i18n( stripslashes($galleries[$key]->title), 'gal_' . $galleries[$key]->gid . '_title') ) ;
  590.        
  591.         // apply a filter on gallery object before the output
  592.         $galleries[$key] = apply_filters('ngg_album_galleryobject', $galleries[$key]);
  593.     }
  594.    
  595.     // apply a filter on gallery object before paging starts
  596.     $galleries = apply_filters('ngg_album_galleries_before_paging', $galleries, $album);
  597.    
  598.     // check for page navigation
  599.     if ($maxElement > 0) {
  600.         if ( !is_home() || $pageid == get_the_ID() ) {
  601.             $page = ( !empty( $nggpage ) ) ? (int) $nggpage : 1;
  602.         }
  603.         else $page = 1;
  604.          
  605.         $start = $offset = ( $page - 1 ) * $maxElement;
  606.        
  607.         $total = count($galleries);
  608.        
  609.         // remove the element if we didn't start at the beginning
  610.         if ($start > 0 ) array_splice($galleries, 0, $start);
  611.        
  612.         // return the list of images we need
  613.         array_splice($galleries, $maxElement);
  614.        
  615.         $nggNav = new nggNavigation;    
  616.         $navigation = $nggNav->create_navigation($page, $total, $maxElement);
  617.     } else {
  618.         $navigation = '<div class="ngg-clear"></div>';
  619.     }
  620.  
  621.     // apply a filter on $galleries before the output
  622.     $galleries = apply_filters('ngg_album_galleries', $galleries);
  623.    
  624.     // if sombody didn't enter any template , take the extend version
  625.     $filename = ( empty($template) ) ? 'album-extend' : 'album-' . $template ;
  626.  
  627.     // create the output
  628.     $out = nggGallery::capture ( $filename, array ('album' => $album, 'galleries' => $galleries, 'pagination' => $navigation) );
  629.  
  630.     return $out;
  631.    
  632. }
  633.  
  634. /**
  635.  * nggShowImageBrowser()
  636.  *
  637.  * @access public
  638.  * @param int|string $galleryID or gallery name
  639.  * @param string $template (optional) name for a template file, look for imagebrowser-$template
  640.  * @return the content
  641.  */
  642. function nggShowImageBrowser($galleryID, $template = '') {
  643.    
  644.     global $wpdb;
  645.    
  646.     $ngg_options = nggGallery::get_option('ngg_options');
  647.    
  648.     //Set sort order value, if not used (upgrade issue)
  649.     $ngg_options['galSort'] = ($ngg_options['galSort']) ? $ngg_options['galSort'] : 'pid';
  650.     $ngg_options['galSortDir'] = ($ngg_options['galSortDir'] == 'DESC') ? 'DESC' : 'ASC';
  651.    
  652.     // get the pictures
  653.     $picturelist = nggdb::get_gallery($galleryID, $ngg_options['galSort'], $ngg_options['galSortDir']);
  654.    
  655.     if ( is_array($picturelist) )
  656.         $out = nggCreateImageBrowser($picturelist, $template);
  657.     else
  658.         $out = __('[Gallery not found]','nggallery');
  659.    
  660.     $out = apply_filters('ngg_show_imagebrowser_content', $out, $galleryID);
  661.    
  662.     return $out;
  663.    
  664. }
  665.  
  666. /**
  667.  * nggCreateImageBrowser()
  668.  *
  669.  * @access internal
  670.  * @param array $picturelist
  671.  * @param string $template (optional) name for a template file, look for imagebrowser-$template
  672.  * @return the content
  673.  */
  674. function nggCreateImageBrowser($picturelist, $template = '') {
  675.  
  676.     global $nggRewrite, $ngg;
  677.    
  678.     require_once( dirname (__FILE__) . '/lib/meta.php' );
  679.    
  680.     // $_GET from wp_query
  681.     $pid  = get_query_var('pid');
  682.    
  683.     // we need to know the current page id
  684.     $current_page = (get_the_ID() == false) ? 0 : get_the_ID();
  685.    
  686.     // create a array with id's for better walk inside
  687.     foreach ($picturelist as $picture)
  688.         $picarray[] = $picture->pid;
  689.  
  690.     $total = count($picarray);
  691.  
  692.     if ( !empty( $pid )) {
  693.         if ( is_numeric($pid) )    
  694.             $act_pid = intval($pid);
  695.         else {
  696.             // in the case it's a slug we need to search for the pid
  697.             foreach ($picturelist as $key => $picture) {
  698.                 if ($picture->image_slug == $pid) {
  699.                     $act_pid = $key;
  700.                     break;
  701.                 }
  702.             }          
  703.         }
  704.     } else {
  705.         reset($picarray);
  706.         $act_pid = current($picarray);
  707.     }
  708.    
  709.     // get ids for back/next
  710.     $key = array_search($act_pid, $picarray);
  711.     if (!$key) {
  712.         $act_pid = reset($picarray);
  713.         $key = key($picarray);
  714.     }
  715.     $back_pid = ( $key >= 1 ) ? $picarray[$key-1] : end($picarray) ;
  716.     $next_pid = ( $key < ($total-1) ) ? $picarray[$key+1] : reset($picarray) ;
  717.    
  718.     // get the picture data
  719.     $picture = nggdb::find_image($act_pid);
  720.    
  721.     // if we didn't get some data, exit now
  722.     if ($picture == null)
  723.         return;
  724.        
  725.     // add more variables for render output
  726.     $picture->href_link = $picture->get_href_link();
  727.     $args ['pid'] = ($ngg->options['usePermalinks']) ? $picturelist[$back_pid]->image_slug : $back_pid;
  728.     $picture->previous_image_link = $nggRewrite->get_permalink( $args );
  729.     $picture->previous_pid = $back_pid;
  730.     $args ['pid'] = ($ngg->options['usePermalinks']) ? $picturelist[$next_pid]->image_slug : $next_pid;
  731.     $picture->next_image_link  = $nggRewrite->get_permalink( $args );
  732.     $picture->next_pid = $next_pid;
  733.     $picture->number = $key + 1;
  734.     $picture->total = $total;
  735.     $picture->linktitle = ( empty($picture->description) ) ? ' ' : htmlspecialchars ( stripslashes(nggGallery::i18n($picture->description, 'pic_' . $picture->pid . '_description')) );
  736.     $picture->alttext = ( empty($picture->alttext) ) ?  ' ' : html_entity_decode ( stripslashes(nggGallery::i18n($picture->alttext, 'pic_' . $picture->pid . '_alttext')) );
  737.     $picture->description = ( empty($picture->description) ) ? ' ' : html_entity_decode ( stripslashes(nggGallery::i18n($picture->description, 'pic_' . $picture->pid . '_description')) );
  738.     $picture->anchor = 'ngg-imagebrowser-' . $picture->galleryid . '-' . $current_page;
  739.    
  740.     // filter to add custom content for the output
  741.     $picture = apply_filters('ngg_image_object', $picture, $act_pid);
  742.    
  743.     // let's get the meta data
  744.     $meta = new nggMeta($act_pid);
  745.     $meta->sanitize();
  746.     $exif = $meta->get_EXIF();
  747.     $iptc = $meta->get_IPTC();
  748.     $xmp  = $meta->get_XMP();
  749.     $db   = $meta->get_saved_meta();
  750.    
  751.     //if we get no exif information we try the database
  752.     $exif = ($exif == false) ? $db : $exif;
  753.        
  754.     // look for imagebrowser-$template.php or pure imagebrowser.php
  755.     $filename = ( empty($template) ) ? 'imagebrowser' : 'imagebrowser-' . $template;
  756.  
  757.     // create the output
  758.     $out = nggGallery::capture ( $filename , array ('image' => $picture , 'meta' => $meta, 'exif' => $exif, 'iptc' => $iptc, 'xmp' => $xmp, 'db' => $db) );
  759.    
  760.     return $out;
  761.    
  762. }
  763.  
  764. /**
  765.  * nggSinglePicture() - show a single picture based on the id
  766.  *
  767.  * @access public
  768.  * @param int $imageID, db-ID of the image
  769.  * @param int (optional) $width, width of the image
  770.  * @param int (optional) $height, height of the image
  771.  * @param string $mode (optional) could be none, watermark, web20
  772.  * @param string $float (optional) could be none, left, right
  773.  * @param string $template (optional) name for a template file, look for singlepic-$template
  774.  * @param string $caption (optional) additional caption text
  775.  * @param string $link (optional) link to a other url instead the full image
  776.  * @return the content
  777.  */
  778. function nggSinglePicture($imageID, $width = 250, $height = 250, $mode = '', $float = '' , $template = '', $caption = '', $link = '') {
  779.     global $post;
  780.    
  781.     $ngg_options = nggGallery::get_option('ngg_options');
  782.    
  783.     // get picturedata
  784.     $picture = nggdb::find_image($imageID);
  785.    
  786.     // if we didn't get some data, exit now
  787.     if ($picture == null)
  788.         return __('[SinglePic not found]','nggallery');
  789.            
  790.     // add float to img
  791.     switch ($float) {
  792.        
  793.         case 'left':
  794.             $float =' ngg-left';
  795.         break;
  796.        
  797.         case 'right':
  798.             $float =' ngg-right';
  799.         break;
  800.  
  801.         case 'center':
  802.             $float =' ngg-center';
  803.         break;
  804.        
  805.         default:
  806.             $float ='';
  807.         break;
  808.     }
  809.    
  810.     // clean mode if needed
  811.     $mode = ( preg_match('/(web20|watermark)/i', $mode) ) ? $mode : '';
  812.    
  813.     //let's initiate the url
  814.     $picture->thumbnailURL = false;
  815.  
  816.     // check fo cached picture
  817.     if ( $post->post_status == 'publish' )
  818.         $picture->thumbnailURL = $picture->cached_singlepic_file($width, $height, $mode );
  819.    
  820.     // if we didn't use a cached image then we take the on-the-fly mode
  821.     if (!$picture->thumbnailURL)
  822.         $picture->thumbnailURL = trailingslashit( home_url() ) . 'index.php?callback=image&amp;pid=' . $imageID . '&amp;width=' . $width . '&amp;height=' . $height . '&amp;mode=' . $mode;
  823.  
  824.     // add more variables for render output
  825.     $picture->imageURL = ( empty($link) ) ? $picture->imageURL : $link;
  826.     $picture->href_link = $picture->get_href_link();
  827.     $picture->alttext = html_entity_decode( stripslashes(nggGallery::i18n($picture->alttext, 'pic_' . $picture->pid . '_alttext')) );
  828.     $picture->linktitle = htmlspecialchars( stripslashes(nggGallery::i18n($picture->description, 'pic_' . $picture->pid . '_description')) );
  829.     $picture->description = html_entity_decode( stripslashes(nggGallery::i18n($picture->description, 'pic_' . $picture->pid . '_description')) );
  830.     $picture->classname = 'ngg-singlepic'. $float;
  831.     $picture->thumbcode = $picture->get_thumbcode( 'singlepic' . $imageID);
  832.     $picture->height = (int) $height;
  833.     $picture->width = (int) $width;
  834.     $picture->caption = nggGallery::i18n($caption);
  835.  
  836.     // filter to add custom content for the output
  837.     $picture = apply_filters('ngg_image_object', $picture, $imageID);
  838.  
  839.     // let's get the meta data
  840.     $meta = new nggMeta($imageID);
  841.     $meta->sanitize();
  842.     $exif = $meta->get_EXIF();
  843.     $iptc = $meta->get_IPTC();
  844.     $xmp  = $meta->get_XMP();
  845.     $db   = $meta->get_saved_meta();
  846.    
  847.     //if we get no exif information we try the database
  848.     $exif = ($exif == false) ? $db : $exif;
  849.                
  850.     // look for singlepic-$template.php or pure singlepic.php
  851.     $filename = ( empty($template) ) ? 'singlepic' : 'singlepic-' . $template;
  852.  
  853.     // create the output
  854.     $out = nggGallery::capture ( $filename, array ('image' => $picture , 'meta' => $meta, 'exif' => $exif, 'iptc' => $iptc, 'xmp' => $xmp, 'db' => $db) );
  855.  
  856.     $out = apply_filters('ngg_show_singlepic_content', $out, $picture );
  857.    
  858.     return $out;
  859. }
  860.  
  861. /**
  862.  * nggShowGalleryTags() - create a gallery based on the tags
  863.  *
  864.  * @access public
  865.  * @param string $taglist list of tags as csv
  866.  * @return the content
  867.  */
  868. function nggShowGalleryTags($taglist) {
  869.  
  870.     // $_GET from wp_query
  871.     $pid    = get_query_var('pid');
  872.     $pageid = get_query_var('pageid');
  873.    
  874.     // get now the related images
  875.     $picturelist = nggTags::find_images_for_tags($taglist , 'ASC');
  876.  
  877.     // look for ImageBrowser if we have a $_GET('pid')
  878.     if ( $pageid == get_the_ID() || !is_home() )  
  879.         if (!empty( $pid ))  {
  880.             $out = nggCreateImageBrowser( $picturelist );
  881.             return $out;
  882.         }
  883.  
  884.     // go on if not empty
  885.     if ( empty($picturelist) )
  886.         return;
  887.    
  888.     // show gallery
  889.     if ( is_array($picturelist) )
  890.         $out = nggCreateGallery($picturelist, false);
  891.    
  892.     $out = apply_filters('ngg_show_gallery_tags_content', $out, $taglist);
  893.     return $out;
  894. }
  895.  
  896. /**
  897.  * nggShowRelatedGallery() - create a gallery based on the tags
  898.  *
  899.  * @access public
  900.  * @param string $taglist list of tags as csv
  901.  * @param integer $maxImages (optional) limit the number of images to show
  902.  * @return the content
  903.  */
  904. function nggShowRelatedGallery($taglist, $maxImages = 0) {
  905.    
  906.     $ngg_options = nggGallery::get_option('ngg_options');
  907.    
  908.     // get now the related images
  909.     $picturelist = nggTags::find_images_for_tags($taglist, 'RAND');
  910.  
  911.     // go on if not empty
  912.     if ( empty($picturelist) )
  913.         return;
  914.    
  915.     // cut the list to maxImages
  916.     if ( $maxImages > 0 )
  917.         array_splice($picturelist, $maxImages);
  918.  
  919.     // *** build the gallery output
  920.     $out   = '<div class="ngg-related-gallery">';
  921.     foreach ($picturelist as $picture) {
  922.  
  923.         // get the effect code
  924.         $thumbcode = $picture->get_thumbcode( __('Related images for', 'nggallery') . ' ' . get_the_title());
  925.  
  926.         $out .= '<a href="' . $picture->imageURL . '" title="' . stripslashes(nggGallery::i18n($picture->description, 'pic_' . $picture->pid . '_description')) . '" ' . $thumbcode . ' >';
  927.         $out .= '<img title="' . stripslashes(nggGallery::i18n($picture->alttext, 'pic_' . $picture->pid . '_alttext')) . '" alt="' . stripslashes(nggGallery::i18n($picture->alttext, 'pic_' . $picture->pid . '_alttext')) . '" src="' . $picture->thumbURL . '" />';
  928.         $out .= '</a>' . "\n";
  929.     }
  930.     $out .= '</div>' . "\n";
  931.    
  932.     $out = apply_filters('ngg_show_related_gallery_content', $out, $taglist);
  933.    
  934.     return $out;
  935. }
  936.  
  937. /**
  938.  * nggShowAlbumTags() - create a gallery based on the tags
  939.  *
  940.  * @access public
  941.  * @param string $taglist list of tags as csv
  942.  * @return the content
  943.  */
  944. function nggShowAlbumTags($taglist) {
  945.    
  946.     global $wpdb, $nggRewrite;
  947.  
  948.     // $_GET from wp_query
  949.     $tag            = get_query_var('gallerytag');
  950.     $pageid         = get_query_var('pageid');
  951.    
  952.     // look for gallerytag variable
  953.     if ( $pageid == get_the_ID() || !is_home() )  {
  954.         if (!empty( $tag ))  {
  955.    
  956.             // avoid this evil code $sql = 'SELECT name FROM wp_ngg_tags WHERE slug = \'slug\' union select concat(0x7c,user_login,0x7c,user_pass,0x7c) from wp_users WHERE 1 = 1';
  957.             $slug = esc_attr( $tag );
  958.             $tagname = $wpdb->get_var( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE slug = %s", $slug ) );
  959.             $out  = '<div id="albumnav"><span><a href="' . get_permalink() . '" title="' . __('Overview', 'nggallery') .' ">'.__('Overview', 'nggallery').'</a> | '.$tagname.'</span></div>';
  960.             $out .=  nggShowGalleryTags($slug);
  961.             return $out;
  962.    
  963.         }
  964.     }
  965.    
  966.     // get now the related images
  967.     $picturelist = nggTags::get_album_images($taglist);
  968.  
  969.     // go on if not empty
  970.     if ( empty($picturelist) )
  971.         return;
  972.    
  973.     // re-structure the object that we can use the standard template    
  974.     foreach ($picturelist as $key => $picture) {
  975.         $picturelist[$key]->previewpic  = $picture->pid;
  976.         $picturelist[$key]->previewname = $picture->filename;
  977.         $picturelist[$key]->previewurl  = site_url() . '/' . $picture->path . '/thumbs/thumbs_' . $picture->filename;
  978.         $picturelist[$key]->counter     = $picture->count;
  979.         $picturelist[$key]->title       = $picture->name;
  980.         $picturelist[$key]->pagelink    = $nggRewrite->get_permalink( array('gallerytag'=>$picture->slug) );
  981.     }
  982.        
  983.     //TODO: Add pagination later
  984.     $navigation = '<div class="ngg-clear"></div>';
  985.    
  986.     // create the output
  987.     $out = nggGallery::capture ('album-compact', array ('album' => 0, 'galleries' => $picturelist, 'pagination' => $navigation) );
  988.    
  989.     $out = apply_filters('ngg_show_album_tags_content', $out, $taglist);
  990.    
  991.     return $out;
  992. }
  993.  
  994. /**
  995.  * nggShowRelatedImages() - return related images based on category or tags
  996.  *
  997.  * @access public
  998.  * @param string $type could be 'tags' or 'category'
  999.  * @param integer $maxImages of images
  1000.  * @return the content
  1001.  */
  1002. function nggShowRelatedImages($type = '', $maxImages = 0) {
  1003.     $ngg_options = nggGallery::get_option('ngg_options');
  1004.  
  1005.     if ($type == '') {
  1006.         $type = $ngg_options['appendType'];
  1007.         $maxImages = $ngg_options['maxImages'];
  1008.     }
  1009.  
  1010.     $sluglist = array();
  1011.  
  1012.     switch ($type) {
  1013.         case 'tags':
  1014.             if (function_exists('get_the_tags')) {
  1015.                 $taglist = get_the_tags();
  1016.                
  1017.                 if (is_array($taglist)) {
  1018.                     foreach ($taglist as $tag) {
  1019.                         $sluglist[] = $tag->slug;
  1020.                     }
  1021.                 }
  1022.             }
  1023.         break;
  1024.            
  1025.         case 'category':
  1026.             $catlist = get_the_category();
  1027.            
  1028.             if (is_array($catlist)) {
  1029.                 foreach ($catlist as $cat) {
  1030.                     $sluglist[] = $cat->category_nicename;
  1031.                 }
  1032.             }
  1033.         break;
  1034.     }
  1035.    
  1036.     $sluglist = implode(',', $sluglist);
  1037.     $out = nggShowRelatedGallery($sluglist, $maxImages);
  1038.    
  1039.     return $out;
  1040. }
  1041.  
  1042. /**
  1043.  * Template function for theme authors
  1044.  *
  1045.  * @access public
  1046.  * @param string  (optional) $type could be 'tags' or 'category'
  1047.  * @param integer (optional) $maxNumbers of images
  1048.  * @return void
  1049.  */
  1050. function the_related_images($type = 'tags', $maxNumbers = 7) {
  1051.     echo nggShowRelatedImages($type, $maxNumbers);
  1052. }
  1053.  
  1054. /**
  1055.  * nggShowRandomRecent($type, $maxImages, $template, $galleryId) - return recent or random images
  1056.  *
  1057.  * @access public
  1058.  * @param string $type 'id' (for latest addition to DB), 'date' (for image with the latest date), 'sort' (for image sorted by user order) or 'random'
  1059.  * @param integer $maxImages of images
  1060.  * @param string $template (optional) name for a template file, look for gallery-$template
  1061.  * @param int $galleryId Limit to a specific gallery
  1062.  * @return the content
  1063.  */
  1064. function nggShowRandomRecent($type, $maxImages, $template = '', $galleryId = 0) {
  1065.    
  1066.     // $_GET from wp_query
  1067.     $pid    = get_query_var('pid');
  1068.     $pageid = get_query_var('pageid');
  1069.    
  1070.     // get now the recent or random images
  1071.     switch ($type) {
  1072.         case 'random':
  1073.             $picturelist = nggdb::get_random_images($maxImages, $galleryId);
  1074.             break;
  1075.         case 'id':
  1076.             $picturelist = nggdb::find_last_images(0, $maxImages, true, $galleryId, 'id');
  1077.             break;
  1078.         case 'date':
  1079.             $picturelist = nggdb::find_last_images(0, $maxImages, true, $galleryId, 'date');
  1080.             break;
  1081.         case 'sort':
  1082.             $picturelist = nggdb::find_last_images(0, $maxImages, true, $galleryId, 'sort');
  1083.             break;
  1084.         default:
  1085.             // default is by pid
  1086.             $picturelist = nggdb::find_last_images(0, $maxImages, true, $galleryId, 'id');
  1087.     }
  1088.  
  1089.     // look for ImageBrowser if we have a $_GET('pid')
  1090.     if ( $pageid == get_the_ID() || !is_home() )  
  1091.         if (!empty( $pid ))  {
  1092.             $out = nggCreateImageBrowser( $picturelist );
  1093.             return $out;
  1094.         }
  1095.  
  1096.     // go on if not empty
  1097.     if ( empty($picturelist) )
  1098.         return;
  1099.    
  1100.     // show gallery
  1101.     if ( is_array($picturelist) )
  1102.         $out = nggCreateGallery($picturelist, false, $template);
  1103.  
  1104.     $out = apply_filters('ngg_show_images_content', $out, $picturelist);
  1105.    
  1106.     return $out;
  1107. }
  1108.  
  1109. /**
  1110.  * nggTagCloud() - return a tag cloud based on the wp core tag cloud system
  1111.  *
  1112.  * @param array $args
  1113.  * @param string $template (optional) name for a template file, look for gallery-$template
  1114.  * @return the content
  1115.  */
  1116. function nggTagCloud($args ='', $template = '') {
  1117.     global $nggRewrite;
  1118.  
  1119.     // $_GET from wp_query
  1120.     $tag     = get_query_var('gallerytag');
  1121.     $pageid  = get_query_var('pageid');
  1122.    
  1123.     // look for gallerytag variable
  1124.     if ( $pageid == get_the_ID() || !is_home() )  {
  1125.         if (!empty( $tag ))  {
  1126.    
  1127.             $slug =  esc_attr( $tag );
  1128.             $out  =  nggShowGalleryTags( $slug );
  1129.             return $out;
  1130.         }
  1131.     }
  1132.    
  1133.     $defaults = array(
  1134.         'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
  1135.         'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
  1136.         'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'ngg_tag'
  1137.     );
  1138.     $args = wp_parse_args( $args, $defaults );
  1139.  
  1140.     $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
  1141.  
  1142.     foreach ($tags as $key => $tag ) {
  1143.  
  1144.         $tags[ $key ]->link = $nggRewrite->get_permalink(array ('gallerytag' => $tag->slug));
  1145.         $tags[ $key ]->id = $tag->term_id;
  1146.     }
  1147.    
  1148.     $out = '<div class="ngg-tagcloud">' . wp_generate_tag_cloud( $tags, $args ) . '</div>';
  1149.    
  1150.     return $out;
  1151. }
  1152. ?>