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\Repository\Searchable; |
10
|
|
|
|
11
|
|
|
class ContentRepository |
12
|
|
|
{ |
13
|
|
|
use Searchable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \App\Model\Admin\Model |
17
|
|
|
*/ |
18
|
|
|
protected static $model = null; |
19
|
|
|
|
20
|
|
|
public static function list($entity, $perPage, $condition = []) |
21
|
|
|
{ |
22
|
|
|
$data = self::$model->newQuery() |
23
|
|
|
->where(function ($query) use ($condition) { |
24
|
|
|
Searchable::buildQuery($query, $condition); |
25
|
|
|
}) |
26
|
|
|
->orderBy('id', 'desc') |
27
|
|
|
->paginate($perPage); |
28
|
|
|
$data->transform(function ($item) use ($entity) { |
29
|
|
|
xssFilter($item); |
30
|
|
|
$item->editUrl = route('admin::content.edit', ['id' => $item->id, 'entity' => $entity]); |
31
|
|
|
$item->deleteUrl = route('admin::content.delete', ['id' => $item->id, 'entity' => $entity]); |
32
|
|
|
$item->commentListUrl = route('admin::comment.index', ['content_id' => $item->id, 'entity_id' => $entity]); |
33
|
|
|
return $item; |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
return [ |
37
|
|
|
'code' => 0, |
38
|
|
|
'msg' => '', |
39
|
|
|
'count' => $data->total(), |
40
|
|
|
'data' => $data->items(), |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function add($data) |
45
|
|
|
{ |
46
|
|
|
return self::$model->setRawAttributes(self::processParams($data))->save(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function update($id, $data) |
50
|
|
|
{ |
51
|
|
|
return self::$model->newQuery()->where('id', $id)->update(self::processParams($data)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function find($id) |
55
|
|
|
{ |
56
|
|
|
return self::$model->newQuery()->find($id); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function findOrFail($id) |
60
|
|
|
{ |
61
|
|
|
return self::$model->newQuery()->findOrFail($id); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function delete($id) |
65
|
|
|
{ |
66
|
|
|
return self::$model->newQuery()->where('id', $id)->delete(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function setTable($table) |
70
|
|
|
{ |
71
|
|
|
self::$model = new Content(); |
72
|
|
|
return self::$model->setTable($table); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function model() |
76
|
|
|
{ |
77
|
|
|
return self::$model; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected static function processParams($data) |
81
|
|
|
{ |
82
|
|
|
return array_map(function ($item) { |
83
|
|
|
if (is_array($item)) { |
84
|
|
|
return implode(',', $item); |
85
|
|
|
} else { |
86
|
|
|
return $item; |
87
|
|
|
} |
88
|
|
|
}, $data); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public static function adjacent($id) |
92
|
|
|
{ |
93
|
|
|
return [ |
94
|
|
|
'previous' => self::$model->newQuery()->where('id', '<', $id)->first(), |
95
|
|
|
'next' => self::$model->newQuery()->where('id', '>', $id)->first() |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public static function paginate($perPage = 10) |
100
|
|
|
{ |
101
|
|
|
return self::$model->newQuery() |
102
|
|
|
->orderBy('id', 'desc') |
103
|
|
|
->paginate($perPage); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|