|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Eddy <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace App\Repository\Admin; |
|
7
|
|
|
|
|
8
|
|
|
use App\Model\Admin\Content; |
|
9
|
|
|
use App\Model\Admin\ContentTag; |
|
10
|
|
|
use App\Model\Admin\Entity; |
|
11
|
|
|
use App\Model\Admin\EntityField; |
|
12
|
|
|
use App\Repository\Searchable; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* 使用当前类时必须先调用 setTable 方法设置所要操作的数据库表 |
|
16
|
|
|
* @package App\Repository\Admin |
|
17
|
|
|
*/ |
|
18
|
|
|
class ContentRepository |
|
19
|
|
|
{ |
|
20
|
|
|
use Searchable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \App\Model\Admin\Model |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static $model = null; |
|
26
|
|
|
|
|
27
|
|
|
public static function list($entity, $perPage, $condition = []) |
|
28
|
|
|
{ |
|
29
|
|
|
$sortField = 'id'; |
|
30
|
|
|
$sortType = 'desc'; |
|
31
|
|
|
if (isset($condition['light_sort_fields'])) { |
|
32
|
|
|
$tmp = explode(',', $condition['light_sort_fields']); |
|
33
|
|
|
$sortField = isset($tmp[0]) && ($tmp[0] != '') ? $tmp[0] : $sortField; |
|
34
|
|
|
$sortType = isset($tmp[1]) && in_array($tmp[1], ['asc', 'desc'], true) ? $tmp[1] : $sortType; |
|
35
|
|
|
unset($condition['light_sort_fields']); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$data = self::$model->newQuery() |
|
39
|
|
|
->where(function ($query) use ($condition) { |
|
40
|
|
|
Searchable::buildQuery($query, $condition); |
|
41
|
|
|
}) |
|
42
|
|
|
->orderBy($sortField, $sortType) |
|
43
|
|
|
->paginate($perPage); |
|
44
|
|
|
$data->transform(function ($item) use ($entity) { |
|
45
|
|
|
xssFilter($item); |
|
46
|
|
|
$item->editUrl = route('admin::content.edit', ['id' => $item->id, 'entity' => $entity]); |
|
47
|
|
|
$item->deleteUrl = route('admin::content.delete', ['id' => $item->id, 'entity' => $entity]); |
|
48
|
|
|
$item->commentListUrl = route('admin::comment.index', ['content_id' => $item->id, 'entity_id' => $entity]); |
|
49
|
|
|
return $item; |
|
50
|
|
|
}); |
|
51
|
|
|
|
|
52
|
|
|
return [ |
|
53
|
|
|
'code' => 0, |
|
54
|
|
|
'msg' => '', |
|
55
|
|
|
'count' => $data->total(), |
|
56
|
|
|
'data' => $data->items(), |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public static function add($data, Entity $entity) |
|
61
|
|
|
{ |
|
62
|
|
|
self::$model->setRawAttributes(self::processParams($data, $entity))->save(); |
|
63
|
|
|
return self::$model; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function update($id, $data, Entity $entity) |
|
67
|
|
|
{ |
|
68
|
|
|
return self::$model->newQuery()->where('id', $id)->update(self::processParams($data, $entity)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public static function find($id) |
|
72
|
|
|
{ |
|
73
|
|
|
return self::$model->newQuery()->find($id); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public static function findOrFail($id) |
|
77
|
|
|
{ |
|
78
|
|
|
return self::$model->newQuery()->findOrFail($id); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public static function delete($id) |
|
82
|
|
|
{ |
|
83
|
|
|
return self::$model->newQuery()->where('id', $id)->delete(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public static function setTable($table) |
|
87
|
|
|
{ |
|
88
|
|
|
self::$model = new Content(); |
|
89
|
|
|
return self::$model->setTable($table); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public static function model() |
|
93
|
|
|
{ |
|
94
|
|
|
return self::$model; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected static function processParams($data, Entity $entity) |
|
98
|
|
|
{ |
|
99
|
|
|
return collect($data)->map(function ($item, $key) use ($entity) { |
|
100
|
|
|
if (is_array($item)) { |
|
101
|
|
|
return implode(',', $item); |
|
102
|
|
|
} elseif ($item === '' || preg_match('/^\d+(,\d+)*/', $item)) { |
|
103
|
|
|
// select多选类型表单,数据类型为 unsignedInteger 的求和保存,查询时可以利用 AND 运算查找对应值 |
|
104
|
|
|
$fieldType = EntityField::where('entity_id', $entity->id) |
|
105
|
|
|
->where('form_type', 'selectMulti') |
|
106
|
|
|
->where('name', $key)->value('type'); |
|
107
|
|
|
if ($fieldType == 'unsignedInteger') { |
|
|
|
|
|
|
108
|
|
|
return array_sum(explode(',', $item)); |
|
109
|
|
|
} |
|
110
|
|
|
return $item; |
|
111
|
|
|
} else { |
|
112
|
|
|
return $item; |
|
113
|
|
|
} |
|
114
|
|
|
})->toArray(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public static function adjacent($id) |
|
118
|
|
|
{ |
|
119
|
|
|
return [ |
|
120
|
|
|
'previous' => self::$model->newQuery()->where('id', '<', $id)->first(), |
|
121
|
|
|
'next' => self::$model->newQuery()->where('id', '>', $id)->first() |
|
122
|
|
|
]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public static function paginate($perPage = 10) |
|
126
|
|
|
{ |
|
127
|
|
|
return self::$model->newQuery() |
|
128
|
|
|
->orderBy('id', 'desc') |
|
129
|
|
|
->paginate($perPage); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public static function tags($entityId, $contentId) |
|
133
|
|
|
{ |
|
134
|
|
|
return ContentTag::query()->where('entity_id', $entityId)->where('content_id', $contentId) |
|
135
|
|
|
->leftJoin('tags', 'tags.id', '=', 'content_tags.tag_id') |
|
136
|
|
|
->get(['name', 'tag_id']); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public static function tagNames($entityId, $contentId) |
|
140
|
|
|
{ |
|
141
|
|
|
return self::tags($entityId, $contentId)->implode('name', ','); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|