Completed
Pull Request — master (#1)
by Nic
03:25
created

ProductWishListControllerExtension::delete()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 10
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 6
nop 1
crap 20
1
<?php
2
3
/**
4
 * Class ProductWishListControllerExtension
5
 */
6
class ProductWishListControllerExtension extends Extension
7
{
8
9
    /**
10
     * @var array
11
     */
12
    private static $allowed_actions = [
13
        'ProductWishListForm',
14
        'update',
15
        'view',
16
    ];
17
18
    /**
19
     * @param null $wishList
20
     *
21
     * @return Form
22
     */
23 1
    public function ProductWishListForm($wishList = null)
24
    {
25 1
        $wishList = ($wishList !== null && $wishList instanceof ProductWishList && $wishList->exists()) ? $wishList : Injector::inst()->get('ProductWishList');
26
27 1
        $controller = $this->getController();
28
29 1
        $form = ProductWishListForm::create($controller, 'ProductWishListForm');
30
31 1
        if ($wishList->getIsEditing()) {
32
            $form->Fields()->push(HiddenField::create('ID')->setValue($wishList->ID));
33
        }
34
35 1
        $form->loadDataFrom($wishList);
36
37 1
        $this->owner->extend('updateProductWishListForm', $form);
38
39 1
        return $form;
40
    }
41
42
    /**
43
     * @return ContentController|Object
44
     */
45 1
    protected function getController()
46
    {
47 1
        return ($this->owner->hasMethod('getCurrentPage'))
48 1
            ? $this->owner->getCurrentPage()
49 1
            : $this->owner;
50
    }
51
52
    /**
53
     * @param $data
54
     * @param Form $form
55
     *
56
     * @return SS_HTTPResponse
57
     */
58
    public function doProcessWishList($data, Form $form)
59
    {
60
        $wishList = (isset($data['ID'])) ? ProductWishList::get()->byID($data['ID']) : ProductWishList::create();
61
        $form->saveInto($wishList);
62
        if ($wishList->write()) {
63
            return $this->getController()->redirect($wishList->Link());
64
        }
65
66
        return $this->getController()->redirect($this->getController()->Link('error'));
67
    }
68
69
    /**
70
     * @param SS_HTTPRequest|null $request
71
     *
72
     * @return mixed
73
     */
74
    public function update(SS_HTTPRequest $request = null)
75
    {
76
        if ($request === null) {
77
            $request = $this->getController()->getRequest();
78
        }
79
80
        $wishList = ProductWishList::get()->filter('URLSegment', $request->param('ID'))->first();
81
82
        return $this->owner->customise([
83
            'ProductWishListForm' => $this->ProductWishListForm($wishList),
84
        ]);
85
    }
86
87
    /**
88
     * @param SS_HTTPRequest|null $request
89
     *
90
     * @return mixed
91
     */
92
    public function delete(SS_HTTPRequest $request = null)
93
    {
94
        if ($request === null) {
95
            $request = $this->getController()->getRequest();
96
        }
97
98
        if ($wishList = ProductWishList::get()->byID($request->param('ID'))) {
99
            if ($wishList->canDelete()) {
100
                $wishList->delete();
101
102
                return $this->getController()->redirect($this->getController()->Link());
103
            }
104
        }
105
106
        return $this->getController()->redirect($this->getController()->Link('error'));
107
    }
108
109
    /**
110
     * @param SS_HTTPRequest|null $request
111
     *
112
     * @return ViewableData_Customised|SS_HTTPResponse
113
     */
114
    public function view(SS_HTTPRequest $request = null)
115
    {
116
        if ($request === null) {
117
            $request = $this->getController()->getRequest();
118
        }
119
        $urlSegment = $request->param('ID');
120
121
        if ($record = ProductWishList::get()->filter('URLSegment', $urlSegment)->first()) {
122
            return $this->getController()->customise([
123
                'WishList' => $record,
124
                'Breadcrumbs' => $record->Breadcrumbs(),
125
                'ProductWishListForm' => false,
126
            ]);
127
        }
128
129
        return $this->getController()->httpError('404');
130
    }
131
132
}