StatusColumn.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2016-03-21 19:36
  7. */
  8. namespace backend\grid;
  9. use Closure;
  10. use yii;
  11. use InvalidArgumentException;
  12. use common\libs\Constants;
  13. use yii\helpers\Html;
  14. use yii\helpers\Url;
  15. /**
  16. * @inheritdoc
  17. */
  18. class StatusColumn extends DataColumn
  19. {
  20. public $format = 'raw';
  21. public $attribute = 'status';
  22. public $headerOptions = ['width' => '25px'];
  23. public $url = '';
  24. public $text = '';
  25. public $aOptions = [];
  26. public $yesClass = "label-primary";
  27. public $noClass = "label-default";
  28. public $formName = "";
  29. /**
  30. * @inheritdoc
  31. */
  32. public function init()
  33. {
  34. parent::init();
  35. if( empty($this->aOptions) ){
  36. if( $this->url !== false ){
  37. $this->aOptions = array_merge($this->aOptions, [
  38. 'data-method' => 'post',
  39. 'data-pjax' => '0',
  40. ]);
  41. }
  42. }
  43. if( !$this->content && $this->content !== false ) {
  44. $this->content = function ($model, $key, $index, $gridView) {
  45. /* @var $model array|yii\db\ActiveRecord */
  46. $field = $this->attribute;
  47. if ($this->text === '') {
  48. $text = Constants::getYesNoItems($model[$field]);
  49. } else {
  50. if ($this->text instanceof Closure) {
  51. $text = call_user_func($this->text, $model, $key, $index, $gridView);
  52. } else {
  53. $text = $this->text;
  54. }
  55. }
  56. if (!is_string($text)) throw new InvalidArgumentException("No status valued {$model[$field]}");
  57. if ($this->url === false) {
  58. $url = '';
  59. } else {
  60. if ($this->url == '') {
  61. $url = Url::to(['update', 'id' => $model['id']]);
  62. } else {
  63. if ($this->url instanceof Closure) {
  64. $url = call_user_func($this->url, $model, $key, $index, $gridView);
  65. } else {
  66. $url = $this->url;
  67. }
  68. }
  69. }
  70. $aOptions = [];
  71. if ($url != '') {
  72. if (!isset($this->aOptions['data-params'])) {
  73. $aOptions = array_merge([
  74. 'data-params' => [
  75. $this->formName ? $this->formName : (strpos(strrev($model->formName()), 'hcraeS') === 0 ? strrev(substr(strrev($model->formName()), 6)) : $model->formName()) . "[{$field}]" => $model[$field] == Constants::YesNo_Yes ? Constants::YesNo_No : Constants::YesNo_Yes],
  76. ], $this->aOptions, $aOptions);
  77. }
  78. if (!isset($this->aOptions['class'])) {
  79. $class = $model[$field] == Constants::YesNo_Yes ? $this->yesClass : $this->noClass;
  80. $aOptions = array_merge([
  81. 'class' => 'label ' . $class,
  82. ], $this->aOptions, $aOptions);
  83. }
  84. if (!isset($this->aOptions['data-confirm'])) {
  85. $aOptions = array_merge([
  86. 'data-confirm' => $model[$field] == Constants::YesNo_Yes ? Yii::t('app', 'Are you sure you want to disable this item?') : Yii::t('app', 'Are you sure you want to enable this item?'),
  87. ], $this->aOptions, $aOptions);
  88. }
  89. }
  90. return Html::a($text, $url, $aOptions);
  91. };
  92. }
  93. }
  94. }