-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
WishlistVariable.php
86 lines (66 loc) · 2.14 KB
/
WishlistVariable.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
<?php
namespace verbb\wishlist\variables;
use verbb\wishlist\Wishlist;
use verbb\wishlist\elements\Item;
use verbb\wishlist\elements\ListElement;
use verbb\wishlist\elements\db\ListQuery;
use verbb\wishlist\elements\db\ItemQuery;
class WishlistVariable
{
// Public Methods
// =========================================================================
public function getPlugin(): Wishlist
{
return Wishlist::$plugin;
}
public function getPluginName(): string
{
return Wishlist::$plugin->getPluginName();
}
public function lists($forUser = true, $forceSave = false): ListQuery
{
if ($forUser) {
if ($forceSave) {
Wishlist::$plugin->getLists()->getList(null, true);
}
return Wishlist::$plugin->getLists()->getListQueryForOwner();
}
return ListElement::find();
}
public function items(): ItemQuery
{
return Item::find();
}
public function item($elementId, $listId = null, $listType = null): ?Item
{
$item = null;
$listTypeId = null;
if (!$elementId) {
return null;
}
if ($listType && $listType = Wishlist::$plugin->getListTypes()->getListTypeByHandle($listType)) {
$listTypeId = $listType->id;
}
// Get the list, don't force it to be created yet
$list = Wishlist::$plugin->getLists()->getList($listId, false, $listTypeId);
if ($list->id) {
$item = Item::find()->elementId($elementId)->listId($list->id)->listTypeId($listTypeId)->one();
}
if (!$item) {
$item = WishList::$plugin->getItems()->createItem($elementId, $listId, $listTypeId);
}
return $item;
}
public function getInUserLists($elementId): bool
{
// Get all lists for the current user (session).
$userListIds = Wishlist::$plugin->getLists()->getListQueryForOwner()->ids();
if (!$userListIds) {
return false;
}
return Item::find()
->elementId($elementId)
->listId($userListIds)
->exists();
}
}