-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActiveCheckColumn.php
83 lines (63 loc) · 2.4 KB
/
ActiveCheckColumn.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
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
<?php
namespace bvanleeuwen1995\checkColumn;
use Yii;
use yii\base\ErrorException;
use yii\grid\DataColumn;
use yii\helpers\BaseHtml;
use yii\helpers\Url;
class ActiveCheckColumn extends DataColumn
{
/**
* The value we expect when the field does not have a value
* @var mixed
*/
public $unsetValue = null;
public $setTemplate = '{check}, {content}';
public $setCheck = '<i class="fa fa-check-square-o text-success" aria-hidden="true"></i>';
public $setContent;
public $setUrl = null;
public function init()
{
if (!isset(Yii::$app->i18n->translations['ActiveCheckColumn'])) {
Yii::$app->i18n->translations['ActiveCheckColumn'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@vendor/bvanleeuwen/yii2-grid-check-column/messages'
];
}
// Get the view
$view = $this->grid->getView();
// Register the asset bundle
ActiveCheckColumnAsset::register($view);
// Set the filter options
$this->filter = [
false => Yii::t('ActiveCheckColumn', 'No value'),
true => Yii::t('ActiveCheckColumn', 'Has value')
];
$this->filterInputOptions = [
'prompt' => Yii::t('ActiveCheckColumn', 'Choose status'),
'class' => 'form-control',
'id' => null
];
// Load custom information for this instance
parent::init(); // TODO: Change the autogenerated stub
// Check to make sure that we have a URL set
if ($this->setUrl === null) {
throw new ErrorException('URL not set');
}
// Translate the URL
$this->setUrl = Url::to($this->setUrl);
}
public function renderDataCellContent($model, $key, $index)
{
// Set the attribute in a nice variable that we can use
$sAttribute = $this->attribute;
// Does the field have the unset value
if ($model->$sAttribute == $this->unsetValue) {
return BaseHtml::checkbox('actionColumChecbox', false, ['data-id' => $model->id, 'class' => 'checkActionColumn', 'data-saveurl' => $this->setUrl]);
}
$this->setContent = Yii::$app->formatter->asDatetime($model->$sAttribute, 'medium');
// Return the set content
return strtr($this->setTemplate, ['{check}' => $this->setCheck, '{content}' => $this->setContent]);
}
}