Code:
public function list_all($clientID = false, $eventID = false, $category = false, $status = false, $featured = false, $start = 0, $limit = DEFAULT_PAGINATION_LIMIT)
{
if( (int) $clientID ) $this -> db -> where(array('c.id' => $clientID) );
if( (int) $status ) $this -> db -> where(array('a.status' => (int) $status) );
if( (bool) $featured ) $this -> db -> where(array('a.is_featured' => 1) );
$result = $this -> db
-> select('a.id album_id, a.title album_title, UNIX_TIMESTAMP(a.date) album_date, a.info album_info, a.client_1_id, a.client_2_id, a.hosted_by_client_id album_venue_id, a.status album_status, a.classification_id, a.is_featured, a.views album_visits, a.page_views album_views, a.tags album_tags, a.slug album_slug, a.status, cy.category album_category, cn.classification album_genre,
e.id event_id, e.title event_title, e.info event_info, e.client_id event_client_id, e.hosted_by_client_id event_venue_id, e.views event_views, e.genre event_genre, cnn.classification event_genre,
IF(e.day_of_the_week > 0, "regular", "special") event_type,
c.name client_name, c.type client_type, c.slug client_slug,
ct.name venue_name, ct.slug venue_slug,
CONCAT(u.first_name, " ", u.last_name) album_photographer, pr.id album_photographer_id,
(SELECT COUNT(p.id) FROM picture p WHERE p.album_id = a.id AND p.status = 1) album_pictures,
(SELECT COUNT(pc.id) FROM picture_comment pc WHERE pc.album_id = a.id AND pc.status = 1) album_comments,
(SELECT COUNT(pt.id) FROM picture_tag pt WHERE pt.album_id = a.id AND pt.status = 1) album_tags,
(SELECT p.file_path FROM picture p WHERE p.album_id = a.id LIMIT 1) path', false)
-> join('category cy', 'cy.id = a.category_id', 'left')
-> join('classification cn', 'cn.id = a.classification_id', 'left')
-> join('event e', 'e.id = a.event_id')
-> join('classification cnn', 'cnn.id = e.classification_id', 'left')
-> join('client c', 'c.id = e.client_id')
-> join('client ct', 'ct.id = e.hosted_by_client_id', 'left')
-> join('photographer pr', 'pr.id = a.photographer_id')
-> join('user u', 'u.id = pr.user_id')
-> limit($limit, $start)
-> order_by('a.date', 'desc')
-> order_by('a.sort', 'asc')
-> order_by('a.created_on', 'desc');
// No caching return raw result
if( ! DEFAULT_CACHE_STATUS ) return $result -> get('album a') -> result();
return return_cached($this -> db -> return_sql(), $result, 'result');
}