Blame view

backend/widgets/GridView/gridColumns/ActionColumn.php 3.12 KB
d1f8bd40   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  <?php
  
  namespace backend\widgets\GridView\gridColumns;
  
  use yii\grid\Column;
  use yii\helpers\Html;
  //
  use thread\app\base\models\ActiveRecord;
  
  /**
   * Class ActionColumn
   *
   * @package backend\widgets\GridView\gridColumns
   * @author FilamentV <vortex.filament@gmail.com>
   * @copyright (c), Thread
   */
  class ActionColumn extends Column
  {
  
      /**
       * @var array
       */
      public $contentOptions = [
          'class' => 'text-center',
      ];
  
      /**
       * @var array
       */
      public $headerOptions = [
          'class' => 'text-center'
      ];
  
      /**
       * @var string|array|boll|Closure
       */
      public $updateLink;
  
      /**
       * @var string|array|boll|Closure
       */
      public $deleteLink;
  
      /**
       * @var string
       */
      public $header = 'Actions';
  
      /**
       * @inheritdoc
       */
      protected function renderDataCellContent($model, $key, $index)
      {
          $updateLink = ($this->getUpdateLink($model) !== false) ? Html::a('<i class="fa fa-pencil"></i> ', $this->getUpdateLink($model), ['class' => 'btn btn-success btn-s']) : '';
          $deleteLink = ($this->getDeleteLink($model) !== false) ? Html::a('<i class="fa fa-trash"></i> ', $this->getDeleteLink($model), ['class' => 'btn btn-danger btn-s']) : '';
  
          return '<table style="display: inline-block;"><tr><td>' . $updateLink .
              '</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>' . $deleteLink . '</td></tr></table>';
      }
  
      /**
       * @param $model
       * @return array|bool|mixed
       */
      protected function getUpdateLink($model)
      {
          if ($this->updateLink === false) {
              return false;
          }
          if (!empty($this->updateLink)) {
              if ($this->updateLink instanceof \Closure) {
                  $f = $this->updateLink;
                  return $f($model);
              } else {
                  $r = ['update'];
                  foreach ($this->updateLink as $data) {
                      $r[$data] = $model->$data;
                  }
                  return $r;
              }
          } else {
              return ['update', 'id' => $model->id];
          }
      }
  
      /**
       * @param $model
       * @return array|bool|mixed
       */
      protected function getDeleteLink($model)
      {
          if ($this->deleteLink === false) {
              return false;
          }
          if (!empty($this->deleteLink)) {
              if ($this->deleteLink instanceof \Closure) {
                  $f = $this->deleteLink;
                  return $f($model);
              } else {
                  $r = ['intrash'];
                  foreach ($this->deleteLink as $data) {
                      $r[$data] = $model->$data;
                  }
  
                  return $r;
              }
          } else {
              return ['intrash', 'id' => $model->id];
          }
      }
  
      /**
       * Renders the header cell content.
       * The default implementation simply renders [[header]].
       * This method may be overridden to customize the rendering of the header cell.
       * @return string the rendering result
       */
      protected function renderHeaderCellContent()
      {
          return trim($this->header) !== '' ? \Yii::t('app', $this->header) : $this->getHeaderCellLabel();
      }
  }