Advertisement
Guest User

NextGen Functions.php

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