|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\CMS\Repositories\FileAsset; |
|
4
|
|
|
|
|
5
|
|
|
use Yajra\CMS\Entities\Configuration; |
|
6
|
|
|
use Yajra\CMS\Entities\FileAsset; |
|
7
|
|
|
use Yajra\CMS\Entities\FileAssetGroup; |
|
8
|
|
|
use Yajra\CMS\Repositories\RepositoryAbstract; |
|
9
|
|
|
use Roumen\Asset\Asset; |
|
10
|
|
|
use Illuminate\Support\Facades\Blade; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class FileAssetEloquentRepository |
|
14
|
|
|
* |
|
15
|
|
|
* @package Yajra\CMS\Repositories\Article |
|
16
|
|
|
*/ |
|
17
|
|
|
class FileAssetEloquentRepository extends RepositoryAbstract implements FileAssetRepository |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Get repository model. |
|
21
|
|
|
* |
|
22
|
|
|
* @return \Yajra\CMS\Entities\FileAsset |
|
23
|
|
|
*/ |
|
24
|
|
|
public function getModel() |
|
25
|
|
|
{ |
|
26
|
|
|
return new FileAsset(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get all file assets. |
|
31
|
|
|
* |
|
32
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
|
33
|
|
|
*/ |
|
34
|
|
|
public function all() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->getModel()->query()->get(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Register admin required assets. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function registerAdminRequireAssets() |
|
43
|
|
|
{ |
|
44
|
|
|
foreach (config('asset.admin_required_assets', []) as $asset => $requiredValue) { |
|
45
|
|
|
Asset::add($requiredValue); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get file asset by name. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @return \Yajra\CMS\Entities\FileAsset |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getByName($name) |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->getModel() |
|
58
|
|
|
->where('name', $name) |
|
59
|
|
|
->where('category', Configuration::key('asset.default')) |
|
60
|
|
|
->first(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Register css blade directive. |
|
65
|
|
|
*/ |
|
66
|
|
View Code Duplication |
public function registerCssBlade() |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
Blade::directive('assetCss', function ($asset) { |
|
69
|
|
|
$asset = $this->getByName($this->strParser($asset . '.css')); |
|
70
|
|
|
|
|
71
|
|
|
return '<?php echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"' . $asset->url . '\">"; ?>'; |
|
72
|
|
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Register javascript blade directive. |
|
77
|
|
|
*/ |
|
78
|
|
View Code Duplication |
public function registerJsBlade() |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
Blade::directive('assetJs', function ($asset) { |
|
81
|
|
|
$asset = $this->getByName($this->strParser($asset . '.js')); |
|
82
|
|
|
|
|
83
|
|
|
return '<?php echo "<script src=\"' . $asset->url . '\"></script>"; ?>'; |
|
84
|
|
|
}); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $str |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
private function strParser($str) |
|
92
|
|
|
{ |
|
93
|
|
|
return str_replace("'", '', str_replace(['(', ')'], '', $str)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get file asset by type and name. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $type |
|
100
|
|
|
* @param string $name |
|
101
|
|
|
* @return \Yajra\CMS\Entities\FileAsset |
|
102
|
|
|
*/ |
|
103
|
|
|
private function getByTypeAndName($type, $name) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->getModel() |
|
106
|
|
|
->where('type', $type) |
|
107
|
|
|
->where('name', $name) |
|
108
|
|
|
->where('category', Configuration::key('asset.default')) |
|
109
|
|
|
->first(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Add site asset. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $type |
|
116
|
|
|
* @return \Roumen\Asset\Asset; |
|
|
|
|
|
|
117
|
|
|
*/ |
|
118
|
|
|
public function addAsset($type) |
|
119
|
|
|
{ |
|
120
|
|
|
$backEndAssets = $this->getGroupByType($type, 'backend'); |
|
121
|
|
|
foreach ($backEndAssets as $asset) { |
|
122
|
|
|
Asset::add($this->getByTypeAndName($type, $asset->file_asset_name)->url); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get asset group by type and user. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $type |
|
130
|
|
|
* @param string $user |
|
131
|
|
|
* @return \Yajra\CMS\Entities\FileAssetGroup |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getGroupByType($type, $user) |
|
134
|
|
|
{ |
|
135
|
|
|
return FileAssetGroup::where('type', $type) |
|
136
|
|
|
->orderBy('order', 'asc') |
|
137
|
|
|
->orderBy('user', $user) |
|
138
|
|
|
->get(); |
|
139
|
|
|
} |
|
140
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.