<?php $c = new Category(); foreach ($c->get_iterated() as $category) { $category->update_counts(); } $done = true;
function edit($id = FALSE) { if ($this->input->post()) { $e = new Event($id); $p = $this->input->post(); if ($this->input->post('smonth') && $this->input->post('sday') && $this->input->post('sday')) { $p['start'] = $this->input->post('syear') . '-' . $this->input->post('smonth') . '-' . $this->input->post('sday'); } if ($this->input->post('emonth') && $this->input->post('eday') && $this->input->post('eyear')) { $p['end'] = $this->input->post('eyear') . '-' . $this->input->post('emonth') . '-' . $this->input->post('eday'); } $e->from_array($p, array('category_id', 'title', 'start', 'end', 'cost', 'address', 'coords', 'description', 'end')); if ($e->save()) { $this->session->set_flashdata('msg', '<div class="alert alert-success"><a class="close" data-dismiss="alert">×</a> The event was succesfully edited.</div>'); redirect('events/index'); } else { $this->data['errors'] = $e->error->all; } } $e = new Event($id); list($syear, $smonth, $sday) = explode('-', $e->start); $this->data['sday'] = $sday; $this->data['smonth'] = $smonth; $this->data['syear'] = $syear; list($eyear, $emonth, $eday) = explode('-', $e->end); $this->data['eday'] = $eday; $this->data['emonth'] = $emonth; $this->data['eyear'] = $eyear; $this->data['e'] = $e; $c = new Category(); $c->get_iterated(); $this->data['categories'][''] = "Choose a category"; foreach ($c as $category) { $this->data['categories'][$category->id] = $category->name; } }
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); }