Content.php 12 KB

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