Completed
Push — master ( 002f4d...61bfd8 )
by Matthew
22:24 queued 07:35
created

src/Model/ProductCartReservation.php (13 issues)

1
<?php
2
3
namespace Dynamic\FoxyStripe\Model;
4
5
use Dynamic\FoxyStripe\Page\ProductPage;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\ReadonlyField;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Security\Permission;
10
use SilverStripe\Security\Security;
11
12
/**
13
 * Class ProductCartReservation
14
 * @package Dynamic\Sheeps\ProductCartExpiry\Model
15
 *
16
 * @property string ReservationCode
17
 */
18
class ProductCartReservation extends DataObject
19
{
20
    /**
21
     * @var string
22
     */
23
    private static $singular_name = 'Product Cart Reservation';
0 ignored issues
show
The private property $singular_name is not used, and could be removed.
Loading history...
24
25
    /**
26
     * @var string
27
     */
28
    private static $plural_name = 'Product Cart Reservations';
0 ignored issues
show
The private property $plural_name is not used, and could be removed.
Loading history...
29
30
    /**
31
     * @var string
32
     */
33
    private static $table_name = 'ProductCartReservation';
0 ignored issues
show
The private property $table_name is not used, and could be removed.
Loading history...
34
35
    /**
36
     * @var array
37
     */
38
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
39
        'ReservationCode' => 'Varchar(255)',
40
        'CartProductID' => 'Int',
41
        'Code' => 'Varchar(255)',
42
        'Expires' => 'DBDatetime',
43
    ];
44
45
    /**
46
     * @var array
47
     */
48
    private static $has_one = [
0 ignored issues
show
The private property $has_one is not used, and could be removed.
Loading history...
49
        'Product' => ProductPage::class,
50
    ];
51
52
    /**
53
     * @var array
54
     */
55
    private static $indexes = [
0 ignored issues
show
The private property $indexes is not used, and could be removed.
Loading history...
56
        'foxystripe-reservation-code' => [
57
            'type' => 'unique',
58
            'columns' => ['ReservationCode'],
59
        ],
60
    ];
61
62
    /**
63
     * @var array
64
     */
65
    private static $summary_fields = [
0 ignored issues
show
The private property $summary_fields is not used, and could be removed.
Loading history...
66
        'ReservationCode' => 'Cart Reservation Record',
67
        'Expires.Nice' => 'Expires',
68
    ];
69
70
    /**
71
     * @return FieldList
72
     */
73
    public function getCMSFields()
74
    {
75
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
76
            $fields->addFieldToTab('Root.Main', ReadonlyField::create('ReservationCode')->setTitle('Reservation Code'));
77
        });
78
79
        return parent::getCMSFields();
80
    }
81
82
    /**
83
     *
84
     */
85
    public function onBeforeWrite()
86
    {
87
        parent::onBeforeWrite();
88
89
        if (!$this->ProductID) {
90
            if ($product = ProductPage::get()->filter('Code', $this->Code)->first()) {
0 ignored issues
show
Bug Best Practice introduced by
The property Code does not exist on Dynamic\FoxyStripe\Model\ProductCartReservation. Since you implemented __get, consider adding a @property annotation.
Loading history...
91
                $this->ProductID = $product->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property ProductID does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
92
            }
93
        }
94
    }
95
96
    /**
97
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
98
     * @param array $context
99
     * @return bool
100
     */
101
    public function canCreate($member = null, $context = [])
102
    {
103
        return false;
104
    }
105
106
    /**
107
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
108
     * @return bool
109
     */
110
    public function canEdit($member = null)
111
    {
112
        return false;
113
    }
114
115
    /**
116
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
117
     * @return bool|int
118
     */
119
    public function canDelete($member = null)
120
    {
121
        return Permission::check('ADMIN', 'any', Security::getCurrentUser());
122
    }
123
124
    /**
125
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
126
     * @return bool
127
     */
128
    public function canView($member = null)
129
    {
130
        return true;
131
    }
132
}
133