/** * 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 * @param array $album_set_array * @param integer $level Level being displayed * @param string $ident String to use as indentation for Categories * @return void **/ function get_subcat_data($parent, &$cat_data, &$album_set_array, $level, $ident = '') { global $CONFIG, $HIDE_USER_CAT, $FORBIDDEN_SET, $cpg_show_private_album; $album_filter = ''; $pic_filter = ''; if (!empty($FORBIDDEN_SET) && !$cpg_show_private_album) { $album_filter = ' and ' . str_replace('p.', 'a.', $FORBIDDEN_SET); $pic_filter = ' and ' . str_replace('p.', $CONFIG['TABLE_PICTURES'] . '.', $FORBIDDEN_SET); } if ($CONFIG['categories_alpha_sort'] == 1) { $cat_sort_order = 'name'; } else { $cat_sort_order = 'pos'; } $result = cpg_db_query("SELECT cid, name, description, thumb FROM {$CONFIG['TABLE_CATEGORIES']} WHERE parent = '{$parent}' ORDER BY {$cat_sort_order}"); if (mysql_num_rows($result) > 0) { $rowset = cpg_db_fetch_rowset($result); foreach ($rowset as $subcat) { if ($subcat['cid'] == USER_GAL_CAT) { $sql = "SELECT aid FROM {$CONFIG['TABLE_ALBUMS']} as a WHERE category>=" . FIRST_USER_CAT . $album_filter; $result = cpg_db_query($sql); $album_count = mysql_num_rows($result); while ($row = mysql_fetch_array($result)) { $album_set_array[] = $row['aid']; } // while mysql_free_result($result); $result = cpg_db_query("SELECT count(*) FROM {$CONFIG['TABLE_PICTURES']} as p, {$CONFIG['TABLE_ALBUMS']} as a WHERE p.aid = a.aid AND approved='YES' AND category >= " . FIRST_USER_CAT . $album_filter); $nbEnr = mysql_fetch_array($result); $pic_count = $nbEnr[0]; $subcat['description'] = preg_replace("/<br.*?>[\r\n]*/i", '<br />' . $ident, bb_decode($subcat['description'])); $link = $ident . "<a href=\"index.php?cat={$subcat['cid']}\">{$subcat['name']}</a>"; if ($album_count) { $cat_data[] = array($link, $ident . $subcat['description'], $album_count, $pic_count); $HIDE_USER_CAT = 0; } else { $HIDE_USER_CAT = 1; } } else { $unaliased_album_filter = str_replace('a.', '', $album_filter); $result = cpg_db_query("SELECT aid FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = {$subcat['cid']}" . $unaliased_album_filter); $album_count = mysql_num_rows($result); while ($row = mysql_fetch_array($result)) { $album_set_array[] = $row['aid']; } // while mysql_free_result($result); $result = cpg_db_query("SELECT count(*) FROM {$CONFIG['TABLE_PICTURES']} as p, {$CONFIG['TABLE_ALBUMS']} as a WHERE p.aid = a.aid AND approved='YES' AND category = {$subcat['cid']}" . $album_filter); $nbEnr = mysql_fetch_array($result); mysql_free_result($result); $pic_count = $nbEnr[0]; if ($subcat['thumb'] > 0) { $sql = "SELECT filepath, filename, url_prefix, pwidth, pheight " . "FROM {$CONFIG['TABLE_PICTURES']} " . "WHERE pid='{$subcat['thumb']}'" . $pic_filter; $result = cpg_db_query($sql); if (mysql_num_rows($result)) { $picture = mysql_fetch_array($result); mysql_free_result($result); $pic_url = get_pic_url($picture, 'thumb'); if (!is_image($picture['filename'])) { $image_info = cpg_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\" {$image_size['geom']} border=\"0\" alt=\"\" />"; $user_thumb = "<a href=\"index.php?cat={$subcat['cid']}\">" . $user_thumb . "</a>"; } } else { $user_thumb = ""; } $subcat['name'] = $subcat['name']; $subcat['description'] = preg_replace("/<br.*?>[\r\n]*/i", '<br />', bb_decode($subcat['description'])); $link = "<a href=\"index.php?cat={$subcat['cid']}\">{$subcat['name']}</a>"; $user_thumb = $ident . $user_thumb; if ($pic_count == 0 && $album_count == 0) { $user_thumb = $ident; $cat_data[] = array($link, $subcat['description'], 'cat_thumb' => $user_thumb); } else { // Check if you need to show subcat_level if ($level == $CONFIG['subcat_level']) { $cat_albums = list_cat_albums($subcat['cid']); } else { $cat_albums = ''; } $cat_data[] = array($link, $subcat['description'], $album_count, $pic_count, 'cat_albums' => $cat_albums, 'cat_thumb' => $user_thumb); } } if ($level > 1) { get_subcat_data($subcat['cid'], $cat_data, $album_set_array, $level - 1, $ident . "</td><td><img src=\"images/spacer.gif\" width=\"20\" height=\"1\" border=\"0\" alt=\"\" /></td><td>"); } } } }
/** * 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 }
function get_subcat_data($parent, &$cat_data, &$album_set_array, $level, $ident = '') { global $db, $CONFIG, $HIDE_USER_CAT, $CPG_M_DIR, $CPG_M_URL; $categories_data = get_categories_data(); $rowset = array(); for ($i = 0; $i < count($categories_data); $i++) { if ($categories_data[$i]['parent'] != $parent) { continue; } $rowset[] = $categories_data[$i]; } if (!empty($rowset)) { $datacount = init_cpg_count(); $albums_data = get_albums_data(); foreach ($rowset as $subcat) { $album_count = 0; if ($subcat['cid'] == USER_GAL_CAT) { for ($i = 0; $i < count($albums_data); $i++) { if ($albums_data[$i]['category'] < FIRST_USER_CAT) { continue; } $album_set_array[] = $albums_data[$i]['aid']; $album_count++; } list($pic_count) = $db->sql_ufetchrow("SELECT count(*) FROM {$CONFIG['TABLE_PICTURES']}, {$CONFIG['TABLE_ALBUMS']} WHERE {$CONFIG['TABLE_PICTURES']}.aid = {$CONFIG['TABLE_ALBUMS']}.aid AND category >= " . FIRST_USER_CAT, SQL_NUM); $subcat['description'] = preg_replace("/<br.*?>[\r\n]*/i", '<br />' . $ident, decode_bbcode($subcat['description'])); $link = $ident . '<a href="' . URL::index("{$_GET['name']}&cat={$subcat['cid']}") . '">' . $subcat['catname'] . '</a>'; // $link = $ident . "<a href=$CPG_M_URL&cat={$subcat['cid']}>{$subcat['catname']}</a>"; if ($album_count) { $cat_data[$subcat['cid']] = array($link, $ident . $subcat['description'], $album_count, $pic_count); $HIDE_USER_CAT = 0; } } else { for ($i = 0; $i < count($albums_data); $i++) { if ($albums_data[$i]['category'] >= FIRST_USER_CAT) { continue; } $album_set_array[] = $albums_data[$i]['aid']; } $pic_count = empty($datacount[$subcat['cid']]['pic_count']) ? 0 : $datacount[$subcat['cid']]['pic_count']; $album_count = empty($datacount[$subcat['cid']]['album_count']) ? 0 : $datacount[$subcat['cid']]['album_count']; $subcat['catname'] = $subcat['catname']; $subcat['description'] = preg_replace("/<br.*?>[\r\n]*/i", '<br />' . $ident, decode_bbcode($subcat['description'])); $link = $ident . '<a href="' . URL::index("{$_GET['name']}&cat={$subcat['cid']}") . '">' . $subcat['catname'] . '</a>'; $cat_albums = ''; if ($pic_count == 0 && $album_count == 0) { $cat_data[$subcat['cid']] = array($link, $ident . $subcat['description'], 0, 0); } else { // Check if you need to show subcat_level if ($level == 0 && $CONFIG['first_level']) { $cat_albums = list_cat_albums($subcat['cid']); } $cat_data[$subcat['cid']] = array($link, $ident . $subcat['description'], $album_count, $pic_count); } if ($level < $CONFIG['subcat_level']) { get_subcat_data($subcat['cid'], $cat_data, $album_set_array, $level + 1, $ident . '<img src="images/spacer.gif" width="20" height="1" />'); } if ($cat_albums) { $cat_data[$subcat['cid']]['cat_albums'] = $cat_albums; } } } } }