SettingsController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 110
rs 10
c 0
b 0
f 0
eloc 61
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 8 2
B edit() 0 37 7
A initialize() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Admin\Controller;
14
15
use App\Model\Table\SettingsTable;
16
use Cake\Http\Exception\NotFoundException;
17
18
/**
19
 * @property SettingsTable $Settings
20
 */
21
class SettingsController extends AdminAppController
22
{
23
24
    public $helpers = [
25
        'Admin.Setting',
26
        'TimeH',
27
    ];
28
29
    protected $settingsShownInAdminIndex = [
30
        'autolink' => ['type' => 'bool'],
31
        'bbcode_img' => ['type' => 'bool'],
32
        // Activates and deactivates the category-chooser on entries/index
33
        'category_chooser_global' => ['type' => 'bool'],
34
        // Allows users to show the category-chooser even if the default
35
        // setting `category_chooser_global` is off
36
        'category_chooser_user_override' => ['type' => 'bool'],
37
        'content_embed_active' => ['type' => 'bool'],
38
        'content_embed_media' => ['type' => 'bool'],
39
        'content_embed_text' => ['type' => 'bool'],
40
        'edit_delay' => 1,
41
        'edit_period' => 1,
42
        'email_contact' => 1,
43
        'email_register' => 1,
44
        'email_system' => 1,
45
        'forum_disabled' => ['type' => 'bool'],
46
        'forum_disabled_text' => 1,
47
        'forum_email' => 1,
48
        'forum_name' => 1,
49
        'quote_symbol' => 1,
50
        'signature_separator' => 1,
51
        'stopwatch_get' => ['type' => 'bool'],
52
        'store_ip' => ['type' => 'bool'],
53
        'store_ip_anonymized' => ['type' => 'bool'],
54
        'subject_maxlength' => 1,
55
        'thread_depth_indent' => 1,
56
        'timezone' => 1,
57
        'topics_per_page' => 1,
58
        'tos_enabled' => ['type' => 'bool'],
59
        'tos_url' => 1,
60
        'video_domains_allowed' => 1,
61
    ];
62
63
    /**
64
     * {@inheritDoc}
65
     */
66
    public function initialize()
67
    {
68
        parent::initialize();
69
        $this->loadModel('Settings');
70
    }
71
72
    /**
73
     * index settings
74
     *
75
     * @return void
76
     */
77
    public function index()
78
    {
79
        $settings = $this->Settings->getSettings();
80
        foreach ($settings as $key => $value) {
81
            $this->request = $this->request->withData($key, $value);
82
        }
83
        $settings = array_intersect_key($settings, $this->settingsShownInAdminIndex);
84
        $this->set('Settings', $settings);
85
    }
86
87
    /**
88
     * edit setting
89
     *
90
     * @param string|null $id settings-ID
91
     *
92
     * @return \Cake\Http\Response|void
93
     */
94
    public function edit(string $id = null)
95
    {
96
        if (empty($id)) {
97
            throw new NotFoundException();
98
        }
99
100
        $setting = $this->Settings->get($id);
101
        if (empty($setting)) {
102
            throw new NotFoundException();
103
        }
104
105
        if ($this->request->is(['post', 'put'])) {
106
            $this->Settings->patchEntity(
107
                $setting,
108
                $this->request->getData(),
0 ignored issues
show
Bug introduced by
It seems like $this->request->getData() can also be of type null; however, parameter $data of Cake\ORM\Table::patchEntity() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
                /** @scrutinizer ignore-type */ $this->request->getData(),
Loading history...
109
                ['fields' => ['value']]
110
            );
111
            if ($this->Settings->save($setting)) {
112
                // @lo
113
                $this->Flash->set('Saved.', ['element' => 'notice']);
114
115
                return $this->redirect(['action' => 'index', '#' => $id]);
116
            }
117
118
            $errors = $setting->getErrors();
119
            // @lo
120
            $msg = !empty($errors) ? current(current($errors)) : 'Something went wrong';
121
            $this->Flash->set($msg, ['element' => 'error']);
122
        }
123
124
        $type = $this->settingsShownInAdminIndex[$id]['type'] ?? null;
125
        $setting->set('type', $type);
126
127
        $this->set('setting', $setting);
128
129
        if ($id === 'timezone') {
130
            $this->render('timezone');
131
        }
132
    }
133
}
134