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. try {
  233. $html = '';
  234. foreach ($this->rows as $row) {
  235. $html .= $row->render();
  236. }
  237. return $html;
  238. } catch (\Throwable $e) {
  239. return Admin::handleException($e);
  240. }
  241. }
  242. /**
  243. * Set success message for content.
  244. *
  245. * @param string $title
  246. * @param string $message
  247. *
  248. * @return $this
  249. */
  250. public function withSuccess($title = '', $message = '')
  251. {
  252. admin_success($title, $message);
  253. return $this;
  254. }
  255. /**
  256. * Set error message for content.
  257. *
  258. * @param string $title
  259. * @param string $message
  260. *
  261. * @return $this
  262. */
  263. public function withError($title = '', $message = '')
  264. {
  265. admin_error($title, $message);
  266. return $this;
  267. }
  268. /**
  269. * Set warning message for content.
  270. *
  271. * @param string $title
  272. * @param string $message
  273. *
  274. * @return $this
  275. */
  276. public function withWarning($title = '', $message = '')
  277. {
  278. admin_warning($title, $message);
  279. return $this;
  280. }
  281. /**
  282. * Set info message for content.
  283. *
  284. * @param string $title
  285. * @param string $message
  286. *
  287. * @return $this
  288. */
  289. public function withInfo($title = '', $message = '')
  290. {
  291. admin_info($title, $message);
  292. return $this;
  293. }
  294. /**
  295. * Set content view.
  296. *
  297. * @param null|string $view
  298. *
  299. * @return $this
  300. */
  301. public function view(?string $view)
  302. {
  303. $this->view = $view;
  304. return $this;
  305. }
  306. /**
  307. * @param string|array $key
  308. * @param mixed $value
  309. *
  310. * @return $this
  311. */
  312. public function with($key, $value = null)
  313. {
  314. if (is_array($key)) {
  315. $this->variables = array_merge($this->variables, $key);
  316. } else {
  317. $this->variables[$key] = $value;
  318. }
  319. return $this;
  320. }
  321. /**
  322. * @param string|array $key
  323. * @param mixed $value
  324. *
  325. * @return $this
  326. */
  327. public function withConfig($key, $value = null)
  328. {
  329. if (is_array($key)) {
  330. $this->config = array_merge($this->config, $key);
  331. } else {
  332. $this->config[$key] = $value;
  333. }
  334. return $this;
  335. }
  336. /**
  337. * @return void
  338. */
  339. protected function shareDefaultErrors()
  340. {
  341. if (! session()->all()) {
  342. view()->share(['errors' => new ViewErrorBag()]);
  343. }
  344. }
  345. /**
  346. * @return array
  347. */
  348. protected function variables()
  349. {
  350. return array_merge([
  351. 'header' => $this->title,
  352. 'description' => $this->description,
  353. 'breadcrumb' => $this->breadcrumb,
  354. 'configData' => $this->applyClasses(),
  355. 'pjaxContainerId' => Admin::getPjaxContainerId(),
  356. ], $this->variables);
  357. }
  358. /**
  359. * @return array
  360. */
  361. protected function applyClasses()
  362. {
  363. // default data array
  364. $defaultData = [
  365. 'theme' => '',
  366. 'sidebar_collapsed' => false,
  367. 'sidebar_style' => 'sidebar-light-primary',
  368. 'navbar_color' => '',
  369. 'navbar_class' => 'sticky',
  370. 'footer_type' => '',
  371. 'body_class' => '',
  372. ];
  373. $data = array_merge(
  374. config('admin.layout') ?: [],
  375. $this->config
  376. );
  377. // 1.0 版本兼容 sidebar_dark 参数
  378. if (empty($data['sidebar_style']) && ! empty($data['sidebar_dark'])) {
  379. $data['sidebar_style'] = 'sidebar-dark-white';
  380. }
  381. $allOptions = [
  382. 'theme' => '',
  383. 'footer_type' => '',
  384. 'body_class' => '',
  385. 'sidebar_style' => ['light' => 'sidebar-light-primary', 'primary' => 'sidebar-primary', 'dark' => 'sidebar-dark-white'],
  386. 'sidebar_collapsed' => [],
  387. 'navbar_color' => [],
  388. 'navbar_class' => ['floating' => 'floating-nav', 'sticky' => 'fixed-top', 'hidden' => 'd-none'],
  389. ];
  390. $maps = [
  391. 'footer_type' => 'footer_type',
  392. ];
  393. foreach ($allOptions as $key => $value) {
  394. if (! array_key_exists($key, $defaultData)) {
  395. continue;
  396. }
  397. if (! isset($data[$key])) {
  398. $data[$key] = $defaultData[$key];
  399. continue;
  400. }
  401. if (
  402. isset($maps[$key])
  403. && ! isset($allOptions[$maps[$key]][$data[$key]])
  404. ) {
  405. $data[$key] = $defaultData[$key];
  406. }
  407. if (! is_array($data[$key]) && isset($value[$data[$key]])) {
  408. $data[$key] = $value[$data[$key]];
  409. }
  410. }
  411. if ($data['body_class'] && Str::contains($data['body_class'], 'dark-mode')) {
  412. $data['sidebar_style'] = 'sidebar-dark-white';
  413. }
  414. return [
  415. 'theme' => $data['theme'],
  416. 'sidebar_collapsed' => $data['sidebar_collapsed'],
  417. 'navbar_color' => $data['navbar_color'],
  418. 'navbar_class' => $allOptions['navbar_class'][$data['navbar_class']],
  419. 'sidebar_class' => $data['sidebar_collapsed'] ? 'sidebar-collapse' : '',
  420. 'body_class' => $data['body_class'],
  421. 'sidebar_style' => $data['sidebar_style'],
  422. ];
  423. }
  424. /**
  425. * Render this content.
  426. *
  427. * @return string
  428. */
  429. public function render()
  430. {
  431. $this->callComposing();
  432. $this->shareDefaultErrors();
  433. $this->variables['content'] = $this->build();
  434. $this->callComposed();
  435. return view($this->view, $this->variables())->render();
  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. }