id(int, AI)
parent(int)
priority(int)
name(int)
use Application\Mvc\Model\Model;
class Category extends Model
{
public static function categoryTreeArray(){
$result = self::find();
$arr = array();
$new = array();
foreach ($result as $a){
$arr[] = [
'id' => $a->getId(),
'name' => $a->getName(),
'parent' => $a->getParent(),
];
}
foreach ($arr as $a){
$new[$a['parent']][] = $a;
}
$tree = Category::createTree($new, array($arr[0]));
return $tree;
}
private function createTree(&$list, $parent){
$tree = array();
foreach ($parent as $k=>$l){
if(isset($list[$l['id']])){
$l['child'] = Category::createTree($list, $list[$l['id']]);
}
$tree[] = $l;
}
return $tree;
}
}
use Application\Mvc\Controller;
class CategoryController extends Controller
{
public function indexAction()
{
$this->helper->title($this->helper->at('Danh mục sản phẩm'), true);
$this->view->categoryForm = $this->categoryForm(Category::categoryTreeArray(),[]);
$this->view->categorySelect = $this->categorySelect(Category::categoryTreeArray());
}
private function categoryForm($categoryTreeArray,$categoryChecked,$html=''){
if (!is_array($html)){
$html = [];
}
if(count($categoryTreeArray) > 0){
$productForm = new ProductForm();
$html[] = '<ul>';
foreach ($categoryTreeArray as $item) {
if(array_key_exists($item['slug'],$categoryChecked)){
$html[] = '<li>'.$productForm->renderDecoratedValue($item['slug']).'</li>';
}else{
$html[] = '<li><div class="well well-sm"><div class="row">
<div class="col-md-8">'.$item['name'].'</div>
<div class="col-md-4 text-right"><a class="text-primary" href="javascript: void(0);">Chỉnh sửa</a> | <a class="text-danger" href="javascript: void(0);">Xóa</a></div>
</div></div>';
}
if(isset($item['child'])){
$html = $this->categoryForm($item['child'],$categoryChecked,$html);
}
$html[] = '</li>';
}
$html[] = '</ul>';
}
return $html;
}
private function categorySelect($categoryTreeArray,$html='',$prefix=''){
if (!is_array($html)){
$html = [];
}
if(count($categoryTreeArray) > 0){
foreach ($categoryTreeArray as $item) {
$html[] = '<option value="'.$item['id'].'">'.(!empty($prefix) ? $prefix.' ' : '').$item['name'].'</option>';
if(isset($item['child'])){
$prefix .= '----';
$html = $this->categorySelect($item['child'],$html,$prefix);
$prefix = str_replace($prefix, '----', '');
}
}
}
return $html;
}
}
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-lg-4">
<div class="well">
<form method="POST">
<div class="form-group">
<label>Thêm danh mục mới</label>
<input class="form-control" name="catName">
</div>
<div class="form-group">
<select class="form-control" name="parent">
<option value="-1">— Danh mục gốc —</option>
{% for cs in categorySelect %}
{{cs}}
{% endfor %}
</select>
<p class="help-block">Để trống nếu không có danh mục gốc</p>
</div>
<button type="submit" class="btn btn-success">Thêm danh mục</button>
</form>
</div>
</div>
<div class="col-lg-8">
<form method="POST">
<div class="panel panel-default">
<div class="panel-heading">
Cây danh mục
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<button type="submit" class="btn btn-success">Lưu thay đổi</button>
<hr>
<ul class="category-list-page">
{% for el in categoryForm %}
{{el}}
{% endfor %}
</ul>
</div>
</form>
</div>
</div>
</div>
</div>
<script src="{{ url.path() }}vendor/html5sortable/jquery.sortable.js"></script>
<script>
$('.category-list-page > ul > li > ul').sortable();
</script>