Content.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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\ViewErrorBag;
  8. class Content implements Renderable
  9. {
  10. use HasBuilderEvents;
  11. /**
  12. * @var string
  13. */
  14. protected $view = 'admin::content';
  15. /**
  16. * Content title.
  17. *
  18. * @var string
  19. */
  20. protected $title = '';
  21. /**
  22. * Content description.
  23. *
  24. * @var string
  25. */
  26. protected $description = '';
  27. /**
  28. * Page breadcrumb.
  29. *
  30. * @var array
  31. */
  32. protected $breadcrumb = [];
  33. /**
  34. * @var Row[]
  35. */
  36. protected $rows = [];
  37. /**
  38. * Content constructor.
  39. *
  40. * @param Closure|null $callback
  41. */
  42. public function __construct(\Closure $callback = null)
  43. {
  44. $this->callResolving();
  45. if ($callback instanceof Closure) {
  46. $callback($this);
  47. }
  48. }
  49. /**
  50. * Create a content instance.
  51. *
  52. * @param mixed ...$params
  53. *
  54. * @return $this
  55. */
  56. public static function make(...$params)
  57. {
  58. return new static(...$params);
  59. }
  60. /**
  61. * @param string $header
  62. *
  63. * @return $this
  64. */
  65. public function header($header = '')
  66. {
  67. return $this->title($header);
  68. }
  69. /**
  70. * Set title of content.
  71. *
  72. * @param string $title
  73. *
  74. * @return $this
  75. */
  76. public function title($title)
  77. {
  78. $this->title = $title;
  79. return $this;
  80. }
  81. /**
  82. * Set description of content.
  83. *
  84. * @param string $description
  85. *
  86. * @return $this
  87. */
  88. public function description($description = '')
  89. {
  90. $this->description = $description;
  91. return $this;
  92. }
  93. /**
  94. * Disable navbar and sidebar.
  95. *
  96. * @return $this
  97. */
  98. public function simple()
  99. {
  100. $this->view = 'admin::contents.simple';
  101. Admin::$disableSkinCss = true;
  102. return $this;
  103. }
  104. /**
  105. * Set breadcrumb of content.
  106. *
  107. * exp:
  108. * $this->breadcrumb('Menu', 'auth/menu', 'fa fa-align-justify');
  109. * $this->breadcrumb([
  110. * ['text' => 'Menu', 'url' => 'auth/menu', 'icon' => 'fa fa-align-justify']
  111. * ]);
  112. *
  113. * @param array ...$breadcrumb
  114. *
  115. * @return $this
  116. */
  117. public function breadcrumb(...$breadcrumb)
  118. {
  119. $this->formatBreadcrumb($breadcrumb);
  120. $this->breadcrumb = array_merge($this->breadcrumb, $breadcrumb);
  121. return $this;
  122. }
  123. /**
  124. * @param array $breadcrumb
  125. *
  126. * @throws \Exception
  127. *
  128. * @return void
  129. */
  130. protected function formatBreadcrumb(array &$breadcrumb)
  131. {
  132. if (! $breadcrumb) {
  133. throw new \Exception('Breadcrumb format error!');
  134. }
  135. $notArray = false;
  136. foreach ($breadcrumb as &$item) {
  137. $isArray = is_array($item);
  138. if ($isArray && ! isset($item['text'])) {
  139. throw new \Exception('Breadcrumb format error!');
  140. }
  141. if (! $isArray && $item) {
  142. $notArray = true;
  143. }
  144. }
  145. if (! $breadcrumb) {
  146. throw new \Exception('Breadcrumb format error!');
  147. }
  148. if ($notArray) {
  149. $breadcrumb = [
  150. [
  151. 'text' => $breadcrumb[0] ?? null,
  152. 'url' => $breadcrumb[1] ?? null,
  153. 'icon' => $breadcrumb[2] ?? null,
  154. ],
  155. ];
  156. }
  157. }
  158. /**
  159. * Alias of method row.
  160. *
  161. * @param mixed $content
  162. *
  163. * @return Content
  164. */
  165. public function body($content)
  166. {
  167. return $this->row($content);
  168. }
  169. /**
  170. * Add one row for content body.
  171. *
  172. * @param $content
  173. *
  174. * @return $this
  175. */
  176. public function row($content)
  177. {
  178. if ($content instanceof Closure) {
  179. $row = new Row();
  180. call_user_func($content, $row);
  181. $this->addRow($row);
  182. } else {
  183. $this->addRow(new Row($content));
  184. }
  185. return $this;
  186. }
  187. /**
  188. * @param $content
  189. *
  190. * @return $this
  191. */
  192. public function prepend($content)
  193. {
  194. if ($content instanceof Closure) {
  195. $row = new Row();
  196. call_user_func($content, $row);
  197. $this->prependRow($row);
  198. } else {
  199. $this->prependRow(new Row($content));
  200. }
  201. return $this;
  202. }
  203. protected function prependRow(Row $row)
  204. {
  205. array_unshift($this->rows, $row);
  206. }
  207. /**
  208. * Add Row.
  209. *
  210. * @param Row $row
  211. */
  212. protected function addRow(Row $row)
  213. {
  214. $this->rows[] = $row;
  215. }
  216. /**
  217. * Build html of content.
  218. *
  219. * @return string
  220. */
  221. public function build()
  222. {
  223. $html = '';
  224. foreach ($this->rows as $row) {
  225. $html .= $row->render();
  226. }
  227. return $html;
  228. }
  229. /**
  230. * Set success message for content.
  231. *
  232. * @param string $title
  233. * @param string $message
  234. *
  235. * @return $this
  236. */
  237. public function withSuccess($title = '', $message = '')
  238. {
  239. admin_success($title, $message);
  240. return $this;
  241. }
  242. /**
  243. * Set error message for content.
  244. *
  245. * @param string $title
  246. * @param string $message
  247. *
  248. * @return $this
  249. */
  250. public function withError($title = '', $message = '')
  251. {
  252. admin_error($title, $message);
  253. return $this;
  254. }
  255. /**
  256. * Set warning message for content.
  257. *
  258. * @param string $title
  259. * @param string $message
  260. *
  261. * @return $this
  262. */
  263. public function withWarning($title = '', $message = '')
  264. {
  265. admin_warning($title, $message);
  266. return $this;
  267. }
  268. /**
  269. * Set info message for content.
  270. *
  271. * @param string $title
  272. * @param string $message
  273. *
  274. * @return $this
  275. */
  276. public function withInfo($title = '', $message = '')
  277. {
  278. admin_info($title, $message);
  279. return $this;
  280. }
  281. /**
  282. * Set content view.
  283. *
  284. * @param null|string $view
  285. *
  286. * @return $this
  287. */
  288. public function view(?string $view)
  289. {
  290. $this->view = $view;
  291. return $this;
  292. }
  293. /**
  294. * Setup styles.
  295. */
  296. protected function setupStyles()
  297. {
  298. if (
  299. $this->view !== 'admin::contents.simple'
  300. && in_array('fixed', (array) config('admin.layout'))
  301. ) {
  302. Admin::style(
  303. <<<'CSS'
  304. #nprogress .spinner{position:fixed!important;top:75px;}#nprogress .bar{top:61px;}.fixed-solution .sticky-table-header{top:61px!important}
  305. CSS
  306. );
  307. }
  308. }
  309. protected function shareDefaultErrors()
  310. {
  311. if (! session()->all()) {
  312. view()->share(['errors' => new ViewErrorBag()]);
  313. }
  314. }
  315. /**
  316. * Render this content.
  317. *
  318. * @return string
  319. */
  320. public function render()
  321. {
  322. $this->callComposing();
  323. $this->setupStyles();
  324. $this->shareDefaultErrors();
  325. $items = [
  326. 'header' => $this->title,
  327. 'description' => $this->description,
  328. 'breadcrumb' => $this->breadcrumb,
  329. 'content' => $this->build(),
  330. ];
  331. return view($this->view, $items)->render();
  332. }
  333. }