<?php
namespace Api\Model;
use Api\Model\BaseModel;
class CatalogModel extends BaseModel {
public function getList($item_id,$isGroup = false ){
if ($item_id > 0 ) {
$ret = $this->where(" item_id = '%d' ",array($item_id))->order(" s_number, cat_id asc ")->select();
}
if ($ret) {
foreach ($ret as $key => $value) {
$ret[$key]['addtime'] = date("Y-m-d H:i:s",$value['addtime']) ;
}
if ($isGroup) {
$ret2 = array() ;
foreach ($ret as $key => $value) {
if ($value['parent_cat_id']) {
}else{
$value['sub'] = $this->_getChlid($value['cat_id'],$ret);
$ret2[] = $value ;
}
}
$ret = $ret2 ;
}
return $ret ;
}else{
return array();
}
}
private function _getChlid($cat_id,$item_data){
$return = array() ;
if ($item_data && $cat_id) {
foreach ($item_data as $key => $value) {
if ($value['parent_cat_id'] == $cat_id ) {
$value['sub'] = $this->_getChlid($value['cat_id'],$item_data);
$return[] = $value ;
}
}
}
return $return;
}
public function getChlid($item_id,$cat_id){
$return = array() ;
$ret = $this->getList($item_id , true) ;
if ($ret) {
foreach ($ret as $key => $value) {
if ($value['cat_id'] == $cat_id) {
$return = $value['sub'] ;
}
if ($value['sub']) {
foreach ($value['sub'] as $key2 => $value2) {
if ($value2['cat_id'] == $cat_id) {
$return = $value2['sub'] ;
}
if ($value2['sub']) {
foreach ($value2['sub'] as $key3 => $value3) {
if ($value3['cat_id'] == $cat_id) {
$return = $value3['sub'] ;
}
}
}
}
}
}
}
return $return ;
}
public function getListByLevel($item_id , $level = 2){
$return = array() ;
$ret = $this->getList($item_id) ;
if ($ret) {
foreach ($ret as $key => $value) {
if ($value['level'] == $level) {
$return[] = $value ;
}
}
}
return $return ;
}
public function deleteCat($cat_id){
if (!$cat_id) {
return false;
}
$cat = $this->where(" parent_cat_id = '$cat_id' ")->find();
if ($cat) {
$this->deleteCat($cat['cat_id']);
}
$cat = $this->where(" cat_id = '$cat_id' ")->find();
$item_id = $cat['item_id'];
$all_pages = D("Page")->where("item_id = '$item_id' and is_del = 0 ")->field("page_id,cat_id")->select();
$pages = array() ;
if ($all_pages) {
foreach ($all_pages as $key => $value) {
if ($value['cat_id'] == $cat_id) {
$pages[] = $value ;
}
}
}
if ($pages) {
foreach ($pages as $key => $value) {
D("Page")->softDeletePage($value['page_id']);
}
}
$this->where(" cat_id = '$cat_id' ")->delete();
return true ;
}
}