5) { return; } 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) { $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.='' . "\n"; } else { $options.='' . "\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.='' . "\n"; } else { $options.='' . "\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; } public function deleteRow($id, $stat = -1) { //$result = $this::destroy($id); $data = ['id' => $id, 'pid' => 0, 'haschild' => 0, 'stat' => $stat,]; $object = $this::update($data); return $object; } protected function setCreatetimeAttr($value, $data) { return time(); } protected function setContentAttr($content, $data) { if (!empty($content)) { $config = \HTMLPurifier_Config::createDefault(); $purifier = new \HTMLPurifier($config); $content = $purifier->purify($content); } return $content; } }