History.php
1.24 KB
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
<?php
namespace artbox\catalog\components;
use artbox\catalog\models\Variant;
use yii\base\Object;
use yii\db\ActiveQuery;
class History extends Object
{
/**
* Add Variant to history
*
* @param int $variantId
*/
public function add(int $variantId)
{
$history = $this->get();
if (!in_array($variantId, $history)) {
array_push($history, $variantId);
}
\Yii::$app->session->set('history', $history);
}
/**
* Get variantIds from history
*
* @return array
*/
public function get(): array
{
return \Yii::$app->session->get('history', []);
}
/**
* Get Variants query from history
*
* @return \yii\db\ActiveQuery
*/
public function getModels(): ActiveQuery
{
return Variant::find()
->where(
[
'id' => $this->get(),
'status' => true,
]
);
}
}