Settings::setTitleEditSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Autocomplete search
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\autocomplete_search\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
14
/**
15
 * Handles incoming requests and outputs data related to Autocomplete search 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
        $this->setData('settings', $this->module->getSettings('autocomplete_search'));
28
        $this->submitSettings();
29
        $this->outputEditSettings();
30
    }
31
32
    /**
33
     * Set title on the module settings page
34
     */
35
    protected function setTitleEditSettings()
36
    {
37
        $title = $this->text('Edit %name settings', array('%name' => $this->text('Autocomplete search')));
38
        $this->setTitle($title);
39
    }
40
41
    /**
42
     * Set breadcrumbs on the module settings page
43
     */
44
    protected function setBreadcrumbEditSettings()
45
    {
46
        $breadcrumbs = array();
47
48
        $breadcrumbs[] = array(
49
            'text' => $this->text('Dashboard'),
50
            'url' => $this->url('admin')
51
        );
52
53
        $breadcrumbs[] = array(
54
            'text' => $this->text('Modules'),
55
            'url' => $this->url('admin/module/list')
56
        );
57
58
        $this->setBreadcrumbs($breadcrumbs);
59
    }
60
61
    /**
62
     * Saves the submitted settings
63
     */
64
    protected function submitSettings()
65
    {
66
        if ($this->isPosted('save') && $this->validateSettings()) {
67
            $this->updateSettings();
68
        }
69
    }
70
71
    /**
72
     * Validate submitted module settings
73
     */
74
    protected function validateSettings()
75
    {
76
        $this->setSubmitted('settings', null, false);
77
78
        $this->validateElement('selector', 'required');
79
        $this->validateElement('min_length', 'numeric');
80
        $this->validateElement('max_result', 'numeric');
81
82
        return !$this->hasErrors();
83
    }
84
85
    /**
86
     * Update module settings
87
     */
88
    protected function updateSettings()
89
    {
90
        $this->controlAccess('module_edit');
91
        $this->module->setSettings('autocomplete_search', $this->getSubmitted());
92
        $this->redirect('', $this->text('Settings have been updated'), 'success');
93
    }
94
95
    /**
96
     * Render and output the module settings page
97
     */
98
    protected function outputEditSettings()
99
    {
100
        $this->output('autocomplete_search|settings');
101
    }
102
103
}
104