helpers.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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_section')) {
  8. /**
  9. * Get the string contents of a section.
  10. *
  11. * @param string $section
  12. * @param mixed $default
  13. * @param array $options
  14. *
  15. * @return mixed
  16. */
  17. function admin_section(string $section, $default = null, array $options = [])
  18. {
  19. return app('admin.sections')->yieldContent($section, $default, $options);
  20. }
  21. }
  22. if (! function_exists('admin_has_section')) {
  23. /**
  24. * Check if section exists.
  25. *
  26. * @param string $section
  27. *
  28. * @return mixed
  29. */
  30. function admin_has_section(string $section)
  31. {
  32. return app('admin.sections')->hasSection($section);
  33. }
  34. }
  35. if (! function_exists('admin_inject_section')) {
  36. /**
  37. * Injecting content into a section.
  38. *
  39. * @param string $section
  40. * @param mixed $content
  41. * @param bool $append
  42. * @param int $priority
  43. */
  44. function admin_inject_section(string $section, $content = null, bool $append = true, int $priority = 10)
  45. {
  46. app('admin.sections')->inject($section, $content, $append, $priority);
  47. }
  48. }
  49. if (! function_exists('admin_inject_section_if')) {
  50. /**
  51. * Injecting content into a section.
  52. *
  53. * @param mixed $condition
  54. * @param string $section
  55. * @param mixed $content
  56. * @param bool $append
  57. * @param int $priority
  58. */
  59. function admin_inject_section_if($condition, $section, $content = null, bool $append = false, int $priority = 10)
  60. {
  61. if ($condition) {
  62. app('admin.sections')->inject($section, $content, $append, $priority);
  63. }
  64. }
  65. }
  66. if (! function_exists('admin_has_default_section')) {
  67. /**
  68. * Check if default section exists.
  69. *
  70. * @param string $section
  71. *
  72. * @return mixed
  73. */
  74. function admin_has_default_section(string $section)
  75. {
  76. return app('admin.sections')->hasDefaultSection($section);
  77. }
  78. }
  79. if (! function_exists('admin_inject_default_section')) {
  80. /**
  81. * Injecting content into a section.
  82. *
  83. * @param string $section
  84. * @param string|Renderable|Htmlable|callable $content
  85. */
  86. function admin_inject_default_section(string $section, $content)
  87. {
  88. app('admin.sections')->injectDefault($section, $content);
  89. }
  90. }
  91. if (! function_exists('admin_trans_field')) {
  92. /**
  93. * Translate the field name.
  94. *
  95. * @param $field
  96. * @param null $locale
  97. *
  98. * @return array|\Illuminate\Contracts\Translation\Translator|null|string
  99. */
  100. function admin_trans_field($field, $locale = null)
  101. {
  102. $slug = admin_controller_slug();
  103. return admin_trans("{$slug}.fields.{$field}", [], $locale);
  104. }
  105. }
  106. if (! function_exists('admin_trans_label')) {
  107. /**
  108. * Translate the label.
  109. *
  110. * @param $label
  111. * @param array $replace
  112. * @param null $locale
  113. *
  114. * @return array|\Illuminate\Contracts\Translation\Translator|null|string
  115. */
  116. function admin_trans_label($label = null, $replace = [], $locale = null)
  117. {
  118. $label = $label ?: admin_controller_name();
  119. $slug = admin_controller_slug();
  120. return admin_trans("{$slug}.labels.{$label}", $replace, $locale);
  121. }
  122. }
  123. if (! function_exists('admin_trans_option')) {
  124. /**
  125. * Translate the field name.
  126. *
  127. * @param $field
  128. * @param array $replace
  129. * @param null $locale
  130. *
  131. * @return array|\Illuminate\Contracts\Translation\Translator|null|string
  132. */
  133. function admin_trans_option($optionValue, $field, $replace = [], $locale = null)
  134. {
  135. $slug = admin_controller_slug();
  136. return admin_trans("{$slug}.options.{$field}.{$optionValue}", $replace, $locale);
  137. }
  138. }
  139. if (! function_exists('admin_trans')) {
  140. /**
  141. * Translate the given message.
  142. *
  143. * @param string $key
  144. * @param array $replace
  145. * @param string $locale
  146. *
  147. * @return \Illuminate\Contracts\Translation\Translator|string|array|null
  148. */
  149. function admin_trans($key, $replace = [], $locale = null)
  150. {
  151. static $method = null;
  152. if ($method === null) {
  153. $method = version_compare(app()->version(), '6.0', '>=') ? 'get' : 'trans';
  154. }
  155. $translator = app('translator');
  156. if ($translator->has($key)) {
  157. return $translator->$method($key, $replace, $locale);
  158. }
  159. if (
  160. mb_strpos($key, 'global.') !== 0
  161. && count($arr = explode('.', $key)) > 1
  162. ) {
  163. unset($arr[0]);
  164. array_unshift($arr, 'global');
  165. $key = implode('.', $arr);
  166. if (! $translator->has($key)) {
  167. return end($arr);
  168. }
  169. return $translator->$method($key, $replace, $locale);
  170. }
  171. return last(explode('.', $key));
  172. }
  173. }
  174. if (! function_exists('admin_controller_slug')) {
  175. /**
  176. * @return string
  177. */
  178. function admin_controller_slug()
  179. {
  180. static $slug = [];
  181. $controller = admin_controller_name();
  182. return $slug[$controller] ?? ($slug[$controller] = Helper::slug($controller));
  183. }
  184. }
  185. if (! function_exists('admin_controller_name')) {
  186. /**
  187. * Get the class "basename" of the current controller.
  188. *
  189. * @return string
  190. */
  191. function admin_controller_name()
  192. {
  193. static $name = [];
  194. $router = app('router');
  195. if (! $router->current()) {
  196. return 'undefined';
  197. }
  198. $actionName = $router->current()->getActionName();
  199. if (! isset($name[$actionName])) {
  200. $controller = class_basename(explode('@', $actionName)[0]);
  201. $name[$actionName] = str_replace('Controller', '', $controller);
  202. }
  203. return $name[$actionName];
  204. }
  205. }
  206. if (! function_exists('admin_path')) {
  207. /**
  208. * Get admin path.
  209. *
  210. * @param string $path
  211. *
  212. * @return string
  213. */
  214. function admin_path($path = '')
  215. {
  216. return ucfirst(config('admin.directory')).($path ? DIRECTORY_SEPARATOR.$path : $path);
  217. }
  218. }
  219. if (! function_exists('admin_url')) {
  220. /**
  221. * Get admin url.
  222. *
  223. * @param string $path
  224. * @param mixed $parameters
  225. * @param bool $secure
  226. *
  227. * @return string
  228. */
  229. function admin_url($path = '', $parameters = [], $secure = null)
  230. {
  231. if (url()->isValidUrl($path)) {
  232. return $path;
  233. }
  234. $secure = $secure ?: (config('admin.https') || config('admin.secure'));
  235. return url(admin_base_path($path), $parameters, $secure);
  236. }
  237. }
  238. if (! function_exists('admin_base_path')) {
  239. /**
  240. * Get admin url.
  241. *
  242. * @param string $path
  243. *
  244. * @return string
  245. */
  246. function admin_base_path($path = '')
  247. {
  248. $prefix = '/'.trim(config('admin.route.prefix'), '/');
  249. $prefix = ($prefix == '/') ? '' : $prefix;
  250. $path = trim($path, '/');
  251. if (is_null($path) || strlen($path) == 0) {
  252. return $prefix ?: '/';
  253. }
  254. return $prefix.'/'.$path;
  255. }
  256. }
  257. if (! function_exists('admin_toastr')) {
  258. /**
  259. * Flash a toastr message bag to session.
  260. *
  261. * @param string $message
  262. * @param string $type
  263. * @param array $options
  264. */
  265. function admin_toastr($message = '', $type = 'success', $options = [])
  266. {
  267. $toastr = new MessageBag(get_defined_vars());
  268. session()->flash('dcat-admin-toastr', $toastr);
  269. }
  270. }
  271. if (! function_exists('admin_success')) {
  272. /**
  273. * Flash a success message bag to session.
  274. *
  275. * @param string $title
  276. * @param string $message
  277. */
  278. function admin_success($title, $message = '')
  279. {
  280. admin_info($title, $message, 'success');
  281. }
  282. }
  283. if (! function_exists('admin_error')) {
  284. /**
  285. * Flash a error message bag to session.
  286. *
  287. * @param string $title
  288. * @param string $message
  289. */
  290. function admin_error($title, $message = '')
  291. {
  292. admin_info($title, $message, 'error');
  293. }
  294. }
  295. if (! function_exists('admin_warning')) {
  296. /**
  297. * Flash a warning message bag to session.
  298. *
  299. * @param string $title
  300. * @param string $message
  301. */
  302. function admin_warning($title, $message = '')
  303. {
  304. admin_info($title, $message, 'warning');
  305. }
  306. }
  307. if (! function_exists('admin_info')) {
  308. /**
  309. * Flash a message bag to session.
  310. *
  311. * @param string $title
  312. * @param string $message
  313. * @param string $type
  314. */
  315. function admin_info($title, $message = '', $type = 'info')
  316. {
  317. $message = new MessageBag(get_defined_vars());
  318. session()->flash($type, $message);
  319. }
  320. }
  321. if (! function_exists('admin_asset')) {
  322. /**
  323. * @param $path
  324. *
  325. * @return string
  326. */
  327. function admin_asset($path)
  328. {
  329. return Dcat\Admin\Admin::asset()->url($path);
  330. }
  331. }
  332. if (! function_exists('admin_api_route')) {
  333. /**
  334. * @param string $path
  335. *
  336. * @return string
  337. */
  338. function admin_api_route(string $path = '')
  339. {
  340. return Dcat\Admin\Admin::app()->getCurrentApiRoutePrefix().$path;
  341. }
  342. }