ActionColumn.php 3.12 KB
<?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();
    }
}