/
PersistCheckBoxColumn.php
84 lines (71 loc) · 2.05 KB
/
PersistCheckBoxColumn.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
84
<?php
/**
* @link http://www.yiiframework.com/
* @license http://www.yiiframework.com/license/
*/
namespace stivehu\grid;
use Closure;
use stivehu\enhancedcookie\EnhancedCookie;
use stivehu\rangecomp\Rangecomp;
use yii\base\InvalidValueException;
use yii\grid\CheckboxColumn;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* PersistCheckBoxColumn create persistent checkbox
*
* @author Gábor István
*/
class PersistCheckBoxColumn extends CheckboxColumn
{
/**
* @var string
*
*/
public $cookieName = 'selected';
/**
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
\Yii::$app->view->registerJs('var cookieName = "' . $this->cookieName . '"', \yii\web\View::POS_HEAD);
Asserts::register($this->grid->getView());
return parent::init();
}
/**
* @param mixed $model
* @param mixed $key
* @param int $index
* @return string
*/
protected function renderDataCellContent($model, $key, $index): string
{
if ($this->checkboxOptions instanceof Closure) {
$options = call_user_func($this->checkboxOptions, $model, $key, $index, $this);
} else {
$options = $this->checkboxOptions;
if (!isset($options['value'])) {
$options['value'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $key;
}
}
$checked = false;
try {
$bigCookie = EnhancedCookie::getBigCookie($this->cookieName);
if ((null !== $bigCookie) && ($selectedItem = Rangecomp::rangeDeCompress(Json::decode($bigCookie)))) {
if (in_array($model->id, $selectedItem)) {
$checked = true;
}
}
} catch (\yii\base\InvalidArgumentException $ex) {
}
return Html::checkbox($this->name, $checked, $options);
}
/**
* No need summary cell
* @return type null
*/
public function renderPageSummaryCell()
{
return null;
}
}