Passed
Branch ss4 (364df2)
by Simon
01:53
created

SiteConfigExtension::updateCMSFields()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 29
nc 2
nop 1
dl 0
loc 39
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\StripeSlack\Extension;
4
5
use Firesphere\StripeSlack\Controller\SlackStatusController;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Forms\CheckboxField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\LiteralField;
12
use SilverStripe\Forms\PasswordField;
13
use SilverStripe\Forms\ReadonlyField;
14
use SilverStripe\Forms\TextField;
15
use SilverStripe\Forms\TreeDropdownField;
16
use SilverStripe\ORM\DataExtension;
17
use SilverStripe\SiteConfig\SiteConfig;
18
19
/**
20
 * Class StripeSlackSiteConfigExtension
21
 *
22
 * @property SiteConfig|SiteConfigExtension $owner
23
 * @property string $SlackURL
24
 * @property string $SlackClientID
25
 * @property string $SlackClientSecret
26
 * @property string $SlackChannel
27
 * @property string $SlackToken
28
 * @property boolean $ClearSecrets
29
 * @property int $SlackBackURLID
30
 * @property int $SlackErrorBackURLID
31
 * @method SiteTree SlackBackURL()
32
 * @method SiteTree SlackErrorBackURL()
33
 */
34
class SiteConfigExtension extends DataExtension
35
{
36
    private static $db = [
1 ignored issue
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
        'SlackURL'          => 'Varchar(255)',
38
        'SlackClientID'     => 'Varchar(255)',
39
        'SlackClientSecret' => 'Varchar(255)',
40
        'SlackChannel'      => 'Varchar(255)',
41
        'SlackToken'        => 'Varchar(255)',
42
        // This is here to support the IDE's
43
        'ClearSecrets'      => 'Boolean(false)'
44
    ];
45
46
    private static $has_one = [
0 ignored issues
show
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
47
        'SlackBackURL'      => SiteTree::class,
48
        'SlackErrorBackURL' => SiteTree::class
49
    ];
50
51
    private static $helptexts = [
52
        'Link'         => 'To activate your StripeSlack, Please have an admin activate StripeSlack by clicking on this link.',
53
        'ClearSecrets' => 'Checking this checkbox will clear out the Client Secret and (invisible in the CMS) Client Token. In case your admin left the group, or you want a new admin to sent the invite',
54
        'Channel'      => 'You can get the ID by right clicking on your channel and select "copy link". Open the copied link in a browser and copy the part after "messages/" in to this field',
55
        'URLHelp'      => 'Include the "https://" part',
56
        'Extended'     => '<p><a href="https://github.com/Firesphere/silverstripe-stripeslack/blob/master/readme.md">Extensive instructions can be found on GitHub</a></p>'
57
    ];
58
59
    public function updateFieldLabels(&$labels)
60
    {
61
        $labels['SlackURL'] = _t('StripeSlackSiteConfigExtension.SlackURL', 'URL Of the Slack channel');
62
        $labels['SlackClientID'] = _t('StripeSlackSiteConfigExtension.SlackClientID', 'Client ID for your Slack App');
63
        $labels['SlackClientSecret'] = _t(
64
            'StripeSlackSiteConfigExtension.SlackClientSecret',
65
            'Client Secret for your Slack App'
66
        );
67
        $labels['SlackChannel'] = _t('StripeSlackSiteConfigExtension.SlackChannel', 'The ID of your channel');
68
        $labels['ClearSecrets'] = _t('StripeSlackSiteConfigExtension.ClearSecrets', 'Clear Secrets and Tokens');
69
        $labels['SlackBackURL'] = _t(
70
            'StripeSlackSiteConfigExtension.SlackBackURL',
71
            'URL to redirect when request is successful (leave empty if you want use the Stripe Slack Page)'
72
        );
73
        $labels['SlackErrorBackURL'] = _t(
74
            'StripeSlackSiteConfigExtension.SlackErrorBackURL',
75
            'URL to redirect when request is unsuccessful (leave empty if you want to use the Stripe Slack Page)'
76
        );
77
    }
78
79
    public function updateCMSFields(FieldList $fields)
80
    {
81
        $fields->removeByName(['SlackToken']);
82
        $fields->addFieldsToTab('Root.Slack', [
83
            $url = TextField::create('SlackURL', $this->owner->fieldLabel('SlackURL')),
84
            $channel = TextField::create('SlackChannel', $this->owner->fieldLabel('SlackChannel')),
85
            TextField::create('SlackClientID', $this->owner->fieldLabel('SlackClientID')),
86
            PasswordField::create('SlackClientSecret', $this->owner->fieldLabel('SlackClientSecret')),
87
            TreeDropdownField::create('SlackBackURLID', $this->owner->fieldLabel('SlackBackURL'), SiteTree::class),
88
            TreeDropdownField::create('SlackErrorBackURLID', $this->owner->fieldLabel('SlackErrorBackURL'), SiteTree::class),
89
        ]);
90
        if (
91
            $this->owner->SlackURL &&
92
            $this->owner->SlackClientID &&
93
            $this->owner->SlackClientSecret &&
94
            !$this->owner->SlackToken
95
        ) {
96
            $domain = Director::absoluteURL('/SlackAuthorization/');
97
            $text = LiteralField::create(
98
                'link',
99
                '<p><a href="' . $this->owner->SlackURL . '/oauth/authorize?client_id=' . $this->owner->SlackClientID . '&scope=client&redirect_uri=' . $domain . '">' .
100
                static::$helptexts['Link'] .
101
                '</a></p>'
102
            );
103
            $fields->addFieldToTab('Root.Slack', $text);
104
        } else {
105
            $fields->addFieldsToTab(
106
                'Root.Slack',
107
                [
108
                    $secretField = CheckboxField::create('ClearSecrets', $this->owner->fieldLabel('ClearSecrets')),
109
                    ReadonlyField::create('Usercount', 'Users on slack', SlackStatusController::create()->usercount())
110
                ]
111
            );
112
            $secretField->setDescription(static::$helptexts['ClearSecrets']);
113
        }
114
        $channel->setDescription(static::$helptexts['Channel']);
115
        $url->setDescription(static::$helptexts['URLHelp']);
116
117
        $fields->addFieldToTab('Root.Slack', LiteralField::create('instructions', static::$helptexts['Extended']));
118
    }
119
120
    /**
121
     * Clear out the secrets if the checkbox is checked
122
     */
123
    public function onBeforeWrite()
124
    {
125
        parent::onBeforeWrite();
126
        if ($this->owner->ClearSecrets) {
127
            $this->owner->SlackClientSecret = '';
128
            $this->owner->SlackToken = '';
129
        }
130
        // Always set back to false so the checkbox won't stay ticked
131
        $this->owner->ClearSecrets = false;
132
    }
133
}
134