|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverCommerce\OrdersAdmin\Model; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
|
6
|
|
|
use SilverStripe\Control\Controller; |
|
7
|
|
|
use SilverStripe\Forms\ReadonlyField; |
|
8
|
|
|
use SilverStripe\Security\Group; |
|
9
|
|
|
use SilverStripe\Core\Convert; |
|
10
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
|
11
|
|
|
use SilverCommerce\OrdersAdmin\Control\ShoppingCart; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class Discount extends DataObject |
|
14
|
|
|
{ |
|
15
|
|
|
private static $table_name = 'Discount'; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
private static $db = array( |
|
|
|
|
|
|
18
|
|
|
"Title" => "Varchar", |
|
19
|
|
|
"Type" => "Enum('Fixed,Percentage,Free Shipping','Percentage')", |
|
20
|
|
|
"Code" => "Varchar(299)", |
|
21
|
|
|
"Amount" => "Decimal", |
|
22
|
|
|
"Country" => "Varchar(255)", |
|
23
|
|
|
"Expires" => "Date" |
|
24
|
|
|
); |
|
25
|
|
|
|
|
26
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
27
|
|
|
"Site" => SiteConfig::class |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
private static $many_many = array( |
|
|
|
|
|
|
31
|
|
|
"Groups" => Group::class |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
35
|
|
|
"Title", |
|
36
|
|
|
"Code", |
|
37
|
|
|
"Expires" |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Generate a random string that we can use for the code by default |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
private static function generateRandomString($length = 10) |
|
46
|
|
|
{ |
|
47
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
48
|
|
|
$string = ''; |
|
49
|
|
|
|
|
50
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
51
|
|
|
$string .= $characters[rand(0, strlen($characters) - 1)]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $string; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Set more complex default data |
|
59
|
|
|
*/ |
|
60
|
|
|
public function populateDefaults() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->setField('Code', self::generateRandomString()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Return a URL that allows this code to be added to a cart |
|
67
|
|
|
* automatically |
|
68
|
|
|
* |
|
69
|
|
|
* @return String |
|
70
|
|
|
*/ |
|
71
|
|
|
public function AddLink() |
|
72
|
|
|
{ |
|
73
|
|
|
$cart = ShoppingCart::get(); |
|
74
|
|
|
|
|
75
|
|
|
return Controller::join_links( |
|
76
|
|
|
$cart->AbsoluteLink("usediscount"), |
|
77
|
|
|
$this->Code |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getCMSFields() |
|
82
|
|
|
{ |
|
83
|
|
|
|
|
84
|
|
|
$this->beforeUpdateCMSFields(function ($fields) { |
|
85
|
|
|
if ($this->Code) { |
|
86
|
|
|
$fields->addFieldToTab( |
|
87
|
|
|
"Root.Main", |
|
88
|
|
|
ReadonlyField::create( |
|
89
|
|
|
"DiscountURL", |
|
|
|
|
|
|
90
|
|
|
_t("OrdersAdmin.AddDiscountURL", "Add discount URL"), |
|
91
|
|
|
$this->AddLink() |
|
92
|
|
|
), |
|
93
|
|
|
"Code" |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
}); |
|
97
|
|
|
|
|
98
|
|
|
return parent::getCMSFields(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function onBeforeWrite() |
|
102
|
|
|
{ |
|
103
|
|
|
parent::onBeforeWrite(); |
|
104
|
|
|
|
|
105
|
|
|
// Ensure that the code is URL safe |
|
106
|
|
|
$this->Code = Convert::raw2url($this->Code); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function canView($member = null, $context = []) |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
$extended = $this->extendedCan('canView', $member); |
|
112
|
|
|
if ($extended !== null) { |
|
113
|
|
|
return $extended; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function canCreate($member = null, $context = []) |
|
120
|
|
|
{ |
|
121
|
|
|
$extended = $this->extendedCan('canCreate', $member); |
|
122
|
|
|
if ($extended !== null) { |
|
123
|
|
|
return $extended; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function canEdit($member = null, $context = []) |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
$extended = $this->extendedCan('canEdit', $member); |
|
132
|
|
|
if ($extended !== null) { |
|
133
|
|
|
return $extended; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return true; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function canDelete($member = null, $context = []) |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
$extended = $this->extendedCan('canDelete', $member); |
|
142
|
|
|
if ($extended !== null) { |
|
143
|
|
|
return $extended; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths