|
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
|
|
|
return $item; |
|
33
|
|
|
}); |
|
34
|
|
|
|
|
35
|
|
|
return [ |
|
36
|
|
|
'code' => 0, |
|
37
|
|
|
'msg' => '', |
|
38
|
|
|
'count' => $data->total(), |
|
39
|
|
|
'data' => $data->items(), |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function add($data) |
|
44
|
|
|
{ |
|
45
|
|
|
return self::$model->setRawAttributes(self::processParams($data))->save(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function update($id, $data) |
|
49
|
|
|
{ |
|
50
|
|
|
return self::$model->newQuery()->where('id', $id)->update(self::processParams($data)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function find($id) |
|
54
|
|
|
{ |
|
55
|
|
|
return self::$model->newQuery()->find($id); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function findOrFail($id) |
|
59
|
|
|
{ |
|
60
|
|
|
return self::$model->newQuery()->findOrFail($id); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function delete($id) |
|
64
|
|
|
{ |
|
65
|
|
|
return self::$model->newQuery()->where('id', $id)->delete(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public static function setTable($table) |
|
69
|
|
|
{ |
|
70
|
|
|
self::$model = new Content(); |
|
71
|
|
|
return self::$model->setTable($table); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected static function processParams($data) |
|
75
|
|
|
{ |
|
76
|
|
|
return array_map(function ($item) { |
|
77
|
|
|
if (is_array($item)) { |
|
78
|
|
|
return implode(',', $item); |
|
79
|
|
|
} else { |
|
80
|
|
|
return $item; |
|
81
|
|
|
} |
|
82
|
|
|
}, $data); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public static function adjacent($id) |
|
86
|
|
|
{ |
|
87
|
|
|
return [ |
|
88
|
|
|
'previous' => self::$model->newQuery()->where('id', '<', $id)->first(), |
|
89
|
|
|
'next' => self::$model->newQuery()->where('id', '>', $id)->first() |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|