Settings   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 112
ccs 0
cts 78
cp 0
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 7 1
A validateForm() 0 28 5
A parse() 0 7 1
A buildForm() 0 54 3
1
<?php
2
3
namespace Backend\Modules\Blog\Actions;
4
5
use Backend\Core\Engine\Base\ActionEdit as BackendBaseActionEdit;
6
use Backend\Core\Engine\Authentication as BackendAuthentication;
7
use Backend\Core\Engine\Model as BackendModel;
8
use Backend\Core\Engine\Form as BackendForm;
9
use Backend\Core\Language\Language as BL;
10
11
/**
12
 * This is the settings-action, it will display a form to set general blog settings
13
 */
14
class Settings extends BackendBaseActionEdit
15
{
16
    /**
17
     * Is the user a god user?
18
     *
19
     * @var bool
20
     */
21
    protected $isGod = false;
22
23
    public function execute(): void
24
    {
25
        parent::execute();
26
        $this->buildForm();
27
        $this->validateForm();
28
        $this->parse();
29
        $this->display();
30
    }
31
32
    private function buildForm(): void
33
    {
34
        $this->isGod = BackendAuthentication::getUser()->isGod();
35
36
        $this->form = new BackendForm('settings');
37
38
        // add fields for pagination
39
        $this->form->addDropdown(
40
            'overview_number_of_items',
41
            array_combine(range(1, 30), range(1, 30)),
42
            $this->get('fork.settings')->get($this->url->getModule(), 'overview_num_items', 10)
43
        );
44
        $this->form->addDropdown(
45
            'recent_articles_full_number_of_items',
46
            array_combine(range(1, 10), range(1, 10)),
47
            $this->get('fork.settings')->get($this->url->getModule(), 'recent_articles_full_num_items', 5)
48
        );
49
        $this->form->addDropdown(
50
            'recent_articles_list_number_of_items',
51
            array_combine(range(1, 10), range(1, 10)),
52
            $this->get('fork.settings')->get($this->url->getModule(), 'recent_articles_list_num_items', 5)
53
        );
54
55
        // add fields for spam
56
        $this->form->addCheckbox('spamfilter', $this->get('fork.settings')->get($this->url->getModule(), 'spamfilter', false));
57
58
        // no Akismet-key, so we can't enable spam-filter
59
        if ($this->get('fork.settings')->get('Core', 'akismet_key') == '') {
60
            $this->form->getField('spamfilter')->setAttribute('disabled', 'disabled');
61
            $this->template->assign('noAkismetKey', true);
62
        }
63
64
        // add fields for comments
65
        $this->form->addCheckbox('allow_comments', $this->get('fork.settings')->get($this->url->getModule(), 'allow_comments', false));
66
        $this->form->addCheckbox('moderation', $this->get('fork.settings')->get($this->url->getModule(), 'moderation', false));
67
68
        // add fields for notifications
69
        $this->form->addCheckbox(
70
            'notify_by_email_on_new_comment_to_moderate',
71
            $this->get('fork.settings')->get($this->url->getModule(), 'notify_by_email_on_new_comment_to_moderate', false)
72
        );
73
        $this->form->addCheckbox(
74
            'notify_by_email_on_new_comment',
75
            $this->get('fork.settings')->get($this->url->getModule(), 'notify_by_email_on_new_comment', false)
76
        );
77
78
        // add fields for RSS
79
        $this->form->addCheckbox('rss_meta', $this->get('fork.settings')->get($this->url->getModule(), 'rss_meta_' . BL::getWorkingLanguage(), true));
80
        $this->form->addText('rss_title', $this->get('fork.settings')->get($this->url->getModule(), 'rss_title_' . BL::getWorkingLanguage()));
81
        $this->form->addTextarea('rss_description', $this->get('fork.settings')->get($this->url->getModule(), 'rss_description_' . BL::getWorkingLanguage()));
82
83
        // god user?
84
        if ($this->isGod) {
85
            $this->form->addCheckbox('show_image_form', $this->get('fork.settings')->get($this->url->getModule(), 'show_image_form', true));
86
        }
87
    }
88
89
    protected function parse(): void
90
    {
91
        parent::parse();
92
93
        // parse additional variables
94
        $this->template->assign('commentsRSSURL', SITE_URL . BackendModel::getUrlForBlock($this->url->getModule(), 'comments_rss'));
95
        $this->template->assign('isGod', $this->isGod);
96
    }
97
98
    private function validateForm(): void
99
    {
100
        if ($this->form->isSubmitted()) {
101
            // validation
102
            $this->form->getField('rss_title')->isFilled(BL::err('FieldIsRequired'));
103
104
            if ($this->form->isCorrect()) {
105
                // set our settings
106
                $this->get('fork.settings')->set($this->url->getModule(), 'overview_num_items', (int) $this->form->getField('overview_number_of_items')->getValue());
107
                $this->get('fork.settings')->set($this->url->getModule(), 'recent_articles_full_num_items', (int) $this->form->getField('recent_articles_full_number_of_items')->getValue());
108
                $this->get('fork.settings')->set($this->url->getModule(), 'recent_articles_list_num_items', (int) $this->form->getField('recent_articles_list_number_of_items')->getValue());
109
                $this->get('fork.settings')->set($this->url->getModule(), 'spamfilter', (bool) $this->form->getField('spamfilter')->getValue());
110
                $this->get('fork.settings')->set($this->url->getModule(), 'allow_comments', (bool) $this->form->getField('allow_comments')->getValue());
111
                $this->get('fork.settings')->set($this->url->getModule(), 'moderation', (bool) $this->form->getField('moderation')->getValue());
112
                $this->get('fork.settings')->set($this->url->getModule(), 'notify_by_email_on_new_comment_to_moderate', (bool) $this->form->getField('notify_by_email_on_new_comment_to_moderate')->getValue());
113
                $this->get('fork.settings')->set($this->url->getModule(), 'notify_by_email_on_new_comment', (bool) $this->form->getField('notify_by_email_on_new_comment')->getValue());
114
                $this->get('fork.settings')->set($this->url->getModule(), 'rss_title_' . BL::getWorkingLanguage(), $this->form->getField('rss_title')->getValue());
115
                $this->get('fork.settings')->set($this->url->getModule(), 'rss_description_' . BL::getWorkingLanguage(), $this->form->getField('rss_description')->getValue());
116
                $this->get('fork.settings')->set($this->url->getModule(), 'rss_meta_' . BL::getWorkingLanguage(), $this->form->getField('rss_meta')->getValue());
117
                if ($this->isGod) {
118
                    $this->get('fork.settings')->set($this->url->getModule(), 'show_image_form', (bool) $this->form->getField('show_image_form')->getChecked());
119
                }
120
                if ($this->get('fork.settings')->get('Core', 'akismet_key') === null) {
121
                    $this->get('fork.settings')->set($this->url->getModule(), 'spamfilter', false);
122
                }
123
124
                // redirect to the settings page
125
                $this->redirect(BackendModel::createUrlForAction('Settings') . '&report=saved');
126
            }
127
        }
128
    }
129
}
130