Template.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Admin\Extensions\Field;
  3. use Encore\Admin\Admin;
  4. use Encore\Admin\Form;
  5. use Encore\Admin\Form\Field;
  6. use Illuminate\Support\Collection;
  7. /**
  8. * Class Template
  9. * 表单模板
  10. * @method Field\Text text($column, $label = '')
  11. * @method Field\Checkbox checkbox($column, $label = '')
  12. * @method Field\Radio radio($column, $label = '')
  13. * @method Field\Select select($column, $label = '')
  14. * @method Field\MultipleSelect multipleSelect($column, $label = '')
  15. * @method Field\Textarea textarea($column, $label = '')
  16. * @method Field\Hidden hidden($column, $label = '')
  17. * @method Field\Id id($column, $label = '')
  18. * @method Field\Ip ip($column, $label = '')
  19. * @method Field\Url url($column, $label = '')
  20. * @method Field\Color color($column, $label = '')
  21. * @method Field\Email email($column, $label = '')
  22. * @method Field\Mobile mobile($column, $label = '')
  23. * @method Field\Slider slider($column, $label = '')
  24. * @method Field\Map map($latitude, $longitude, $label = '')
  25. * @method Field\Editor editor($column, $label = '')
  26. * @method Field\File file($column, $label = '')
  27. * @method Field\Image image($column, $label = '')
  28. * @method Field\Date date($column, $label = '')
  29. * @method Field\Datetime datetime($column, $label = '')
  30. * @method Field\Time time($column, $label = '')
  31. * @method Field\Year year($column, $label = '')
  32. * @method Field\Month month($column, $label = '')
  33. * @method Field\DateRange dateRange($start, $end, $label = '')
  34. * @method Field\DateTimeRange datetimeRange($start, $end, $label = '')
  35. * @method Field\TimeRange timeRange($start, $end, $label = '')
  36. * @method Field\Number number($column, $label = '')
  37. * @method Field\Currency currency($column, $label = '')
  38. * @method Field\HasMany hasMany($relationName, $callback)
  39. * @method Field\SwitchField switch($column, $label = '')
  40. * @method Field\Display display($column, $label = '')
  41. * @method Field\Rate rate($column, $label = '')
  42. * @method Field\Divide divider()
  43. * @method Field\Password password($column, $label = '')
  44. * @method Field\Decimal decimal($column, $label = '')
  45. * @method Field\Html html($html, $label = '')
  46. * @method Field\Tags tags($column, $label = '')
  47. * @method Field\Icon icon($column, $label = '')
  48. * @method Field\Embeds embeds($column, $label = '')
  49. */
  50. class Template
  51. {
  52. /**
  53. * 对应表单
  54. * @var Collection
  55. */
  56. protected $templates;
  57. protected $nowId;
  58. public function __construct()
  59. {
  60. $this->templates=new Collection();
  61. }
  62. public function setTemplatesId($id)
  63. {
  64. $this->nowId=$id;
  65. if (!$this->templates->has($id)) {
  66. $this->templates->put($id, new Collection());
  67. }
  68. return $this;
  69. }
  70. protected function pushField(Field $field)
  71. {
  72. $this->templates->get($this->nowId)->push($field);
  73. }
  74. protected function getHtmlAndScript()
  75. {
  76. $htmls = [];
  77. $scripts = [];
  78. /* @var Field $field */
  79. foreach ($this->templates as $key => $template) {
  80. $html='';
  81. $script=[];
  82. foreach ($template as $field) {
  83. //when field render, will push $script to Admin
  84. $html.= $field->render();
  85. /*
  86. * Get and remove the last script of Admin::$script stack.
  87. */
  88. if ($field->getScript()) {
  89. $script[] = array_pop(Admin::$script);
  90. }
  91. }
  92. $htmls[$key]=$html;
  93. $scripts[$key]=implode("\r\n", $script);
  94. }
  95. return [$htmls, $scripts];
  96. }
  97. /**
  98. * Add nested-form fields dynamically.
  99. *
  100. * @param string $method
  101. * @param array $arguments
  102. *
  103. * @return mixed
  104. */
  105. public function __call($method, $arguments)
  106. {
  107. if ($className = Form::findFieldClass($method)) {
  108. $column = array_get($arguments, 0, '');
  109. /* @var Field $field */
  110. $field = new $className($column, array_slice($arguments, 1));
  111. $this->pushField($field);
  112. return $field;
  113. }
  114. return $this;
  115. }
  116. public function render()
  117. {
  118. list($template, $script) = $this->getHtmlAndScript();
  119. return view('admin.field.template')->with('templates', $template)->with('scripts', $script)->render();
  120. }
  121. }