diff -r 000000000000 -r 7caf561c50ee plugins/gallery/functions.php
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/gallery/functions.php Wed Jul 25 16:36:26 2007 -0400
@@ -0,0 +1,317 @@
+sql_query('SELECT img_title, img_id, folder_parent FROM '.table_prefix.'gallery WHERE img_id=' . $img_id . ';');
+ if ( !$q )
+ $db->_die();
+ $row = $db->fetchrow();
+ if ( !$row )
+ {
+ break;
+ }
+ if ( $sanity > 1 )
+ {
+ $ret[] = $row['img_title'];
+ }
+ if ( !$row['folder_parent'] )
+ {
+ break;
+ }
+ if ( in_array($row['img_id'], $sanity_stack) )
+ return array('Infinite loop');
+ $sanity_stack[] = $row['img_id'];
+ $img_id = $row['folder_parent'];
+ }
+ return $ret;
+}
+
+/**
+ * Generates a hierarchy of Gallery folders.
+ * @return array
+ */
+
+function gallery_folder_hierarchy()
+{
+ global $db, $session, $paths, $template, $plugins; // Common objects
+
+ $q = $db->sql_query('SELECT img_id, img_title, folder_parent FROM '.table_prefix.'gallery WHERE is_folder=1');
+ if ( !$q )
+ $db->_die();
+
+ if ( $db->numrows() < 1 )
+ {
+ return array();
+ }
+
+ $lookup_table = array();
+ $hier = array('_id' => 'NULL');
+ $orphans = array();
+ $persist_orphans = array();
+
+ while ( $row = $db->fetchrow() )
+ {
+ if ( !$row['folder_parent'] )
+ {
+ // root-level folder
+ $hier[ $row['img_title'] ] = array('_id' => $row['img_id']);
+ $lookup_table[$row['img_id']] =& $hier[ $row['img_title'] ];
+ }
+ else if ( $row['folder_parent'] && isset($lookup_table[$row['folder_parent']]) )
+ {
+ // child folder, parent is resolved
+ $lookup_table[ $row['folder_parent'] ][ $row['img_title'] ] = array('_id' => $row['img_id']);
+ $lookup_table[ $row['img_id'] ] =& $lookup_table[ $row['folder_parent'] ][ $row['img_title'] ];
+ }
+ else if ( $row['folder_parent'] && !isset($lookup_table[$row['folder_parent']]) )
+ {
+ // child folder, orphan as of yet
+ $orphans[] = $row;
+ }
+ }
+
+ // Resolve orphans
+ do
+ {
+ $persist_orphans = array();
+ while ( count($orphans) > 0 )
+ {
+ $orphan =& $orphans[ ( count($orphans) - 1 ) ];
+ if ( isset($lookup_table[$orphan['folder_parent']]) )
+ {
+ $lookup_table[ $orphan['folder_parent'] ][ $orphan['img_title'] ] = array('_id' => $orphan['img_id']);
+ $lookup_table[ $orphan['img_id'] ] =& $lookup_table[ $orphan['folder_parent'] ][ $orphan['img_title'] ];
+ }
+ else
+ {
+ $persist_orphans[] = $orphans[ ( count($orphans) - 1 ) ];
+ //echo 'BUG: ' . htmlspecialchars($orphan['img_title']) . ' (' . $orphan['img_id'] . ') is an orphan folder (parent is ' . $orphan['folder_parent'] . '); placing in root
';
+ // $hier[ $orphan['img_title'] ] = array();
+ // $lookup_table[$orphan['img_id']] =& $hier[ $orphan['img_title'] ];
+ }
+ unset($orphan, $orphans[ ( count($orphans) - 1 ) ]);
+ }
+ $orphans = $persist_orphans;
+ //die('insanity:
'.print_r($hier,true).print_r($lookup_table,true).print_r($persist_orphans,true).''); + } + while ( count($persist_orphans) > 0 ); + + return $hier; + +} + +/** + * Generates HTML for a folder selector. + * @param string The form field name, defaults to folder_id. + * @param bool Whether to auto-select the root or not. Defaults to true. + * @return string + */ + +function gallery_hier_formfield($field_name = 'folder_id', $autosel = true) +{ + $hier = gallery_folder_hierarchy(); + $img_join = scriptPath . '/images/icons/joinbottom.gif'; + $img_join_term = scriptPath . '/images/icons/join.gif'; + $img_line = scriptPath . '/images/icons/line.gif'; + $img_empty = scriptPath . '/images/icons/empty.gif'; + + $html = _gallery_hier_form_inner($hier, '