ContentFull.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <?php
  2. namespace UCore\DcatAdmin\Widgets;
  3. use Closure;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Exception\RuntimeException;
  6. use Dcat\Admin\Traits\HasBuilderEvents;
  7. use Illuminate\Contracts\Support\Renderable;
  8. use Illuminate\Support\Traits\Macroable;
  9. use Illuminate\Support\ViewErrorBag;
  10. use Dcat\Admin\Layout\Row;
  11. use Dcat\Admin\Widgets\Widget;
  12. class ContentFull extends Widget implements Renderable
  13. {
  14. use HasBuilderEvents, Macroable;
  15. /**
  16. * @var string
  17. */
  18. protected $view = 'admin_core.widgets.layouts.full-content';
  19. /**
  20. * @var array
  21. */
  22. protected $variables = [];
  23. /**
  24. * Content title.
  25. *
  26. * @var string
  27. */
  28. protected $title = '';
  29. /**
  30. * Content description.
  31. *
  32. * @var string
  33. */
  34. protected $description = '';
  35. /**
  36. * Page breadcrumb.
  37. *
  38. * @var array
  39. */
  40. protected $breadcrumb = [];
  41. /**
  42. * @var Row[]
  43. */
  44. protected $rows = [];
  45. /**
  46. * @var array
  47. */
  48. protected $config = [];
  49. /**
  50. * Content constructor.
  51. *
  52. * @param Closure|null $callback
  53. */
  54. public function __construct(\Closure $callback = null)
  55. {
  56. $this->callResolving();
  57. if ($callback instanceof Closure) {
  58. $callback($this);
  59. }
  60. }
  61. /**
  62. * Create a content instance.
  63. *
  64. * @param mixed ...$params
  65. * @return $this
  66. */
  67. public static function make(...$params)
  68. {
  69. return new static(...$params);
  70. }
  71. /**
  72. * @param string $header
  73. * @return $this
  74. */
  75. public function header($header = '')
  76. {
  77. return $this->title($header);
  78. }
  79. /**
  80. * Set title of content.
  81. *
  82. * @param string $title
  83. * @return $this
  84. */
  85. public function title($title)
  86. {
  87. $this->title = $title;
  88. return $this;
  89. }
  90. /**
  91. * Set description of content.
  92. *
  93. * @param string $description
  94. * @return $this
  95. */
  96. public function description($description = '')
  97. {
  98. $this->description = $description;
  99. return $this;
  100. }
  101. /**
  102. * 设置翻译文件路径.
  103. *
  104. * @param string|null $translation
  105. * @return $this
  106. */
  107. public function translation(?string $translation)
  108. {
  109. Admin::translation($translation);
  110. return $this;
  111. }
  112. /**
  113. * Build full page.
  114. *
  115. * @return $this
  116. */
  117. public function full()
  118. {
  119. return $this->view('admin_core.widgets.layouts.full-content');
  120. }
  121. /**
  122. * Set breadcrumb of content.
  123. *
  124. * @example
  125. * $this->breadcrumb('Menu', 'auth/menu', 'fa fa-align-justify');
  126. * $this->breadcrumb([
  127. * ['text' => 'Menu', 'url' => 'auth/menu', 'icon' => 'fa fa-align-justify']
  128. * ]);
  129. *
  130. * @param array ...$breadcrumb
  131. * @return $this
  132. */
  133. public function breadcrumb(...$breadcrumb)
  134. {
  135. $this->formatBreadcrumb($breadcrumb);
  136. $this->breadcrumb = array_merge($this->breadcrumb, $breadcrumb);
  137. return $this;
  138. }
  139. /**
  140. * @param array $breadcrumb
  141. * @return void
  142. *
  143. * @throws \Exception
  144. */
  145. protected function formatBreadcrumb(array &$breadcrumb)
  146. {
  147. if (! $breadcrumb) {
  148. throw new RuntimeException('Breadcrumb format error!');
  149. }
  150. $notArray = false;
  151. foreach ($breadcrumb as &$item) {
  152. $isArray = is_array($item);
  153. if ($isArray && ! isset($item['text'])) {
  154. throw new RuntimeException('Breadcrumb format error!');
  155. }
  156. if (! $isArray && $item) {
  157. $notArray = true;
  158. }
  159. }
  160. if (! $breadcrumb) {
  161. throw new RuntimeException('Breadcrumb format error!');
  162. }
  163. if ($notArray) {
  164. $breadcrumb = [
  165. [
  166. 'text' => $breadcrumb[0] ?? null,
  167. 'url' => $breadcrumb[1] ?? null,
  168. 'icon' => $breadcrumb[2] ?? null,
  169. ],
  170. ];
  171. }
  172. }
  173. /**
  174. * Alias of method row.
  175. *
  176. * @param mixed $content
  177. * @return Content
  178. */
  179. public function body($content)
  180. {
  181. return $this->row($content);
  182. }
  183. /**
  184. * Add one row for content body.
  185. *
  186. * @param $content
  187. * @return $this
  188. */
  189. public function row($content)
  190. {
  191. if ($content instanceof Closure) {
  192. $row = new Row();
  193. call_user_func($content, $row);
  194. $this->addRow($row);
  195. } else {
  196. $this->addRow(new Row($content));
  197. }
  198. return $this;
  199. }
  200. /**
  201. * @param $content
  202. * @return $this
  203. */
  204. public function prepend($content)
  205. {
  206. if ($content instanceof Closure) {
  207. $row = new Row();
  208. call_user_func($content, $row);
  209. $this->prependRow($row);
  210. } else {
  211. $this->prependRow(new Row($content));
  212. }
  213. return $this;
  214. }
  215. protected function prependRow(Row $row)
  216. {
  217. array_unshift($this->rows, $row);
  218. }
  219. /**
  220. * Add Row.
  221. *
  222. * @param Row $row
  223. */
  224. protected function addRow(Row $row)
  225. {
  226. $this->rows[] = $row;
  227. }
  228. /**
  229. * Build html of content.
  230. *
  231. * @return string
  232. */
  233. public function build()
  234. {
  235. try {
  236. $html = '';
  237. foreach ($this->rows as $row) {
  238. $html .= $row->render();
  239. }
  240. return $html;
  241. } catch (\Throwable $e) {
  242. return $this->handleException($e);
  243. }
  244. }
  245. /**
  246. * @param \Throwable $e
  247. * @return mixed|string
  248. */
  249. protected function handleException(\Throwable $e)
  250. {
  251. $response = Admin::handleException($e);
  252. if (is_string($response) || $response instanceof Renderable) {
  253. $row = new Row($response);
  254. return $row->render();
  255. }
  256. return $response;
  257. }
  258. /**
  259. * Set success message for content.
  260. *
  261. * @param string $title
  262. * @param string $message
  263. * @return $this
  264. */
  265. public function withSuccess($title = '', $message = '')
  266. {
  267. admin_success($title, $message);
  268. return $this;
  269. }
  270. /**
  271. * Set error message for content.
  272. *
  273. * @param string $title
  274. * @param string $message
  275. * @return $this
  276. */
  277. public function withError($title = '', $message = '')
  278. {
  279. admin_error($title, $message);
  280. return $this;
  281. }
  282. /**
  283. * Set warning message for content.
  284. *
  285. * @param string $title
  286. * @param string $message
  287. * @return $this
  288. */
  289. public function withWarning($title = '', $message = '')
  290. {
  291. admin_warning($title, $message);
  292. return $this;
  293. }
  294. /**
  295. * Set info message for content.
  296. *
  297. * @param string $title
  298. * @param string $message
  299. * @return $this
  300. */
  301. public function withInfo($title = '', $message = '')
  302. {
  303. admin_info($title, $message);
  304. return $this;
  305. }
  306. /**
  307. * Set content view.
  308. *
  309. * @param null|string $view
  310. * @return $this
  311. */
  312. public function view(?string $view)
  313. {
  314. $this->view = $view;
  315. return $this;
  316. }
  317. /**
  318. * @param string|array $key
  319. * @param mixed $value
  320. * @return $this
  321. */
  322. public function with($key, $value = null)
  323. {
  324. if (is_array($key)) {
  325. $this->variables = array_merge($this->variables, $key);
  326. } else {
  327. $this->variables[$key] = $value;
  328. }
  329. return $this;
  330. }
  331. /**
  332. * @param string|array $key
  333. * @param mixed $value
  334. * @return $this
  335. */
  336. public function withConfig($key, $value = null)
  337. {
  338. if (is_array($key)) {
  339. $this->config = array_merge($this->config, $key);
  340. } else {
  341. $this->config[$key] = $value;
  342. }
  343. return $this;
  344. }
  345. /**
  346. * @return void
  347. */
  348. protected function shareDefaultErrors()
  349. {
  350. if (! session()->all()) {
  351. view()->share(['errors' => new ViewErrorBag()]);
  352. }
  353. }
  354. /**
  355. * @return array
  356. */
  357. protected function variables()
  358. {
  359. return array_merge([
  360. 'header' => $this->title,
  361. 'title' => $this->title,
  362. 'description' => $this->description,
  363. 'breadcrumb' => $this->breadcrumb,
  364. 'configData' => $this->applyClasses(),
  365. 'pjaxContainerId' => Admin::getPjaxContainerId(),
  366. ], $this->variables);
  367. }
  368. /**
  369. * @return array
  370. */
  371. protected function applyClasses()
  372. {
  373. // default data array
  374. $defaultData = [
  375. 'theme' => '',
  376. 'sidebar_collapsed' => false,
  377. 'sidebar_style' => 'sidebar-light-primary',
  378. 'navbar_color' => '',
  379. 'navbar_class' => 'sticky',
  380. 'footer_type' => '',
  381. 'body_class' => [],
  382. 'horizontal_menu' => false,
  383. ];
  384. $data = array_merge(
  385. config('admin.layout') ?: [],
  386. $this->config
  387. );
  388. $allOptions = [
  389. 'theme' => '',
  390. 'footer_type' => '',
  391. 'body_class' => [],
  392. 'sidebar_style' => ['light' => 'sidebar-light-primary', 'primary' => 'sidebar-primary', 'dark' => 'sidebar-dark-white'],
  393. 'sidebar_collapsed' => [],
  394. 'navbar_color' => [],
  395. 'navbar_class' => ['floating' => 'floating-nav', 'sticky' => 'fixed-top', 'hidden' => 'd-none'],
  396. 'horizontal_menu' => [],
  397. ];
  398. $maps = [
  399. 'footer_type' => 'footer_type',
  400. ];
  401. foreach ($allOptions as $key => $value) {
  402. if (! array_key_exists($key, $defaultData)) {
  403. continue;
  404. }
  405. if (! isset($data[$key])) {
  406. $data[$key] = $defaultData[$key];
  407. continue;
  408. }
  409. if (
  410. isset($maps[$key])
  411. && ! isset($allOptions[$maps[$key]][$data[$key]])
  412. ) {
  413. $data[$key] = $defaultData[$key];
  414. }
  415. if (! is_array($data[$key]) && isset($value[$data[$key]])) {
  416. $data[$key] = $value[$data[$key]];
  417. }
  418. }
  419. if (! is_array($data['body_class'])) {
  420. $data['body_class'] = explode(' ', (string) $data['body_class']);
  421. }
  422. if ($data['body_class'] && in_array('dark-mode', $data['body_class'], true)) {
  423. $data['sidebar_style'] = 'sidebar-dark-white';
  424. }
  425. if ($data['horizontal_menu']) {
  426. $data['body_class'][] = 'horizontal-menu';
  427. }
  428. return [
  429. 'theme' => $data['theme'],
  430. 'sidebar_collapsed' => $data['sidebar_collapsed'],
  431. 'navbar_color' => $data['navbar_color'],
  432. 'navbar_class' => $allOptions['navbar_class'][$data['navbar_class']],
  433. 'sidebar_class' => $data['sidebar_collapsed'] ? 'sidebar-collapse' : '',
  434. 'body_class' => implode(' ', $data['body_class']),
  435. 'sidebar_style' => $data['sidebar_style'],
  436. 'horizontal_menu' => $data['horizontal_menu'],
  437. ];
  438. }
  439. /**
  440. * Render this content.
  441. *
  442. * @return string
  443. */
  444. public function render()
  445. {
  446. $this->callComposing();
  447. $this->shareDefaultErrors();
  448. $this->variables['content'] = $this->build();
  449. $this->callComposed();
  450. if (Admin::shouldPrevent()) {
  451. return Admin::renderContents();
  452. }
  453. return view($this->view, $this->variables())->render();
  454. }
  455. /**
  456. * Register a composed event.
  457. *
  458. * @param callable $callback
  459. * @param bool $once
  460. */
  461. public static function composed(callable $callback, bool $once = false)
  462. {
  463. static::addBuilderListeners('builder.composed', $callback, $once);
  464. }
  465. /**
  466. * Call the composed callbacks.
  467. *
  468. * @param array ...$params
  469. */
  470. protected function callComposed(...$params)
  471. {
  472. $this->fireBuilderEvent('builder.composed', ...$params);
  473. }
  474. }