helpers.php 9.2 KB

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