public function action_get()
 {
     try {
         if (is_numeric($id_category = $this->request->param('id'))) {
             $cat = array();
             $category = new Model_Category($id_category);
             if ($category->loaded()) {
                 $cat = $category->as_array();
                 $cat['price'] = i18n::money_format($category->price);
                 $cat['parents'] = $category->get_parents_ids();
                 $cat['siblings'] = $category->get_siblings_ids();
                 $cat['customfields'] = Model_Field::get_by_category($category->id_category);
                 $cat['icon'] = $category->get_icon();
                 $this->rest_output(array('category' => $cat));
             } else {
                 $this->_error(__('Category not found'), 404);
             }
         } else {
             $this->_error(__('Category not found'), 404);
         }
     } catch (Kohana_HTTP_Exception $khe) {
         $this->_error($khe);
         return;
     }
 }
 /**
  * counts how many ads have each category
  * @param  boolean $location_filter filters by location 
  * @param  Model_Location $location 
  * @return array
  */
 public static function get_category_count($location_filter = TRUE, $location = NULL)
 {
     //cache by location
     if ($location_filter === TRUE and $location and $location->loaded()) {
         $id_location = $location->id_location;
     } elseif ($location_filter === TRUE and Model_Location::current()->loaded()) {
         $id_location = Model_Location::current()->id_location;
     } else {
         $id_location = 'all';
     }
     //name used in the cache for storage
     $cache_name = 'get_category_count_' . $id_location;
     if (($cats_count = Core::cache($cache_name)) === NULL) {
         $expr_date = is_numeric(core::config('advertisement.expire_date')) ? core::config('advertisement.expire_date') : 0;
         $db_prefix = Database::instance('default')->table_prefix();
         //get the categories that have ads id_category->num ads
         $count_ads = DB::select('c.id_category', array(DB::expr('COUNT("a.id_ad")'), 'count'))->from(array('categories', 'c'))->join(array('ads', 'a'))->using('id_category')->where('a.id_category', '=', DB::expr($db_prefix . 'c.id_category'))->where(DB::expr('IF(' . $expr_date . ' <> 0, DATE_ADD( published, INTERVAL ' . $expr_date . ' DAY), DATE_ADD( NOW(), INTERVAL 1 DAY))'), '>', Date::unix2mysql())->where('a.status', '=', Model_Ad::STATUS_PUBLISHED);
         //filter the count by location
         if ($location_filter === TRUE and $location and $location->loaded()) {
             $count_ads = $count_ads->where('a.id_location', 'in', $location->get_siblings_ids());
         } elseif ($location_filter === TRUE and Model_Location::current()->loaded()) {
             $count_ads = $count_ads->where('a.id_location', 'in', Model_Location::current()->get_siblings_ids());
         }
         $count_ads = $count_ads->group_by('c.id_category')->order_by('c.order', 'asc')->cached()->execute();
         $count_ads = $count_ads->as_array('id_category');
         //getting the count of ads into the parents
         $parents_count = array();
         foreach ($count_ads as $count_ad) {
             $id_category = $count_ad['id_category'];
             $count = $count_ad['count'];
             //adding himself if doesnt exists
             if (!isset($parents_count[$id_category])) {
                 $parents_count[$id_category] = $count_ad;
                 $parents_count[$id_category]['has_siblings'] = FALSE;
             }
             $category = new Model_Category($id_category);
             //for each parent of this category add the count
             $parents_ids = $category->get_parents_ids();
             if (count($parents_ids) > 0) {
                 foreach ($parents_ids as $id) {
                     if (isset($parents_count[$id])) {
                         $parents_count[$id]['count'] += $count_ads[$category->id_category]['count'];
                     } else {
                         $parents_count[$id]['count'] = $count_ads[$category->id_category]['count'];
                     }
                     $parents_count[$id]['has_siblings'] = TRUE;
                 }
             }
         }
         //get all the categories with level 0 and 1
         $categories = new self();
         $categories = $categories->where('id_category', '!=', 1)->where('parent_deep', 'IN', array(0, 1))->order_by('order', 'asc')->cached()->find_all();
         //generating the array
         $cats_count = array();
         foreach ($categories as $category) {
             $has_siblings = isset($parents_count[$category->id_category]) ? $parents_count[$category->id_category]['has_siblings'] : FALSE;
             //they may not have counted the siblings since the count was 0 but he actually has siblings...
             if ($has_siblings === FALSE and $category->has_siblings()) {
                 $has_siblings = TRUE;
             }
             $cats_count[$category->id_category] = $category->as_array();
             $cats_count[$category->id_category] = array('id_category' => $category->id_category, 'seoname' => $category->seoname, 'name' => $category->name, 'id_category_parent' => $category->id_category_parent, 'parent_deep' => $category->parent_deep, 'order' => $category->order, 'price' => $category->price, 'has_siblings' => $has_siblings, 'count' => isset($parents_count[$category->id_category]) ? $parents_count[$category->id_category]['count'] : 0);
         }
         //cache the result is expensive!
         Core::cache($cache_name, $cats_count);
     }
     return $cats_count;
 }