Completed
Push — master ( 7f0603...aacf62 )
by Iurii
01:13
created

Settings::validateGaProfileSettings()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 5
nop 0
1
<?php
2
3
/**
4
 * @package Google Analytics Report
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\ga_report\controllers;
11
12
use gplcart\core\controllers\backend\Controller as BackendController;
13
use gplcart\modules\ga_report\models\Report as GaReportModuleReportModel;
14
15
/**
16
 * Handles incoming requests and outputs data related to Google Analytics Report module
17
 */
18
class Settings extends BackendController
19
{
20
21
    /**
22
     * Google Analytics Report Report model instance
23
     * @var \gplcart\modules\ga_report\models\Report $ga_report_model
24
     */
25
    protected $ga_report_model;
26
27
    /**
28
     * @param GaReportModuleReportModel $ga_report_model
29
     */
30
    public function __construct(GaReportModuleReportModel $ga_report_model)
31
    {
32
        parent::__construct();
33
34
        $this->ga_report_model = $ga_report_model;
35
    }
36
37
    /**
38
     * Route page callback
39
     * Displays the module settings page
40
     */
41
    public function editSettings()
42
    {
43
        $this->setTitleEditSettings();
44
        $this->setBreadcrumbEditSettings();
45
46
        $this->setData('stores', $this->store->getList());
47
        $this->setData('credentials', $this->getCredentialSettings());
48
        $this->setData('handlers', $this->ga_report_model->getHandlers());
49
        $this->setData('settings', $this->module->getSettings('ga_report'));
50
51
        $this->submitSettings();
52
        $this->outputEditSettings();
53
    }
54
55
    /**
56
     * Returns an array of Google API credentials
57
     * @return array
58
     */
59
    protected function getCredentialSettings()
60
    {
61
        /** @var \gplcart\modules\gapi\Main $instance */
62
        $instance = $this->module->getInstance('gapi');
63
        return $instance->getCredentials(array('type' => 'service'));
64
    }
65
66
    /**
67
     * Set title on the module settings page
68
     */
69
    protected function setTitleEditSettings()
70
    {
71
        $title = $this->text('Edit %name settings', array(
72
            '%name' => $this->text('Google Analytics Report')));
73
74
        $this->setTitle($title);
75
    }
76
77
    /**
78
     * Set breadcrumbs on the module settings page
79
     */
80
    protected function setBreadcrumbEditSettings()
81
    {
82
        $breadcrumbs = array();
83
84
        $breadcrumbs[] = array(
85
            'url' => $this->url('admin'),
86
            'text' => $this->text('Dashboard')
87
        );
88
89
        $breadcrumbs[] = array(
90
            'text' => $this->text('Modules'),
91
            'url' => $this->url('admin/module/list')
92
        );
93
94
        $this->setBreadcrumbs($breadcrumbs);
95
    }
96
97
    /**
98
     * Saves the submitted settings
99
     */
100
    protected function submitSettings()
101
    {
102
        if ($this->isPosted('clear_cache')) {
103
            $this->deleteCacheSettings();
104
        } else if ($this->isPosted('save') && $this->validateSettings()) {
105
            $this->updateSettings();
106
        }
107
    }
108
109
    /**
110
     * Deletes all Google Analytics cached data
111
     */
112
    protected function deleteCacheSettings()
113
    {
114
        $this->ga_report_model->clearCache();
115
        $this->redirect('', $this->text('Cache has been deleted'), 'success');
116
    }
117
118
    /**
119
     * Validate submitted module settings
120
     */
121
    protected function validateSettings()
122
    {
123
        $this->setSubmitted('settings');
124
125
        $this->validateElement('limit', 'regexp', '/^[\d]{1,3}$/');
126
        $this->validateElement('cache', 'regexp', '/^[\d]{1,8}$/');
127
        $this->validateElement('credential_id', 'regexp', '/^[\d]{1,10}$/');
128
129
        $this->validateGaProfileSettings();
130
131
        return !$this->hasErrors();
132
    }
133
134
    /**
135
     * Validates Google Analytics profiles
136
     */
137
    protected function validateGaProfileSettings()
138
    {
139
        $profiles = $this->getSubmitted('ga_profile_id', array());
140
141
        if (empty($profiles)) {
142
            $this->setError('ga_profile_id', $this->text('Profile ID is required'));
143
            return false;
144
        }
145
146
        $stores = $this->store->getList();
147
148
        foreach ($profiles as $store_id => $profile_id) {
149
150
            if (empty($profile_id)) {
151
                $this->setError('ga_profile_id', $this->text('Profile ID is required'));
152
                return false;
153
            }
154
155
            if (empty($stores[$store_id])) {
156
                $this->setError('ga_profile_id', $this->text('Unknown store ID'));
157
                return false;
158
            }
159
        }
160
161
        return true;
162
    }
163
164
    /**
165
     * Update module settings
166
     */
167
    protected function updateSettings()
168
    {
169
        $this->controlAccess('module_edit');
170
171
        $this->module->setSettings('ga_report', $this->getSubmitted());
172
        $this->redirect('', $this->text('Settings have been updated'), 'success');
173
    }
174
175
    /**
176
     * Render and output the module settings page
177
     */
178
    protected function outputEditSettings()
179
    {
180
        $this->output('ga_report|settings');
181
    }
182
183
}
184