コード例 #1
0
 /**
  * Return children of specified category
  * 
  * @retunr string Children data in JSON format
  */
 public function get_children()
 {
     header('Content-Type: application/json');
     try {
         $categoryId = $this->input->get('id');
         if (false === $categoryId) {
             throw new Exception('Parent category not specified');
         }
         $categoryModel = new Sppc_CategoryModel();
         $category = $categoryModel->findObjectById($categoryId);
         if (is_null($category)) {
             throw new Exception('Specified category not found');
         }
         $children = $category->getChildCategories();
         $response = array();
         foreach ($children as $child) {
             $childCategory = array('attributes' => array('id' => 'category_' . $child->getId()), 'data' => $child->getName());
             if ($child->hasChildren()) {
                 $childCategory['state'] = 'closed';
             }
             $response[] = $childCategory;
         }
         echo json_encode($response);
     } catch (Exception $e) {
         echo json_encode(array());
     }
 }
コード例 #2
0
 /**
  * Отправка описания выбранной категории в формате JSON.  
  *
  */
 public function ajax_get_category_details()
 {
     $categoryDetails = new stdClass();
     $categoryDetails->id_category = 0;
     $categoryDetails->sites_in_cat = 0;
     $categoryDetails->sites_in_cat_and_subcat = 0;
     try {
         $categoryId = $this->input->post('id_category');
         if (false === $categoryId) {
             throw new Sppc_Exception('Category not specified');
         }
         $categoryModel = new Sppc_CategoryModel();
         $category = $categoryModel->findObjectById($categoryId);
         if (is_null($category)) {
             throw new Sppc_Exception('Specified category not found');
         }
         $categoryDetails->id_category = $category->getId();
         $categoryDetails->description = $category->getDescription();
         $siteModel = new Sppc_SiteModel();
         $searchFilter = new Sppc_Site_SearchFilter();
         $searchFilter->setHasCpc(true)->setCategories($category);
         $categoryDetails->sites_in_cat = $siteModel->getCount($searchFilter);
         $categories = array($category);
         $childs = $category->getChildCategories(true);
         $categories = array_merge($categories, $childs);
         $searchFilter->setCategories($categories);
         $categoryDetails->sites_in_cat_and_subcat = $siteModel->getCount($searchFilter);
         echo json_encode(array('message' => 'OK', 'category_details' => $categoryDetails));
     } catch (Exception $e) {
         echo json_encode(array('message' => $e->getMessage(), 'category_details' => $categoryDetails));
     }
 }