plugins/gallery/sidebar.php
author Dan Fuhry <dan@enanocms.org>
Sat, 21 Aug 2010 23:51:10 -0400
changeset 44 17cfc3da0dcc
parent 42 7c6e2e97aa08
permissions -rw-r--r--
Put a noscript around that
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
{
42
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    21
	global $db, $session, $paths, $template, $plugins; // Common objects
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    22
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    23
	$q = $db->sql_query('SELECT img_id,img_title FROM '.table_prefix.'gallery WHERE is_folder=0;');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    24
	if ( !$q )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    25
		$db->_die();
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    26
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    27
	$images = array();
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    28
	while ( $row = $db->fetchrow() )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    29
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    30
		$id = intval($row['img_id']);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    31
		$images[$id] = $row['img_title'];
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    32
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    33
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    34
	// Loop through all gallery images until we find one we can read (typically on the first try, but you never know...)
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    35
	$my_image = false;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    36
	while ( count($images) > 0 )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    37
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    38
		$rand = array_rand($images);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    39
		$image = $images[$rand];
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    40
		$acl = $session->fetch_page_acl(strval($rand), 'Gallery');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    41
		if ( is_object($acl) && $acl->get_permissions('read') )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    42
		{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    43
			$my_image = $image;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    44
			break;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    45
		}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    46
		unset($images[$rand]);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    47
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    48
	if ( $my_image )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    49
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    50
		// Generate sidebar HTML
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    51
		$image_link = '<div style="padding: 5px; text-align: center;">
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    52
 										<a href="' . makeUrlNS('Gallery', $rand) . '">
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
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;" />
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    54
 											<span style="color: black;">' . htmlspecialchars($my_image) . '</span>
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    55
 										</a>
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    56
 									</div>';
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    57
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    58
	else
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    59
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    60
		$image_link = 'No images in the gallery.';
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    61
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 13
diff changeset
    62
	$template->sidebar_widget('Random image', $image_link);
5
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
?>