/**
  * Render Custom Taxonomies Archives pages.
  * 
  * This method is a bit complex because it can handle a couple of
  * things. If a letter param is set, will get the list of terms
  * starting with that letter, plus sorting/pagination options.
  * 
  * If no letter is set, simply render a paginated list of all
  * taxonomy' terms.
  * 
  * @since    2.1
  * 
  * @param    string    $taxonomy Taxonomy slug
  * 
  * @return   string    HTML markup
  */
 public static function taxonomy_archives($taxonomy)
 {
     global $wpdb;
     $term_title = '';
     if ('collection' == $taxonomy) {
         $term_title = __('View all movies from collection « %s »', 'wpmovielibrary');
     } else {
         if ('genre' == $taxonomy) {
             $term_title = __('View all « %s » movies', 'wpmovielibrary');
         } else {
             if ('actor' == $taxonomy) {
                 $term_title = __('View all movies staring « %s »', 'wpmovielibrary');
             }
         }
     }
     global $wp_query;
     $params = self::parse_terms_query_vars($wp_query->query);
     // Allow URL params to override settings
     $vars = array('number', 'orderby', 'letter');
     foreach ($vars as $var) {
         $params[$var] = get_query_var($var, $params[$var]);
     }
     $name = WPMOLY_Cache::wpmoly_cache_name("{$taxonomy}_archive");
     $content = WPMOLY_Cache::output($name, function () use($wpdb, $taxonomy, $term_title, $params) {
         $has_menu = wpmoly_o('tax-archives-menu', $default = true);
         $hide_empty = wpmoly_o('tax-archives-hide-empty', $default = true);
         extract($params);
         $_orderby = 't.name';
         if ('count' == $orderby) {
             $_orderby = 'tt.count';
         }
         // Limit the maximum number of terms to get
         $number = min($number, wpmoly_o('tax-archives-terms-limit', $default = true));
         if (!$number) {
             $number = wpmoly_o('tax-archives-terms-per-page', $default = true);
         }
         // Calculate offset
         $offset = 0;
         if ($paged) {
             $offset = $number * ($paged - 1);
         }
         $limit = sprintf('LIMIT %d,%d', $offset, $number);
         $where = '';
         if ('0' != $hide_empty) {
             $where = 'tt.count > 0 AND';
         }
         // This is actually a hard rewriting of get_terms()
         // to get exactly what we want without getting into
         // trouble with multiple filters and stuff.
         if ('' != $letter) {
             $like = wpmoly_esc_like($letter) . '%';
             $query = "SELECT SQL_CALC_FOUND_ROWS t.*, tt.*\n\t\t\t\t\t\t    FROM {$wpdb->terms} AS t\n\t\t\t\t\t\t   INNER JOIN {$wpdb->term_taxonomy} AS tt\n\t\t\t\t\t\t      ON t.term_id = tt.term_id\n\t\t\t\t\t\t   WHERE {$where} tt.taxonomy = %s\n\t\t\t\t\t\t     AND t.name LIKE %s\n\t\t\t\t\t\t   ORDER BY {$_orderby} {$order}\n\t\t\t\t\t\t   {$limit}";
             $query = $wpdb->prepare($query, $taxonomy, $like);
             $terms = $wpdb->get_results($query);
         } else {
             $query = "SELECT SQL_CALC_FOUND_ROWS t.*, tt.*\n\t\t\t\t\t\t    FROM {$wpdb->terms} AS t\n\t\t\t\t\t\t   INNER JOIN {$wpdb->term_taxonomy} AS tt\n\t\t\t\t\t\t      ON t.term_id = tt.term_id\n\t\t\t\t\t\t   WHERE {$where} tt.taxonomy = %s\n\t\t\t\t\t\t   ORDER BY {$_orderby} {$order}\n\t\t\t\t\t\t   {$limit}";
             $query = $wpdb->prepare($query, $taxonomy);
             $terms = $wpdb->get_results($query);
         }
         $total = $wpdb->get_var('SELECT FOUND_ROWS() AS total');
         $terms = apply_filters('get_terms', $terms, (array) $taxonomy, array());
         $links = array();
         // Setting up the terms list...
         if (is_wp_error($terms)) {
             $links = $terms;
         } else {
             foreach ($terms as $term) {
                 $links[] = array('url' => get_term_link($term), 'attr_title' => sprintf($term_title, $term->name), 'title' => $term->name, 'count' => sprintf(_n('%d movie', '%d movies', $term->count, 'wpmovielibrary'), $term->count));
             }
         }
         // ... the main menu...
         $menu = '';
         if ($has_menu) {
             $args = compact('order', 'orderby', 'number', 'letter');
             // PHP 5.3
             $menu = WPMOLY_Archives::taxonomy_archive_menu($taxonomy, $args);
         }
         $args['letter'] = $letter;
         $args['baseurl'] = get_permalink();
         $url = WPMOLY_Utils::build_meta_permalink($args);
         global $wp_rewrite;
         $format = '/page/%#%';
         if ('' == $wp_rewrite->permalink_structure) {
             $format = '&paged=%#%';
         }
         // ... and the pagination menu.
         $args = array('type' => 'list', 'total' => ceil(($total - 1) / $number), 'current' => max(1, $paged), 'format' => $url . $format);
         $pagination = WPMOLY_Utils::paginate_links($args);
         $pagination = '<div id="wpmoly-movies-pagination">' . $pagination . '</div>';
         $attributes = array('taxonomy' => $taxonomy, 'links' => $links);
         $content = WPMovieLibrary::render_template('archives/archives.php', $attributes, $require = 'always');
         $content = $menu . $content . $pagination;
         return $content;
     });
     return $content;
 }