Settings::outputEditSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Web Hook
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 GNU General Public License 3.0
8
 */
9
10
namespace gplcart\modules\webhook\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
14
/**
15
 * Handles incoming requests and outputs data related to Web Hook module
16
 */
17
class Settings extends Controller
18
{
19
20
    /**
21
     * Route page callback to display the module settings page
22
     */
23
    public function editSettings()
24
    {
25
        $this->setTitleEditSettings();
26
        $this->setBreadcrumbEditSettings();
27
28
        $this->setData('hooks', $this->getHookNames());
29
        $this->setData('settings', $this->module->getSettings('webhook'));
30
31
        $this->submitSettings();
32
        $this->outputEditSettings();
33
    }
34
35
    /**
36
     * Set title on the module settings page
37
     */
38
    protected function setTitleEditSettings()
39
    {
40
        $title = $this->text('Edit %name settings', array('%name' => $this->text('Web Hook')));
41
        $this->setTitle($title);
42
    }
43
44
    /**
45
     * Set breadcrumbs on the module settings page
46
     */
47
    protected function setBreadcrumbEditSettings()
48
    {
49
        $breadcrumbs = array();
50
51
        $breadcrumbs[] = array(
52
            'text' => $this->text('Dashboard'),
53
            'url' => $this->url('admin')
54
        );
55
56
        $breadcrumbs[] = array(
57
            'text' => $this->text('Modules'),
58
            'url' => $this->url('admin/module/list')
59
        );
60
61
        $this->setBreadcrumbs($breadcrumbs);
62
    }
63
64
    /**
65
     * Saves the submitted settings
66
     */
67
    protected function submitSettings()
68
    {
69
        if ($this->isPosted('save') && $this->validateSettings()) {
70
            $this->updateSettings();
71
        }
72
    }
73
74
    /**
75
     * Update module settings
76
     */
77
    protected function updateSettings()
78
    {
79
        $this->controlAccess('module_edit');
80
        $this->module->setSettings('webhook', $this->getSubmitted());
81
        $this->redirect('', $this->text('Settings have been updated'), 'success');
82
    }
83
84
    /**
85
     * Validate submitted module settings
86
     */
87
    protected function validateSettings()
88
    {
89
        $this->setSubmitted('settings');
90
91
        if ($this->getSubmitted('sender', '') === '') {
92
            $this->setError('sender', $this->text('Sender is required'));
93
        }
94
95
        if ($this->getSubmitted('key', '') !== '' && $this->getSubmitted('salt', '') === '') {
96
            $this->setError('salt', $this->text('Salt is required'));
97
        }
98
99
        if (!$this->url->isAbsolute($this->getSubmitted('url'))) {
100
            $this->setError('url', $this->text('URL does not look valid'));
101
        }
102
103
        return !$this->hasErrors();
104
    }
105
106
    /**
107
     * Render and output the module settings page
108
     */
109
    protected function outputEditSettings()
110
    {
111
        $this->output('webhook|settings');
112
    }
113
114
    /**
115
     * Returns an array of hook names
116
     * @return array
117
     */
118
    protected function getHookNames()
119
    {
120
        $class = $this->module->getClass('webhook');
121
        $excluded = array('hookRouteList');
122
123
        $list = array();
124
125
        foreach ($this->module->getHooks($class) as $hook) {
126
            if (!in_array($hook, $excluded)) {
127
                // Split capitalized parts
128
                $parts = preg_split('/(?=[A-Z])/', $hook);
129
                array_shift($parts); // Skip "hook" prefix
130
                $list[$hook] = implode(' ', $parts);
131
            }
132
        }
133
134
        return gplcart_array_split($list, 4);
135
    }
136
137
}
138