These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * BuyableStockDecorator |
||
5 | * Extension for any buyable - adding stock level capabilities. |
||
6 | */ |
||
7 | |||
8 | class BuyableStockDecorator extends DataExtension |
||
0 ignored issues
–
show
|
|||
9 | { |
||
10 | |||
11 | /** |
||
12 | * Array of Class Names of classes that are buyables |
||
13 | * @to do, do we need this here, why not use the original buyable dod. |
||
14 | * @var Array |
||
15 | * |
||
16 | */ |
||
17 | private static $buyables = array(); |
||
0 ignored issues
–
show
|
|||
18 | |||
19 | /** |
||
20 | * Selector for field in the HTML where quantities are set |
||
21 | * @to do - move this out to template... |
||
22 | * @var String |
||
23 | * |
||
24 | */ |
||
25 | private static $quantity_field_selector = ""; |
||
0 ignored issues
–
show
|
|||
26 | |||
27 | /** |
||
28 | * Standard SS method |
||
29 | * |
||
30 | * @return Array |
||
31 | */ |
||
32 | private static $db = array( |
||
0 ignored issues
–
show
|
|||
33 | 'MinQuantity' => 'Int', |
||
34 | 'MaxQuantity' => 'Int', |
||
35 | 'UnlimitedStock' => 'Boolean' |
||
36 | ); |
||
37 | |||
38 | private static $casting = array( |
||
0 ignored issues
–
show
|
|||
39 | 'ActualQuantity' => 'Int' |
||
40 | ); |
||
41 | |||
42 | private static $defaults = array( |
||
0 ignored issues
–
show
|
|||
43 | 'UnlimitedStock' => 1, |
||
44 | 'MinQuantity' => 0, |
||
45 | 'MaxQuantity' => 0 |
||
46 | ); |
||
47 | |||
48 | /* |
||
49 | * Allow setting stock level in CMS |
||
50 | */ |
||
51 | public function updateCMSFields(FieldList $fields) |
||
52 | { |
||
53 | $tabName = 'Root.Stock'; |
||
54 | $fields->addFieldsToTab( |
||
55 | $tabName, |
||
56 | array( |
||
57 | new HeaderField('MinMaxHeader', 'Minimum and Maximum Quantities per Order', 3), |
||
58 | new NumericField('MinQuantity', 'Min. Qty per order'), |
||
59 | new NumericField('MaxQuantity', 'Max. Qty per order'), |
||
60 | new HeaderField('ActualQuantityHeader', 'Stock available', 3), |
||
61 | new CheckboxField('UnlimitedStock', 'Unlimited Stock'), |
||
62 | new NumericField('ActualQuantity', 'Actual Stock Available', $this->getActualQuantity()), |
||
63 | new HeaderField('ActualQuantityAdjustmentHeader', 'Adjust all stock', 3), |
||
64 | new LiteralField('ActualQuantityAdjustmentLink', 'This CMS also provides a <a href="/update-stock/" target="_blank">quick stock adjuster</a>.') |
||
65 | ) |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | /* |
||
70 | * Getter for stock level |
||
71 | */ |
||
72 | public function ActualQuantity() |
||
0 ignored issues
–
show
The return type could not be reliably inferred; please add a
@return annotation.
Our type inference engine in quite powerful, but sometimes the code does not
provide enough clues to go by. In these cases we request you to add a
Loading history...
|
|||
73 | { |
||
74 | return $this->getActualQuantity(); |
||
75 | } |
||
76 | public function getActualQuantity() |
||
0 ignored issues
–
show
The return type could not be reliably inferred; please add a
@return annotation.
Our type inference engine in quite powerful, but sometimes the code does not
provide enough clues to go by. In these cases we request you to add a
Loading history...
|
|||
77 | { |
||
78 | return BuyableStockCalculatedQuantity::get_quantity_by_buyable($this->owner); |
||
79 | } |
||
80 | |||
81 | /* |
||
82 | * Setter for stock level |
||
83 | * @param int $value |
||
84 | */ |
||
85 | public function setActualQuantity($value) |
||
86 | { |
||
87 | if (!$this->owner->ID) { |
||
88 | $this->owner->write(); |
||
89 | } |
||
90 | //only set stock level if it differs from previous |
||
91 | $shopAdminCode = EcommerceConfig::get("EcommerceRole", "admin_permission_code"); |
||
92 | if ($this->owner->ID) { |
||
93 | if ($shopAdminCode && Permission::check($shopAdminCode)) { |
||
94 | if ($value != $this->owner->getActualQuantity()) { |
||
95 | $parent = BuyableStockCalculatedQuantity::get_by_buyable($this->owner); |
||
96 | if ($parent) { |
||
97 | $member = Member::currentUser(); |
||
98 | $obj = new BuyableStockManualUpdate(); |
||
99 | $obj->ParentID = $parent->ID; |
||
0 ignored issues
–
show
The property
ParentID does not exist on object<BuyableStockManualUpdate> . Since you implemented __set , maybe consider adding a @property annotation.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property.
Loading history...
|
|||
100 | $obj->Quantity = (int)$value; |
||
0 ignored issues
–
show
The property
Quantity does not exist on object<BuyableStockManualUpdate> . Since you implemented __set , maybe consider adding a @property annotation.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property.
Loading history...
|
|||
101 | $obj->MemberID = $member->ID; |
||
0 ignored issues
–
show
The property
MemberID does not exist on object<BuyableStockManualUpdate> . Since you implemented __set , maybe consider adding a @property annotation.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property.
Loading history...
|
|||
102 | $obj->write(); |
||
103 | } else { |
||
104 | user_error("Could not write BuyableStockCalculatedQuantity Object because there was no parent ".$this->owner->Title); |
||
105 | } |
||
106 | } |
||
107 | } else { |
||
108 | user_error("Could not write BuyableStockCalculatedQuantity Object because you do not have permissions ".$this->owner->Title); |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | |||
113 | |||
114 | /** |
||
115 | * This is a pivotal method. |
||
116 | * Only allow purchase if stock levels allow |
||
117 | * TODO: customise this to a certain stock level, on, or off |
||
118 | * @return null (equals TRUE!), true or false |
||
0 ignored issues
–
show
|
|||
119 | */ |
||
120 | public function canPurchase($member = null) |
||
0 ignored issues
–
show
|
|||
121 | { |
||
122 | if ($this->owner->UnlimitedStock) { |
||
123 | return null; |
||
124 | } |
||
125 | if ($this->owner->getActualQuantity() < $this->owner->MinQuantity) { |
||
126 | return false; |
||
127 | } |
||
128 | if ($this->owner->getActualQuantity() <= 0) { |
||
129 | return false; |
||
130 | } |
||
131 | return null; //returning null ensures that the value from this method is ignored. |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * stanard SS metehod |
||
136 | */ |
||
137 | public function onAfterWrite() |
||
138 | { |
||
139 | BuyableStockCalculatedQuantity::get_by_buyable($this->owner); |
||
140 | if (isset($_REQUEST["ActualQuantity"])) { |
||
141 | $actualQuantity = intval($_REQUEST["ActualQuantity"]); |
||
142 | if ($actualQuantity != $this->owner->getActualQuantity() && ($actualQuantity === 0 || $actualQuantity)) { |
||
143 | $this->owner->setActualQuantity($actualQuantity); |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | |||
149 | class BuyableStockDecorator_Extension extends Extension |
||
0 ignored issues
–
show
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.
You can fix this by adding a namespace to your class: namespace YourVendor;
class YourClass { }
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.
Loading history...
|
|||
150 | { |
||
151 | /** |
||
152 | * TO DO: review method below |
||
153 | * - move to init??? |
||
154 | * - decorate Ecommerce Quantity Field? |
||
155 | * |
||
156 | **/ |
||
157 | |||
158 | |||
159 | public function index() |
||
160 | { |
||
161 | $min = 0; |
||
162 | $max = 0; |
||
163 | $msg = Config::inst()->get("MinMaxModifier", "sorry_message"); |
||
164 | $fieldSelector = Config::inst()->get("BuyableStockDecorator", "quantity_field_selector"); |
||
165 | if ($minField = Config::inst()->get("MinMaxModifier", "min_field")) { |
||
166 | $min = $this->owner->$minField; |
||
167 | } |
||
168 | if ($maxField = Config::inst()->get("MinMaxModifier", "max_field")) { |
||
169 | $max = $this->owner->$maxField; |
||
170 | } |
||
171 | $js = 'MinMaxModifier.add_item("'.$fieldSelector.'", '.intval($min).', '.intval($max).', "'.addslashes($msg).'");'; |
||
172 | Requirements::javascript("ecommerce_stockcontrol/javascript/MinMaxModifier.js"); |
||
173 | Requirements::customScript($js, $fieldSelector); |
||
174 | return array(); |
||
175 | } |
||
176 | } |
||
177 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.