Completed
Pull Request — master (#1867)
by
unknown
02:55
created

UnsplashBgUpdaterPlugin::configMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © 2019 Denis Vadimov aka BloodyAltair
4
 */
5
6
    /** @noinspection PhpUndefinedClassInspection */
7
8
    include_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
9
10
    use Crew\Unsplash\HttpClient;
11
    use Crew\Unsplash\Photo;
12
    use RainLoop\Enumerations\PluginPropertyType;
13
    use RainLoop\Plugins\AbstractPlugin;
14
    use RainLoop\Plugins\Property;
15
16
    /** @noinspection SpellCheckingInspection */
17
18
class UnsplashBgUpdaterPlugin extends AbstractPlugin {
19
20
    public function Init() {
21
        $this->addJs ('js/unsplashbg.min.js');
22
        $this->addJs ('js/plugin.js');
23
        /**
24
         * Admin JS
25
         */
26
        $this->addJs ('js/unsplashbg.min.js', true);
27
        $this->addJs ('js/plugin.js', true);
28
29
        $this->addAjaxHook('getNewImage', 'getNewImage');
30
    }
31
32
    /**
33
     * Gets new image from Unsplash API
34
     * @return \RainLoop\Plugins\AbstractPlugin
35
     */
36
    public function getNewImage() {
37
        if(is_array($response = $this->checkConfiguration ())) {
38
            $response['success'] = false;
39
        } else {
40
            try {
41
                /** @noinspection PhpUndefinedClassInspection */
42
                HttpClient::init ([
43
                    'applicationId' => $this->Config ()->Get ('plugin', 'AccessToken', NULL),
44
                    'utmSource' => $this->Config ()->Get ('plugin', 'AppName', NULL)
45
                ]);
46
                /** @noinspection PhpUndefinedClassInspection */
47
                $data = Photo::random ([
48
                    'query' => $this->Config ()->Get ('plugin', 'Query', NULL),
49
                    'w' => $this->Config ()->Get ('plugin', 'Width', NULL),
50
                    'h' => $this->Config ()->Get ('plugin', 'Height', NULL)
51
                ]);
52
                /** @noinspection PhpUndefinedFieldInspection */
53
                $response = [
54
                    'success' => true,
55
                    'url' => $data->urls['custom'],
56
                    'image_user_name' => $data->user['name'],
57
                    'image_user_url' => $data->user['links']['html']
58
                ];
59
                if ($data === null)
60
                    $response = [
61
                        "success" => false,
62
                        "error" => "API unreachable. Probably reached requests limit."
63
                    ];
64
            } catch (Exception $e) {
65
                $response = [
66
                    'success' => false,
67
                    'error' => $e->getMessage (),
68
                ];
69
            }
70
        }
71
        return $this->ajaxResponse(__FUNCTION__, $response);
72
    }
73
74
    /**
75
     * Checks configuration. Returned array means error
76
     * @return array|bool
77
     */
78
    public function checkConfiguration() {
79
        $access_token   = $this->Config()->Get('plugin', 'AccessToken', null);
80
        $app_name       = $this->Config()->Get('plugin', 'AppName', null);
81
        $width          = $this->Config()->Get('plugin', 'Width', -1);
82
        $height         = $this->Config()->Get('plugin', 'Height', -1);
83
        $check          = $this->Config()->Get('plugin', 'Check', false);
84
        $update_rate    = $this->Config()->Get('plugin', 'UpdateRate', -1);
85
86
        $return = true;
87
        if(!$access_token) {
88
            $return = [
89
                'error' => "[Settings] Access token is mandatory!"
90
            ];
91
        }
92
        if(!$app_name) {
93
            $return = [
94
                'error' => "[Settings] You must supply app name!"
95
            ];
96
        }
97
        if(!$width || !$height) {
98
            $return = [
99
                'error' => "[Settings] Width and height are mandatory positive integers!"
100
            ];
101
        }
102
        if(!$check) {
103
            $return = [
104
                'error' => "[Settings] Toggle checkbox on settings page :)"
105
            ];
106
        }
107
        if(!$update_rate) {
108
            $return = [
109
                'error' => "[Settings] Update rate is mandatory positive integer!"
110
            ];
111
        }
112
        return $return;
113
    }
114
115
    /**
116
     * @return array
117
     */
118
    public function configMapping() {
119
        return [
120
            Property::NewInstance('AccessToken')->SetDescription ('You can get it by registering an application on https://unsplash.com/developers')
121
                ->SetLabel('Unsplash access key')
122
                ->SetType(PluginPropertyType::STRING)
123
                ->SetAllowedInJs (false),
124
            Property::NewInstance('AppName')->SetDescription ('(e.g "My Awesome BG Updater") You must supply this parameter: it is Unsplash API terms requirement')
125
                ->SetLabel('Name of your API application')
126
                ->SetType(PluginPropertyType::STRING)
127
                ->SetAllowedInJs (true),
128
            Property::NewInstance('UpdateRate')->SetDescription ('Note: different types of API applications allowed to use different request rate. 
129
                Read API docs at https://unsplash.com/documentation#rate-limiting')
130
                ->SetLabel('Background update rate (seconds)')
131
                ->SetType(PluginPropertyType::INT)
132
                ->SetDefaultValue (120)
133
                ->SetAllowedInJs (true),
134
            Property::NewInstance('Query')->SetLabel('Custom search query')
135
                ->SetType(PluginPropertyType::STRING)
136
                ->SetAllowedInJs (false),
137
            Property::NewInstance('Width')->SetLabel('Image width')
138
                ->SetType(PluginPropertyType::INT)
139
                ->SetDefaultValue (1920)
140
                ->SetAllowedInJs (true),
141
            Property::NewInstance('Height')->SetLabel('Image height')
142
                ->SetType(PluginPropertyType::INT)
143
                ->SetDefaultValue (1080)
144
                ->SetAllowedInJs (true),
145
            Property::NewInstance ('Check')->SetLabel ('I will not forget to install the theme `Default` or any other that looks good with this plugin')
146
                ->SetType (PluginPropertyType::BOOL)
147
                ->SetDefaultValue (false)
148
                ->SetAllowedInJs (true),
149
        ];
150
    }
151
}
152
153