197 lines
6.5 KiB
PHP
Executable File
197 lines
6.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\common\model;
|
|
|
|
use think\Model;
|
|
use think\Request;
|
|
use think\Config;
|
|
|
|
class DownloadCategory extends Model {
|
|
|
|
use \app\common\traits\IndexModel;
|
|
|
|
public function getCategoryLists($where = null, $order = null, $field = null, $limit = 20, $level = 0, array &$alldata = array()) {
|
|
if ($level > 5) {
|
|
return;
|
|
}
|
|
if (is_array($where)) {
|
|
$where = array_merge(['stat' => 0], $where);
|
|
}
|
|
if ($where) {
|
|
$this->where($where);
|
|
}
|
|
if ($order) {
|
|
$this->order($order);
|
|
}
|
|
if ($field) {
|
|
$this->field($field);
|
|
}
|
|
if (empty($limit)) {
|
|
$limit = Config::get('list_rows');
|
|
$limit = $limit? : 12;
|
|
}
|
|
if ($limit) {
|
|
$this->limit($limit);
|
|
}
|
|
$list = $this->select();
|
|
if ($list) {
|
|
foreach ($list as $k => $row) {
|
|
$row['level'] = $level;
|
|
$alldata[] = $row;
|
|
$where['pid'] = $row['id'];
|
|
//self::getCategoryLists($where, $order, $field, $limit, $level + 1, $alldata);
|
|
!$row['haschild'] ? '' : self::getCategoryLists($where, $order, $field, $limit, $level + 1, $alldata);
|
|
}
|
|
}
|
|
return $alldata;
|
|
}
|
|
|
|
public function getChildIDArray($id) {
|
|
$list = $this->where(['pid' => $id, 'stat' => 0])->field(['id', 'haschild'])->select();
|
|
$childIDArray = array((int) $id);
|
|
if ($list) {
|
|
foreach ($list as $val) {
|
|
$childArray = $val['haschild'] ? self::getChildIDArray($val['id']) : array((int) $val['id']);
|
|
$childIDArray = array_merge($childIDArray, $childArray);
|
|
}
|
|
}
|
|
return $childIDArray;
|
|
}
|
|
|
|
public function getTopParentID($id, $where = null) {
|
|
$data = $this::get(function($query)use($id, $where) {
|
|
$query->where(['id' => $id]);
|
|
if ($where) {
|
|
$query->where($where);
|
|
}
|
|
$query->field(['id', 'pid']);
|
|
});
|
|
if (isset($data['pid']) && $data['pid']) {
|
|
$topid = self::getTopParentID($data['pid'], $where);
|
|
} else {
|
|
$topid = $id;
|
|
}
|
|
return $topid;
|
|
}
|
|
|
|
public function getBreadCrumb($id, $where = null, array &$catarr = array()) {
|
|
if (!$id) {
|
|
return array();
|
|
}
|
|
if (is_array($where)) {
|
|
$where = array_merge(['stat' => ['eq', '0']], $where);
|
|
}
|
|
if ($where) {
|
|
$this->where($where);
|
|
}
|
|
$data = $this->field(['id', 'name', 'pid'])->get($id);
|
|
$catarr[] = $data;
|
|
if (isset($data['pid']) && $data['pid']) {
|
|
self::getBreadCrumb($data['pid'], $where, $catarr);
|
|
} else {
|
|
return array_reverse($catarr);
|
|
}
|
|
return $catarr;
|
|
}
|
|
|
|
public function getCategoryOption($id = 0, $where = null, $order = null, $field = null, $limit = null) {
|
|
$options = '';
|
|
if (is_array($where)) {
|
|
$where = array_merge(['stat' => ['eq', '0']], $where);
|
|
}
|
|
if ($where) {
|
|
$this->where($where);
|
|
}
|
|
if ($order) {
|
|
$this->order($order);
|
|
}
|
|
if ($field) {
|
|
$this->field($field);
|
|
}
|
|
if ($limit) {
|
|
$this->limit($limit);
|
|
}
|
|
$list = $this->select();
|
|
if ($list) {
|
|
foreach ($list as $k => $row) {
|
|
if ($row['id'] == $id) {
|
|
$options.='<option value="' . $row['id'] . '" selected>' . $row['name'] . '</option>' . "\n";
|
|
} else {
|
|
$options.='<option value="' . $row['id'] . '">' . $row['name'] . '</option>' . "\n";
|
|
}
|
|
}
|
|
}
|
|
return $options;
|
|
}
|
|
|
|
public function getCategoryOptions($id = 0, $where = null, $order = null, $field = null, $limit = null, $level = 0) {
|
|
$options = '';
|
|
if (is_array($where)) {
|
|
$where = array_merge(['stat' => ['eq', '0']], $where);
|
|
}
|
|
if ($where) {
|
|
$this->where($where);
|
|
}
|
|
if ($order) {
|
|
$this->order($order);
|
|
}
|
|
if ($field) {
|
|
$this->field($field);
|
|
}
|
|
if (empty($limit)) {
|
|
$limit = Config::get('list_rows');
|
|
}
|
|
if ($limit) {
|
|
$this->limit($limit);
|
|
}
|
|
$list = $this->select();
|
|
if ($list) {
|
|
foreach ($list as $k => $row) {
|
|
if ($row['id'] == $id) {
|
|
$options.='<option value="' . $row['id'] . '" selected>' . ($level ? str_repeat('    ', $level) . '⌊' : '') . $row['name'] . '</option>' . "\n";
|
|
} else {
|
|
$options.='<option value="' . $row['id'] . '">' . ($level ? str_repeat('    ', $level) . '⌊' : '') . $row['name'] . '</option>' . "\n";
|
|
}
|
|
$where['pid'] = $row['id'];
|
|
//$options.=self::getCategoryOptions($id, $where, $order, $field, $limit, $level + 1);
|
|
$options.=!$row['haschild'] ? '' : self::getCategoryOptions($id, $where, $order, $field, $limit, $level + 1);
|
|
}
|
|
}
|
|
return $options;
|
|
}
|
|
|
|
public function getCategoryTree($where = null, $order = null, $field = null, $limit = 50, $level = 0) {
|
|
if (is_array($where)) {
|
|
$where = array_merge(['stat' => ['eq', '0']], $where);
|
|
}
|
|
$result = array();
|
|
if ($level > 5) {
|
|
return $result;
|
|
}
|
|
if ($where) {
|
|
$this->where($where);
|
|
}
|
|
if ($order) {
|
|
$this->order($order);
|
|
}
|
|
if ($field) {
|
|
$this->field($field);
|
|
}
|
|
if ($limit) {
|
|
$this->limit($limit);
|
|
}
|
|
$list = $this->select(); // 获取
|
|
if ($list) {
|
|
foreach ($list as $row) {
|
|
$row['level'] = $level;
|
|
$where['pid'] = $row['id'];
|
|
//$row['child'] = self::getCategoryTree($where, $order, $field, $limit, $level + 1);
|
|
$row['child'] = !$row['haschild'] ? array() : self::getCategoryTree($where, $order, $field, $limit, $level + 1);
|
|
$result[] = $row;
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
}
|