Content.php 11 KB

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