public function categoriesAction()
 {
     $Categories = new Model_Categories();
     if ($this->getRequest()->isXmlHttpRequest()) {
         $this->_helper->layout()->disableLayout();
         $this->_helper->viewRenderer->setNoRender(true);
         if (isset($_POST['CategoryTree'])) {
             $this->view->category_tree = $Categories->listCategoryTree();
             $this->view->orphan_categories = $Categories->listOrphanCategories();
             $asdf = $this->renderScript('admin/ajax/category_tree.phtml');
         }
         if (isset($_POST['Submit'])) {
             if (isset($_POST['CategoryID']) && !empty($_POST['CategoryID'])) {
                 #Update
                 $strUpdateCategoryName = "Update Category set Name = " . $Categories->db->quote($_POST['CategoryName']) . " where ID = {$_POST['CategoryID']} ";
                 $sqlUpdateCategoryName = $Categories->db->query($strUpdateCategoryName);
                 if ($sqlUpdateCategoryName) {
                     echo json_encode(array('true', $_POST['CategoryName'] . ' updated.'));
                 } else {
                     echo json_encode(array('false', 'Something went wrong. Please Reset and try again.'));
                 }
                 #if ($Categories->updateCategory($_POST)) {
                 #	echo $_POST['CategoryName'] . " updated!";
                 #} else {
                 #	echo "There was a problem processing your request. Please RESET and try again.";
                 #}
             } else {
                 #Create New
                 $succeed = $Categories->createCategory($_POST);
                 $allCats = array();
                 $all_categories = $Categories->getData('Category', '', ' CustomOrder ASC, Name ASC ');
                 foreach ($all_categories as $cat) {
                     $allCats[] = array($cat['ID'], $cat['Name']);
                 }
                 $succeed[] = $allCats;
                 echo json_encode($succeed);
             }
             exit;
         }
         if (isset($_POST['DeleteCategory'])) {
             $TreeID = isset($_POST['TreeID']) ? $_POST['TreeID'] : '0-0-0';
             $parent_child = explode('-', $TreeID);
             $ParentID = isset($parent_child[0]) ? $parent_child[0] : 0;
             $SubParentID = isset($parent_child[1]) ? $parent_child[1] : 0;
             $ChildID = isset($parent_child[2]) ? $parent_child[2] : 0;
             $sqlDelete = false;
             if ($ParentID != 0 && $SubParentID == 0 && $ChildID == 0) {
                 #parent
                 $sqlDelete = $Categories->db->query("delete from ParentChildCategories where ParentID = {$ParentID}");
             } else {
                 if ($ParentID != 0 && $SubParentID != 0 && $ChildID == 0) {
                     #subparent
                     $sqlDelete = $Categories->db->query("delete from ParentChildCategories where ParentID = {$ParentID} and SubParentID = {$SubParentID}");
                 } else {
                     if ($ParentID != 0 && $SubParentID != 0 && $ChildID != 0) {
                         #child
                         $sqlDelete = $Categories->db->query("delete from ParentChildCategories where ParentID = {$ParentID} and SubParentID = {$SubParentID} and ChildID = {$ChildID}");
                     } else {
                         #delete orphan category
                         $rootCat = 0;
                         if ($ChildID != 0) {
                             $rootCat = $ChildID;
                         } else {
                             if ($SubParentID != 0) {
                                 $rootCat = $SubParentID;
                             } else {
                                 if ($ParentID != 0) {
                                     $rootCat = $ParentID;
                                 }
                             }
                         }
                         $sqlDelete = $Categories->db->query("delete from Category where ID = {$rootCat}");
                     }
                 }
             }
             $retVal = array();
             $retVal['msg'] = $sqlDelete ? "Category deleted!" : "Something went wrong. Please Reset and try again.";
             $retVal['allCats'] = array();
             $all_categories = $Categories->getData('Category', '', ' CustomOrder ASC, Name ASC ');
             foreach ($all_categories as $cat) {
                 $retVal['allCats'][] = array($cat['ID'], $cat['Name']);
             }
             echo json_encode($retVal);
             exit;
         }
     } else {
         $this->view->all_categories = $Categories->getData('Category', '', ' CustomOrder ASC, Name ASC ');
     }
 }