PriceModifier::setPriceModification()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * PriceModifier.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 31/03/17
7
 */
8
9
10
namespace Broarm\EventTickets;
11
12
use DataObject;
13
use FieldList;
14
use ManyManyList;
15
use SQLUpdate;
16
use Tab;
17
use TabSet;
18
use TextField;
19
20
/**
21
 * Class PriceModifier
22
 *
23
 * @package Broarm\EventTickets
24
 *
25
 * @property string Title
26
 * @property float  PriceModification
27
 * @method ManyManyList Reservations
28
 */
29
class PriceModifier extends DataObject implements PriceModifierInterface
30
{
31
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
32
        'Title' => 'Varchar(255)',
33
        'Sort' => 'Int'
34
    );
35
36
    private static $default_sort = 'Sort ASC, ID DESC';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
37
38
    private static $defaults = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
39
        'Sort' => 0
40
    );
41
42
    private static $many_many = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
43
        'Reservations' => 'Broarm\EventTickets\Reservation'
44
    );
45
46
    private static $many_many_extraFields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
47
        'Reservations' => array(
48
            'PriceModification' => 'Currency'
49
        )
50
    );
51
52
    private static $casting = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
53
        'TableValue' => 'Currency'
54
    );
55
56
    private static $summary_fields = array(
57
        'TableTitle',
58
        'TableValue'
59
    );
60
61
    public function getCMSFields()
62
    {
63
        $fields = new FieldList(new TabSet('Root', $mainTab = new Tab('Main')));
64
        $fields->addFieldsToTab('Root.Main', array(
65
            TextField::create('Title', _t('PriceModifier.TITLE', 'Title'))
66
        ));
67
68
        $this->extend('updateCMSFields', $fields);
69
        return $fields;
70
    }
71
72
    /**
73
     * Modify the given total
74
     * Implement this on your modifier
75
     *
76
     * @param float $total
77
     * @param Reservation $reservation
78
     */
79
    public function updateTotal(&$total, Reservation $reservation) {}
80
81
    /**
82
     * Return a title to display in the summary table
83
     *
84
     * @return string
85
     */
86
    public function getTableTitle()
87
    {
88
        return $this->Title;
89
    }
90
91
    /**
92
     * Return a value to display in the summary table
93
     *
94
     * By default go out from a price reduction.
95
     * if you created a modifier that adds value, like a shipping calculator, make sure to overwrite this method
96
     *
97
     * @return float
98
     */
99
    public function getTableValue()
100
    {
101
        return $this->PriceModification * -1;
102
    }
103
104
    /**
105
     * Set the price modification on the join
106
     *
107
     * @param $value
108
     */
109
    public function setPriceModification($value)
110
    {
111
        if ($this->exists()) {
112
            $join = $this->manyMany('Reservations');
113
            $table = end($join);
114
            $where = $this->getSourceQueryParam('Foreign.Filter');
115
            $where["`{$this->baseTable()}ID`"] = $this->ID;
116
            SQLUpdate::create(
117
                "`{$table}`",
118
                array('`PriceModification`' => $value),
119
                $where
120
            )->execute();
121
        }
122
    }
123
}
124