1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Products\Page; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\RedirectorPage; |
6
|
|
|
use SilverStripe\CMS\Model\VirtualPage; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\NumericField; |
9
|
|
|
use SilverStripe\Security\Security; |
10
|
|
|
|
11
|
|
|
class ProductCategory extends \Page |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private static $db = [ |
|
|
|
|
17
|
|
|
'ProductsPerPage' => 'Int', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private static $defaults = [ |
|
|
|
|
24
|
|
|
'ProductsPerPage' => 12, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private static $table_name = 'ProductCategory'; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private static $allowed_children = [ |
|
|
|
|
36
|
|
|
ProductCategory::class, |
37
|
|
|
Product::class, |
38
|
|
|
RedirectorPage::class, |
39
|
|
|
VirtualPage::class, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return \SilverStripe\Forms\FieldList |
44
|
|
|
*/ |
45
|
|
|
public function getCMSFields() |
46
|
|
|
{ |
47
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
48
|
|
|
$fields->addFieldToTab( |
49
|
|
|
'Root.Main', |
50
|
|
|
NumericField::create('ProductsPerPage') |
51
|
|
|
->setTitle(_t(__CLASS__ . '.ProductsPerPage', 'Products Per Page')), |
52
|
|
|
'Content' |
53
|
|
|
); |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
return parent::getCMSFields(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
public function getProductList() |
63
|
|
|
{ |
64
|
|
|
$categories = ProductCategory::get()->filter('ParentID', $this->data()->ID)->column('ID'); |
65
|
|
|
$categories[] = $this->data()->ID; |
66
|
|
|
$products = Product::get() |
67
|
|
|
->filterAny([ |
68
|
|
|
'ParentID' => $categories, |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
$this->extend('updateProductList', $products, $categories); |
72
|
|
|
|
73
|
|
|
$products = $products->filterByCallback(function ($page) { |
74
|
|
|
return $page->canView(Security::getCurrentUser()); |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
return $products; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|