Settings::validateSettings()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
3
/**
4
 * @package Social Login
5
 * @author Iurii Makukh
6
 * @copyright Copyright (c) 2017, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\social_login\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
use gplcart\modules\oauth\models\Oauth;
14
15
/**
16
 * Handles incoming requests and outputs data related to Social Login module
17
 */
18
class Settings extends Controller
19
{
20
21
    /**
22
     * Oauth model instance
23
     * @var \gplcart\modules\oauth\models\Oauth $oauth
24
     */
25
    protected $oauth;
26
27
    /**
28
     * Settings constructor.
29
     * @param Oauth $oauth
30
     */
31
    public function __construct(Oauth $oauth)
32
    {
33
        parent::__construct();
34
35
        $this->oauth = $oauth;
36
    }
37
38
    /**
39
     * Route page callback to display the module settings page
40
     */
41
    public function editSettings()
42
    {
43
        $this->setTitleEditSettings();
44
        $this->setBreadcrumbEditSettings();
45
46
        $this->setData('settings', $this->module->getSettings('social_login'));
47
        $this->setData('providers', $this->oauth->getProviders(array('type' => 'login')));
48
49
        $this->submitSettings();
50
        $this->outputEditSettings();
51
    }
52
53
    /**
54
     * Set title on the module settings page
55
     */
56
    protected function setTitleEditSettings()
57
    {
58
        $title = $this->text('Edit %name settings', array(
59
            '%name' => $this->text('Social Login')));
60
61
        $this->setTitle($title);
62
    }
63
64
    /**
65
     * Set breadcrumbs on the module settings page
66
     */
67
    protected function setBreadcrumbEditSettings()
68
    {
69
        $breadcrumbs = array();
70
71
        $breadcrumbs[] = array(
72
            'text' => $this->text('Dashboard'),
73
            'url' => $this->url('admin')
74
        );
75
76
        $breadcrumbs[] = array(
77
            'text' => $this->text('Modules'),
78
            'url' => $this->url('admin/module/list')
79
        );
80
81
        $this->setBreadcrumbs($breadcrumbs);
82
    }
83
84
    /**
85
     * Saves the submitted settings
86
     */
87
    protected function submitSettings()
88
    {
89
        if ($this->isPosted('save') && $this->validateSettings()) {
90
            $this->updateSettings();
91
        }
92
    }
93
94
    /**
95
     * Validate submitted module settings
96
     */
97
    protected function validateSettings()
98
    {
99
        $this->setSubmitted('settings');
100
101
        $this->setSubmittedBool('register');
102
        $this->setSubmittedBool('register_login');
103
        $this->setSubmittedBool('register_status');
104
105
        if ($this->getSubmitted('register_login')) {
106
            $this->setSubmitted('register_status', true);
107
        }
108
109
        return !$this->hasErrors();
110
    }
111
112
    /**
113
     * Update module settings
114
     */
115
    protected function updateSettings()
116
    {
117
        $this->controlAccess('module_edit');
118
        $this->module->setSettings('social_login', $this->getSubmitted());
119
        $this->redirect('', $this->text('Settings have been updated'), 'success');
120
    }
121
122
    /**
123
     * Render and output the module settings page
124
     */
125
    protected function outputEditSettings()
126
    {
127
        $this->output('social_login|settings');
128
    }
129
130
}
131