plugins/gallery/browser.php
changeset 0 7caf561c50ee
child 2 88c954d2846c
equal deleted inserted replaced
-1:000000000000 0:7caf561c50ee
       
     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 ## BROWSER INTERFACE
       
    17 ##
       
    18 
       
    19 $plugins->attachHook('base_classes_initted', '
       
    20   global $paths;
       
    21     $paths->add_page(Array(
       
    22       \'name\'=>\'Image gallery\',
       
    23       \'urlname\'=>\'Gallery\',
       
    24       \'namespace\'=>\'Special\',
       
    25       \'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
       
    26       ));
       
    27   ');
       
    28 
       
    29 /**
       
    30  * Class to handle building the HTML for gallery pages. Called by the pagination function.
       
    31  * @package Enano
       
    32  * @subpackage Snapr
       
    33  * @access private
       
    34  */
       
    35  
       
    36 class SnaprFormatter
       
    37 {
       
    38   
       
    39   /**
       
    40    * Counter for how many cells we've printed out in this row.
       
    41    * @var int
       
    42    */
       
    43   
       
    44   var $cell_count = 0;
       
    45   
       
    46   /**
       
    47    * Icons to print per row.
       
    48    * @var int
       
    49    */
       
    50   
       
    51   var $icons_per_row = 5;
       
    52   
       
    53   /**
       
    54    * Main render method, called from pagination function
       
    55    * @access private
       
    56    */
       
    57   
       
    58   function render($column_crap, $row, $row_crap)
       
    59   {
       
    60     global $db, $session, $paths, $template, $plugins; // Common objects
       
    61     
       
    62     $out = '';
       
    63     
       
    64     if ( $this->cell_count == $this->icons_per_row )
       
    65     {
       
    66       $out .= '</tr><tr>';
       
    67       $this->cell_count = 0;
       
    68     }
       
    69     $this->cell_count++;
       
    70     
       
    71     $title_safe = $row['img_title'];
       
    72     $title_safe = htmlspecialchars($title_safe);
       
    73     
       
    74     if ( $row['is_folder'] == 1 )
       
    75     {
       
    76       // It's a folder, show the icon
       
    77       $f_url_particle = sanitize_page_id($row['img_title']);
       
    78       $f_url_particle = htmlspecialchars($f_url_particle);
       
    79       $image_link = makeUrl( $paths->fullpage . '/' . $f_url_particle );
       
    80       $image_url = scriptPath . '/plugins/gallery/folder.png';
       
    81     }
       
    82     else
       
    83     {
       
    84       // It's an image, show a thumbnail
       
    85       $image_link = makeUrlNS('Gallery', $row['img_id']);
       
    86       $image_url  = makeUrlNS('Special', 'GalleryFetcher/thumb/' . $row['img_id']);
       
    87     }
       
    88     
       
    89     $image_url_js = addslashes($image_link);
       
    90     $jsclick = ( $session->user_level < USER_LEVEL_ADMIN ) ? ' onclick="window.location=\'' . $image_url_js . '\'"' : '';
       
    91     
       
    92     $out .= '<td style="text-align: center;">
       
    93             <div class="gallery_icon"' . $jsclick . '>';
       
    94     
       
    95     $out .= '<a href="' . $image_link . '"><img alt="&lt;Thumbnail&gt;" class="gallery_thumb" src="' . $image_url . '" /></a>';
       
    96     
       
    97     if ( $session->user_level < USER_LEVEL_ADMIN )
       
    98     {
       
    99       $out .= $title_safe;
       
   100     }
       
   101     else if ( $session->user_level >= USER_LEVEL_ADMIN )
       
   102     {
       
   103       $out .= '<div class="menu_nojs" style="text-align: center;"><a href="#" onclick="return false;" style="margin: 0 auto;">' . $title_safe . '</a>';
       
   104       
       
   105       $url_delete = makeUrlNS('Special', 'GalleryUpload', 'rm=' . $row['img_id'], true);
       
   106       $url_edit   = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $row['img_id'], true);
       
   107       
       
   108       // Tools menu
       
   109       $out .= '<ul style="text-align: left;">';
       
   110       $out .= '<li><a href="' . $url_delete . '">Delete ' . ( $row['is_folder'] == 1 ? 'this folder and all contents' : 'this image' ) . '</a></li>';
       
   111       $out .= '<li><a href="' . $url_edit   . '">Rename, move, or edit description</a></li>';
       
   112       $out .= '</ul>';
       
   113       $out .= '</div>';
       
   114       $out .= '<span class="menuclear"></span>';
       
   115     }
       
   116     
       
   117     $out .= '  </div>';
       
   118     
       
   119     $out .= '</td>';
       
   120     
       
   121     return $out;
       
   122   }
       
   123   
       
   124 }
       
   125 
       
   126 function page_Special_Gallery()
       
   127 {
       
   128   global $db, $session, $paths, $template, $plugins; // Common objects
       
   129   
       
   130   // die('<pre>' . print_r(gallery_folder_hierarchy(), true) . '</pre>');
       
   131   
       
   132   $sort_column = ( isset($_GET['sort'])  && in_array($_GET['sort'],  array('img_title', 'img_time_upload', 'img_time_mod')) ) ? $_GET['sort'] : 'img_title';
       
   133   $sort_order  = ( isset($_GET['order']) && in_array($_GET['order'], array('ASC', 'DESC')) ) ? $_GET['order'] : 'ASC';
       
   134   
       
   135   // Determine number of pictures per page
       
   136   $template->load_theme();
       
   137   
       
   138   global $theme;
       
   139   $fn = ENANO_ROOT . '/themes/' . $template->theme . '/theme.cfg';
       
   140   require( $fn );
       
   141   if ( isset($theme['snapr_gallery_rows']) )
       
   142   {
       
   143     $rows_in_browser = intval($theme['snapr_gallery_rows']);
       
   144     if ( empty($rows_in_browser) )
       
   145     {
       
   146       $rows_in_browser = 5;
       
   147     }
       
   148   }
       
   149   else
       
   150   {
       
   151     $rows_in_browser = 5;
       
   152   }
       
   153   
       
   154   $where = 'WHERE folder_parent IS NULL ' . "\n  ORDER BY is_folder DESC, $sort_column $sort_order, img_title ASC";
       
   155   $parms = $paths->getAllParams();
       
   156   
       
   157   $sql = "SELECT img_id, img_title, is_folder FROM ".table_prefix."gallery $where;";
       
   158   
       
   159   // Breadcrumb browser
       
   160   $breadcrumbs = array();
       
   161   $breadcrumbs[] = '<a href="' . makeUrlNS('Special', 'Gallery') . '">Gallery index</a>';
       
   162   
       
   163   $breadcrumb_urlcache = '';
       
   164   
       
   165   // CSS for gallery browser
       
   166   $template->add_header('<link rel="stylesheet" href="' . scriptPath . '/plugins/gallery/browser.css" type="text/css" />');
       
   167   $template->add_header('<link rel="stylesheet" href="' . scriptPath . '/plugins/gallery/dropdown.css" type="text/css" />');
       
   168   
       
   169   $header = $template->getHeader();
       
   170   
       
   171   if ( !empty($parms) )
       
   172   {
       
   173     $parms = dirtify_page_id($parms);
       
   174     if ( strstr($parms, '/') )
       
   175     {
       
   176       $folders = explode('/', $parms);
       
   177     }
       
   178     else
       
   179     {
       
   180       $folders = array(0 => $parms);
       
   181     }
       
   182     foreach ( $folders as $i => $_crap )
       
   183     {
       
   184       $folder =& $folders[$i];
       
   185       
       
   186       $f_url = sanitize_page_id($folder);
       
   187       $breadcrumb_urlcache .= '/' . $f_url;
       
   188       $breadcrumb_url = makeUrlNS('Special', 'Gallery' . $breadcrumb_urlcache);
       
   189       
       
   190       $folder = str_replace('_', ' ', $folder);
       
   191       
       
   192       if ( $i == ( count($folders) - 1 ) )
       
   193       {
       
   194         $breadcrumbs[] = htmlspecialchars($folder);
       
   195       }
       
   196       else
       
   197       {
       
   198         $breadcrumbs[] = '<a href="' . $breadcrumb_url . '">' . htmlspecialchars($folder) . '</a>';
       
   199       }
       
   200     }
       
   201     unset($folder);
       
   202     $folders = array_reverse($folders);
       
   203     // This is one of the best MySQL tricks on the market. We're going to reverse-travel a folder path using LEFT JOIN and the incredible power of metacoded SQL
       
   204     $sql = 'SELECT gm.img_id, gm.img_title, gm.is_folder, g0.img_title AS folder_name, g0.img_id AS folder_id FROM '.table_prefix.'gallery AS gm' . "\n  " . 'LEFT JOIN '.table_prefix.'gallery AS g0' . "\n    " . 'ON ( gm.folder_parent = g0.img_id )';
       
   205     $where = "\n  " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\'';
       
   206     foreach ( $folders as $i => $folder )
       
   207     {
       
   208       if ( $i == 0 )
       
   209         continue;
       
   210       $i_dec = $i - 1;
       
   211       $folder = $db->escape($folder);
       
   212       $sql .= "\n  LEFT JOIN ".table_prefix."gallery AS g{$i}\n    ON ( g{$i}.img_id=g{$i_dec}.folder_parent AND g{$i}.img_title='$folder' )";
       
   213       $where .= "\n    ".'AND g'.$i.'.img_id IS NOT NULL';
       
   214     }
       
   215     $where .= "\n    AND g{$i}.folder_parent IS NULL";
       
   216     $sql .= $where . "\n  ORDER BY is_folder DESC, gm.$sort_column $sort_order, gm.img_title ASC" . ';';
       
   217   }
       
   218   
       
   219   $img_query = $db->sql_query($sql);
       
   220   if ( !$img_query )
       
   221     $db->_die('The folder ID could not be selected.');
       
   222   
       
   223   if ( $db->numrows() < 1 )
       
   224   {
       
   225     // Nothing in this folder, for one of two reasons:
       
   226     //   1) The folder doesn't exist
       
   227     //   2) The folder exists but doesn't have any images in it
       
   228     
       
   229     /*
       
   230     $folders_old = $folders;
       
   231     $folders = array(
       
   232       0 => $folders_old[0]
       
   233       );
       
   234     $x = $folders_old;
       
   235     unset($x[0]);
       
   236     $folders = array_merge($folders, $x);
       
   237     unset($x);
       
   238     */
       
   239     // die('<pre>' . print_r($folders, true) . '</pre>');
       
   240     
       
   241     // This next query will try to determine if the folder itself exists
       
   242     $sql = 'SELECT g0.img_id, g0.img_title FROM '.table_prefix.'gallery AS g0';
       
   243     $where = "\n  " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\'';
       
   244     foreach ( $folders as $i => $folder )
       
   245     {
       
   246       if ( $i == 0 )
       
   247         continue;
       
   248       $i_dec = $i - 1;
       
   249       $folder = $db->escape($folder);
       
   250       $sql .= "\n  LEFT JOIN ".table_prefix."gallery AS g{$i}\n    ON ( g{$i}.img_id=g{$i_dec}.folder_parent AND g{$i}.img_title='$folder' )";
       
   251       $where .= "\n    ".'AND g'.$i.'.img_id IS NOT NULL';
       
   252     }
       
   253     $where .= "\n    AND g{$i}.folder_parent IS NULL";
       
   254     $where .= "\n    AND g0.is_folder=1";
       
   255     $sql .= $where . ';';
       
   256    
       
   257     $nameq = $db->sql_query($sql);
       
   258     if ( !$nameq )
       
   259       $db->_die();
       
   260     
       
   261     if ( $db->numrows($nameq) < 1 )
       
   262     {
       
   263       die_friendly('Folder not found', '<p>The folder you requested doesn\'t exist. Please check the URL and try again, or return to the <a href="' . makeUrlNS('Special', 'Gallery') . '">gallery index</a>.</p>');
       
   264     }
       
   265     
       
   266     $row = $db->fetchrow($nameq);
       
   267     
       
   268     // Generate title
       
   269     $title = dirtify_page_id($row['img_title']);
       
   270     $title = str_replace('_', ' ', $title);
       
   271     $title = htmlspecialchars($title);
       
   272     
       
   273     $template->tpl_strings['PAGE_NAME'] = $title;
       
   274     
       
   275     $first_row = $row;
       
   276     
       
   277     $db->sql_data_seek(0, $img_query);
       
   278     
       
   279     /* $folders = $folders_old; */
       
   280   }
       
   281   else if ( !empty($parms) )
       
   282   {
       
   283     $row = $db->fetchrow($img_query);
       
   284     $first_row = $row;
       
   285     
       
   286     // Generate title
       
   287     $title = htmlspecialchars($row['folder_name']);
       
   288     
       
   289     $template->tpl_strings['PAGE_NAME'] = $title;
       
   290     
       
   291     $db->sql_data_seek(0, $img_query);
       
   292   }
       
   293   else
       
   294   {
       
   295     $row = $db->fetchrow($img_query);
       
   296     $first_row = $row;
       
   297     
       
   298     $template->tpl_strings['PAGE_NAME'] = 'Image Gallery';
       
   299     $breadcrumbs = array('<b>Gallery index</b>');
       
   300     
       
   301     $db->sql_data_seek(0, $img_query);
       
   302   }
       
   303   
       
   304   $f_errors = array();
       
   305   
       
   306   if ( $session->user_level >= USER_LEVEL_ADMIN && isset($_POST['create_folder']) && isset($first_row['folder_id']) )
       
   307   {
       
   308     if ( empty($_POST['create_folder']) )
       
   309     {
       
   310       $f_errors[] = 'Please enter a folder name.';
       
   311     }
       
   312     if ( $_POST['create_folder'] == '_id' )
       
   313     {
       
   314       $f_errors[] = 'The name "_id" is reserved for internal functions and cannot be used on any image or folder.';
       
   315     }
       
   316     if ( count($f_errors) < 1 )
       
   317     {
       
   318       $q = $db->sql_query('INSERT INTO '.table_prefix.'gallery(img_title, is_folder, folder_parent) VALUES(\'' . $db->escape($_POST['create_folder']) . '\', 1, ' . $first_row['folder_id'] . ');');
       
   319       if ( !$q )
       
   320         $db->_die();
       
   321       redirect(makeUrl($paths->fullpage), 'Folder created', 'The folder "' . htmlspecialchars($_POST['create_folder']) . '" has been created. Redirecting to last viewed folder...', 2);
       
   322     }
       
   323   }
       
   324   
       
   325   echo $header;
       
   326   
       
   327   if ( count($f_errors) > 0 )
       
   328   {
       
   329     echo '<div class="error-box">Error creating folder:<ul><li>' . implode('</li><li>', $f_errors) . '</li></ul></div>';
       
   330   }
       
   331   
       
   332   // From here, this breadcrumb stuff is a piece of... sourdough French bread :-) *smacks lips*
       
   333   echo '<div class="tblholder" style="padding: 4px; margin-bottom: 7px;">';
       
   334   // Upload image link for admins
       
   335   if ( $session->user_level >= USER_LEVEL_ADMIN )
       
   336   {
       
   337     echo '<div style="float: right; font-size: smaller;">';
       
   338     echo '<b><a href="' . makeUrlNS('Special', 'GalleryUpload') . '">Upload new image(s)</a></b>';
       
   339     echo '</div>';
       
   340   }
       
   341   // The actual breadcrumbs
       
   342   echo '<b><small>' . implode(' &raquo; ', $breadcrumbs) . '</small></b>';
       
   343   echo '</div>';
       
   344   
       
   345   // "Edit all" link
       
   346   if ( $row = $db->fetchrow($img_query) && $session->user_level >= USER_LEVEL_ADMIN )
       
   347   {
       
   348     $img_list = array();
       
   349     $fol_list = array();
       
   350     $all_list = array();
       
   351     do
       
   352     {
       
   353       if ( $row === true && isset($first_row) )
       
   354       {
       
   355         $row = $first_row;
       
   356       }
       
   357         // die('<pre>' . var_dump($row) . $db->sql_backtrace() . '</pre>');
       
   358       if ( !$row['img_id'] )
       
   359         break;
       
   360       $all_list[] = $row['img_id'];
       
   361       if ( $row['is_folder'] == 1 )
       
   362         $fol_list[] = $row['img_id'];
       
   363       else
       
   364         $img_list[] = $row['img_id'];
       
   365     }
       
   366     while ( $row = $db->fetchrow($img_query) );
       
   367     
       
   368     $all_list = implode(',', $all_list);
       
   369     $fol_list = implode(',', $fol_list);
       
   370     $img_list = implode(',', $img_list);
       
   371     
       
   372     if ( !empty($all_list) )
       
   373     {
       
   374       echo '<div style="float: right;">
       
   375               Edit all in this folder: ';
       
   376       if ( !empty($img_list) )
       
   377       {
       
   378         $edit_link = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $img_list, true);
       
   379         echo "<a href=\"$edit_link\">images</a> ";
       
   380       }
       
   381       if ( !empty($fol_list) )
       
   382       {
       
   383         $edit_link = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $fol_list, true);
       
   384         echo "<a href=\"$edit_link\">folders</a> ";
       
   385       }
       
   386       if ( !empty($img_list) && !empty($fol_list) )
       
   387       {
       
   388         $edit_link = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $all_list, true);
       
   389         echo "<a href=\"$edit_link\">both</a> ";
       
   390       }
       
   391       // " Bypass stupid jEdit bug
       
   392       echo '</div>';
       
   393     }
       
   394   }
       
   395   
       
   396   $url_sort_name_asc  = makeUrl($paths->fullpage, 'sort=img_title&order=ASC', true);
       
   397   $url_sort_name_desc = makeUrl($paths->fullpage, 'sort=img_title&order=DESC', true);
       
   398   $url_sort_upl_asc   = makeUrl($paths->fullpage, 'sort=img_time_upload&order=ASC', true);
       
   399   $url_sort_mod_asc   = makeUrl($paths->fullpage, 'sort=img_time_mod&order=ASC', true);
       
   400   $url_sort_upl_desc  = makeUrl($paths->fullpage, 'sort=img_time_upload&order=DESC', true);
       
   401   $url_sort_mod_desc  = makeUrl($paths->fullpage, 'sort=img_time_mod&order=DESC', true);
       
   402   
       
   403   // "Sort by" selector (pure CSS!)
       
   404   echo '<div class="select-outer">
       
   405           <span>Sort by...</span>
       
   406           <div class="select-inner">
       
   407             <a href="' . $url_sort_name_asc  . '">Image title (A-Z) <b>(default)</b></a>
       
   408             <a href="' . $url_sort_name_desc . '">Image title (Z-A)</a>
       
   409             <a href="' . $url_sort_upl_desc  . '">Time first uploaded (newest first)</a>
       
   410             <a href="' . $url_sort_upl_asc   . '">Time first uploaded (oldest first)</a>
       
   411             <a href="' . $url_sort_mod_desc  . '">Date of last modification (newest first)</a>
       
   412             <a href="' . $url_sort_mod_asc   . '">Date of last modification (oldest first)</a>
       
   413           </div>
       
   414         </div>
       
   415         <div class="select-pad">&nbsp;</div>';
       
   416   
       
   417   $db->sql_data_seek(0, $img_query);
       
   418   
       
   419   //
       
   420   // Main fetcher
       
   421   //
       
   422   
       
   423   $renderer = new SnaprFormatter();
       
   424   $callers = array(
       
   425     'img_id' => array($renderer, 'render')
       
   426     );
       
   427   
       
   428   $renderer->icons_per_row = $rows_in_browser;
       
   429   
       
   430   $start = 0;
       
   431   if ( isset($_GET['start']) && preg_match('/^[0-9]+$/', $_GET['start']) )
       
   432   {
       
   433     $start = intval($_GET['start']);
       
   434   }
       
   435   
       
   436   $per_page = $rows_in_browser * 5;
       
   437   
       
   438   $html = paginate($img_query, '{img_id}', $db->numrows($img_query), makeUrl($paths->fullpage, 'start=%s', true), $start, $per_page, $callers, '<table border="0" cellspacing="8"><tr>', '</tr></table>');
       
   439   echo $html;
       
   440   
       
   441   if ( $session->user_level >= USER_LEVEL_ADMIN )
       
   442   {
       
   443     echo '<div class="select-outer">Create new folder';
       
   444     echo '<div class="select-inner" style="padding-top: 4px;">';
       
   445     echo '<form action="' . makeUrl($paths->fullpage) . '" method="post">';
       
   446     echo '<input type="text" name="create_folder" size="30" /> <input type="submit" value="Create" />';
       
   447     echo '</form></div>';
       
   448     echo '</div><div class="select-pad">&nbsp;</div><br />';
       
   449   }
       
   450   
       
   451   $template->footer();
       
   452   
       
   453 }
       
   454 
       
   455 ?>