function wppa_album_select_a($args)
{
    global $wpdb;
    $args = wp_parse_args($args, array('exclude' => '', 'selected' => '', 'disabled' => '', 'addpleaseselect' => false, 'addnone' => false, 'addall' => false, 'addgeneric' => false, 'addblank' => false, 'addselected' => false, 'addseparate' => false, 'addselbox' => false, 'disableancestors' => false, 'checkaccess' => false, 'checkowner' => false, 'checkupload' => false, 'addmultiple' => false, 'addnumbers' => false, 'path' => false, 'root' => false, 'content' => false, 'sort' => true));
    // Provide default selection if no selected given
    if ($args['selected'] === '') {
        $args['selected'] = wppa_get_last_album();
    }
    // See if selection is valid
    if ($args['selected'] == $args['exclude'] || $args['checkupload'] && !wppa_allow_uploads($args['selected']) || $args['disableancestors'] && wppa_is_ancestor($args['exclude'], $args['selected'])) {
        $args['selected'] = '0';
    }
    $albums = $wpdb->get_results("SELECT * FROM `" . WPPA_ALBUMS . "` " . wppa_get_album_order($args['root']), ARRAY_A);
    // Add to secondary cache
    if ($albums) {
        wppa_cache_album('add', $albums);
    }
    if ($albums) {
        // Filter for root
        if ($args['root']) {
            $root = $args['root'];
            switch ($root) {
                // case '0': all, will be skipped as it returns false in 'if ( $args['root'] )'
                case '-2':
                    // Generic only
                    foreach (array_keys($albums) as $albidx) {
                        if (wppa_is_separate($albums[$albidx]['id'])) {
                            unset($albums[$albidx]);
                        }
                    }
                    break;
                case '-1':
                    // Separate only
                    foreach (array_keys($albums) as $albidx) {
                        if (!wppa_is_separate($albums[$albidx]['id'])) {
                            unset($albums[$albidx]);
                        }
                    }
                    break;
                default:
                    foreach (array_keys($albums) as $albidx) {
                        if (!wppa_is_ancestor($root, $albums[$albidx]['id'])) {
                            unset($albums[$albidx]);
                        }
                    }
                    break;
            }
        }
        // Filter for must have content
        if ($args['content']) {
            foreach (array_keys($albums) as $albidx) {
                if (wppa_get_photo_count($albums[$albidx]['id']) <= wppa_get_mincount()) {
                    unset($albums[$albidx]);
                }
            }
        }
        // Add paths
        if ($args['path']) {
            $albums = wppa_add_paths($albums);
        } else {
            foreach (array_keys($albums) as $index) {
                $albums[$index]['name'] = __(stripslashes($albums[$index]['name']));
            }
        }
        // Sort
        if ($args['sort']) {
            $albums = wppa_array_sort($albums, 'name');
        }
    }
    // Output
    $result = '';
    $selected = $args['selected'] == '0' ? ' selected="selected"' : '';
    if ($args['addpleaseselect']) {
        $result .= '<option value="0" disabled="disabled" ' . $selected . ' >' . (is_admin() ? __('- select an album -', 'wppa') : __a('- select an album -')) . '</option>';
    }
    $selected = $args['selected'] == '0' ? ' selected="selected"' : '';
    if ($args['addnone']) {
        $result .= '<option value="0"' . $selected . ' >' . (is_admin() ? __('--- none ---', 'wppa') : __a('--- none ---')) . '</option>';
    }
    $selected = $args['selected'] == '0' ? ' selected="selected"' : '';
    if ($args['addall']) {
        $result .= '<option value="0"' . $selected . ' >' . (is_admin() ? __('--- all ---', 'wppa') : __a('--- all ---')) . '</option>';
    }
    $selected = $args['selected'] == '-2' ? ' selected="selected"' : '';
    if ($args['addall']) {
        $result .= '<option value="-2"' . $selected . ' >' . (is_admin() ? __('--- generic ---', 'wppa') : __a('--- generic ---')) . '</option>';
    }
    $selected = $args['selected'] == '0' ? ' selected="selected"' : '';
    if ($args['addblank']) {
        $result .= '<option value="0"' . $selected . ' >' . '</option>';
    }
    $selected = $args['selected'] == '-99' ? ' selected="selected"' : '';
    if ($args['addmultiple']) {
        $result .= '<option value="-99"' . $selected . ' >' . (is_admin() ? __('--- multiple see below ---', 'wppa') : __a('--- multiple see below ---')) . '</option>';
    }
    $selected = $args['selected'] == '0' ? ' selected="selected"' : '';
    if ($args['addselbox']) {
        $result .= '<option value="0"' . $selected . ' >' . (is_admin() ? __('--- a selection box ---', 'wppa') : __a('--- a selection box ---')) . '</option>';
    }
    if ($albums) {
        foreach ($albums as $album) {
            if ($args['disabled'] == $album['id'] || $args['exclude'] == $album['id'] || $args['checkupload'] && !wppa_allow_uploads($album['id']) || $args['disableancestors'] && wppa_is_ancestor($args['exclude'], $album['id'])) {
                $disabled = ' disabled="disabled"';
            } else {
                $disabled = '';
            }
            if ($args['selected'] == $album['id'] && !$disabled) {
                $selected = ' selected="selected"';
            } else {
                $selected = '';
            }
            $ok = true;
            // Assume this will be in the list
            if ($args['checkaccess'] && !wppa_have_access($album['id'])) {
                $ok = false;
            }
            if ($args['checkowner'] && wppa_switch('upload_owner_only')) {
                // Need to check
                if ($album['owner'] != wppa_get_user() && $album['owner'] != '--- public ---') {
                    // Not 'mine'
                    if (!wppa_user_is('administrator')) {
                        // No admin
                        $ok = false;
                    }
                }
            }
            if ($selected && $args['addselected']) {
                $ok = true;
            }
            if ($ok) {
                if ($args['addnumbers']) {
                    $number = ' ( ' . $album['id'] . ' )';
                } else {
                    $number = '';
                }
                $result .= '<option value="' . $album['id'] . '" ' . $selected . $disabled . '>' . $album['name'] . $number . '</option>';
            }
        }
    }
    $selected = $args['selected'] == '-1' ? ' selected="selected"' : '';
    if ($args['addseparate']) {
        $result .= '<option value="-1"' . $selected . '>' . (is_admin() ? __('--- separate ---', 'wppa') : __a('--- separate ---')) . '</option>';
    }
    return $result;
}
    function do_album_navigator($parent, $page, $skip, $propclass, $extraclause = '')
    {
        global $wpdb;
        static $level;
        static $ca;
        if (!$level) {
            $level = '1';
            if (isset($_REQUEST['wppa-album'])) {
                $ca = $_REQUEST['wppa-album'];
            } elseif (isset($_REQUEST['album'])) {
                $ca = $_REQUEST['album'];
            } else {
                $ca = '0';
            }
            $ca = wppa_force_numeric_else($ca, '0');
            if ($ca && !wppa_album_exists($ca)) {
                //				wppa_log('dbg', 'Non-existent album '.$ca.' in url. Referrer= '.$_ENV["HTTP_REFERER"].', Request uri= '.$_ENV["REQUEST_URI"]);
                $ca = '0';
            }
        } else {
            $level++;
        }
        $slide = wppa_opt('album_navigator_widget_linktype') == 'slide' ? '&amp;wppa-slide=1' : '';
        $w = $this->get_widget_id();
        $p = $parent;
        $result = '';
        $albums = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `a_parent` = %s " . $extraclause . wppa_get_album_order(max('0', $parent)), $parent), ARRAY_A);
        if (!empty($albums)) {
            wppa_cache_album('add', $albums);
            $result .= '<ul>';
            foreach ($albums as $album) {
                $a = $album['id'];
                $treecount = wppa_treecount_a($a);
                if ($treecount['albums'] || $treecount['photos'] > wppa_opt('min_thumbs') || $skip == 'no') {
                    $result .= '
						<li class="anw-' . $w . '-' . $p . $propclass . '" style="list-style:none; display:' . ($level == '1' ? '' : 'none') . ';">';
                    if (wppa_has_children($a)) {
                        $result .= '
							<div style="cursor:default;width:12px;float:left;text-align:center;font-weight:bold;" class="anw-' . $w . '-' . $a . '-" onclick="jQuery(\'.anw-' . $w . '-' . $a . '\').css(\'display\',\'\'); jQuery(\'.anw-' . $w . '-' . $a . '-\').css(\'display\',\'none\');" >' . ($a == $ca ? '&raquo;' : '+') . '</div>
							<div style="cursor:default;width:12px;float:left;text-align:center;font-weight:bold;display:none;" class="anw-' . $w . '-' . $a . '" onclick="jQuery(\'.anw-' . $w . '-' . $a . '-\').css(\'display\',\'\'); jQuery(\'.anw-' . $w . '-' . $a . '\').css(\'display\',\'none\'); jQuery(\'.p-' . $w . '-' . $a . '\').css(\'display\',\'none\');" >' . ($a == $ca ? '&raquo;' : '-') . '</div>';
                    } else {
                        $result .= '
							<div style="width:12px;float:left;" >&nbsp;' . ($a == $ca ? '&raquo;' : '') . '</div>';
                    }
                    $result .= '
							<a href="' . wppa_encrypt_url(wppa_get_permalink($page) . '&amp;wppa-album=' . $a . '&amp;wppa-cover=0&amp;wppa-occur=1' . $slide) . '">' . wppa_get_album_name($a) . '</a>
						</li>';
                    $newpropclass = $propclass . ' p-' . $w . '-' . $p;
                    $result .= '<li class="anw-' . $w . '-' . $p . $propclass . '" style="list-style:none;" >' . $this->do_album_navigator($a, $page, $skip, $newpropclass, $extraclause) . '</li>';
                }
            }
            $result .= '</ul>';
            if ($level == '1' && $ca) {
                // && $parent != '-1' ) {
                $result .= '<script type="text/javascript" >';
                while ($ca != '0' && $ca != '-1') {
                    $result .= '
								jQuery(\'.anw-' . $w . '-' . $ca . '\').css(\'display\',\'\'); jQuery(\'.anw-' . $w . '-' . $ca . '-\').css(\'display\',\'none\');';
                    $ca = wppa_get_parentalbumid($ca);
                }
                $result .= '</script>';
            }
        }
        $level--;
        return str_replace('<ul></ul>', '', $result);
    }
Exemplo n.º 3
0
 /** @see WP_Widget::widget */
 function widget($args, $instance)
 {
     global $wpdb;
     global $wppa;
     global $thumb;
     require_once dirname(__FILE__) . '/wppa-links.php';
     require_once dirname(__FILE__) . '/wppa-styles.php';
     require_once dirname(__FILE__) . '/wppa-functions.php';
     require_once dirname(__FILE__) . '/wppa-thumbnails.php';
     require_once dirname(__FILE__) . '/wppa-boxes-html.php';
     require_once dirname(__FILE__) . '/wppa-slideshow.php';
     wppa_initialize_runtime();
     $wppa['in_widget'] = 'alb';
     $wppa['mocc']++;
     extract($args);
     $instance = wp_parse_args((array) $instance, array('title' => '', 'parent' => 'none', 'name' => 'no', 'skip' => 'yes'));
     $widget_title = apply_filters('widget_title', $instance['title']);
     $page = in_array(wppa_opt('wppa_album_widget_linktype'), $wppa['links_no_page']) ? '' : wppa_get_the_landing_page('wppa_album_widget_linkpage', __a('Photo Albums'));
     $max = wppa_opt('wppa_album_widget_count');
     if (!$max) {
         $max = '10';
     }
     $parent = $instance['parent'];
     $name = $instance['name'];
     $skip = $instance['skip'];
     if (is_numeric($parent)) {
         $albums = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . WPPA_ALBUMS . '` WHERE `a_parent` = %s ' . wppa_get_album_order($parent), $parent), ARRAY_A);
     } else {
         switch ($parent) {
             case 'all':
                 $albums = $wpdb->get_results('SELECT * FROM `' . WPPA_ALBUMS . '` ' . wppa_get_album_order(), ARRAY_A);
                 break;
             case 'last':
                 $albums = $wpdb->get_results('SELECT * FROM `' . WPPA_ALBUMS . '` ORDER BY `timestamp` DESC', ARRAY_A);
                 break;
             default:
                 wppa_dbg_msg('Error, unimplemented album selection: ' . $parent . ' in Album widget.', 'red', true);
         }
     }
     $widget_content = "\n" . '<!-- WPPA+ album Widget start -->';
     $maxw = wppa_opt('wppa_album_widget_size');
     $maxh = $maxw;
     if ($name == 'yes') {
         $maxh += 18;
     }
     $count = 0;
     if ($albums) {
         foreach ($albums as $album) {
             if ($count < $max) {
                 global $thumb;
                 $imageid = wppa_get_coverphoto_id($album['id']);
                 $image = $wpdb->get_row($wpdb->prepare('SELECT * FROM `' . WPPA_PHOTOS . '` WHERE `id` = %s', $imageid), ARRAY_A);
                 $imgcount = $wpdb->get_var($wpdb->prepare('SELECT COUNT(*) FROM ' . WPPA_PHOTOS . ' WHERE `album` = %s', $album['id']));
                 $subalbumcount = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `" . WPPA_ALBUMS . "` WHERE `a_parent` = %s", $album['id']));
                 $thumb = $image;
                 // Make the HTML for current picture
                 if ($image && ($imgcount > wppa_opt('wppa_min_thumbs') || $subalbumcount)) {
                     $link = wppa_get_imglnk_a('albwidget', $image['id']);
                     $file = wppa_get_thumb_path($image['id']);
                     $imgevents = wppa_get_imgevents('thumb', $image['id'], true);
                     $imgstyle_a = wppa_get_imgstyle_a($image['id'], $file, $maxw, 'center', 'albthumb');
                     $imgstyle = $imgstyle_a['style'];
                     $width = $imgstyle_a['width'];
                     $height = $imgstyle_a['height'];
                     $cursor = $imgstyle_a['cursor'];
                     if (wppa_switch('wppa_show_albwidget_tooltip')) {
                         $title = esc_attr(strip_tags(wppa_get_album_desc($album['id'])));
                     } else {
                         $title = '';
                     }
                     $imgurl = wppa_get_thumb_url($image['id'], '', $width, $height);
                 } else {
                     $link = '';
                     $file = '';
                     $imgevents = '';
                     $imgstyle = 'width:' . $maxw . ';height:' . $maxh . ';';
                     $width = $maxw;
                     $height = $maxw;
                     // !!
                     $cursor = 'default';
                     $title = sprintf(__a('Upload at least %d photos to this album!', 'wppa_theme'), wppa_opt('wppa_min_thumbs') - $imgcount + 1);
                     if ($imageid) {
                         // The 'empty album has a cover image
                         $file = wppa_get_thumb_path($image['id']);
                         $imgstyle_a = wppa_get_imgstyle_a($image['id'], $file, $maxw, 'center', 'albthumb');
                         $imgstyle = $imgstyle_a['style'];
                         $width = $imgstyle_a['width'];
                         $height = $imgstyle_a['height'];
                         $imgurl = wppa_get_thumb_url($image['id'], '', $width, $height);
                     } else {
                         $imgurl = wppa_get_imgdir() . 'album32.png';
                     }
                 }
                 $imgurl = wppa_fix_poster_ext($imgurl, $image['id']);
                 if ($imgcount > wppa_opt('wppa_min_thumbs') || $skip == 'no') {
                     $widget_content .= "\n" . '<div class="wppa-widget" style="width:' . $maxw . 'px; height:' . $maxh . 'px; margin:4px; display:inline; text-align:center; float:left;">';
                     if ($link) {
                         if ($link['is_url']) {
                             // Is a href
                             $widget_content .= "\n\t" . '<a href="' . $link['url'] . '" title="' . $title . '" target="' . $link['target'] . '" >';
                             if (wppa_is_video($image['id'])) {
                                 $widget_content .= wppa_get_video_html(array('id' => $image['id'], 'width' => $width, 'height' => $height, 'controls' => false, 'margin_top' => $imgstyle_a['margin-top'], 'margin_bottom' => $imgstyle_a['margin-bottom'], 'cursor' => 'pointer', 'events' => $imgevents, 'tagid' => 'i-' . $image['id'] . '-' . $wppa['mocc'], 'title' => $title));
                             } else {
                                 $widget_content .= "\n\t\t" . '<img id="i-' . $image['id'] . '-' . $wppa['mocc'] . '" title="' . $title . '" src="' . $imgurl . '" width="' . $width . '" height="' . $height . '" style="' . $imgstyle . ' cursor:pointer;" ' . $imgevents . ' ' . wppa_get_imgalt($image['id']) . ' >';
                             }
                             $widget_content .= "\n\t" . '</a>';
                         } elseif ($link['is_lightbox']) {
                             $thumbs = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` = %s " . wppa_get_photo_order($album['id']), $album['id']), 'ARRAY_A');
                             if ($thumbs) {
                                 foreach ($thumbs as $thumb) {
                                     $title = wppa_get_lbtitle('alw', $thumb['id']);
                                     if (wppa_is_video($thumb['id'])) {
                                         $siz['0'] = wppa_get_videox($thumb['id']);
                                         $siz['1'] = wppa_get_videoy($thumb['id']);
                                     } else {
                                         //	$siz = getimagesize( wppa_get_photo_path( $thumb['id'] ) );
                                         $siz['0'] = wppa_get_photox($thumb['id']);
                                         $siz['1'] = wppa_get_photoy($thumb['id']);
                                     }
                                     $link = wppa_fix_poster_ext(wppa_get_photo_url($thumb['id'], '', $siz['0'], $siz['1']), $thumb['id']);
                                     $is_video = wppa_is_video($thumb['id']);
                                     $has_audio = wppa_has_audio($thumb['id']);
                                     $widget_content .= "\n\t" . '<a href="' . $link . '"' . ($is_video ? ' data-videohtml="' . esc_attr(wppa_get_video_body($thumb['id'])) . '"' . ' data-videonatwidth="' . wppa_get_videox($thumb['id']) . '"' . ' data-videonatheight="' . wppa_get_videoy($thumb['id']) . '"' : '') . ($has_audio ? ' data-audiohtml="' . esc_attr(wppa_get_audio_body($thumb['id'])) . '"' : '') . ' ' . wppa('rel') . '="' . wppa_opt('wppa_lightbox_name') . '[alw-' . $wppa['mocc'] . '-' . $album['id'] . ']"' . ' ' . wppa('lbtitle') . '="' . $title . '" >';
                                     if ($thumb['id'] == $image['id']) {
                                         // the cover image
                                         if (wppa_is_video($image['id'])) {
                                             $widget_content .= wppa_get_video_html(array('id' => $image['id'], 'width' => $width, 'height' => $height, 'controls' => false, 'margin_top' => $imgstyle_a['margin-top'], 'margin_bottom' => $imgstyle_a['margin-bottom'], 'cursor' => $cursor, 'events' => $imgevents, 'tagid' => 'i-' . $image['id'] . '-' . $wppa['mocc'], 'title' => wppa_zoom_in($image['id'])));
                                         } else {
                                             $widget_content .= "\n\t\t" . '<img id="i-' . $image['id'] . '-' . $wppa['mocc'] . '" title="' . wppa_zoom_in($image['id']) . '" src="' . $imgurl . '" width="' . $width . '" height="' . $height . '" style="' . $imgstyle . $cursor . '" ' . $imgevents . ' ' . wppa_get_imgalt($image['id']) . ' >';
                                         }
                                     }
                                     $widget_content .= "\n\t" . '</a>';
                                 }
                             }
                         } else {
                             // Is an onclick unit
                             if (wppa_is_video($image['id'])) {
                                 $widget_content .= wppa_get_video_html(array('id' => $image['id'], 'width' => $width, 'height' => $height, 'controls' => false, 'margin_top' => $imgstyle_a['margin-top'], 'margin_bottom' => $imgstyle_a['margin-bottom'], 'cursor' => 'pointer', 'events' => $imgevents . ' onclick="' . $link['url'] . '"', 'tagid' => 'i-' . $image['id'] . '-' . $wppa['mocc'], 'title' => $title));
                             } else {
                                 $widget_content .= "\n\t" . '<img id="i-' . $image['id'] . '-' . $wppa['mocc'] . '" title="' . $title . '" src="' . $imgurl . '" width="' . $width . '" height="' . $height . '" style="' . $imgstyle . ' cursor:pointer;" ' . $imgevents . ' onclick="' . $link['url'] . '" ' . wppa_get_imgalt($image['id']) . ' >';
                             }
                         }
                     } else {
                         if (wppa_is_video($image['id'])) {
                             $widget_content .= wppa_get_video_html(array('id' => $image['id'], 'width' => $width, 'height' => $height, 'controls' => false, 'margin_top' => $imgstyle_a['margin-top'], 'margin_bottom' => $imgstyle_a['margin-bottom'], 'cursor' => 'pointer', 'events' => $imgevents, 'tagid' => 'i-' . $image['id'] . '-' . $wppa['mocc'], 'title' => $title));
                         } else {
                             $widget_content .= "\n\t" . '<img id="i-' . $image['id'] . '-' . $wppa['mocc'] . '" title="' . $title . '" src="' . $imgurl . '" width="' . $width . '" height="' . $height . '" style="' . $imgstyle . '" ' . $imgevents . ' ' . wppa_get_imgalt($image['id']) . ' >';
                         }
                     }
                     if ($name == 'yes') {
                         $widget_content .= "\n\t" . '<span style="font-size:' . wppa_opt('wppa_fontsize_widget_thumb') . 'px; min-height:100%;">' . __(stripslashes($album['name'])) . '</span>';
                     }
                     $widget_content .= "\n" . '</div>';
                     $count++;
                 }
             }
         }
     } else {
         $widget_content .= 'There are no albums (yet).';
     }
     $widget_content .= '<div style="clear:both"></div>';
     $widget_content .= "\n" . '<!-- WPPA+ thumbnail Widget end -->';
     echo "\n" . $before_widget;
     if (!empty($widget_title)) {
         echo $before_title . $widget_title . $after_title;
     }
     echo $widget_content . $after_widget;
     $wppa['in_widget'] = false;
 }
function wppa_get_albums()
{
    global $wpdb;
    global $wppa_session;
    wppa_dbg_msg('get_albums entered: ' . wppa('mocc') . ' Start_album=' . wppa('start_album') . ', Cover=' . wppa('is_cover'));
    if (wppa('is_topten')) {
        return false;
    }
    if (wppa('is_lasten')) {
        return false;
    }
    if (wppa('is_comten')) {
        return false;
    }
    if (wppa('is_featen')) {
        return false;
    }
    if (wppa('is_tag')) {
        return false;
    }
    if (wppa('photos_only')) {
        return false;
    }
    if (wppa('src') && wppa_switch('photos_only')) {
        return false;
    }
    if (wppa('is_owner') && !wppa('start_album')) {
        return false;
    }
    // No owner album( s )
    if (wppa('calendar') == 'exifdtm') {
        return false;
    }
    if (wppa('calendar') == 'timestamp') {
        return false;
    }
    if (wppa('calendar') == 'modified') {
        return false;
    }
    // Supersearch?
    if (wppa('supersearch')) {
        $ss_data = explode(',', wppa('supersearch'));
        $data = $ss_data['3'];
        switch ($ss_data['1']) {
            // Category
            case 'c':
                $catlist = wppa_get_catlist();
                if (strpos($data, '.')) {
                    $temp = explode('.', $data);
                    $ids = $catlist[$temp['0']]['ids'];
                    $i = '1';
                    while ($i < count($temp)) {
                        $ids = array_intersect($ids, $catlist[$temp[$i]]['ids']);
                        $i++;
                    }
                } else {
                    $ids = $catlist[$data]['ids'];
                }
                if (empty($ids)) {
                    $ids = array('0');
                    // Dummy
                }
                $query = "SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `id` IN (" . implode(',', $ids) . ")";
                $albums = $wpdb->get_results($query, ARRAY_A);
                wppa_dbg_q('Q_SS');
                break;
                // Name. Name is converted to number or enum
            // Name. Name is converted to number or enum
            case 'n':
                $query = $wpdb->prepare("SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `name` = %s", $data);
                $albums = $wpdb->get_results($query, ARRAY_A);
                wppa_dbg_q('Q_SS');
                break;
                // Text
            // Text
            case 't':
                if (strpos($data, '.')) {
                    $temp = explode('.', $data);
                    $query = $wpdb->prepare("SELECT * FROM `" . WPPA_INDEX . "` WHERE `slug` = %s", $temp['0']);
                    $indexes = $wpdb->get_row($query, ARRAY_A);
                    wppa_dbg_q('Q_SS');
                    $ids = explode('.', wppa_expand_enum($indexes['albums']));
                    $i = '1';
                    while ($i < count($temp)) {
                        $query = $wpdb->prepare("SELECT * FROM `" . WPPA_INDEX . "` WHERE `slug` = %s", $temp[$i]);
                        $indexes = $wpdb->get_row($query, ARRAY_A);
                        wppa_dbg_q('Q_SS');
                        $ids = array_intersect($ids, explode('.', wppa_expand_enum($indexes['albums'])));
                        $i++;
                    }
                } else {
                    $query = $wpdb->prepare("SELECT * FROM `" . WPPA_INDEX . "` WHERE `slug` = %s", $data);
                    $indexes = $wpdb->get_row($query, ARRAY_A);
                    wppa_dbg_q('Q_SS');
                    $ids = explode('.', wppa_expand_enum($indexes['albums']));
                }
                if (empty($ids)) {
                    $ids = array('0');
                    // Dummy
                }
                $query = "SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `id` IN (" . implode(',', $ids) . ")";
                $albums = $wpdb->get_results($query, ARRAY_A);
                wppa_dbg_q('Q_SS');
                break;
        }
    } elseif (wppa('src')) {
        $final_array = array();
        $chunks = explode(',', stripslashes(strtolower(wppa('searchstring'))));
        // all chunks
        foreach ($chunks as $chunk) {
            if (strlen(trim($chunk))) {
                $words = wppa_index_raw_to_words($chunk);
                $album_array = array();
                // all words in the searchstring
                foreach ($words as $word) {
                    $word = trim($word);
                    if (strlen($word) > 1) {
                        if (strlen($word) > 10) {
                            $word = substr($word, 0, 10);
                        }
                        if (wppa_switch('wild_front')) {
                            $aidxs = $wpdb->get_results("SELECT `slug`, `albums` FROM `" . WPPA_INDEX . "` WHERE `slug` LIKE '%" . $word . "%'", ARRAY_A);
                        } else {
                            $aidxs = $wpdb->get_results("SELECT `slug`, `albums` FROM `" . WPPA_INDEX . "` WHERE `slug` LIKE '" . $word . "%'", ARRAY_A);
                        }
                        $albums = '';
                        if ($aidxs) {
                            foreach ($aidxs as $ai) {
                                $albums .= $ai['albums'] . ',';
                            }
                        }
                        $album_array[] = wppa_index_string_to_array(trim($albums, ','));
                    }
                }
                // Must meet all words: intersect photo sets
                foreach (array_keys($album_array) as $idx) {
                    if ($idx > '0') {
                        $album_array['0'] = array_intersect($album_array['0'], $album_array[$idx]);
                    }
                }
                // Save partial result
                if (isset($album_array['0'])) {
                    $final_array = array_merge($final_array, $album_array['0']);
                }
            }
        }
        // Compose WHERE clause
        $selection = " `id` = '0' ";
        foreach (array_keys($final_array) as $p) {
            $selection .= "OR `id` = '" . $final_array[$p] . "' ";
        }
        // Get them
        $albums = $wpdb->get_results("SELECT * FROM `" . WPPA_ALBUMS . "` WHERE " . $selection . " " . wppa_get_album_order('0'), ARRAY_A);
        wppa_dbg_q('Q10');
        // Exclusive separate albums?
        if (wppa_switch('excl_sep')) {
            foreach (array_keys($albums) as $idx) {
                if (wppa_is_separate($albums[$idx]['id'])) {
                    unset($albums[$idx]);
                }
            }
        }
        // Rootsearch?
        if (wppa('is_rootsearch')) {
            $root = $wppa_session['search_root'];
            if (is_array($albums)) {
                $c1 = count($albums);
                foreach (array_keys($albums) as $idx) {
                    if (!wppa_is_ancestor($root, $albums[$idx]['id'])) {
                        unset($albums[$idx]);
                    }
                }
                $c2 = count($albums);
                wppa_dbg_msg('Rootsearch albums:' . $c1 . ' -> ' . $c2);
            }
        }
        // Check maximum
        if (is_array($albums) && count($albums) > wppa_opt('max_search_albums') && wppa_opt('max_search_albums') != '0') {
            $alert_text = sprintf(__('There are %s albums found. Only the first %s will be shown. Please refine your search criteria.', 'wp-photo-album-plus'), count($albums), wppa_opt('max_search_albums'));
            wppa_alert($alert_text);
            foreach (array_keys($albums) as $idx) {
                if ($idx >= wppa_opt('max_search_albums')) {
                    unset($albums[$idx]);
                }
            }
        }
        if (is_array($albums)) {
            wppa('any', true);
        }
    } else {
        // Its not search
        $id = wppa('start_album');
        if (!$id) {
            $id = '0';
        }
        // Do the query
        if ($id == '-2') {
            // All albums
            if (wppa('is_cover')) {
                $q = "SELECT * FROM `" . WPPA_ALBUMS . "` " . wppa_get_album_order();
                wppa_dbg_q('Q11d');
                $albums = $wpdb->get_results($q, ARRAY_A);
            } else {
                $albums = false;
            }
        } elseif (wppa('last_albums')) {
            // is_cover = true. For the order sequence, see remark in wppa_albums()
            if (wppa('last_albums_parent')) {
                $q = $wpdb->prepare("SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `a_parent` = %s ORDER BY `timestamp` DESC LIMIT %d", wppa('last_albums_parent'), wppa('last_albums'));
            } else {
                $q = $wpdb->prepare("SELECT * FROM `" . WPPA_ALBUMS . "` ORDER BY `timestamp` DESC LIMIT %d", wppa('last_albums'));
            }
            wppa_dbg_q('Q11a');
            $albums = $wpdb->get_results($q, ARRAY_A);
        } elseif (wppa_is_int($id)) {
            if (wppa('is_cover')) {
                $q = $wpdb->prepare('SELECT * FROM ' . WPPA_ALBUMS . ' WHERE `id` = %s', $id);
            } else {
                $q = $wpdb->prepare('SELECT * FROM ' . WPPA_ALBUMS . ' WHERE `a_parent` = %s ' . wppa_get_album_order($id), $id);
            }
            wppa_dbg_q('Q11b');
            $albums = $wpdb->get_results($q, ARRAY_A);
            if (!wppa('is_cover')) {
                wppa_verify_treecounts($id, 'albums', count($albums));
            }
        } elseif (strpos($id, '.') !== false) {
            // Album enum
            $ids = wppa_series_to_array($id);
            if (wppa('is_cover')) {
                $q = "SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `id` = " . implode(" OR `id` = ", $ids) . " " . wppa_get_album_order();
            } else {
                $q = "SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `a_parent` = " . implode(" OR `a_parent` = ", $ids) . " " . wppa_get_album_order();
            }
            wppa_dbg_q('Q11c');
            wppa_dbg_msg($q, 'red');
            $albums = $wpdb->get_results($q, ARRAY_A);
        } else {
            $albums = false;
        }
    }
    // Check for empty albums
    if (wppa_switch('skip_empty_albums')) {
        $user = wppa_get_user();
        if (is_array($albums)) {
            foreach (array_keys($albums) as $albumkey) {
                $albumid = $albums[$albumkey]['id'];
                $albumowner = $albums[$albumkey]['owner'];
                $treecount = wppa_treecount_a($albums[$albumkey]['id']);
                $photocount = $treecount['photos'];
                if (!$photocount && !wppa_user_is('administrator') && $user != $albumowner) {
                    unset($albums[$albumkey]);
                }
            }
        }
    }
    // Copy data into secondary cache
    if ($albums) {
        wppa_cache_album('add', $albums);
    }
    wppa('album_count', count($albums));
    return $albums;
}
function wppa_album_select_a($args)
{
    global $wpdb;
    $args = wp_parse_args($args, array('exclude' => '', 'selected' => '', 'disabled' => '', 'addpleaseselect' => false, 'addnone' => false, 'addall' => false, 'addgeneric' => false, 'addblank' => false, 'addselected' => false, 'addseparate' => false, 'addselbox' => false, 'addowner' => false, 'disableancestors' => false, 'checkaccess' => false, 'checkowner' => false, 'checkupload' => false, 'addmultiple' => false, 'addnumbers' => false, 'path' => false, 'root' => false, 'content' => false, 'sort' => true, 'checkarray' => false, 'array' => array(), 'optionclass' => ''));
    // Provide default selection if no selected given
    if ($args['selected'] === '') {
        $args['selected'] = wppa_get_last_album();
    }
    // See if selection is valid
    if ($args['selected'] == $args['exclude'] || $args['checkupload'] && !wppa_allow_uploads($args['selected']) || $args['disableancestors'] && wppa_is_ancestor($args['exclude'], $args['selected'])) {
        $args['selected'] = '0';
    }
    // Get roughly the albums that might be in the selection
    if ($args['checkarray'] && !empty($args['array'])) {
        $albums = $wpdb->get_results("SELECT `id`, `name` " . "FROM `" . WPPA_ALBUMS . "` " . "WHERE `id` IN (" . implode(',', $args['array']) . ") " . ($args['checkowner'] && wppa_switch('upload_owner_only') && !wppa_user_is('administrator') ? "AND `owner` IN ( '--- public ---', '" . wppa_get_user() . "' ) " : "") . wppa_get_album_order($args['root']), ARRAY_A);
    } else {
        $albums = $wpdb->get_results("SELECT `id`, `name` " . "FROM `" . WPPA_ALBUMS . "` " . ($args['checkowner'] && wppa_switch('upload_owner_only') && !wppa_user_is('administrator') ? "WHERE `owner` IN ( '--- public ---', '" . wppa_get_user() . "' ) " : "") . wppa_get_album_order($args['root']), ARRAY_A);
    }
    /* Can not add to cache because only "SELECT * " can be added
    	// Add to secondary cache
    	if ( $albums ) {
    		wppa_cache_album( 'add', $albums );
    	}
    	*/
    if ($albums) {
        // Filter for root
        if ($args['root']) {
            $root = $args['root'];
            switch ($root) {
                // case '0': all, will be skipped as it returns false in 'if ( $args['root'] )'
                case '-2':
                    // Generic only
                    foreach (array_keys($albums) as $albidx) {
                        if (wppa_is_separate($albums[$albidx]['id'])) {
                            unset($albums[$albidx]);
                        }
                    }
                    break;
                case '-1':
                    // Separate only
                    foreach (array_keys($albums) as $albidx) {
                        if (!wppa_is_separate($albums[$albidx]['id'])) {
                            unset($albums[$albidx]);
                        }
                    }
                    break;
                default:
                    foreach (array_keys($albums) as $albidx) {
                        if (!wppa_is_ancestor($root, $albums[$albidx]['id'])) {
                            unset($albums[$albidx]);
                        }
                    }
                    break;
            }
        }
        // Filter for must have content
        if ($args['content']) {
            foreach (array_keys($albums) as $albidx) {
                if (wppa_get_photo_count($albums[$albidx]['id']) <= wppa_get_mincount()) {
                    unset($albums[$albidx]);
                }
            }
        }
        // Add paths
        if ($args['path']) {
            $albums = wppa_add_paths($albums);
        } else {
            foreach (array_keys($albums) as $index) {
                $albums[$index]['name'] = __(stripslashes($albums[$index]['name']));
            }
        }
        // Sort
        if ($args['sort']) {
            $albums = wppa_array_sort($albums, 'name');
        }
    }
    // Output
    $result = '';
    $selected = $args['selected'] == '0' ? ' selected="selected"' : '';
    if ($args['addpleaseselect']) {
        $result .= '<option value="0" disabled="disabled" ' . $selected . ' >' . __('- select an album -', 'wp-photo-album-plus') . '</option>';
    }
    $selected = $args['selected'] == '0' ? ' selected="selected"' : '';
    if ($args['addnone']) {
        $result .= '<option value="0"' . $selected . ' >' . __('--- none ---', 'wp-photo-album-plus') . '</option>';
    }
    $selected = $args['selected'] == '0' ? ' selected="selected"' : '';
    if ($args['addall']) {
        $result .= '<option value="0"' . $selected . ' >' . __('--- all ---', 'wp-photo-album-plus') . '</option>';
    }
    $selected = $args['selected'] == '-2' ? ' selected="selected"' : '';
    if ($args['addall']) {
        $result .= '<option value="-2"' . $selected . ' >' . __('--- generic ---', 'wp-photo-album-plus') . '</option>';
    }
    $selected = $args['selected'] == '-3' ? ' selected="selected"' : '';
    if ($args['addowner']) {
        $result .= '<option value="-3"' . $selected . ' >' . __('--- owner/public ---', 'wp-photo-album-plus') . '</option>';
    }
    $selected = $args['selected'] == '0' ? ' selected="selected"' : '';
    if ($args['addblank']) {
        $result .= '<option value="0"' . $selected . ' >' . '</option>';
    }
    $selected = $args['selected'] == '-99' ? ' selected="selected"' : '';
    if ($args['addmultiple']) {
        $result .= '<option value="-99"' . $selected . ' >' . __('--- multiple see below ---', 'wp-photo-album-plus') . '</option>';
    }
    $selected = $args['selected'] == '0' ? ' selected="selected"' : '';
    if ($args['addselbox']) {
        $result .= '<option value="0"' . $selected . ' >' . __('--- a selection box ---', 'wp-photo-album-plus') . '</option>';
    }
    // In case multiple
    if (strpos($args['selected'], ',') !== false) {
        $selarr = explode(',', $args['selected']);
    } else {
        $selarr = array($args['selected']);
    }
    if ($albums) {
        foreach ($albums as $album) {
            if ($args['disabled'] == $album['id'] || $args['exclude'] == $album['id'] || $args['checkupload'] && !wppa_allow_uploads($album['id']) || $args['disableancestors'] && wppa_is_ancestor($args['exclude'], $album['id'])) {
                $disabled = ' disabled="disabled"';
            } else {
                $disabled = '';
            }
            if (in_array($album['id'], $selarr, true) && !$disabled) {
                $selected = ' selected="selected"';
            } else {
                $selected = '';
            }
            $ok = true;
            // Assume this will be in the list
            if ($args['checkaccess'] && !wppa_have_access($album['id'])) {
                $ok = false;
            }
            /* This is in the query now
            		if ( $args['checkowner'] && wppa_switch( 'upload_owner_only' ) ) { 							// Need to check
            			if ( $album['owner'] != wppa_get_user() && $album['owner']  != '--- public ---' ) { 	// Not 'mine'
            				if ( ! wppa_user_is( 'administrator' ) ) {											// No admin
            					$ok = false;
            				}
            			}
            		}
            		*/
            /* This is in the query now
            		if ( $args['checkarray'] ) {
            			if ( ! in_array( $album['id'], $args['array'] ) ) {
            				$ok = false;
            			}
            		}
            		*/
            if ($selected && $args['addselected']) {
                $ok = true;
            }
            if ($ok) {
                if ($args['addnumbers']) {
                    $number = ' ( ' . $album['id'] . ' )';
                } else {
                    $number = '';
                }
                $result .= '<option class="' . $args['optionclass'] . '" value="' . $album['id'] . '" ' . $selected . $disabled . '>' . $album['name'] . $number . '</option>';
            }
        }
    }
    $selected = $args['selected'] == '-1' ? ' selected="selected"' : '';
    if ($args['addseparate']) {
        $result .= '<option value="-1"' . $selected . '>' . __('--- separate ---', 'wp-photo-album-plus') . '</option>';
    }
    return $result;
}
function wppa_subalbumlinks_html($id, $top = true)
{
    global $wpdb;
    // Do they need us? Anything to display?
    if (wppa_opt('cover_sublinks_display') == 'none') {
        return;
    }
    // Display type
    $display_type = wppa_opt('cover_sublinks_display');
    // Link type
    $link_type = wppa_opt('cover_sublinks');
    // Init
    $is_list = $display_type == 'list' || $display_type == 'recursivelist';
    $is_recursive = $display_type == 'recursivelist';
    $first = true;
    // Get the children
    $subs = $wpdb->get_results("SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `a_parent` = " . $id . " " . wppa_get_album_order($id), ARRAY_A);
    // Only if there are sub-albums
    if (!empty($subs)) {
        wppa_out('<div>');
        // Local css
        if ($top) {
            if ($is_list) {
                wppa_out('<style scoped="scoped" >' . 'ul, li { margin:0; }' . 'ul {' . 'list-style-type:disc;' . 'list-style-position:inside;' . 'padding:0 0 0 24px;' . '}' . 'li { cursor:pointer; }' . '</style>');
            }
            if ($display_type == 'microthumbs') {
                wppa_out('<style scoped="scoped" >' . 'img {' . 'max-width:100px;' . 'max-height:50px;' . 'padding:1px;' . 'margin:1px;' . 'background-color:' . wppa_opt('bgcolor_img') . ';' . 'float:left;' . '}' . '</style>');
            }
        }
        // Start list if required
        if ($is_list) {
            wppa_out('<ul class="wppa-cover-sublink-list" style="clear:both;" >');
        } else {
            wppa_out('<div style="clear:both;" ></div>');
        }
        // Process the sub-albums
        foreach ($subs as $album) {
            // What is the albums title linktype
            $linktype = $album['cover_linktype'];
            if (!$linktype) {
                $linktype = 'content';
            }
            // Default
            // What is the albums title linkpage
            $linkpage = $album['cover_linkpage'];
            if ($linkpage == '-1') {
                $linktype = 'none';
            }
            // for backward compatibility
            // Find the content 'View' link
            $albumid = $album['id'];
            $photocount = wppa_get_photo_count($albumid, 'use_treecounts');
            // Thumbnails and covers, show sub-album covers
            // in case slideshow is requested on an empty album
            if (wppa_opt('cover_sublinks') == 'content' || !$photocount) {
                if (wppa_switch('allow_ajax') && !$linkpage) {
                    $href_content = '';
                    $onclick_content = "wppaDoAjaxRender( " . wppa('mocc') . ", '" . wppa_get_album_url_ajax($albumid, $linkpage) . "', '" . wppa_convert_to_pretty($href_content) . "' )";
                } else {
                    $href_content = wppa_convert_to_pretty(wppa_get_album_url($albumid, $linkpage));
                    $onclick_content = '';
                }
            } else {
                if (wppa_switch('allow_ajax') && !$linkpage) {
                    $href_content = '';
                    $onclick_content = "wppaDoAjaxRender( " . wppa('mocc') . ", '" . wppa_get_slideshow_url_ajax($albumid, $linkpage) . "', '" . wppa_convert_to_pretty($href_content) . "' )";
                } else {
                    $href_content = wppa_convert_to_pretty(wppa_get_slideshow_url($albumid, $linkpage));
                    $onclick_content = '';
                }
            }
            // Do the output
            $title = esc_attr(__('View the album', 'wp-photo-album-plus') . ': ' . wppa_get_album_name($album['id']));
            switch ($display_type) {
                case 'list':
                case 'recursivelist':
                    if ($link_type == 'none') {
                        wppa_out('<li>' . wppa_get_album_name($album['id']) . '</li>');
                    } else {
                        wppa_out('<li>' . '<a' . ($href_content ? ' href="' . $href_content . '"' : '') . ($onclick_content ? ' onclick="' . $onclick_content . '"' : '') . ' title="' . $title . '"' . ' >' . wppa_get_album_name($album['id']) . '</a>' . '</li>');
                    }
                    break;
                case 'enum':
                    if (!$first) {
                        wppa_out(', ');
                    }
                    if ($link_type == 'none') {
                        wppa_out(wppa_get_album_name($album['id']));
                    } else {
                        wppa_out('<a' . ($href_content ? ' href="' . $href_content . '"' : '') . ($onclick_content ? ' onclick="' . $onclick_content . '"' : '') . ' title="' . $title . '"' . ' >' . wppa_get_album_name($album['id']) . '</a>');
                    }
                    $first = false;
                    break;
                case 'microthumbs':
                    $coverphoto_id = wppa_get_coverphoto_id($album['id']);
                    $src = wppa_fix_poster_ext(wppa_get_thumb_url($coverphoto_id), $coverphoto_id);
                    if ($link_type == 'none') {
                        wppa_out('<img' . ' class="wppa-cover-sublink-img"' . ' src="' . $src . '"' . ' alt="' . wppa_get_album_name($album['id']) . '"' . ' />');
                    } else {
                        wppa_out('<a' . ($href_content ? ' href="' . $href_content . '"' : '') . ($onclick_content ? ' onclick="' . $onclick_content . '"' : '') . ' title="' . $title . '"' . ' >' . '<img' . ' class="wppa-cover-sublink-img"' . ' src="' . $src . '"' . ' alt="' . wppa_get_album_name($album['id']) . '"' . ' />' . '</a>');
                    }
                    break;
            }
            // Go deeper for grandchildren
            if ($is_recursive) {
                wppa_subalbumlinks_html($album['id'], false);
            }
        }
        // End list
        if ($is_list) {
            wppa_out('</ul>');
        }
        wppa_out('</div>');
    }
}
function _wppa_page_export()
{
    global $wpdb;
    // Export Photos admin page
    // Do the export if requested
    if (isset($_POST['wppa-export-submit'])) {
        check_admin_referer('$wppa_nonce', WPPA_NONCE);
        wppa_export_photos();
    }
    ?>
	<div class="wrap">
		<?php 
    $iconurl = WPPA_URL . '/images/album32.png';
    ?>
		<div id="icon-camera" class="icon32" style="background: transparent url(<?php 
    echo $iconurl;
    ?>
) no-repeat">		
		</div>
		<?php 
    $iconurl = WPPA_URL . '/images/arrow32.png';
    ?>
		<div id="icon-arrow" class="icon32" style="background: transparent url(<?php 
    echo $iconurl;
    ?>
) no-repeat">
		</div>
		<?php 
    $iconurl = WPPA_URL . '/images/disk32.png';
    ?>
		<div id="icon-disk" class="icon32" style="background: transparent url(<?php 
    echo $iconurl;
    ?>
) no-repeat">
			<br />
		</div>

		<h2><?php 
    _e('Export Photos', 'wp-photo-album-plus');
    ?>
</h2><br />

		<form action="<?php 
    echo wppa_dbg_url(get_admin_url() . 'admin.php?page=wppa_export_photos');
    ?>
" method="post">
			<?php 
    wp_nonce_field('$wppa_nonce', WPPA_NONCE);
    ?>
			<?php 
    echo sprintf(__('Photos will be exported to: <b>%s</b>.', 'wp-photo-album-plus'), WPPA_DEPOT);
    ?>
			<h2><?php 
    _e('Export photos from album <span style="font-size:12px;">(Including Album information)</span>:', 'wp-photo-album-plus');
    ?>
</h2>
			<?php 
    $albums = $wpdb->get_results("SELECT * FROM `" . WPPA_ALBUMS . "` " . wppa_get_album_order(), ARRAY_A);
    $high = '0';
    ?>
			
			<table class="form-table albumtable">
				<thead>
				</thead>
				<tbody>
				<tr>
					<?php 
    $ct = 0;
    foreach ($albums as $album) {
        $line = '&nbsp;' . $album['id'] . ':&nbsp;' . __(stripslashes($album['name']));
        if ($album['id'] > $high) {
            $high = $album['id'];
        }
        ?>
						<td>
							<input type="checkbox" name="album-<?php 
        echo $album['id'];
        ?>
" />&nbsp;<?php 
        echo $line;
        ?>
						</td>
						<?php 
        if ($ct == 4) {
            // Wrap to newline
            echo '</tr><tr>';
            $ct = 0;
        } else {
            $ct++;
        }
    }
    ?>
				</tr>
				</tbody>
			</table>
			<input type="hidden" name="high" value="<?php 
    echo $high;
    ?>
" />
			<p>
				<input type="submit" class="button-primary" name="wppa-export-submit" value="<?php 
    _e('Export', 'wp-photo-album-plus');
    ?>
" />
			</p>
		</form>
	</div>
<?php 
}
function wppa_album_sequence($parent)
{
    global $wpdb;
    // Get the albums
    $albumorder = wppa_get_album_order($parent);
    $is_descending = strpos($albumorder, 'DESC') !== false;
    $albums = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . WPPA_ALBUMS . '` WHERE `a_parent` = %s ' . $albumorder, $parent), ARRAY_A);
    // Anything to do here ?
    if (empty($albums)) {
        return;
    }
    // Check my access rights
    foreach ($albums as $album) {
        if (!wppa_have_access($album['id'])) {
            return;
        }
    }
    // Check album order
    if (!strpos($albumorder, 'a_order')) {
        if ($parent == '0') {
            echo '<br />';
            _e('You can edit top-level album sequence order here when you set the album order to "Order #" or "Order # desc" in Table IV-D1.');
        } else {
            _e('You can edit sub-album sequence order here when you set the album order to "Order #" or "Order # desc" in the "Sub album sort order:" selection box above.');
        }
        return;
    }
    echo '<h2>' . __('Manage album order', 'wp-photo-album-plus') . ' - ' . '<small>' . '<i>' . __('Change sequence order by drag and drop, or use the up/down arrows.', 'wp-photo-album-plus') . '</i>' . ' ' . __('Do not leave this page unless the bar is entirely green.', 'wp-photo-album-plus') . '</small>' . '</h2>';
    echo '<table>' . '<thead>' . '<tr>' . '<th>' . __('Color', 'wp-photo-album-plus') . '</th>' . '<th>' . __('Meaning', 'wp-photo-album-plus') . '</th>' . '</tr>' . '</thead>' . '<tbody>' . '<tr>' . '<td>' . '<div style="background-color:green;height:12px;" ></div>' . '</td>' . '<td>' . __('Up to date', 'wp-photo-album-plus') . '</td>' . '</tr>' . '<tr>' . '<td>' . '<div style="background-color:yellow;height:12px;" ></div>' . '</td>' . '<td>' . __('Updating', 'wp-photo-album-plus') . '</td>' . '</tr>' . '<tr>' . '<td>' . '<div style="background-color:orange;height:12px;" ></div>' . '</td>' . '<td>' . __('Needs update', 'wp-photo-album-plus') . '</td>' . '</tr>' . '<tr>' . '<td>' . '<div style="background-color:red;height:12px;" ></div>' . '</td>' . '<td>' . __('Error', 'wp-photo-album-plus') . '</td>' . '</tr>' . '</tbody>' . '</table>';
    ?>
		<style>
			.sortable-placeholder-albums {
				width: 100%;
				height: 60px;
				margin: 5px;
				border: 1px dotted #cccccc;
				border-radius:3px;
				float: left;
			}
			.ui-state-default-albums {
				position: relative;
				width: 100%;
				height: 60px;
				margin: 5px;
				border: 1px solid #cccccc;
				border-radius:3px;
				float: left;
			}
			.ui-state-default-albums td {
				padding:0;
				line-height:12px;
				text-align:center;
			}
		</style>
		<script>
			jQuery( function() {
				jQuery( "#sortable-albums" ).sortable( {
					cursor: 		"move",
					placeholder: 	"sortable-placeholder-albums",
					stop: 			function( event, ui ) { wppaDoRenumber(); }
				} );
			} );
			var wppaRenumberPending = false;
			var wppaAjaxInProgress 	= 0;

			function wppaDoRenumber() {

				// Busy?
				if ( wppaAjaxInProgress > 0 ) {
					wppaRenumberPending = true;
				}

				// Not busy
				else {
					_wppaDoRenumber();
				}
			}

			function _wppaDoRenumber() {

				// Init
				var ids = jQuery( ".wppa-sort-item-albums" );
				var seq = jQuery( ".wppa-sort-seqn-albums" );
				var descend = <?php 
    if ($is_descending) {
        echo 'true';
    } else {
        echo 'false';
    }
    ?>
;

				// Mark needs update
				var idx = 0;
				while ( idx < ids.length ) {
					var newvalue;
					if ( descend ) newvalue = ids.length - idx;
					else newvalue = idx + 1;
					var oldvalue = seq[idx].value;
					var album = ids[idx].value;
					if ( newvalue != oldvalue ) {
						jQuery( '#wppa-pb-'+idx ).css({backgroundColor:'orange'});
					}
					idx++;
				}

				// Process
				var idx = 0;
				while ( idx < ids.length ) {
					var newvalue;
					if ( descend ) newvalue = ids.length - idx;
					else newvalue = idx + 1;
					var oldvalue = seq[idx].value;
					var album = ids[idx].value;
					if ( newvalue != oldvalue ) {
						wppaDoSeqUpdateAlbum( album, newvalue );
						jQuery( '#wppa-pb-'+idx ).css({backgroundColor:'yellow'});
						wppaLastAlbum = album;
					}
					idx++;
				}
			}

			function wppaDoSeqUpdateAlbum( album, seqno ) {

				var data = 	'action=wppa' +
							'&wppa-action=update-album' +
							'&album-id=' + album +
							'&item=a_order' +
							'&wppa-nonce=' + document.getElementById( 'album-nonce-' + album ).value +
							'&value=' + seqno;
				var xmlhttp = new XMLHttpRequest();

				xmlhttp.onreadystatechange = function() {
					if ( xmlhttp.readyState == 4 && xmlhttp.status != 404 ) {
						var ArrValues = xmlhttp.responseText.split( "||" );
						if ( ArrValues[0] != '' ) {
							alert( 'The server returned unexpected output:\n' + ArrValues[0] );
						}
						switch ( ArrValues[1] ) {
							case '0':	// No error
								var i = seqno - 1;
								var descend = <?php 
    if ($is_descending) {
        echo 'true';
    } else {
        echo 'false';
    }
    ?>
;
								if ( descend ) {
									i = <?php 
    echo count($albums);
    ?>
 - seqno;
								}
								jQuery( '#wppa-album-seqno-' + album ).html( seqno );
								if ( wppaRenumberPending ) {
									jQuery( '#wppa-pb-'+i ).css({backgroundColor:'orange'});
								}
								else {
									jQuery( '#wppa-pb-'+i ).css({backgroundColor:'green'});
								}
								if ( wppaLastAlbum = album ) {
									wppaRenumberBusy = false;
								}
								break;
							default:	// Any error
								jQuery( '#wppa-album-seqno-' + album ).html( '<span style="color"red" >Err:' + ArrValues[1] + '</span>' );
								break;
						}
						wppaAjaxInProgress--;

						// No longer busy?
						if ( wppaAjaxInProgress == 0 ) {

							if ( wppaRenumberPending ) {

								// Redo
								wppaRenumberPending = false;
								wppaDoRenumber();
							}
						}
					}
				}
				xmlhttp.open( 'POST',wppaAjaxUrl,true );
				xmlhttp.setRequestHeader( "Content-type","application/x-www-form-urlencoded" );
				xmlhttp.send( data );
				wppaAjaxInProgress++;

				jQuery( "#wppa-sort-seqn-albums-" + album ).attr( 'value', seqno );	// set hidden value to new value to prevent duplicate action
				var spinnerhtml = '<img src="' + wppaImageDirectory + 'spinner.gif' + '" />';
				jQuery( '#wppa-album-seqno-' + album ).html( spinnerhtml );
			}
		</script>

		<br />

		<div id="wppa-progbar" style="width:100%;height:12px;" >
			<?php 
    $c = count($albums);
    $l = 100 / $c;
    $i = 0;
    while ($i < $c) {
        echo '<div' . ' id="wppa-pb-' . $i . '"' . ' style="display:inline;float:left;background-color:green;height:12px;width:' . $l . '%;"' . ' >' . '</div>';
        $i++;
    }
    ?>
		</div>

		<br />

		<div class="widefat" style="max-width:600px;" >
			<div id="sortable-albums">
				<?php 
    foreach ($albums as $album) {
        $cover_photo_id = wppa_get_coverphoto_id($album['id']);
        echo '
					<div' . ' id="albumitem-' . $album['id'] . '"' . ' class="ui-state-default-albums"' . ' style="background-color:#eeeeee;cursor:move;"' . ' >' . '<div' . ' style="height:100%;width:25%;float:left;text-align:center;overflow:hidden;" >';
        if (wppa_is_video($cover_photo_id)) {
            echo wppa_get_video_html(array('id' => $cover_photo_id, 'height' => '50', 'margin_top' => '5', 'margin_bottom' => '5', 'controls' => false));
        } else {
            echo '<img' . ' class="wppa-cover-image"' . ' src="' . wppa_fix_poster_ext(wppa_get_thumb_url(wppa_get_coverphoto_id($album['id'])), $cover_photo_id) . '"' . ' style="max-height:50px; margin: 5px;"' . ' />';
        }
        echo '</div>' . '<div style="height:100%;width:40%;float:left;font-size:12px;overflow:hidden;" >' . '<b>' . wppa_get_album_name($album['id']) . '</b>' . '<br />' . wppa_get_album_desc($album['id']) . '</div>' . '<div style="float:right;width:10%;" >' . '<table>' . '<tr><td>' . '<img' . ' src="' . wppa_get_imgdir('up.png') . '"' . ' title="' . esc_attr(__('To top', 'wp-photo-album-plus')) . '"' . ' style="cursor:pointer;"' . ' onclick="' . 'jQuery( \'#albumitem-' . $album['id'] . '\' ).parent().prepend(jQuery( \'#albumitem-' . $album['id'] . '\' ));' . 'wppaDoRenumber();' . '"' . ' />' . '</td></tr>' . '<tr><td>' . '<img' . ' src="' . wppa_get_imgdir('up.png') . '"' . ' title="' . esc_attr(__('One up', 'wp-photo-album-plus')) . '"' . ' style="cursor:pointer;width:24px;"' . ' onclick="' . 'jQuery( \'#albumitem-' . $album['id'] . '\' ).prev().before(jQuery( \'#albumitem-' . $album['id'] . '\' ));' . 'wppaDoRenumber();' . '"' . ' />' . '</td></tr>' . '<tr><td>' . '<img' . ' src="' . wppa_get_imgdir('down.png') . '"' . ' title="' . esc_attr(__('One down', 'wp-photo-album-plus')) . '"' . ' style="cursor:pointer;width:24px;"' . ' onclick="' . 'jQuery( \'#albumitem-' . $album['id'] . '\' ).next().after(jQuery( \'#albumitem-' . $album['id'] . '\' ));' . 'wppaDoRenumber();' . '"' . ' />' . '</td></tr>' . '<tr><td>' . '<img' . ' src="' . wppa_get_imgdir('down.png') . '"' . ' title="' . esc_attr(__('To bottom', 'wp-photo-album-plus')) . '"' . ' style="cursor:pointer;"' . ' onclick="' . 'jQuery( \'#albumitem-' . $album['id'] . '\' ).parent().append(jQuery( \'#albumitem-' . $album['id'] . '\' ));' . 'wppaDoRenumber();' . '"' . ' />' . '</td></tr>' . '</table>' . '</div>' . '<div style="float:right; width:25%;" >' . '<span style=""> ' . __('Id:', 'wp-photo-album-plus') . ' ' . $album['id'] . '</span>' . '<span style=""> - ' . __('Ord:', 'wp-photo-album-plus') . '</span>' . '<span id="wppa-album-seqno-' . $album['id'] . '" > ' . $album['a_order'] . '</span>' . '<br />' . '<a href="' . wppa_ea_url($album['id']) . '" style="position:absolute;bottom:0;" >' . __('Edit', 'wp-photo-album-plus') . '</a>' . '</div>' . '<input type="hidden" id="album-nonce-' . $album['id'] . '" value="' . wp_create_nonce('wppa_nonce_' . $album['id']) . '" />' . '<input type="hidden" class="wppa-sort-item-albums" value="' . $album['id'] . '" />' . '<input type="hidden" class="wppa-sort-seqn-albums" id="wppa-sort-seqn-albums-' . $album['id'] . '" value="' . $album['a_order'] . '" />' . '</div>';
    }
    ?>
			</div>
			<div style="clear:both;"></div>
		</div>
		<?php 
}
function wppa_album_sequence($parent)
{
    global $wpdb;
    // Get the albums
    $albumorder = wppa_get_album_order($parent);
    $is_descending = strpos($albumorder, 'DESC') !== false;
    $albums = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . WPPA_ALBUMS . '` WHERE `a_parent` = %s ' . $albumorder, $parent), ARRAY_A);
    // Anything to do here ?
    if (empty($albums)) {
        return;
    }
    // Check my access rights
    foreach ($albums as $album) {
        if (!wppa_have_access($album['id'])) {
            return;
        }
    }
    // Check album order
    if (!strpos($albumorder, 'a_order')) {
        if ($parent == '0') {
            echo '<br />';
            _e('You can edit top-level album sequence order here when you set the album order to "Order #" or "Order # desc" in Table IV-D1.');
        } else {
            _e('You can edit sub-album sequence order here when you set the album order to "Order #" or "Order # desc" in the "Sub album sort order:" selection box above.');
        }
        return;
    }
    echo '<h2>' . __('Manage album order', 'wp-photo-album-plus') . ' - ' . '<small><i>' . __('Change sequence order by drag and drop', 'wp-photo-album-plus') . '</i></small>' . '</h2>';
    ?>
		<style>
			.sortable-placeholder-albums {
				width: 100%;
				height: 60px;
				margin: 5px;
				border: 1px dotted #cccccc;
				border-radius:3px;
				float: left;
			}
			.ui-state-default-albums {
				position: relative;
				width: 100%;
				height: 60px;
				margin: 5px;
				border: 1px solid #cccccc;
				border-radius:3px;
				float: left;
			}
		</style>
		<script>
			jQuery( function() {
				jQuery( "#sortable-albums" ).sortable( {
					cursor: "move",
					placeholder: "sortable-placeholder-albums",
					stop: function( event, ui ) {
						var ids = jQuery( ".wppa-sort-item-albums" );
						var seq = jQuery( ".wppa-sort-seqn-albums" );
						var idx = 0;
						var descend = <?php 
    if ($is_descending) {
        echo 'true';
    } else {
        echo 'false';
    }
    ?>
;
						while ( idx < ids.length ) {
							var newvalue;
							if ( descend ) newvalue = ids.length - idx;
							else newvalue = idx + 1;
							var oldvalue = seq[idx].value;
							var album = ids[idx].value;
							if ( newvalue != oldvalue ) {
								wppaDoSeqUpdateAlbum( album, newvalue );
							}
							idx++;
						}
					}
				} );
			} );
			function wppaDoSeqUpdateAlbum( album, seqno ) {
				var data = 	'action=wppa' +
							'&wppa-action=update-album' +
							'&album-id=' + album +
							'&item=a_order' +
							'&wppa-nonce=' + document.getElementById( 'album-nonce-' + album ).value +
							'&value=' + seqno;
				var xmlhttp = new XMLHttpRequest();

				xmlhttp.onreadystatechange = function() {
					if ( xmlhttp.readyState == 4 && xmlhttp.status != 404 ) {
						var ArrValues = xmlhttp.responseText.split( "||" );
						if ( ArrValues[0] != '' ) {
							alert( 'The server returned unexpected output:\n' + ArrValues[0] );
						}
						switch ( ArrValues[1] ) {
							case '0':	// No error
								jQuery( '#wppa-album-seqno-' + album ).html( seqno );
								break;
							default:	// Any error
								jQuery( '#wppa-album-seqno-' + album ).html( '<span style="color"red" >Err:' + ArrValues[1] + '</span>' );
								break;
						}
					}
				}
				xmlhttp.open( 'POST',wppaAjaxUrl,true );
				xmlhttp.setRequestHeader( "Content-type","application/x-www-form-urlencoded" );
				xmlhttp.send( data );
				jQuery( "#wppa-sort-seqn-albums-" + album ).attr( 'value', seqno );	// set hidden value to new value to prevent duplicate action
				var spinnerhtml = '<img src="' + wppaImageDirectory + 'wpspin.gif' + '" />';
				jQuery( '#wppa-album-seqno-' + album ).html( spinnerhtml );
			}
		</script>

		<div class="widefat" style="max-width:500px;" >
			<div id="sortable-albums">
				<?php 
    foreach ($albums as $album) {
        echo '
					<div' . ' id="albumitem-' . $album['id'] . '"' . ' class="ui-state-default-albums"' . ' style="background-color:#eeeeee;cursor:move;"' . ' >' . '<div' . ' style="height:100%;width:25%;float:left;text-align:center;overflow:hidden;" >' . '<img' . ' class="wppa-cover-image"' . ' src="' . wppa_fix_poster_ext(wppa_get_thumb_url(wppa_get_coverphoto_id($album['id'])), wppa_get_coverphoto_id($album['id'])) . '"' . ' style="max-height:50px; margin: 5px;"' . ' />' . '</div>' . '<div style="height:100%;width:50%;float:left;font-size:12px;overflow:hidden;" >' . '<b>' . wppa_get_album_name($album['id']) . '</b>' . '<br />' . wppa_get_album_desc($album['id']) . '</div>' . '<div style="float:right; width:25%;" >' . '<span style=""> ' . __('Id:', 'wp-photo-album-plus') . ' ' . $album['id'] . '</span>' . '<span style=""> - ' . __('Ord:', 'wp-photo-album-plus') . '</span>' . '<span id="wppa-album-seqno-' . $album['id'] . '" > ' . $album['a_order'] . '</span>' . '<br />' . '<a href="' . wppa_ea_url($album['id']) . '" style="position:absolute;bottom:0;" >' . __('Edit', 'wp-photo-album-plus') . '</a>' . '</div>' . '<input type="hidden" id="album-nonce-' . $album['id'] . '" value="' . wp_create_nonce('wppa_nonce_' . $album['id']) . '" />' . '<input type="hidden" class="wppa-sort-item-albums" value="' . $album['id'] . '" />' . '<input type="hidden" class="wppa-sort-seqn-albums" id="wppa-sort-seqn-albums-' . $album['id'] . '" value="' . $album['a_order'] . '" />' . '</div>';
    }
    ?>
			</div>
			<div style="clear:both;"></div>
		</div>
		<?php 
}