plugins/gallery/sidebar.php
changeset 5 c0c324f0eeb5
child 13 f6ca7cead82c
equal deleted inserted replaced
4:ad3b061a1c76 5:c0c324f0eeb5
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Snapr
       
     5  * Version 0.1 beta 1
       
     6  * Copyright (C) 2007 Dan Fuhry
       
     7  *
       
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    12  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    13  */
       
    14  
       
    15 //
       
    16 // "Random Image" sidebar block
       
    17 //
       
    18 
       
    19 function gal_sidebar_block()
       
    20 {
       
    21   global $db, $session, $paths, $template, $plugins; // Common objects
       
    22   
       
    23   $q = $db->sql_query('SELECT img_id,img_title FROM '.table_prefix.'gallery WHERE is_folder=0;');
       
    24   if ( !$q )
       
    25     $db->_die();
       
    26   
       
    27   $images = array();
       
    28   while ( $row = $db->fetchrow() )
       
    29   {
       
    30     $id = intval($row['img_id']);
       
    31     $images[$id] = $row['img_title'];
       
    32   }
       
    33   
       
    34   // Loop through all gallery images until we find one we can read (typically on the first try, but you never know...)
       
    35   $my_image = false;
       
    36   while ( count($images) > 0 )
       
    37   {
       
    38     $rand = array_rand($images);
       
    39     $image = $images[$rand];
       
    40     $acl = $session->fetch_page_acl(strval($rand), 'Gallery');
       
    41     if ( $acl->get_permissions('read') )
       
    42     {
       
    43       $my_image = $image;
       
    44       break;
       
    45     }
       
    46     unset($images[$rand]);
       
    47   }
       
    48   if ( $my_image )
       
    49   {
       
    50     // Generate sidebar HTML
       
    51     $image_link = '<div style="padding: 5px; text-align: center;">
       
    52                      <a href="' . makeUrlNS('Gallery', $rand) . '">
       
    53                        <img alt="&lt;thumbnail&gt;" src="' . makeUrlNS('Special', 'GalleryFetcher/thumb/' . $rand) . '" style="border-width: 0; display: block; margin: 0 auto 5px auto;" />
       
    54                        <span style="color: black;">' . htmlspecialchars($my_image) . '</span>
       
    55                      </a>
       
    56                    </div>';
       
    57   }
       
    58   else
       
    59   {
       
    60     $image_link = 'No images in the gallery.';
       
    61   }
       
    62   $template->sidebar_widget('Random image', $image_link);
       
    63 }
       
    64 
       
    65 $plugins->attachHook('compile_template', 'gal_sidebar_block();');
       
    66 
       
    67 ?>