1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Discounts\Tests\TestOnly\Extension; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Discounts\Tests\TestOnly\Page\ProductPage; |
6
|
|
|
use SilverStripe\Dev\TestOnly; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
10
|
|
|
use SilverStripe\ORM\DataExtension; |
11
|
|
|
use SilverStripe\Versioned\GridFieldArchiveAction; |
12
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class TestDiscountExtension |
16
|
|
|
* @package Dynamic\Foxy\Discounts\Tests\TestOnly\Extension |
17
|
|
|
*/ |
18
|
|
|
class TestDiscountExtension extends DataExtension implements TestOnly |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private static $many_many = [ |
|
|
|
|
24
|
|
|
'Products' => ProductPage::class, |
25
|
|
|
'ExcludeProducts' => ProductPage::class, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param FieldList $fields |
30
|
|
|
*/ |
31
|
|
|
public function updateCMSFields(FieldList $fields) |
32
|
|
|
{ |
33
|
|
|
if ($this->owner->ID) { |
34
|
|
|
// Products |
35
|
|
|
$field = $fields->dataFieldByName('Products'); |
36
|
|
|
$fields->removeByName('Products'); |
37
|
|
|
$fields->addFieldToTab('Root.Included', $field); |
38
|
|
|
$field->setDescription('Limit the discount to these products.'); |
39
|
|
|
$config = $field->getConfig(); |
40
|
|
|
$config |
41
|
|
|
->removeComponentsByType([ |
42
|
|
|
GridFieldAddExistingAutocompleter::class, |
43
|
|
|
GridFieldAddNewButton::class, |
44
|
|
|
GridFieldArchiveAction::class, |
45
|
|
|
]) |
46
|
|
|
->addComponents([ |
47
|
|
|
new GridFieldAddExistingSearchButton(), |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
$exclusions = $fields->dataFieldByName('ExcludeProducts'); |
51
|
|
|
$fields->removeByName('ExcludeProducts'); |
52
|
|
|
$fields->addFieldToTab('Root.Excluded', $exclusions); |
53
|
|
|
$exclusions->setDescription('Products in this list will ALWAYS be excluded from the discount.'); |
54
|
|
|
$excludeConfig = $exclusions->getConfig(); |
55
|
|
|
$excludeConfig |
56
|
|
|
->removeComponentsByType([ |
57
|
|
|
GridFieldAddExistingAutocompleter::class, |
58
|
|
|
GridFieldAddNewButton::class, |
59
|
|
|
GridFieldArchiveAction::class, |
60
|
|
|
]) |
61
|
|
|
->addComponents([ |
62
|
|
|
new GridFieldAddExistingSearchButton(), |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function getRestrictions() |
71
|
|
|
{ |
72
|
|
|
if ($this->owner->Products()->count() == 0) { |
73
|
|
|
$products = ProductPage::get()->column(); |
74
|
|
|
} else { |
75
|
|
|
$products = $this->owner->Products()->column(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
foreach ($this->owner->ExcludeProducts()->column() as $id) { |
79
|
|
|
if (in_array($id, $products)) { |
80
|
|
|
$key = array_search($id, $products); |
81
|
|
|
unset($products[$key]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $products; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|