Example #1
0
/**
 * get_subcat_data()
 *
 * Get the data about the sub categories which are going to be shown on the index page, this function is called recursively
 *
 * @param integer $parent Parent Category
 * @param array $cat_data
 * @return void
 **/
function get_subcat_data(&$cat_data)
{
    global $CONFIG, $HIDE_USER_CAT, $cpg_show_private_album;
    global $lft, $rgt, $RESTRICTEDWHERE, $CURRENT_CAT_DEPTH, $FORBIDDEN_SET_DATA, $PAGE;
    $indent = "</td><td><img src=\"images/spacer.gif\" width=\"20\" height=\"1\" border=\"0\" alt=\"\" /></td><td>";
    // TODO: support private album icon data
    // just ignore the restriction and let the display code handle things
    // there can be no subcategories in user galleries, so don't even bother
    if ($CURRENT_CAT_DEPTH == -1) {
        return false;
    }
    function cpg_get_last_visible_cid($categories, $lft)
    {
        global $CONFIG, $CURRENT_CAT_DEPTH;
        static $lft_cid_map = array();
        if (array_key_exists($lft, $lft_cid_map)) {
            return $lft_cid_map[$lft];
        }
        foreach ($categories as $cid => $cat) {
            if ($cat['details']['level'] == $CURRENT_CAT_DEPTH + $CONFIG['subcat_level'] && $lft > $cat['details']['lft'] && $lft < $cat['details']['rgt']) {
                $lft_cid_map[$lft] = $cid;
                return $cid;
            }
        }
    }
    $categories = array();
    $forbidden_set = !$CONFIG['show_private'] && $FORBIDDEN_SET_DATA ? "AND r.aid NOT IN (" . implode(', ', $FORBIDDEN_SET_DATA) . ")" : "";
    $lft_rgt = $rgt ? "AND lft BETWEEN {$lft} AND {$rgt}" : "";
    //TODO: optimize this for when first level album thumbs are disabled
    // all we need then is a count
    // collect info about all normal categories
    // restrict to 'subcat_level' categories deeper than current depth
    $sql = "SELECT cid, lft, rgt, name, description, thumb, depth AS level, '0' AS alb_count, '0' AS subalb_count\n        FROM {$CONFIG['TABLE_CATEGORIES']}\n        WHERE depth BETWEEN {$CURRENT_CAT_DEPTH} + 1 AND {$CURRENT_CAT_DEPTH} + {$CONFIG['subcat_level']}\n        {$lft_rgt}\n        ORDER BY lft";
    $result = cpg_db_query($sql);
    while ($row = $result->fetchAssoc()) {
        $categories[$row['cid']]['details'] = $row;
        if ($row['cid'] == USER_GAL_CAT) {
            // collect stats for albums in the user galleries category
            // all we need here is the total number of albums and pictures
            $sql = "SELECT COUNT(DISTINCT(p.aid)) AS alb_count, COUNT(*) AS pic_count\n                FROM {$CONFIG['TABLE_ALBUMS']} AS r\n                INNER JOIN {$CONFIG['TABLE_PICTURES']} AS p ON p.aid = r.aid\n                WHERE r.category > " . FIRST_USER_CAT . "\n                AND approved = 'YES'\n                {$forbidden_set}";
            $result2 = cpg_db_query($sql);
            $row2 = $result2->fetchAssoc(true);
            $categories[$row['cid']]['details']['alb_count'] = $row2['alb_count'];
            $categories[$row['cid']]['subalbums'][0]['pic_count'] = $row2['pic_count'];
        }
    }
    // while
    $result->free();
    $sort_order = cpg_album_sort_order('r.');
    // collect album info and album counts
    $sql = "SELECT aid, title, r.description, keyword, alb_hits, category, visibility, r.thumb, r.owner, depth AS level, lft, '0' AS pic_count\n        FROM {$CONFIG['TABLE_CATEGORIES']} AS c\n        INNER JOIN {$CONFIG['TABLE_ALBUMS']} AS r ON r.category = c.cid\n        WHERE c.depth >= {$CURRENT_CAT_DEPTH} + 1\n        {$forbidden_set}\n        {$lft_rgt}\n        ORDER BY {$sort_order}";
    $result = cpg_db_query($sql);
    while ($row = $result->fetchAssoc()) {
        if ($row['level'] > $CURRENT_CAT_DEPTH + $CONFIG['subcat_level']) {
            // add album count for invisible sub-categories to last visible category
            $categories[cpg_get_last_visible_cid($categories, $row['lft'])]['details']['alb_count']++;
            $categories[cpg_get_last_visible_cid($categories, $row['lft'])]['details']['subalb_count']++;
        } else {
            $categories[$row['category']]['subalbums'][$row['aid']] = $row;
            $categories[$row['category']]['details']['alb_count']++;
        }
    }
    $result->free();
    // album stats for regular albums
    $sql = "SELECT c.cid, r.aid, COUNT(pid) AS pic_count, MAX(pid) AS last_pid, MAX(ctime) AS last_upload, depth AS level, lft\n        FROM {$CONFIG['TABLE_CATEGORIES']} AS c\n        INNER JOIN {$CONFIG['TABLE_ALBUMS']} AS r ON r.category = c.cid\n        INNER JOIN {$CONFIG['TABLE_PICTURES']} AS p ON p.aid = r.aid\n        WHERE c.depth >= {$CURRENT_CAT_DEPTH} + 1\n        AND approved = 'YES'\n        {$forbidden_set}\n        {$lft_rgt}\n        GROUP BY r.aid\n        ORDER BY NULL";
    $result = cpg_db_query($sql);
    while ($row = $result->fetchAssoc()) {
        if ($row['level'] > $CURRENT_CAT_DEPTH + $CONFIG['subcat_level']) {
            // add picture count for invisible sub-categories to last visible category
            $categories[cpg_get_last_visible_cid($categories, $row['lft'])]['subalbums'][-1]['pic_count'] += $row['pic_count'];
        } else {
            $categories[$row['cid']]['subalbums'][$row['aid']]['pic_count'] = $row['pic_count'];
            $categories[$row['cid']]['subalbums'][$row['aid']]['last_pid'] = $row['last_pid'];
            $categories[$row['cid']]['subalbums'][$row['aid']]['last_upload'] = $row['last_upload'];
        }
    }
    $result->free();
    foreach ($categories as $cid => $cat) {
        $level = $cat['details']['level'] - $CURRENT_CAT_DEPTH;
        if ($level == 0) {
            continue;
        }
        $album_count = $cat['details']['alb_count'];
        $pic_count = 0;
        if (!empty($cat['subalbums'])) {
            foreach ($cat['subalbums'] as $alb) {
                $pic_count += $alb['pic_count'];
            }
        }
        // Remove albums in not displayed sub categories from the output
        if (isset($cat['subalbums'][-1])) {
            unset($cat['subalbums'][-1]);
        }
        $cat['details']['alb_count'] -= $cat['details']['subalb_count'];
        if (!empty($cat['subalbums'])) {
            $cat['subalbums'] = array_slice_preserve_keys($cat['subalbums'], 0, $CONFIG['albums_per_page'], true);
        }
        $cat['details']['description'] = preg_replace("/<br.*?>[\r\n]*/i", '<br />', bb_decode($cat['details']['description']));
        if ($cat['details']['thumb'] > 0) {
            $sql = "SELECT filepath, filename, url_prefix, pwidth, pheight FROM {$CONFIG['TABLE_PICTURES']} AS p WHERE pid = {$cat['details']['thumb']} {$FORBIDDEN_SET}";
            $result = cpg_db_query($sql);
            if ($result->numRows()) {
                $picture = $result->fetchAssoc(true);
                $pic_url = get_pic_url($picture, 'thumb');
                if (!is_image($picture['filename'])) {
                    $image_info = getimagesize(urldecode($pic_url));
                    $picture['pwidth'] = $image_info[0];
                    $picture['pheight'] = $image_info[1];
                }
                $image_size = compute_img_size($picture['pwidth'], $picture['pheight'], $CONFIG['alb_list_thumb_size']);
                $user_thumb = "<img src=\"" . $pic_url . "\" class=\"image thumbnail\" {$image_size['geom']} border=\"0\" alt=\"\" />";
                $user_thumb = "<a href=\"index.php?cat={$cid}\">" . $user_thumb . "</a>";
            } else {
                $user_thumb = "";
            }
        } else {
            $user_thumb = "";
        }
        $link = "<a href=\"index.php?cat={$cid}\">{$cat['details']['name']}</a>";
        $user_thumb = str_repeat($indent, $level - 1) . $user_thumb;
        if ($cid == USER_GAL_CAT && $pic_count == 0) {
            $HIDE_USER_CAT = 1;
        } elseif ($pic_count == 0 && $album_count == 0) {
            $user_thumb = str_repeat($indent, $level - 1);
            $cat_data[] = array($link, $cat['details']['description'], 'cat_thumb' => $user_thumb);
        } else {
            // Check if you need to show first level album thumbnails
            if ($CONFIG['first_level'] && $cid != USER_GAL_CAT && $level <= $CONFIG['subcat_level']) {
                $cat_albums = list_cat_albums($cid, $cat);
            } else {
                $cat_albums = '';
            }
            $cat_data[] = array($link, $cat['details']['description'], $album_count, $pic_count, 'cat_albums' => $cat_albums, 'cat_thumb' => $user_thumb);
        }
    }
    // foreach categories
}
Example #2
0
function fb_display_album($content, $page_id)
{
    // turn off content filter so that <p> and <br> tags aren't added
    remove_filter('the_content', 'wpautop');
    // buffer the output
    ob_start();
    $albums_page_link = htmlentities(get_permalink(get_option('fb_albums_page')));
    $page_link = get_permalink($page_id);
    $album_id = fb_get_album_id($page_id);
    $album = fb_get_album($album_id);
    $photos = fb_get_photos($album_id);
    $photo_count = sizeof($photos);
    if ($photo_count == 0) {
        echo '<p>This album is empty.</p>';
        return false;
    }
    array_unshift($photos, '');
    // moves all the keys down
    unset($photos[0]);
    // check if page is hidden
    if ($album['hidden'] == 1) {
        $message = '<p>This album is not available. <a href="' . get_permalink(get_option('fb_albums_page')) . '">Return to albums</a>.</p>';
        return $message . $content;
    }
    // html encode all captions
    foreach ($photos as $key => $photo) {
        $photos[$key]['caption'] = function_exists('seems_utf8') && seems_utf8($photo['caption']) ? htmlentities($photo['caption'], ENT_QUOTES, 'utf-8') : htmlentities($photo['caption'], ENT_QUOTES);
    }
    $thumb_size = get_option('fb_thumb_size');
    $number_cols = get_option('fb_number_cols');
    $number_rows = get_option('fb_number_rows') == 0 ? ceil($photo_count / $number_cols) : get_option('fb_number_rows');
    $photos_per_page = $number_cols * $number_rows;
    $page_count = ceil($photo_count / $photos_per_page);
    $curr_page = $_GET['album_p'] <= $page_count && $_GET['album_p'] > 0 ? $_GET['album_p'] : 1;
    $first_photo = ($curr_page - 1) * $photos_per_page + 1;
    $last_photo = $first_photo + $photos_per_page - 1;
    $last_photo = $last_photo > $photo_count ? $photo_count : $last_photo;
    $rows_curr_page = ceil(($last_photo - $first_photo + 1) / $number_cols);
    // generate pagination
    if ($page_count == 1) {
        $prev_link = '';
        $next_link = '';
        $pagination = '&nbsp;';
    } else {
        $prev_link = $curr_page > 1 ? $curr_page - 1 : false;
        if ($prev_link !== false) {
            $prev_link = $page_link . (strstr($page_link, '?') ? '&amp;album_p=' . $prev_link : '?album_p=' . $prev_link);
        }
        $next_link = $curr_page < $page_count ? $curr_page + 1 : null;
        if ($next_link) {
            $next_link = $page_link . (strstr($page_link, '?') ? '&amp;album_p=' . $next_link : '?album_p=' . $next_link);
        }
        $pagination = '';
        for ($i = 1; $i <= $page_count; $i++) {
            if ($i == $curr_page) {
                $pagination .= '<b>' . $i . '</b>';
            } else {
                $link = $page_link . (strstr($page_link, '?') ? '&amp;album_p=' . $i : '?album_p=' . $i);
                $pagination .= "<a href='{$link}'>" . $i . "</a>";
            }
        }
    }
    // album info
    $description = $album['description'];
    $location = $album['location'];
    // add hidden links for all images before so that next and previous
    // buttons in lightbox will display these images as well
    $hidden_top = '';
    $hidden_bottom = '';
    for ($i = 1; $i < $first_photo; $i++) {
        $hidden_top .= "<a href=\"{$photos[$i]['src_big']}\" rel=\"fotobook\" title=\"{$photos[$i]['caption']}\"></a>";
    }
    for ($i = $last_photo + 1; $i <= $photo_count; $i++) {
        $hidden_bottom .= "<a href=\"{$photos[$i]['src_big']}\" rel=\"fotobook\" title=\"{$photos[$i]['caption']}\"></a>";
    }
    // now get rid of all photos in the array that aren't displayed on this page
    $photos = array_slice_preserve_keys($photos, $first_photo - 1, $photos_per_page);
    ?>
	<br />
	<p style="display: none"><?php 
    echo $hidden_top;
    ?>
</p>
	<?php 
    include FB_STYLE_PATH . 'album.php';
    ?>
	<p style="display: none"><?php 
    echo $hidden_bottom;
    ?>
</p>
<?php 
    $content .= ob_get_clean();
    return $content;
}