RemoveFromWishListForm::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 29
ccs 0
cts 18
cp 0
rs 9.7
cc 2
nc 2
nop 3
crap 6
1
<?php
2
3
namespace Dynamic\Wishlist\Form;
4
5
use Dynamic\Wishlist\Model\ProductWishList;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\Forms\FormAction;
11
use SilverStripe\Forms\HiddenField;
12
use SilverStripe\Security\Security;
13
14
/**
15
 * Class RemoveFromWishListForm
16
 * @package Dynamic\Wishlist\Form
17
 */
18
class RemoveFromWishListForm extends Form
19
{
20
    /**
21
     * RemoveFromWishListForm constructor.
22
     * @param \SilverStripe\Control\Controller $controller
23
     * @param string $name Defaults to AddToWishListForm
24
     * @param int $productID An easy way to override the id of the product to be added. If one is not provided the current page id will be used instead.
25
     */
26
    public function __construct(Controller $controller, $name = 'RemoveFromWishListForm', $productID = 0)
27
    {
28
        if ($productID < 1) {
29
            $productID = $controller->ID;
30
        }
31
32
        $lists = ProductWishList::get()->filter([
33
            'MemberID' => Security::getCurrentUser()->ID,
34
        ]);
35
        $this->extend('updateWishLists', $lists);
36
37
        $fields = FieldList::create(
38
            HiddenField::create('ProductID', 'ProductID', $productID),
39
            DropdownField::create('List', 'Wish List', $lists->map('ID', 'Title'))
40
        );
41
        $this->extend('updateFields', $fields, $productID);
42
43
        $actions = FieldList::create(
44
            FormAction::create('removeFromWishList')->setTitle('Remove From List')
45
        );
46
        $this->extend('updateActions', $actions, $productID);
47
48
        parent::__construct($controller, $name, $fields, $actions);
49
50
        // makes a unique id for each wishlist form
51
        $reflection = new \ReflectionClass($this);
52
        $shortName = str_replace(array('.', '/'), '', $this->getName());
53
        $this->setHTMLID($reflection->getShortName() . '_' . $shortName . '_' . $productID);
54
        $this->addExtraClass('remove-from-wishlist-form');
55
    }
56
}
57