public function fetch_categories($args) { # load categories if ($blog_filter = $this->include_exclude_blogs($args)) { $blog_filter = 'and category_blog_id ' . $blog_filter; } elseif (isset($args['blog_id'])) { $blog_filter = 'and category_blog_id = ' . intval($args['blog_id']); } if (isset($args['parent'])) { $parent = $args['parent']; if (is_array($parent)) { $parent_filter = 'and category_parent in (' . implode(',', $parent) . ')'; } else { $parent_filter = 'and category_parent = ' . intval($parent); } } if (isset($args['category_id'])) { if (isset($args['children'])) { if (isset($this->_cat_id_cache['c' . $args['category_id']])) { $cat = $this->_cat_id_cache['c' . $args['category_id']]; $children = $cat->children(); if (!empty($children)) { if ($children === false) { return null; } else { return $children; } } } $cat_filter = 'and category_parent = ' . intval($args['category_id']); } else { $cat_filter = 'and category_id = ' . intval($args['category_id']); $limit = 1; } } elseif (isset($args['label'])) { if (is_array($args['label'])) { $labels = ''; foreach ($args['label'] as $c) { if ($labels != '') { $labels .= ','; } $labels .= "'" . $this->escape($c) . "'"; } $cat_filter = 'and category_label in (' . $labels . ')'; } else { $cat_filter = 'and category_label = \'' . $this->escape($args['label']) . '\''; } } else { $limit = $args['lastn']; } if (isset($args['sort_order'])) { if ($args['sort_order'] == 'ascend') { $sort_order = 'asc'; } elseif ($args['sort_order'] == 'descend') { $sort_order = 'desc'; } } else { $sort_order = ''; } $sort_by = 'user_custom'; if (isset($args['sort_by'])) { $sort_by = strtolower($args['sort_by']); if ('user_custom' != $sort_by) { require_once 'class.mt_category.php'; $category_class = new Category(); if ($category_class->has_column('category_' . $sort_by)) { $tableInfo =& $category_class->TableInfo(); if ($tableInfo->flds['category_' . $sort_by]->type == "CLOB") { $sort_by = $this->decorate_column('category_' . $sort_by); } else { $sort_by = 'category_' . $sort_by; } } else { $sort_by = 'user_custom'; } } } $count_column = 'placement_id'; if ($args['show_empty']) { $join_clause = 'left outer join mt_placement on placement_category_id = category_id'; if (isset($args['entry_id'])) { $join_clause .= ' left outer join mt_entry on placement_entry_id = entry_id and entry_id = ' . intval($args['entry_id']); } else { $join_clause .= ' left outer join mt_entry on placement_entry_id = entry_id and entry_status = 2'; } $count_column = 'entry_id'; } else { $join_clause = ', mt_entry, mt_placement'; $cat_filter .= ' and placement_category_id = category_id'; if (isset($args['entry_id'])) { $entry_filter = ' and placement_entry_id = entry_id and placement_entry_id = ' . intval($args['entry_id']); } else { $entry_filter = ' and placement_entry_id = entry_id and entry_status = 2'; } } if (isset($args['class'])) { $class = $this->escape($args['class']); } else { $class = "category"; } $class_filter = " and category_class='{$class}'"; $sql = "\n select category_id, count({$count_column}) as category_count\n from mt_category {$join_clause}\n where 1 = 1\n {$cat_filter}\n {$entry_filter}\n {$blog_filter}\n {$parent_filter}\n {$class_filter}\n group by category_id\n "; if ($limit <= 0) { $limit = -1; } $categories = $this->db()->SelectLimit($sql, $limit, -1); if ($categories->EOF) { return null; } if (isset($args['children']) && isset($parent_cat)) { $parent_cat['_children'] =& $categories; } else { $ids = array(); $counts = array(); while (!$categories->EOF) { $ids[] = $categories->Fields('category_id'); $categories->MoveNext(); } $list = implode(",", $ids); require_once 'class.mt_category.php'; $category = new Category(); $base_sort = 'user_custom' == $sort_by ? 'category_label' : $sort_by; $where = "category_id in ({$list})\n order by {$base_sort} {$sort_order}"; $categories = $category->Find($where); if (count($categories) > 1 && 'user_custom' == $sort_by) { $mt = MT::get_instance(); $ctx = $mt->context(); $blog = $ctx->stash('blog'); $meta = $class . '_order'; try { $custom_order = $blog->{$meta}; if (!empty($custom_order)) { $order_list = preg_split('/\\s*,\\s*/', $custom_order); $cats = array(); foreach ($categories as $c) { if (in_array($c->id, $order_list)) { $key = array_search($c->id, $order_list); $cats[$key] = $c; } else { array_push($cats, $c); } } if ('desc' == $sort_order) { krsort($cats); } else { ksort($cats); } $categories = array_values($cats); } } catch (Exception $e) { } } else { } $id_list = array(); $top_cats = array(); $record_count = count($categories); for ($i = 0; $i < $record_count; $i++) { $cat = $categories[$i]; $cat_id = $cat->category_id; if (isset($args['top_level_categories']) || !isset($this->_cat_id_cache['c' . $cat_id])) { $id_list[] = $cat_id; $this->_cat_id_cache['c' . $cat_id] = $categories[$i]; } if (isset($args['top_level_categories'])) { $this->_cat_id_cache['c' . $cat_id]->children(false); } if ($cat->category_parent > 0) { $parent_id = $cat->category_parent; if (isset($this->_cat_id_cache['c' . $parent_id])) { if (isset($args['top_level_categories'])) { $parent = $this->fetch_category($parent_id); $children = $parent->children(); if (empty($children) || $children === false) { $parent->children(array(&$categories[$i])); } else { $parent->children($categories[$i]); } } } } if (!$cat->category_parent && isset($args['top_level_categories'])) { $top_cats[] = $categories[$i]; } } $this->cache_category_links($id_list); if (isset($args['top_level_categories'])) { return $top_cats; } } return $categories; }