helpers.php 13 KB

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