plugins/gallery/sidebar.php
changeset 42 7c6e2e97aa08
parent 13 f6ca7cead82c
equal deleted inserted replaced
41:0944c9354e9c 42:7c6e2e97aa08
    16 // "Random Image" sidebar block
    16 // "Random Image" sidebar block
    17 //
    17 //
    18 
    18 
    19 function gal_sidebar_block()
    19 function gal_sidebar_block()
    20 {
    20 {
    21   global $db, $session, $paths, $template, $plugins; // Common objects
    21 	global $db, $session, $paths, $template, $plugins; // Common objects
    22   
    22 	
    23   $q = $db->sql_query('SELECT img_id,img_title FROM '.table_prefix.'gallery WHERE is_folder=0;');
    23 	$q = $db->sql_query('SELECT img_id,img_title FROM '.table_prefix.'gallery WHERE is_folder=0;');
    24   if ( !$q )
    24 	if ( !$q )
    25     $db->_die();
    25 		$db->_die();
    26   
    26 	
    27   $images = array();
    27 	$images = array();
    28   while ( $row = $db->fetchrow() )
    28 	while ( $row = $db->fetchrow() )
    29   {
    29 	{
    30     $id = intval($row['img_id']);
    30 		$id = intval($row['img_id']);
    31     $images[$id] = $row['img_title'];
    31 		$images[$id] = $row['img_title'];
    32   }
    32 	}
    33   
    33 	
    34   // Loop through all gallery images until we find one we can read (typically on the first try, but you never know...)
    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;
    35 	$my_image = false;
    36   while ( count($images) > 0 )
    36 	while ( count($images) > 0 )
    37   {
    37 	{
    38     $rand = array_rand($images);
    38 		$rand = array_rand($images);
    39     $image = $images[$rand];
    39 		$image = $images[$rand];
    40     $acl = $session->fetch_page_acl(strval($rand), 'Gallery');
    40 		$acl = $session->fetch_page_acl(strval($rand), 'Gallery');
    41     if ( is_object($acl) && $acl->get_permissions('read') )
    41 		if ( is_object($acl) && $acl->get_permissions('read') )
    42     {
    42 		{
    43       $my_image = $image;
    43 			$my_image = $image;
    44       break;
    44 			break;
    45     }
    45 		}
    46     unset($images[$rand]);
    46 		unset($images[$rand]);
    47   }
    47 	}
    48   if ( $my_image )
    48 	if ( $my_image )
    49   {
    49 	{
    50     // Generate sidebar HTML
    50 		// Generate sidebar HTML
    51     $image_link = '<div style="padding: 5px; text-align: center;">
    51 		$image_link = '<div style="padding: 5px; text-align: center;">
    52                      <a href="' . makeUrlNS('Gallery', $rand) . '">
    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;" />
    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>
    54  											<span style="color: black;">' . htmlspecialchars($my_image) . '</span>
    55                      </a>
    55  										</a>
    56                    </div>';
    56  									</div>';
    57   }
    57 	}
    58   else
    58 	else
    59   {
    59 	{
    60     $image_link = 'No images in the gallery.';
    60 		$image_link = 'No images in the gallery.';
    61   }
    61 	}
    62   $template->sidebar_widget('Random image', $image_link);
    62 	$template->sidebar_widget('Random image', $image_link);
    63 }
    63 }
    64 
    64 
    65 $plugins->attachHook('compile_template', 'gal_sidebar_block();');
    65 $plugins->attachHook('compile_template', 'gal_sidebar_block();');
    66 
    66 
    67 ?>
    67 ?>