Passed
Push — master ( 0b0446...99fa3b )
by Nic
12:39
created

DonationProductController::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Dynamic\FoxyStripe\Page;
4
5
use Dynamic\FoxyStripe\Page\ProductPage;
6
use Dynamic\FoxyStripe\Page\ProductPageController;
7
use Dynamic\FoxyStripe\Form\FoxyStripePurchaseForm;
8
use Dynamic\FoxyStripe\Model\FoxyStripeSetting;
9
use SilverStripe\View\Requirements;
10
use SilverStripe\Forms\CurrencyField;
11
use SilverStripe\Control\Director;
12
13
/**
14
 * Class DonationProductController
15
 *
16
 * @mixin \Dynamic\FoxyStripe\Page\DonationProduct
17
 */
18
class DonationProductController extends ProductPageController
19
{
20
    /**
21
     * @var array
22
     */
23
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
24
        'PurchaseForm',
25
        'updatevalue',
26
    ];
27
28
    /**
29
     *
30
     */
31
    public function init()
32
    {
33
        parent::init();
34
        //Requirements::javascript('framework/thirdparty/jquery/jquery.js');
35
    }
36
37
    /**
38
     * @return FoxyStripePurchaseForm
39
     */
40
    public function PurchaseForm()
41
    {
42
        $form = parent::PurchaseForm();
43
44
        $fields = $form->Fields();
45
46
        $fields->dataFieldByName('price')
47
            ->setAttribute('id', 'price');
48
49
        $fields->insertAfter('price', $currencyField = CurrencyField::create('x:visiblePrice', 'Amount'));
50
51
        $currencyField->setAttribute('id', 'visiblePrice');
52
53
        $fields->removeByName([
54
            'x:visibleQuantity',
55
            ProductPage::getGeneratedValue($this->Code, 'weight', $this->Weight),
0 ignored issues
show
Bug Best Practice introduced by
The property Code does not exist on Dynamic\FoxyStripe\Page\DonationProductController. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property Weight does not exist on Dynamic\FoxyStripe\Page\DonationProductController. Since you implemented __get, consider adding a @property annotation.
Loading history...
56
        ]);
57
58
        if (FoxyStripeSetting::current_foxystripe_setting()->CartValidation) {
59
            Requirements::javascript("silverstripe/userforms:client/dist/js/jquery-validation/jquery.validate.min.js");
60
            Requirements::javascriptTemplate('_resources/vendor/dynamic/foxystripe/javascript/donationProduct.js', [
61
                'Trigger' => (string)$currencyField->getAttribute('id'),
62
                'UpdateURL' => Director::absoluteURL($this->Link('updatevalue')),
63
            ]);
64
        }
65
66
        $form->setAttribute('data-updateurl', $this->Link('updatevalue'));
67
68
        return $form;
69
    }
70
71
    /**
72
     * create new encrypted price value based on user input.
73
     *
74
     * @param $request
75
     *
76
     * @return string|\SilverStripe\Control\HTTPResponse
77
     */
78
    public function updatevalue(\SilverStripe\Control\HTTPRequest $request)
79
    {
80
        if ($request->getVar('Price') && FoxyStripeSetting::current_foxystripe_setting()->CartValidation) {
81
            $price = $request->getVar('Price');
82
            $signedPrice = \FoxyCart_Helper::fc_hash_value($this->Code, 'price', $price, 'value', false);
0 ignored issues
show
Bug Best Practice introduced by
The property Code does not exist on Dynamic\FoxyStripe\Page\DonationProductController. Since you implemented __get, consider adding a @property annotation.
Loading history...
83
            $json = json_encode(['Price' => $signedPrice]);
84
85
            $this->response->setBody($json);
86
            $this->response->addHeader('Content-Type', 'application/json');
87
88
            return $this->response;
89
        }
90
91
        return 'false';
92
    }
93
}
94