示例#1
0
 public function action_add()
 {
     $data = array();
     if (Request::$method == "POST") {
         $cat = new ProductCategory();
         $root_id = $_POST['parent_id'];
         $post = $cat->validate_create($_POST);
         if ($post->check()) {
             $post = $post->as_array();
             $cat->user_id = Auth::instance()->get_user()->id;
             $cat->name = $post['name'];
             $cat->slug = $post['slug'];
             $cat->meta_keys = $post['meta_keys'];
             $cat->meta_desc = $post['meta_desc'];
             $cat->save();
             //rebuil tree
             if ($root_id == 0) {
                 // if no parent was selected, add/move this node to be a new root in the tree
                 $categoryTree = ProductCategory::bll()->getTree();
                 $categoryTree->createRoot($cat);
             } else {
                 $parent = ProductCategory::bll()->findOneBy('id', $root_id);
                 $cat->getNode()->insertAsLastChildOf($parent);
             }
             Message::success('Thêm mới danh mục sản phẩm thành công!');
             Request::instance()->redirect('/admin/productcategory/index');
         } else {
             $data['errors'] = $post->errors();
             $_POST = $post->as_array();
         }
     }
     $this->template->title = __('Thêm mới danh mục sản phẩm');
     $this->template->section_title = __('Thêm mới danh mục sản phẩm');
     $q = ProductCategory::bll()->createQuery();
     $q->addOrderBy('root_id asc')->addOrderBy('lft asc');
     //echo $q->getSqlQuery();
     $objects = $q->execute();
     ///$trees[0] = ' --- ROOT ---';
     foreach ($objects as $object) {
         $prefix = '|';
         if ($object['level'] == 0) {
             $prefix = null;
         }
         $trees[$object['id']] = $prefix . ' ' . str_repeat('- ', $object['level'] * 2) . $object['name'];
     }
     $data['trees'] = $trees;
     $this->template->content = View::factory('admin/product/category/cat_add', $data);
 }