Passed
Pull Request — master (#12)
by Matthew
01:33
created

Setting::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Model;
4
5
use Dynamic\Foxy\Admin\FoxyAdmin;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\FormAction;
8
use SilverStripe\Forms\ReadonlyField;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\DB;
11
use SilverStripe\ORM\ValidationException;
12
use SilverStripe\Security\Permission;
13
use SilverStripe\Security\PermissionProvider;
14
use SilverStripe\Security\Security;
15
use SilverStripe\View\TemplateGlobalProvider;
16
17
/**
18
 * Class Setting
19
 * @package Dynamic\Foxy\Model
20
 *
21
 * @property string $StoreKey
22
 */
23
class Setting extends DataObject implements PermissionProvider, TemplateGlobalProvider
24
{
25
    /**
26
     * @var string
27
     */
28
    private static $singular_name = 'FoxyStripe Setting';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
29
    /**
30
     * @var string
31
     */
32
    private static $plural_name = 'FoxyStripe Settings';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
33
    /**
34
     * @var string
35
     */
36
    private static $description = 'Update the settings for your store';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
37
38
    /**
39
     * @var string
40
     */
41
    private static $table_name = 'FoxyStripeSetting';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
42
43
    /**
44
     * @var string
45
     */
46
    private static $keyPrefix = "dYnm1c";
0 ignored issues
show
introduced by
The private property $keyPrefix is not used, and could be removed.
Loading history...
47
48
    /**
49
     * @var array
50
     */
51
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
52
        'StoreKey' => 'Varchar(60)',
53
        // TODO
54
    ];
55
56
    /**
57
     * Default permission to check for 'LoggedInUsers' to create or edit pages.
58
     *
59
     * @var array
60
     * @config
61
     */
62
    private static $required_permission = ['CMS_ACCESS_CMSMain', 'CMS_ACCESS_LeftAndMain'];
0 ignored issues
show
introduced by
The private property $required_permission is not used, and could be removed.
Loading history...
63
64
    /**
65
     * @return FieldList
66
     */
67
    public function getCMSFields()
68
    {
69
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
70
            // TODO
71
            $fields->addFieldsToTab('Root.Main', [
72
            ]);
73
74
            $fields->addFieldsToTab('Root.Advanced', [
75
                ReadonlyField::create('StoreKey', 'Store Key', $this->StoreKey),
76
            ]);
77
        });
78
79
        return parent::getCMSFields();
80
    }
81
82
    /**
83
     * @return FieldList
84
     */
85
    public function getCMSActions()
86
    {
87
        if (Permission::check('ADMIN') || Permission::check('EDIT_FOXY_SETTING')) {
88
            $actions = new FieldList(
89
                FormAction::create('save_foxystripe_setting', _t(static::class . '.SAVE', 'Save'))
90
                    ->addExtraClass('btn-primary font-icon-save')
91
            );
92
        } else {
93
            $actions = FieldList::create();
94
        }
95
        $this->extend('updateCMSActions', $actions);
96
        return $actions;
97
    }
98
99
    /**
100
     *
101
     */
102
    public function requireDefaultRecords()
103
    {
104
        parent::requireDefaultRecords();
105
        if (!self::current_foxy_setting()) {
106
            self::make_foxy_setting();
107
            DB::alteration_message('Added default FoxyStripe Setting', 'created');
108
        }
109
    }
110
111
    /**
112
     *
113
     */
114
    public function onBeforeWrite()
115
    {
116
        parent::onBeforeWrite();
117
        if (!$this->StoreKey) {
118
            $key = $this->generateStoreKey();
119
            while (!ctype_alnum($key)) {
120
                $key = $this->generateStoreKey();
121
            }
122
            $this->StoreKey = $key;
123
            DB::alteration_message('Created FoxyCart Store Key ' . $key, 'created');
124
        }
125
    }
126
127
    /**
128
     * @param int $count
129
     * @return string
130
     */
131
    public function generateStoreKey($count = 0)
132
    {
133
        $length = $this->obj('StoreKey')->getSize() - strlen($this->config()->get('keyPrefix'));
134
        $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' . strtotime('now');
135
        $strLength = strlen($charset);
136
        $str = '';
137
138
        while ($count < $length) {
139
            $str .= $charset[mt_rand(0, $strLength - 1)];
140
            $count++;
141
        }
142
        return $this->config()->get('keyPrefix') . substr(base64_encode($str), 0, $length);
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    public function CMSEditLink()
149
    {
150
        return FoxyAdmin::singleton()->Link();
151
    }
152
153
    /**
154
     * @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...
155
     *
156
     * @return bool|int|null
157
     */
158
    public function canEdit($member = null)
159
    {
160
        if (!$member) {
0 ignored issues
show
introduced by
$member is of type null, thus it always evaluated to false.
Loading history...
161
            $member = Security::getCurrentUser();
162
        }
163
        $extended = $this->extendedCan('canEdit', $member);
164
        if ($extended !== null) {
165
            return $extended;
166
        }
167
        return Permission::checkMember($member, 'EDIT_FOXY_SETTING');
168
    }
169
170
    /**
171
     * @return array
172
     */
173
    public function providePermissions()
174
    {
175
        return [
176
            'EDIT_FOXY_SETTING' => [
177
                'name' => _t(
178
                    static::class . '.EDIT_FOXY_SETTING',
179
                    'Manage FoxyStripe settings'
180
                ),
181
                'category' => _t(
182
                    static::class . '.PERMISSIONS_FOXY_SETTING',
183
                    'FoxyStripe'
184
                ),
185
                'help' => _t(
186
                    static::class . '.EDIT_PERMISSION_FOXY_SETTING',
187
                    'Ability to edit the settings of a FoxyStripe Store.'
188
                ),
189
                'sort' => 400,
190
            ],
191
        ];
192
    }
193
194
    /**
195
     * Get the current sites {@link GlobalSiteSetting}, and creates a new one
196
     * through {@link make_global_config()} if none is found.
197
     *
198
     * @return self|DataObject
199
     */
200
    public static function current_foxy_setting()
201
    {
202
        if ($config = self::get()->first()) {
203
            return $config;
204
        }
205
        return self::make_foxy_setting();
206
    }
207
208
    /**
209
     * Create {@link GlobalSiteSetting} with defaults from language file.
210
     *
211
     * @return self
212
     */
213
    public static function make_foxy_setting()
214
    {
215
        $config = self::create();
216
        try {
217
            $config->write();
218
        } catch (ValidationException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
219
        }
220
        return $config;
221
    }
222
223
    /**
224
     * Add $GlobalConfig to all SSViewers.
225
     */
226
    public static function get_template_global_variables()
227
    {
228
        return [
229
            'FoxyStripe' => 'current_foxy_setting',
230
        ];
231
    }
232
}
233