function index() { list($params, $id, $slug) = $this->parse_params(func_get_args()); // Create or update if ($this->method != 'get') { $c = new Category(); switch ($this->method) { case 'post': case 'put': if ($this->method == 'put') { if (is_null($id)) { $this->error('403', 'Required parameter "id" not present.'); return; } // Update $c->get_by_id($id); if (!$c->exists()) { $this->error('404', "Category with ID: {$id} not found."); return; } } // Don't allow these fields to be saved generically $private = array('album_count', 'content_count', 'text_count'); foreach ($private as $p) { unset($_POST[$p]); } if (!$c->from_array($_POST, array(), true)) { // TODO: More info $this->error('500', 'Save failed.'); return; } $this->redirect("/categories/{$c->id}"); break; case 'delete': if (is_null($id)) { $this->error('403', 'Required parameter "id" not present.'); return; } else { if (is_numeric($id)) { $category = $c->get_by_id($id); $title = $category->title; if ($category->exists()) { $s = new Slug(); $this->db->query("DELETE FROM {$s->table} WHERE id = 'category.{$category->slug}'"); if (!$category->delete()) { // TODO: More info $this->error('500', 'Delete failed.'); return; } $id = null; } else { $this->error('404', "Category with ID: {$id} not found."); return; } } else { $id = explode(',', $id); $c->where_in('id', $id); $cats = $c->get_iterated(); foreach ($cats as $c) { if ($c->exists()) { $s = new Slug(); $this->db->query("DELETE FROM {$s->table} WHERE id = 'category.{$c->slug}'"); $c->delete(); } } } } exit; break; } } $c = new Category(); // No id, so we want a list if (is_null($id) && !$slug) { $final = $c->listing($params); } else { if (!is_null($id)) { $category = $c->get_by_id($id); } else { if ($slug) { $category = $c->where('slug', $slug)->get(); } } if ($category->exists()) { $options = array('page' => 1, 'limit' => 50); $options = array_merge($options, $params); $category_arr = $category->to_array($options); $options['category'] = $category->id; list($final, $counts) = $this->aggregate('category', $options); $prev = new Category(); $next = new Category(); $prev->where('title <', $category->title)->where_func('', array('@content_count', '+', '@text_count', '+', '@album_count', '>', 0), null)->order_by('title DESC, id DESC'); $next->where('title >', $category->title)->where_func('', array('@content_count', '+', '@text_count', '+', '@album_count', '>', 0), null)->order_by('title ASC, id ASC'); $max = $next->get_clone()->count(); $min = $prev->get_clone()->count(); $context = array('total' => $max + $min + 1, 'position' => $min + 1); $prev->limit(1)->get(); $next->limit(1)->get(); unset($options['category']); if ($prev->exists()) { $context['previous'] = array($prev->to_array($options)); } else { $context['previous'] = array(); } if ($next->exists()) { $context['next'] = array($next->to_array($options)); } else { $context['next'] = array(); } $final = array_merge($category_arr, $final); $final['counts'] = $counts; $final['context'] = $context; } else { $this->error('404', "Category with ID: {$id} not found."); return; } } $this->set_response_data($final); }