Open index.php, find
foreach ($catdata['subalbums'] as $aid => $album) {
$approved = ' AND approved=\'YES\'';
$forbidden_set_string = ((count($FORBIDDEN_SET_DATA) > 0) ? ' AND aid NOT IN (' . implode(', ', $FORBIDDEN_SET_DATA) . ')' : '');
$keyword = ($album['keyword'] ? "AND (keywords like '%".addslashes($album['keyword'])."%' $forbidden_set_string)" : '');
if ($CONFIG['link_pic_count'] == 1 || $album['pic_count'] == 0) {
if (!empty($album['keyword'])) {
$query = "SELECT count(pid) AS link_pic_count, max(pid) AS link_last_pid "
." FROM {$CONFIG['TABLE_PICTURES']} "
." WHERE ((aid != '$aid' $forbidden_set_string) $keyword) $approved";
$result = cpg_db_query($query);
$link_stat = mysql_fetch_assoc($result);
mysql_free_result($result);
$album['link_pic_count'] = $link_stat['link_pic_count'];
$album['last_pid'] = !empty($album['last_pid']) && ($album['last_pid'] > $link_stat['link_last_pid']) ? $album['last_pid'] : $link_stat['link_last_pid'];
}
}
and replace with
$approved = ' AND approved=\'YES\'';
$forbidden_set_string = ((count($FORBIDDEN_SET_DATA) > 0) ? ' AND aid NOT IN (' . implode(', ', $FORBIDDEN_SET_DATA) . ')' : '');
$last_pids = array();
$last_pid_data = array();
foreach ($catdata['subalbums'] as $aid => $album) {
if ($CONFIG['link_pic_count'] == 1 || $album['pic_count'] == 0) {
if (!empty($album['keyword'])) {
$keyword = ($album['keyword'] ? "AND (keywords like '%".addslashes($album['keyword'])."%' $forbidden_set_string)" : '');
$query = "SELECT count(pid) AS link_pic_count, max(pid) AS link_last_pid "
." FROM {$CONFIG['TABLE_PICTURES']} "
." WHERE ((aid != '$aid' $forbidden_set_string) $keyword) $approved";
$result = cpg_db_query($query);
$link_stat = mysql_fetch_assoc($result);
mysql_free_result($result);
$catdata['subalbums'][$aid]['link_pic_count'] = $link_stat['link_pic_count'];
$catdata['subalbums'][$aid]['last_pid'] = !empty($album['last_pid']) && ($album['last_pid'] > $link_stat['link_last_pid']) ? $album['last_pid'] : $link_stat['link_last_pid'];
}
}
if ($catdata['subalbums'][$aid]['last_pid']) {
$last_pids[] = $catdata['subalbums'][$aid]['last_pid'];
}
if ($album['thumb'] > 0) {
$last_pids[] = $album['thumb'];
}
}
if (count($last_pids)) {
$result = cpg_db_query("SELECT pid, filepath, filename, url_prefix, pwidth, pheight FROM {$CONFIG['TABLE_PICTURES']} WHERE pid IN (".implode(',', $last_pids).")");
while ($row = mysql_fetch_assoc($result)) {
$last_pid_data[$row['pid']] = $row;
unset($last_pid_data[$row['pid']]['pid']);
}
mysql_free_result($result);
}
unset($last_pids);
foreach ($catdata['subalbums'] as $aid => $album) {
find
// Inserts a thumbnail if the album contains 1 or more images
if ($album['thumb'] > 0) {
$sql = "SELECT filepath, filename, url_prefix, pwidth, pheight "
." FROM {$CONFIG['TABLE_PICTURES']} WHERE pid='{$album['thumb']}'";
$result = cpg_db_query($sql);
$picture = mysql_fetch_assoc($result);
if (!is_array($picture)) {
$sql = "SELECT filepath, filename, url_prefix, pwidth, pheight "
. "FROM {$CONFIG['TABLE_PICTURES']} WHERE pid='{$album['last_pid']}'";
$result = cpg_db_query($sql);
$picture = mysql_fetch_assoc($result);
}
mysql_free_result($result);
and replace with
if (!empty($last_pid_data[$album['thumb']]['filename'])) {
$picture = $last_pid_data[$album['thumb']];
find
$sql = "SELECT filepath, filename, url_prefix, pwidth, pheight "
. "FROM {$CONFIG['TABLE_PICTURES']} WHERE pid='{$album['last_pid']}'";
$result = cpg_db_query($sql);
$picture = mysql_fetch_assoc($result);
mysql_free_result($result);
and replace with
$picture = $last_pid_data[$album['last_pid']];
As you maybe noticed I also optimized the query amount if you set a particular picture as album thumbnail (that's why we need to replace 3 blocks instead of 2 blocks).
Please report if everything works as expected. To test all changes, please enable "Show first level album thumbnails in categories" in the config, if not already done. Have a look at the query count and if any selected album thumbnail remains. Thank you.
If everything works as expected, I'll commit the changes to our SVN repository.