1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: sheldon |
||
5 | * Date: 18-3-23 |
||
6 | * Time: 上午9:21. |
||
7 | */ |
||
8 | if (!function_exists('backend_path')) { |
||
9 | |||
10 | /** |
||
11 | * Get backend path. |
||
12 | * |
||
13 | * @param string $path |
||
14 | * |
||
15 | * @return string |
||
16 | */ |
||
17 | function backend_path($path = '') |
||
18 | { |
||
19 | return ucfirst(config('yeelight.backend.directory')).($path ? DIRECTORY_SEPARATOR.$path : $path); |
||
20 | } |
||
21 | } |
||
22 | |||
23 | if (!function_exists('backend_url')) { |
||
24 | /** |
||
25 | * Get backend url. |
||
26 | * |
||
27 | * @param string $path |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | function backend_url($path = '') |
||
32 | { |
||
33 | if (\Illuminate\Support\Facades\URL::isValidUrl($path)) { |
||
34 | return $path; |
||
35 | } |
||
36 | |||
37 | return url(backend_base_path($path)); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
38 | } |
||
39 | } |
||
40 | |||
41 | if (!function_exists('backend_base_path')) { |
||
42 | /** |
||
43 | * Get backend url. |
||
44 | * |
||
45 | * @param string $path |
||
46 | * |
||
47 | * @return string |
||
48 | */ |
||
49 | function backend_base_path($path = '') |
||
50 | { |
||
51 | $prefix = '/'.trim(config('yeelight.backend.route.prefix'), '/'); |
||
52 | |||
53 | $prefix = ($prefix == '/') ? '' : $prefix; |
||
54 | |||
55 | return $prefix.'/'.trim($path, '/'); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | if (!function_exists('backend_auth_path')) { |
||
60 | /** |
||
61 | * Get backend url. |
||
62 | * |
||
63 | * @param string $path |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | function backend_auth_path($path = '') |
||
68 | { |
||
69 | $prefix = '/'.trim(config('yeelight.backend.auth_route.prefix'), '/'); |
||
70 | |||
71 | $prefix = ($prefix == '/') ? '' : $prefix; |
||
72 | |||
73 | return $prefix.'/'.trim($path, '/'); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | if (!function_exists('backend_toastr')) { |
||
78 | |||
79 | /** |
||
80 | * Flash a toastr message bag to session. |
||
81 | * |
||
82 | * @param string $message |
||
83 | * @param string $type |
||
84 | * @param array $options |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | function backend_toastr($message = '', $type = 'success', $options = []) |
||
89 | { |
||
90 | $toastr = new \Illuminate\Support\MessageBag(get_defined_vars()); |
||
91 | |||
92 | \Illuminate\Support\Facades\Session::flash('toastr', $toastr); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | if (!function_exists('backend_asset')) { |
||
97 | |||
98 | /** |
||
99 | * @param $path |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | function backend_asset($path) |
||
104 | { |
||
105 | return asset('assets/'.$path, config('yeelight.backend.secure')); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | if (!function_exists('backend_user')) { |
||
110 | |||
111 | /** |
||
112 | * Get current backend login user. |
||
113 | * |
||
114 | * @return mixed |
||
115 | */ |
||
116 | function backend_user() |
||
117 | { |
||
118 | return Auth::guard(config('yeelight.backend.route.prefix'))->user(); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | if (!function_exists('backend_title')) { |
||
123 | |||
124 | /** |
||
125 | * Get backend title. |
||
126 | * |
||
127 | * @return Config |
||
128 | */ |
||
129 | function backend_title() |
||
130 | { |
||
131 | return config('yeelight.backend.title'); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | if (!function_exists('backend_export_url')) { |
||
136 | |||
137 | /** |
||
138 | * Get the export url. |
||
139 | * |
||
140 | * @param int $scope |
||
141 | * @param null $args |
||
0 ignored issues
–
show
|
|||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | function backend_export_url($scope = -1, $args = null) |
||
146 | { |
||
147 | return \Yeelight\Services\Exporters\Exporter::exportUrl($scope, $args); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | if (!function_exists('column_sorter')) { |
||
152 | /** |
||
153 | * Create the column sorter. |
||
154 | * |
||
155 | * @param $column_name |
||
156 | * |
||
157 | * @return string|void |
||
158 | */ |
||
159 | function column_sorter($column_name) |
||
160 | { |
||
161 | $icon = 'fa-sort'; |
||
162 | $type = 'desc'; |
||
163 | $isSorted = false; |
||
164 | |||
165 | $orderBy = app('request')->get(config('repository.criteria.params.orderBy', 'orderBy'), null); |
||
166 | $sortedBy = app('request')->get(config('repository.criteria.params.sortedBy', 'sortedBy'), null); |
||
167 | |||
168 | if (!empty($orderBy)) { |
||
169 | $isSorted = isset($orderBy) && $orderBy == $column_name; |
||
170 | } |
||
171 | |||
172 | if ($isSorted) { |
||
173 | $type = $sortedBy == 'desc' ? 'asc' : 'desc'; |
||
174 | $icon .= "-amount-{$sortedBy}"; |
||
175 | } |
||
176 | |||
177 | $query = app('request')->all(); |
||
178 | $query = array_merge($query, [ |
||
179 | config('repository.criteria.params.orderBy') => $column_name, |
||
180 | config('repository.criteria.params.sortedBy') => $type, |
||
181 | ]); |
||
182 | |||
183 | $url = URL::current().'?'.http_build_query($query); |
||
184 | |||
185 | return "<a class=\"fa fa-fw $icon\" href=\"$url\"></a>"; |
||
186 | } |
||
187 | } |
||
188 | |||
189 | if (!function_exists('get_resource')) { |
||
190 | /** |
||
191 | * get resource uri. |
||
192 | * |
||
193 | * @param null $slice |
||
0 ignored issues
–
show
|
|||
194 | * @param string $mode |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | function get_resource($slice = null, $mode = '') |
||
199 | { |
||
200 | $segments = explode('/', trim(app('request')->getUri(), '/')); |
||
201 | if ($mode == 'create') { |
||
202 | $segments = array_slice($segments, 0, -1); |
||
203 | } |
||
204 | if ($slice !== null) { |
||
205 | if ($slice != 0) { |
||
206 | $segments = array_slice($segments, 0, $slice); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | if (strpos(end($segments), '?')) { |
||
211 | $segments[count($segments) - 1] = substr(end($segments), 0, strpos(end($segments), '?')); |
||
212 | } |
||
213 | |||
214 | return implode('/', $segments); |
||
215 | } |
||
216 | } |
||
217 | |||
218 | if (!function_exists('url_without_filters')) { |
||
219 | /** |
||
220 | * Get url without filter queryString. |
||
221 | * |
||
222 | * @param $columns |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | function url_without_filters($columns) |
||
227 | { |
||
228 | /** @var \Illuminate\Http\Request $request * */ |
||
229 | $request = Request::instance(); |
||
230 | |||
231 | $query = $request->query(); |
||
232 | array_forget($query, array_keys($columns)); |
||
233 | |||
234 | $question = $request->getBaseUrl().$request->getPathInfo() == '/' ? '/?' : '?'; |
||
235 | |||
236 | return count($request->query()) > 0 |
||
237 | ? $request->url().$question.http_build_query($query) |
||
238 | : $request->fullUrl(); |
||
239 | } |
||
240 | } |
||
241 |