BoxField.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace backend\widgets;
  3. use yii\helpers\Html;
  4. class BoxField extends \yii\widgets\ActiveField
  5. {
  6. public $collapsed = false;
  7. public $options = [
  8. 'class' => 'box box-solid'
  9. ];
  10. public $headerOptions = [
  11. 'class' => 'box-header with-border'
  12. ];
  13. public $bodyOptions = [
  14. 'class' => 'box-body'
  15. ];
  16. public $footerOptions = [
  17. 'class' => 'box-footer small'
  18. ];
  19. public $template = "{header}\n{body}\n{footer}";
  20. public function render($content = null)
  21. {
  22. if ($content === null) {
  23. if (! isset($this->parts['{body}'])) {
  24. $this->body();
  25. }
  26. if (! isset($this->parts['{header}'])) {
  27. $this->header();
  28. }
  29. if (! isset($this->parts['{footer}'])) {
  30. $this->footer();
  31. }
  32. $content = strtr($this->template, $this->parts);
  33. } elseif (! is_string($content)) {
  34. $content = call_user_func($content, $this);
  35. }
  36. return $this->begin() . "\n" . $content . "\n" . $this->end();
  37. }
  38. public function begin()
  39. {
  40. if($this->collapsed == true) {
  41. $this->options["class"] .= " collapsed-box";
  42. }
  43. return parent::begin();
  44. }
  45. public function header($title = null, $options = [])
  46. {
  47. if ($title === false) { // 为false则不显示
  48. $this->parts['{header}'] = '';
  49. return $this;
  50. }
  51. $options = array_merge($this->headerOptions, $options);
  52. $attribute = Html::getAttributeName($this->attribute);
  53. if ($title !== null) {
  54. $options['title'] = $title;
  55. }
  56. $title = isset($options['title']) ? $options['title'] : Html::encode($this->model->getAttributeLabel($attribute));
  57. if($this->collapsed == true) {
  58. $faclass= "fa-plus";
  59. } else {
  60. $faclass= "fa-minus";
  61. }
  62. $content = '<h3 class="box-title">' . $title . '</h3>
  63. <div class="box-tools pull-right">
  64. <button class="btn btn-box-tool" data-widget="collapse"><i class="fa '.$faclass.'"></i></button>
  65. </div>';
  66. $this->parts['{header}'] = Html::tag("div", $content, $options);
  67. return $this;
  68. }
  69. public function body($options = [])
  70. {
  71. $options = array_merge($this->bodyOptions, $options);
  72. if (! isset($this->parts['{input}'])) {
  73. $this->textInput();
  74. }
  75. $content = $this->parts['{input}'];
  76. $this->parts['{body}'] = Html::tag("div", $content, $options);
  77. return $this;
  78. }
  79. public function footer($options = [])
  80. {
  81. if (isset($options["hidden"])) {
  82. $this->parts['{footer}'] = '';
  83. return $this;
  84. }
  85. $options = array_merge($this->footerOptions, $options);
  86. if (! isset($this->parts['{error}'])) {
  87. $this->error();
  88. }
  89. $error = $this->parts['{error}'];
  90. if (! isset($this->parts['{hint}'])) {
  91. $this->hint(null);
  92. }
  93. $hint = $this->parts['{hint}'];
  94. $this->parts['{footer}'] = Html::tag("div", $hint . "\n" . $error, $options);
  95. return $this;
  96. }
  97. }
  98. ?>