Content.php 7.1 KB

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