helpers.php 8.9 KB

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