Files
orico-official-website-old/app/common/model/Singlepage.php
2024-10-29 14:04:59 +08:00

156 lines
4.9 KiB
PHP
Executable File

<?php
namespace app\common\model;
use think\Model;
use think\Request;
use think\Config;
class Singlepage extends Model {
use \app\common\traits\IndexModel;
public function getSinglepageLists($where = null, $order = null, $field = null, $limit = 20, $level = 0, array &$alldata = array()) {
if ($level > 3) {
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::getSinglepageLists($where, $order, $field, $limit, $level + 1, $alldata);
}
}
return $alldata;
}
public function getChildIDArray($id) {
$list = $this->where(['pid' => $id])->field('id')->select();
$childIDArray = array((int) $id);
if ($list) {
foreach ($list as $val) {
$childArray = self::getChildIDArray($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 getOption($id = 0, $where = null, $order = null, $field = null, $limit = 20) {
$options = '';
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 getOptions($id = 0, $where = null, $order = null, $field = null, $limit = null, $level = 0) {
$options = '';
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('&#160;&#160;&#160;&#160;', $level) . '&#8970;' : '') . $row['name'] . '</option>' . "\n";
} else {
$options.='<option value="' . $row['id'] . '">' . ($level ? str_repeat('&#160;&#160;&#160;&#160;', $level) . '&#8970;' : '') . $row['name'] . '</option>' . "\n";
}
$where['pid'] = $row['id'];
$options.=self::getOptions($id, $where, $order, $field, $limit, $level + 1);
}
}
return $options;
}
}