-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDataColumn.php
47 lines (39 loc) · 1.4 KB
/
DataColumn.php
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
<?php
namespace enigmatix\widgets;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* Class DataColumn
* @package enigmatix\widgets
* @author Joel Small
* @email [email protected]
*
* This class wraps each column value in a link if a link is specified in the widget options
*/
class DataColumn extends \yii\grid\DataColumn
{
protected function renderDataCellContent($model, $key, $index)
{
$content = parent::renderDataCellContent($model, $key, $index);
return Html::a($content,$this->getViewUrl($model),['class' => 'btn-block','data-pjax' => 0]);
}
protected function getViewUrl($model)
{
$func = ArrayHelper::getValue($this->options, 'getViewLink');
if (is_callable($func))
return call_user_func($func, $model);
if (method_exists($model, 'getViewLink')){
$link = call_user_func([$model, 'getViewLink']);
if (!empty($link))
return $link;
}
if (method_exists($model, 'getController')){
$controller = call_user_func([$model, 'getController']);
return [$controller . '/view', 'id' => $model->id];
}
throw new InvalidConfigException("Method getViewLink does not exist in model or in widget options"
. " and model does not have a 'getController' method to guess the URL");
}
}
?>