Completed
Push — master ( 6219d6...964648 )
by Iurii
01:04
created

Settings::prepareDataEditSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/**
4
 * @package Error Notifier
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\error_notifier\controllers;
11
12
use gplcart\core\models\Module as ModuleModel;
13
use gplcart\core\controllers\backend\Controller as BackendController;
14
15
/**
16
 * Handles incoming requests and outputs data related to Error Notifier module
17
 */
18
class Settings extends BackendController
19
{
20
21
    /**
22
     * Module model instance
23
     * @var \gplcart\core\models\Module $module
24
     */
25
    protected $module;
26
27
    /**
28
     * @param ModuleModel $module
29
     */
30
    public function __construct(ModuleModel $module)
31
    {
32
        parent::__construct();
33
34
        $this->module = $module;
35
    }
36
37
    /**
38
     * Route page callback to display the module settings page
39
     */
40
    public function editSettings()
41
    {
42
        $this->setTitleEditSettings();
43
        $this->setBreadcrumbEditSettings();
44
45
        $this->setData('settings', $this->config->module('error_notifier'));
46
47
        $this->submitSettings();
48
        $this->prepareDataEditSettings();
49
50
        $this->outputEditSettings();
51
    }
52
53
    /**
54
     * Prepare data before passing to templates
55
     */
56
    protected function prepareDataEditSettings()
57
    {
58
        $recipients = $this->getData('settings.recipient');
59
60
        if (is_array($recipients)) {
61
            $this->setData('settings.recipient', implode("\n", $recipients));
62
        }
63
    }
64
65
    /**
66
     * Set title on the module settings page
67
     */
68
    protected function setTitleEditSettings()
69
    {
70
        $vars = array('%name' => $this->text('Error Notifier'));
71
        $title = $this->text('Edit %name settings', $vars);
72
        $this->setTitle($title);
73
    }
74
75
    /**
76
     * Set breadcrumbs on the module settings page
77
     */
78
    protected function setBreadcrumbEditSettings()
79
    {
80
        $breadcrumbs = array();
81
82
        $breadcrumbs[] = array(
83
            'text' => $this->text('Dashboard'),
84
            'url' => $this->url('admin')
85
        );
86
87
        $breadcrumbs[] = array(
88
            'text' => $this->text('Modules'),
89
            'url' => $this->url('admin/module/list')
90
        );
91
92
        $this->setBreadcrumbs($breadcrumbs);
93
    }
94
95
    /**
96
     * Saves the submitted settings
97
     */
98
    protected function submitSettings()
99
    {
100
        if ($this->isPosted('save') && $this->validateSettings()) {
101
            $this->updateSettings();
102
        }
103
    }
104
105
    /**
106
     * Validate submitted module settings
107
     */
108
    protected function validateSettings()
109
    {
110
        $this->setSubmitted('settings');
111
        $this->setSubmittedBool('email');
112
113
        $this->validateElement('live_limit', 'integer');
114
        $this->validateElement('email_limit', 'integer');
115
116
        foreach ($this->setSubmittedArray('recipient') as $email) {
117
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
118
                $message = $this->text('@field has invalid value', array('@field' => $this->text('Recipients')));
119
                $this->setError('recipient', $message);
120
                break;
121
            }
122
        }
123
124
        return !$this->hasErrors();
125
    }
126
127
    /**
128
     * Update module settings
129
     */
130
    protected function updateSettings()
131
    {
132
        $this->controlAccess('module_edit');
133
        $this->module->setSettings('error_notifier', $this->getSubmitted());
134
        $this->redirect('', $this->text('Settings have been updated'), 'success');
135
    }
136
137
    /**
138
     * Render and output the module settings page
139
     */
140
    protected function outputEditSettings()
141
    {
142
        $this->output('error_notifier|settings');
143
    }
144
145
}
146