Content.php 13 KB

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