-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
98 lines (80 loc) · 2.82 KB
/
Module.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace panix\mod\wishlist;
use panix\mod\wishlist\models\WishList;
use Yii;
use panix\engine\WebModule;
use panix\mod\wishlist\components\WishListComponent;
use panix\mod\wishlist\models\WishListProducts;
use yii\base\BootstrapInterface;
use yii\caching\DbDependency;
/**
* Class Module
*
* @property int $count
* @package panix\mod\wishlist
*/
class Module extends WebModule implements BootstrapInterface
{
private $_user_id;
public $count = 0;
private $_model;
private $_ids;
public function bootstrap($app)
{
if (Yii::$app->id != 'console') {
$app->urlManager->addRules(
[
'wishlist' => 'wishlist/default/index',
'wishlist/add/<id:\d+>' => 'wishlist/default/add',
'wishlist/remove/<id:\d+>' => 'wishlist/default/remove',
'wishlist/view/<key:[0-9a-zA-Z]+>' => 'wishlist/default/view',
],
false
);
$this->_user_id = Yii::$app->user->id;
if (!Yii::$app->request->headers->has('filter-callback-ajax')) {
$app->setComponents([
'wishlist' => ['class' => 'panix\mod\wishlist\components\WishListComponent'],
]);
if ($this->_model === null) {
// $model = WishList::findOne(['user_id'=>$this->getUserId()]);
$model = WishList::find()
->where(['user_id' => $this->getUserId()])
->one();
if ($model === null) {
$model = new WishList;
$model->creater($this->getUserId());
}
$this->_model = $model;
$table = WishListProducts::tableName();
$dependency = new DbDependency(['sql' => "SELECT COUNT(*) FROM {$table} WHERE wishlist_id=" . $this->getModel()->id]);
$this->_ids = Yii::$app->db->createCommand("SELECT product_id FROM {$table} WHERE wishlist_id=:id")
->bindValue(':id', $this->getModel()->id)
//->cache($this->cacheDuration, $dependency)
->queryColumn();
}
}
/*$this->count = (new WishListComponent)->count();*/
}
}
public function getUserId()
{
return $this->_user_id;
}
public function getModel()
{
return $this->_model;
}
public function getIds()
{
if (Yii::$app->user->isGuest) {
$session = Yii::$app->session;
if (isset($session['wishlist_products'])) {
return array_unique($session['wishlist_products']);
}
} else {
return $this->_ids;
}
return [];
}
}