public function editpostAction()
 {
     if ($datas = $this->getRequest()->getPost()) {
         try {
             if (empty($datas['value_id'])) {
                 throw new Exception($this->_('An error occurred while saving the category. Please try again later.'));
             }
             $option_value = new Application_Model_Option_Value();
             $option_value->find($datas['value_id']);
             $html = array();
             $category = new Catalog_Model_Category();
             if (!empty($datas['category_id'])) {
                 $category->find($datas['category_id']);
             }
             $isNew = (bool) (!$category->getId());
             if ($category->getId() and $category->getValueId() != $option_value->getId()) {
                 throw new Exception($this->_('An error occurred while saving the category. Please try again later.'));
             }
             if (!isset($datas['is_active'])) {
                 $datas['is_active'] = 1;
             }
             $datas['value_id'] = $option_value->getId();
             if (!empty($datas['is_deleted']) and $category->getParentId()) {
                 foreach ($category->getProducts() as $product) {
                     $product->setCategoryId($category->getParentId())->save();
                 }
             }
             //                $category->setPosIds(!empty($datas['outlets']) ? $datas['outlets'] : array());
             $category->addData($datas);
             $category->save();
             $html = array('success' => 1, 'is_new' => (int) $isNew, 'parent_id' => $category->getParentId(), 'category_id' => $category->getId(), 'is_deleted' => $category->getIsDeleted() ? 1 : 0);
             if ($isNew) {
                 if ($category->getParentId()) {
                     $html['li_html'] = $this->getLayout()->addPartial('row', 'admin_view_default', 'catalog/application/edit/category/subcategory.phtml')->setCategory($category->getParent())->setSubcategory($category)->setOptionValue($option_value)->toHtml();
                 } else {
                     $html['category_html'] = $this->getLayout()->addPartial('row', 'admin_view_default', 'catalog/application/edit/category.phtml')->setCategory($category)->setOptionValue($option_value)->toHtml();
                 }
             }
         } catch (Exception $e) {
             $html['message'] = $e->getMessage();
         }
         $this->getLayout()->setHtml(Zend_Json::encode($html));
     }
     return $this;
 }
 public function findallAction()
 {
     if ($value_id = $this->getRequest()->getParam("value_id")) {
         try {
             $category = new Catalog_Model_Category();
             $categories = $category->findByValueId($value_id, null, true, true);
             $data = array("categories" => array());
             foreach ($categories as $category) {
                 $products = array();
                 foreach ($category->getProducts() as $product) {
                     $products[] = $this->_productToJson($product, $value_id);
                 }
                 usort($products, array($this, "_sortProducts"));
                 $category_data = array("id" => $category->getId(), "name" => $category->getName());
                 $children = $category->getChildren();
                 if ($children->count()) {
                     foreach ($category->getChildren() as $child) {
                         $child_products = array();
                         foreach ($child->getProducts() as $product) {
                             $child_products[] = $products[] = $this->_productToJson($product, $value_id);
                         }
                         usort($child_products, array($this, "_sortProducts"));
                         $category_data["children"][] = array("id" => $child->getId(), "name" => $child->getName(), "collection" => $child_products);
                     }
                     array_unshift($category_data["children"], array("id" => $child->getId(), "name" => $this->_("%s - All", $category->getName()), "collection" => $products));
                 } else {
                     $category_data["collection"] = $products;
                 }
                 $data["categories"][] = $category_data;
             }
             $data["page_title"] = $this->getCurrentOptionValue()->getTabbarName();
             $data["header_right_button"]["picto_url"] = $this->_getColorizedImage($this->_getImage('pictos/more.png', true), $this->getApplication()->getBlock('subheader')->getColor());
         } catch (Exception $e) {
             $data = array('error' => 1, 'message' => $e->getMessage());
         }
         $this->_sendHtml($data);
     }
 }