helpers.php 9.2 KB

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