Example #1
0
 /**
  * Handle GET requests.
  */
 public function action_index()
 {
     try {
         if (is_numeric($this->request->param('id'))) {
             $this->action_get();
         } else {
             $output = array();
             $cats = new Model_Category();
             //make a search with q? param
             if (isset($this->_params['q']) and strlen($this->_params['q'])) {
                 $cats->where_open()->where('name', 'like', '%' . $this->_params['q'] . '%')->or_where('description', 'like', '%' . $this->_params['q'] . '%')->where_close();
             }
             //filter results by param, verify field exists and has a value and sort the results
             $cats->api_filter($this->_filter_params)->api_sort($this->_sort);
             //how many? used in header X-Total-Count
             $count = $cats->count_all();
             $cats = $cats->cached()->find_all();
             //as array
             foreach ($cats as $category) {
                 $cat = $category->as_array();
                 //$cat['parents']  = $category->get_parents_ids();
                 //$cat['siblings'] = $category->get_siblings_ids();
                 $cat['icon'] = $category->get_icon();
                 $cat['price'] = i18n::money_format($category->price);
                 $output[] = $cat;
             }
             $this->rest_output(array('categories' => $output), 200, $count);
         }
     } catch (Kohana_HTTP_Exception $khe) {
         $this->_error($khe);
         return;
     }
 }