ContentRepository.php 3.3 KB
Newer Older
孙建华 已提交
1 2 3 4 5 6 7 8
<?php
/**
 * @author  Eddy <cumtsjh@163.com>
 */

namespace App\Repository\Admin;

use App\Model\Admin\Content;
E
eddy8 已提交
9
use App\Model\Admin\ContentTag;
孙建华 已提交
10 11
use App\Repository\Searchable;

E
eddy8 已提交
12 13 14 15
/**
 * 使用当前类时必须先调用 setTable 方法设置所要操作的数据库表
 * @package App\Repository\Admin
 */
孙建华 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
class ContentRepository
{
    use Searchable;

    /**
     * @var \App\Model\Admin\Model
     */
    protected static $model = null;

    public static function list($entity, $perPage, $condition = [])
    {
        $data = self::$model->newQuery()
            ->where(function ($query) use ($condition) {
                Searchable::buildQuery($query, $condition);
            })
            ->orderBy('id', 'desc')
            ->paginate($perPage);
        $data->transform(function ($item) use ($entity) {
孙建华 已提交
34
            xssFilter($item);
孙建华 已提交
35 36
            $item->editUrl = route('admin::content.edit', ['id' => $item->id, 'entity' => $entity]);
            $item->deleteUrl = route('admin::content.delete', ['id' => $item->id, 'entity' => $entity]);
孙建华 已提交
37
            $item->commentListUrl = route('admin::comment.index', ['content_id' => $item->id, 'entity_id' => $entity]);
孙建华 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50
            return $item;
        });

        return [
            'code' => 0,
            'msg' => '',
            'count' => $data->total(),
            'data' => $data->items(),
        ];
    }

    public static function add($data)
    {
E
eddy8 已提交
51 52
        self::$model->setRawAttributes(self::processParams($data))->save();
        return self::$model;
孙建华 已提交
53 54 55 56
    }

    public static function update($id, $data)
    {
孙建华 已提交
57
        return self::$model->newQuery()->where('id', $id)->update(self::processParams($data));
孙建华 已提交
58 59 60 61 62 63 64
    }

    public static function find($id)
    {
        return self::$model->newQuery()->find($id);
    }

孙建华 已提交
65 66 67 68 69
    public static function findOrFail($id)
    {
        return self::$model->newQuery()->findOrFail($id);
    }

孙建华 已提交
70 71 72 73 74 75 76 77 78 79
    public static function delete($id)
    {
        return self::$model->newQuery()->where('id', $id)->delete();
    }

    public static function setTable($table)
    {
        self::$model = new Content();
        return self::$model->setTable($table);
    }
孙建华 已提交
80

81 82 83 84 85
    public static function model()
    {
        return self::$model;
    }

孙建华 已提交
86 87 88 89 90 91 92 93 94 95
    protected static function processParams($data)
    {
        return array_map(function ($item) {
            if (is_array($item)) {
                return implode(',', $item);
            } else {
                return $item;
            }
        }, $data);
    }
孙建华 已提交
96 97 98 99 100 101 102 103

    public static function adjacent($id)
    {
        return [
            'previous' => self::$model->newQuery()->where('id', '<', $id)->first(),
            'next' => self::$model->newQuery()->where('id', '>', $id)->first()
        ];
    }
104 105 106 107 108 109 110

    public static function paginate($perPage = 10)
    {
        return self::$model->newQuery()
            ->orderBy('id', 'desc')
            ->paginate($perPage);
    }
E
eddy8 已提交
111 112 113 114 115 116 117 118 119 120 121 122

    public static function tags($entityId, $contentId)
    {
        return ContentTag::query()->where('entity_id', $entityId)->where('content_id', $contentId)
            ->leftJoin('tags', 'tags.id', '=', 'content_tags.tag_id')
            ->get(['name', 'tag_id']);
    }

    public static function tagNames($entityId, $contentId)
    {
        return self::tags($entityId, $contentId)->implode('name', ',');
    }
孙建华 已提交
123
}