helpers.php 14 KB

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