helpers.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. use Dcat\Admin\Admin;
  3. use Dcat\Admin\Support\Helper;
  4. use Illuminate\Contracts\Support\Htmlable;
  5. use Illuminate\Contracts\Support\Renderable;
  6. use Illuminate\Support\MessageBag;
  7. use Symfony\Component\HttpFoundation\Response;
  8. if (! function_exists('admin_setting')) {
  9. /**
  10. * @param string|array $key
  11. * @param mixed $default
  12. *
  13. * @return \Dcat\Admin\Support\Setting|mixed
  14. */
  15. function admin_setting($key = null, $default = null)
  16. {
  17. if ($key === null) {
  18. return app('admin.setting');
  19. }
  20. if (is_array($key)) {
  21. app('admin.setting')->save($key);
  22. return;
  23. }
  24. return app('admin.setting')->get($key, $default);
  25. }
  26. }
  27. if (! function_exists('admin_setting_array')) {
  28. /**
  29. * @param string $key
  30. * @param mixed $default
  31. *
  32. * @return \Dcat\Admin\Support\Setting|mixed
  33. */
  34. function admin_setting_array(?string $key, $default = [])
  35. {
  36. return app('admin.setting')->getArray($key, $default);
  37. }
  38. }
  39. if (! function_exists('admin_extension_setting')) {
  40. /**
  41. * @param string $extension
  42. * @param string|array $key
  43. * @param mixed $default
  44. *
  45. * @return mixed
  46. */
  47. function admin_extension_setting($extension, $key = null, $default = null)
  48. {
  49. $extension = app($extension);
  50. if ($extension instanceof Dcat\Admin\Extend\ServiceProvider) {
  51. return $extension->config($key, $default);
  52. }
  53. }
  54. }
  55. if (! function_exists('admin_section')) {
  56. /**
  57. * Get the string contents of a section.
  58. *
  59. * @param string $section
  60. * @param mixed $default
  61. * @param array $options
  62. *
  63. * @return mixed
  64. */
  65. function admin_section(string $section, $default = null, array $options = [])
  66. {
  67. return app('admin.sections')->yieldContent($section, $default, $options);
  68. }
  69. }
  70. if (! function_exists('admin_has_section')) {
  71. /**
  72. * Check if section exists.
  73. *
  74. * @param string $section
  75. *
  76. * @return mixed
  77. */
  78. function admin_has_section(string $section)
  79. {
  80. return app('admin.sections')->hasSection($section);
  81. }
  82. }
  83. if (! function_exists('admin_inject_section')) {
  84. /**
  85. * Injecting content into a section.
  86. *
  87. * @param string $section
  88. * @param mixed $content
  89. * @param bool $append
  90. * @param int $priority
  91. */
  92. function admin_inject_section(string $section, $content = null, bool $append = true, int $priority = 10)
  93. {
  94. app('admin.sections')->inject($section, $content, $append, $priority);
  95. }
  96. }
  97. if (! function_exists('admin_inject_section_if')) {
  98. /**
  99. * Injecting content into a section.
  100. *
  101. * @param mixed $condition
  102. * @param string $section
  103. * @param mixed $content
  104. * @param bool $append
  105. * @param int $priority
  106. */
  107. function admin_inject_section_if($condition, $section, $content = null, bool $append = false, int $priority = 10)
  108. {
  109. if ($condition) {
  110. app('admin.sections')->inject($section, $content, $append, $priority);
  111. }
  112. }
  113. }
  114. if (! function_exists('admin_has_default_section')) {
  115. /**
  116. * Check if default section exists.
  117. *
  118. * @param string $section
  119. *
  120. * @return mixed
  121. */
  122. function admin_has_default_section(string $section)
  123. {
  124. return app('admin.sections')->hasDefaultSection($section);
  125. }
  126. }
  127. if (! function_exists('admin_inject_default_section')) {
  128. /**
  129. * Injecting content into a section.
  130. *
  131. * @param string $section
  132. * @param string|Renderable|Htmlable|callable $content
  133. */
  134. function admin_inject_default_section(string $section, $content)
  135. {
  136. app('admin.sections')->injectDefault($section, $content);
  137. }
  138. }
  139. if (! function_exists('admin_trans_field')) {
  140. /**
  141. * Translate the field name.
  142. *
  143. * @param $field
  144. * @param null $locale
  145. *
  146. * @return array|\Illuminate\Contracts\Translation\Translator|null|string
  147. */
  148. function admin_trans_field($field, $locale = null)
  149. {
  150. $slug = admin_controller_slug();
  151. return admin_trans("{$slug}.fields.{$field}", [], $locale);
  152. }
  153. }
  154. if (! function_exists('admin_trans_label')) {
  155. /**
  156. * Translate the label.
  157. *
  158. * @param $label
  159. * @param array $replace
  160. * @param null $locale
  161. *
  162. * @return array|\Illuminate\Contracts\Translation\Translator|null|string
  163. */
  164. function admin_trans_label($label = null, $replace = [], $locale = null)
  165. {
  166. $label = $label ?: admin_controller_name();
  167. $slug = admin_controller_slug();
  168. return admin_trans("{$slug}.labels.{$label}", $replace, $locale);
  169. }
  170. }
  171. if (! function_exists('admin_trans_option')) {
  172. /**
  173. * Translate the field name.
  174. *
  175. * @param $field
  176. * @param array $replace
  177. * @param null $locale
  178. *
  179. * @return array|\Illuminate\Contracts\Translation\Translator|null|string
  180. */
  181. function admin_trans_option($optionValue, $field, $replace = [], $locale = null)
  182. {
  183. $slug = admin_controller_slug();
  184. return admin_trans("{$slug}.options.{$field}.{$optionValue}", $replace, $locale);
  185. }
  186. }
  187. if (! function_exists('admin_trans')) {
  188. /**
  189. * Translate the given message.
  190. *
  191. * @param string $key
  192. * @param array $replace
  193. * @param string $locale
  194. *
  195. * @return \Illuminate\Contracts\Translation\Translator|string|array|null
  196. */
  197. function admin_trans($key, $replace = [], $locale = null)
  198. {
  199. static $method = null;
  200. if ($method === null) {
  201. $method = version_compare(app()->version(), '6.0', '>=') ? 'get' : 'trans';
  202. }
  203. $translator = app('translator');
  204. if ($translator->has($key)) {
  205. return $translator->$method($key, $replace, $locale);
  206. }
  207. if (
  208. mb_strpos($key, 'global.') !== 0
  209. && count($arr = explode('.', $key)) > 1
  210. ) {
  211. unset($arr[0]);
  212. array_unshift($arr, 'global');
  213. $key = implode('.', $arr);
  214. if (! $translator->has($key)) {
  215. return end($arr);
  216. }
  217. return $translator->$method($key, $replace, $locale);
  218. }
  219. return last(explode('.', $key));
  220. }
  221. }
  222. if (! function_exists('admin_controller_slug')) {
  223. /**
  224. * @return string
  225. */
  226. function admin_controller_slug()
  227. {
  228. static $slug = [];
  229. $controller = admin_controller_name();
  230. return $slug[$controller] ?? ($slug[$controller] = Helper::slug($controller));
  231. }
  232. }
  233. if (! function_exists('admin_controller_name')) {
  234. /**
  235. * Get the class "basename" of the current controller.
  236. *
  237. * @return string
  238. */
  239. function admin_controller_name()
  240. {
  241. static $name = [];
  242. $router = app('router');
  243. if (! $router->current()) {
  244. return 'undefined';
  245. }
  246. $actionName = $router->current()->getActionName();
  247. if (! isset($name[$actionName])) {
  248. $controller = class_basename(explode('@', $actionName)[0]);
  249. $name[$actionName] = str_replace('Controller', '', $controller);
  250. }
  251. return $name[$actionName];
  252. }
  253. }
  254. if (! function_exists('admin_path')) {
  255. /**
  256. * Get admin path.
  257. *
  258. * @param string $path
  259. *
  260. * @return string
  261. */
  262. function admin_path($path = '')
  263. {
  264. return ucfirst(config('admin.directory')).($path ? DIRECTORY_SEPARATOR.$path : $path);
  265. }
  266. }
  267. if (! function_exists('admin_url')) {
  268. /**
  269. * Get admin url.
  270. *
  271. * @param string $path
  272. * @param mixed $parameters
  273. * @param bool $secure
  274. *
  275. * @return string
  276. */
  277. function admin_url($path = '', $parameters = [], $secure = null)
  278. {
  279. if (url()->isValidUrl($path)) {
  280. return $path;
  281. }
  282. $secure = $secure ?: (config('admin.https') || config('admin.secure'));
  283. return url(admin_base_path($path), $parameters, $secure);
  284. }
  285. }
  286. if (! function_exists('admin_base_path')) {
  287. /**
  288. * Get admin url.
  289. *
  290. * @param string $path
  291. *
  292. * @return string
  293. */
  294. function admin_base_path($path = '')
  295. {
  296. $prefix = '/'.trim(config('admin.route.prefix'), '/');
  297. $prefix = ($prefix == '/') ? '' : $prefix;
  298. $path = trim($path, '/');
  299. if (is_null($path) || strlen($path) == 0) {
  300. return $prefix ?: '/';
  301. }
  302. return $prefix.'/'.$path;
  303. }
  304. }
  305. if (! function_exists('admin_toastr')) {
  306. /**
  307. * Flash a toastr message bag to session.
  308. *
  309. * @param string $message
  310. * @param string $type
  311. * @param array $options
  312. */
  313. function admin_toastr($message = '', $type = 'success', $options = [])
  314. {
  315. $toastr = new MessageBag(get_defined_vars());
  316. session()->flash('dcat-admin-toastr', $toastr);
  317. }
  318. }
  319. if (! function_exists('admin_success')) {
  320. /**
  321. * Flash a success message bag to session.
  322. *
  323. * @param string $title
  324. * @param string $message
  325. */
  326. function admin_success($title, $message = '')
  327. {
  328. admin_info($title, $message, 'success');
  329. }
  330. }
  331. if (! function_exists('admin_error')) {
  332. /**
  333. * Flash a error message bag to session.
  334. *
  335. * @param string $title
  336. * @param string $message
  337. */
  338. function admin_error($title, $message = '')
  339. {
  340. admin_info($title, $message, 'error');
  341. }
  342. }
  343. if (! function_exists('admin_warning')) {
  344. /**
  345. * Flash a warning message bag to session.
  346. *
  347. * @param string $title
  348. * @param string $message
  349. */
  350. function admin_warning($title, $message = '')
  351. {
  352. admin_info($title, $message, 'warning');
  353. }
  354. }
  355. if (! function_exists('admin_info')) {
  356. /**
  357. * Flash a message bag to session.
  358. *
  359. * @param string $title
  360. * @param string $message
  361. * @param string $type
  362. */
  363. function admin_info($title, $message = '', $type = 'info')
  364. {
  365. $message = new MessageBag(get_defined_vars());
  366. session()->flash($type, $message);
  367. }
  368. }
  369. if (! function_exists('admin_asset')) {
  370. /**
  371. * @param $path
  372. *
  373. * @return string
  374. */
  375. function admin_asset($path)
  376. {
  377. return Dcat\Admin\Admin::asset()->url($path);
  378. }
  379. }
  380. if (! function_exists('admin_api_route')) {
  381. /**
  382. * @param string $path
  383. *
  384. * @return string
  385. */
  386. function admin_api_route(?string $path = '')
  387. {
  388. return Dcat\Admin\Admin::app()->getCurrentApiRoutePrefix().$path;
  389. }
  390. }
  391. if (! function_exists('admin_extension_path')) {
  392. /**
  393. * @param string|null $path
  394. *
  395. * @return string
  396. */
  397. function admin_extension_path(?string $path = null)
  398. {
  399. $dir = rtrim(config('admin.extension.dir'), '/') ?: base_path('dcat-admin-extensions');
  400. $path = ltrim($path, '/');
  401. return $path ? $dir.'/'.$path : $dir;
  402. }
  403. }
  404. if (! function_exists('admin_color')) {
  405. /**
  406. * @param string|null $color
  407. *
  408. * @return string|\Dcat\Admin\Color
  409. */
  410. function admin_color(?string $color = null)
  411. {
  412. if ($color === null) {
  413. return Admin::color();
  414. }
  415. return Admin::color()->get($color);
  416. }
  417. }
  418. if (! function_exists('admin_view')) {
  419. /**
  420. * @param string $view
  421. * @param array $data
  422. *
  423. * @return string
  424. *
  425. * @throws \Throwable
  426. */
  427. function admin_view($view, array $data = [])
  428. {
  429. return Admin::view($view, $data);
  430. }
  431. }
  432. if (! function_exists('admin_script')) {
  433. /**
  434. * @param string $js
  435. * @param bool $direct
  436. *
  437. * @return void
  438. */
  439. function admin_script($script, bool $direct = false)
  440. {
  441. Admin::script($script, $direct);
  442. }
  443. }
  444. if (! function_exists('admin_style')) {
  445. /**
  446. * @param string $style
  447. *
  448. * @return void
  449. */
  450. function admin_style($style)
  451. {
  452. Admin::style($style);
  453. }
  454. }
  455. if (! function_exists('admin_js')) {
  456. /**
  457. * @param string|array $js
  458. *
  459. * @return void
  460. */
  461. function admin_js($js)
  462. {
  463. Admin::js($js);
  464. }
  465. }
  466. if (! function_exists('admin_css')) {
  467. /**
  468. * @param string|array $css
  469. *
  470. * @return void
  471. */
  472. function admin_css($css)
  473. {
  474. Admin::css($css);
  475. }
  476. }
  477. if (! function_exists('admin_require_assets')) {
  478. /**
  479. * @param string|array $asset
  480. *
  481. * @return void
  482. */
  483. function admin_require_assets($asset)
  484. {
  485. Admin::requireAssets($asset);
  486. }
  487. }
  488. if (! function_exists('admin_javascript_json')) {
  489. /**
  490. * @param array|object $data
  491. *
  492. * @return string
  493. */
  494. function admin_javascript_json($data)
  495. {
  496. return Dcat\Admin\Support\JavaScript::format($data);
  497. }
  498. }
  499. if (! function_exists('admin_exit')) {
  500. /**
  501. * 响应并中断后续逻辑.
  502. *
  503. * @param Response|string|array $response
  504. *
  505. * @throws \Illuminate\Http\Exceptions\HttpResponseException
  506. */
  507. function admin_exit($response = '')
  508. {
  509. Admin::exit($response);
  510. }
  511. }