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