diff -r 000000000000 -r 7caf561c50ee plugins/gallery/browser.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/gallery/browser.php Wed Jul 25 16:36:26 2007 -0400 @@ -0,0 +1,455 @@ +attachHook('base_classes_initted', ' + global $paths; + $paths->add_page(Array( + \'name\'=>\'Image gallery\', + \'urlname\'=>\'Gallery\', + \'namespace\'=>\'Special\', + \'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\', + )); + '); + +/** + * Class to handle building the HTML for gallery pages. Called by the pagination function. + * @package Enano + * @subpackage Snapr + * @access private + */ + +class SnaprFormatter +{ + + /** + * Counter for how many cells we've printed out in this row. + * @var int + */ + + var $cell_count = 0; + + /** + * Icons to print per row. + * @var int + */ + + var $icons_per_row = 5; + + /** + * Main render method, called from pagination function + * @access private + */ + + function render($column_crap, $row, $row_crap) + { + global $db, $session, $paths, $template, $plugins; // Common objects + + $out = ''; + + if ( $this->cell_count == $this->icons_per_row ) + { + $out .= ''; + $this->cell_count = 0; + } + $this->cell_count++; + + $title_safe = $row['img_title']; + $title_safe = htmlspecialchars($title_safe); + + if ( $row['is_folder'] == 1 ) + { + // It's a folder, show the icon + $f_url_particle = sanitize_page_id($row['img_title']); + $f_url_particle = htmlspecialchars($f_url_particle); + $image_link = makeUrl( $paths->fullpage . '/' . $f_url_particle ); + $image_url = scriptPath . '/plugins/gallery/folder.png'; + } + else + { + // It's an image, show a thumbnail + $image_link = makeUrlNS('Gallery', $row['img_id']); + $image_url = makeUrlNS('Special', 'GalleryFetcher/thumb/' . $row['img_id']); + } + + $image_url_js = addslashes($image_link); + $jsclick = ( $session->user_level < USER_LEVEL_ADMIN ) ? ' onclick="window.location=\'' . $image_url_js . '\'"' : ''; + + $out .= ' + '; + + $out .= ''; + + return $out; + } + +} + +function page_Special_Gallery() +{ + global $db, $session, $paths, $template, $plugins; // Common objects + + // die('
' . print_r(gallery_folder_hierarchy(), true) . '
'); + + $sort_column = ( isset($_GET['sort']) && in_array($_GET['sort'], array('img_title', 'img_time_upload', 'img_time_mod')) ) ? $_GET['sort'] : 'img_title'; + $sort_order = ( isset($_GET['order']) && in_array($_GET['order'], array('ASC', 'DESC')) ) ? $_GET['order'] : 'ASC'; + + // Determine number of pictures per page + $template->load_theme(); + + global $theme; + $fn = ENANO_ROOT . '/themes/' . $template->theme . '/theme.cfg'; + require( $fn ); + if ( isset($theme['snapr_gallery_rows']) ) + { + $rows_in_browser = intval($theme['snapr_gallery_rows']); + if ( empty($rows_in_browser) ) + { + $rows_in_browser = 5; + } + } + else + { + $rows_in_browser = 5; + } + + $where = 'WHERE folder_parent IS NULL ' . "\n ORDER BY is_folder DESC, $sort_column $sort_order, img_title ASC"; + $parms = $paths->getAllParams(); + + $sql = "SELECT img_id, img_title, is_folder FROM ".table_prefix."gallery $where;"; + + // Breadcrumb browser + $breadcrumbs = array(); + $breadcrumbs[] = 'Gallery index'; + + $breadcrumb_urlcache = ''; + + // CSS for gallery browser + $template->add_header(''); + $template->add_header(''); + + $header = $template->getHeader(); + + if ( !empty($parms) ) + { + $parms = dirtify_page_id($parms); + if ( strstr($parms, '/') ) + { + $folders = explode('/', $parms); + } + else + { + $folders = array(0 => $parms); + } + foreach ( $folders as $i => $_crap ) + { + $folder =& $folders[$i]; + + $f_url = sanitize_page_id($folder); + $breadcrumb_urlcache .= '/' . $f_url; + $breadcrumb_url = makeUrlNS('Special', 'Gallery' . $breadcrumb_urlcache); + + $folder = str_replace('_', ' ', $folder); + + if ( $i == ( count($folders) - 1 ) ) + { + $breadcrumbs[] = htmlspecialchars($folder); + } + else + { + $breadcrumbs[] = '' . htmlspecialchars($folder) . ''; + } + } + unset($folder); + $folders = array_reverse($folders); + // 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 + $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 )'; + $where = "\n " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\''; + foreach ( $folders as $i => $folder ) + { + if ( $i == 0 ) + continue; + $i_dec = $i - 1; + $folder = $db->escape($folder); + $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' )"; + $where .= "\n ".'AND g'.$i.'.img_id IS NOT NULL'; + } + $where .= "\n AND g{$i}.folder_parent IS NULL"; + $sql .= $where . "\n ORDER BY is_folder DESC, gm.$sort_column $sort_order, gm.img_title ASC" . ';'; + } + + $img_query = $db->sql_query($sql); + if ( !$img_query ) + $db->_die('The folder ID could not be selected.'); + + if ( $db->numrows() < 1 ) + { + // Nothing in this folder, for one of two reasons: + // 1) The folder doesn't exist + // 2) The folder exists but doesn't have any images in it + + /* + $folders_old = $folders; + $folders = array( + 0 => $folders_old[0] + ); + $x = $folders_old; + unset($x[0]); + $folders = array_merge($folders, $x); + unset($x); + */ + // die('
' . print_r($folders, true) . '
'); + + // This next query will try to determine if the folder itself exists + $sql = 'SELECT g0.img_id, g0.img_title FROM '.table_prefix.'gallery AS g0'; + $where = "\n " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\''; + foreach ( $folders as $i => $folder ) + { + if ( $i == 0 ) + continue; + $i_dec = $i - 1; + $folder = $db->escape($folder); + $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' )"; + $where .= "\n ".'AND g'.$i.'.img_id IS NOT NULL'; + } + $where .= "\n AND g{$i}.folder_parent IS NULL"; + $where .= "\n AND g0.is_folder=1"; + $sql .= $where . ';'; + + $nameq = $db->sql_query($sql); + if ( !$nameq ) + $db->_die(); + + if ( $db->numrows($nameq) < 1 ) + { + die_friendly('Folder not found', '

The folder you requested doesn\'t exist. Please check the URL and try again, or return to the gallery index.

'); + } + + $row = $db->fetchrow($nameq); + + // Generate title + $title = dirtify_page_id($row['img_title']); + $title = str_replace('_', ' ', $title); + $title = htmlspecialchars($title); + + $template->tpl_strings['PAGE_NAME'] = $title; + + $first_row = $row; + + $db->sql_data_seek(0, $img_query); + + /* $folders = $folders_old; */ + } + else if ( !empty($parms) ) + { + $row = $db->fetchrow($img_query); + $first_row = $row; + + // Generate title + $title = htmlspecialchars($row['folder_name']); + + $template->tpl_strings['PAGE_NAME'] = $title; + + $db->sql_data_seek(0, $img_query); + } + else + { + $row = $db->fetchrow($img_query); + $first_row = $row; + + $template->tpl_strings['PAGE_NAME'] = 'Image Gallery'; + $breadcrumbs = array('Gallery index'); + + $db->sql_data_seek(0, $img_query); + } + + $f_errors = array(); + + if ( $session->user_level >= USER_LEVEL_ADMIN && isset($_POST['create_folder']) && isset($first_row['folder_id']) ) + { + if ( empty($_POST['create_folder']) ) + { + $f_errors[] = 'Please enter a folder name.'; + } + if ( $_POST['create_folder'] == '_id' ) + { + $f_errors[] = 'The name "_id" is reserved for internal functions and cannot be used on any image or folder.'; + } + if ( count($f_errors) < 1 ) + { + $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'] . ');'); + if ( !$q ) + $db->_die(); + redirect(makeUrl($paths->fullpage), 'Folder created', 'The folder "' . htmlspecialchars($_POST['create_folder']) . '" has been created. Redirecting to last viewed folder...', 2); + } + } + + echo $header; + + if ( count($f_errors) > 0 ) + { + echo '
Error creating folder:
'; + } + + // From here, this breadcrumb stuff is a piece of... sourdough French bread :-) *smacks lips* + echo '
'; + // Upload image link for admins + if ( $session->user_level >= USER_LEVEL_ADMIN ) + { + echo '
'; + echo 'Upload new image(s)'; + echo '
'; + } + // The actual breadcrumbs + echo '' . implode(' » ', $breadcrumbs) . ''; + echo '
'; + + // "Edit all" link + if ( $row = $db->fetchrow($img_query) && $session->user_level >= USER_LEVEL_ADMIN ) + { + $img_list = array(); + $fol_list = array(); + $all_list = array(); + do + { + if ( $row === true && isset($first_row) ) + { + $row = $first_row; + } + // die('
' . var_dump($row) . $db->sql_backtrace() . '
'); + if ( !$row['img_id'] ) + break; + $all_list[] = $row['img_id']; + if ( $row['is_folder'] == 1 ) + $fol_list[] = $row['img_id']; + else + $img_list[] = $row['img_id']; + } + while ( $row = $db->fetchrow($img_query) ); + + $all_list = implode(',', $all_list); + $fol_list = implode(',', $fol_list); + $img_list = implode(',', $img_list); + + if ( !empty($all_list) ) + { + echo '
+ Edit all in this folder: '; + if ( !empty($img_list) ) + { + $edit_link = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $img_list, true); + echo "images "; + } + if ( !empty($fol_list) ) + { + $edit_link = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $fol_list, true); + echo "folders "; + } + if ( !empty($img_list) && !empty($fol_list) ) + { + $edit_link = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $all_list, true); + echo "both "; + } + // " Bypass stupid jEdit bug + echo '
'; + } + } + + $url_sort_name_asc = makeUrl($paths->fullpage, 'sort=img_title&order=ASC', true); + $url_sort_name_desc = makeUrl($paths->fullpage, 'sort=img_title&order=DESC', true); + $url_sort_upl_asc = makeUrl($paths->fullpage, 'sort=img_time_upload&order=ASC', true); + $url_sort_mod_asc = makeUrl($paths->fullpage, 'sort=img_time_mod&order=ASC', true); + $url_sort_upl_desc = makeUrl($paths->fullpage, 'sort=img_time_upload&order=DESC', true); + $url_sort_mod_desc = makeUrl($paths->fullpage, 'sort=img_time_mod&order=DESC', true); + + // "Sort by" selector (pure CSS!) + echo '
+ Sort by... +
+ Image title (A-Z) (default) + Image title (Z-A) + Time first uploaded (newest first) + Time first uploaded (oldest first) + Date of last modification (newest first) + Date of last modification (oldest first) +
+
+
 
'; + + $db->sql_data_seek(0, $img_query); + + // + // Main fetcher + // + + $renderer = new SnaprFormatter(); + $callers = array( + 'img_id' => array($renderer, 'render') + ); + + $renderer->icons_per_row = $rows_in_browser; + + $start = 0; + if ( isset($_GET['start']) && preg_match('/^[0-9]+$/', $_GET['start']) ) + { + $start = intval($_GET['start']); + } + + $per_page = $rows_in_browser * 5; + + $html = paginate($img_query, '{img_id}', $db->numrows($img_query), makeUrl($paths->fullpage, 'start=%s', true), $start, $per_page, $callers, '', '
'); + echo $html; + + if ( $session->user_level >= USER_LEVEL_ADMIN ) + { + echo '
Create new folder'; + echo '
'; + echo '
'; + echo ' '; + echo '
'; + echo '
 

'; + } + + $template->footer(); + +} + +?>