1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Form; |
4
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Model\FoxyStripeSetting; |
6
|
|
|
use Dynamic\FoxyStripe\Page\ProductPage; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Control\HTTPRequest; |
9
|
|
|
use SilverStripe\Forms\NumericField; |
10
|
|
|
use SilverStripe\View\Requirements; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class QuantityField |
14
|
|
|
* @package Dynamic\FoxyStripe\Form |
15
|
|
|
*/ |
16
|
|
|
class QuantityField extends NumericField |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private static $allowed_actions = [ |
|
|
|
|
22
|
|
|
'newvalue', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $properties |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function Field($properties = []) |
30
|
|
|
{ |
31
|
|
|
//Requirements::javascript('dynamic/foxystripe: javascript/quantity.js'); |
32
|
|
|
//Requirements::css('dynamic/foxystripe: client/dist/css/quantityfield.css'); |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
$this->setAttribute('data-link', $this->Link('newvalue')); |
36
|
|
|
$this->setAttribute('data-code', $this->getProduct()->Code); |
37
|
|
|
$this->setAttribute('data-id', $this->getProduct()->ID); |
38
|
|
|
|
39
|
|
|
return parent::Field($properties); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return ProductPage |
44
|
|
|
*/ |
45
|
|
|
public function getProduct() |
46
|
|
|
{ |
47
|
|
|
return $this->getForm()->getProduct(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param SS_HTTPRequest $request |
|
|
|
|
52
|
|
|
* @return bool|string |
53
|
|
|
*/ |
54
|
|
|
public function newvalue(HTTPRequest $request) |
55
|
|
|
{ |
56
|
|
|
if (!$value = $request->getVar('value')) { |
57
|
|
|
return ''; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!$code = $request->getVar('code')) { |
61
|
|
|
return ''; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->extend('updateQuantity', $value); |
65
|
|
|
|
66
|
|
|
$data = array( |
67
|
|
|
'quantity' => $value, |
68
|
|
|
'quantityGenerated' => ProductPage::getGeneratedValue($code, 'quantity', $value, 'value'), |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$this->extend('updateData', $data); |
72
|
|
|
return json_encode($data); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|