123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace addons\blog\model;
- use think\Model;
- /**
- * 区块模型
- */
- class Block extends Model
- {
- protected $name = "blog_block";
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = '';
- protected $updateTime = '';
- // 追加属性
- protected $append = [
- ];
- protected static $config = [];
- protected static function init()
- {
- $config = get_addon_config('blog');
- self::$config = $config;
- }
- public function getImageAttr($value, $data)
- {
- $value = $value ? $value : '/assets/addons/blog/img/thumb.png';
- return cdnurl($value);
- }
- /**
- * 获取区块列表
- * @param $params
- * @return false|\PDOStatement|string|\think\Collection
- */
- public static function getBlockList($params)
- {
- $name = empty($params['name']) ? '' : $params['name'];
- $condition = empty($params['condition']) ? '' : $params['condition'];
- $field = empty($params['field']) ? '*' : $params['field'];
- $row = empty($params['row']) ? 10 : (int)$params['row'];
- $orderby = empty($params['orderby']) ? 'id' : $params['orderby'];
- $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
- $limit = empty($params['limit']) ? $row : $params['limit'];
- $cache = !isset($params['cache']) ? true : (int)$params['cache'];
- $imgwidth = empty($params['imgwidth']) ? '' : $params['imgwidth'];
- $imgheight = empty($params['imgheight']) ? '' : $params['imgheight'];
- $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
- $cache = !$cache ? false : $cache;
- $where = ['status' => 'normal'];
- if ($name !== '') {
- $where['name'] = $name;
- }
- $order = $orderby == 'rand' ? 'rand()' : (in_array($orderby, ['name', 'id', 'createtime', 'updatetime', 'weigh']) ? "{$orderby} {$orderway}" : "id {$orderway}");
- $list = self::where($where)
- ->where($condition)
- ->field($field)
- ->order($order)
- ->limit($limit)
- ->cache($cache)
- ->select();
- self::render($list, $imgwidth, $imgheight);
- return $list;
- }
- public static function render(&$list, $imgwidth, $imgheight)
- {
- $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
- $height = $imgheight ? 'height="' . $imgheight . '"' : '';
- foreach ($list as $k => &$v) {
- $v['hasimage'] = $v->getData('image') ? true : false;
- $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['title'] . '</a>';
- $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' /></a>';
- $v['img'] = '<img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' />';
- }
- return $list;
- }
- public static function getBlockContent($params)
- {
- $field = isset($params['id']) ? 'id' : 'name';
- $value = isset($params[$field]) ? $params[$field] : '';
- $cache = !isset($params['cache']) ? true : (int)$params['cache'];
- $row = self::where($field, $value)
- ->where('status', 'normal')
- ->cache($cache)
- ->find();
- $result = '';
- if ($row) {
- if ($row['content']) {
- $result = $row['content'];
- } elseif ($row['image']) {
- $result = '<img src="' . $row['image'] . '" class="img-responsive"/>';
- } else {
- $result = $row['title'];
- }
- if ($row['url'] && !$row['content']) {
- $result = $row['url'] ? '<a href="' . (preg_match("/^https?:\/\/(.*)/i", $row['url']) ? $row['url'] : url($row['url'])) . '">' . $result . '</a>' : $result;
- }
- }
- return $result;
- }
- }
|