Completed
Push — master ( 0d0ae6...7c7732 )
by Stone
19s
created

Config   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A updateConfig() 0 23 5
A __construct() 0 4 1
1
<?php
2
3
namespace App\Controllers\Admin;
4
5
use App\Models\ConfigModel;
6
use Core\AdminController;
7
use Core\Traits\StringFunctions;
8
use Core\Container;
9
10
/**
11
 * all to do with the config page
12
 * Class Config
13
 * @package App\Controllers\Admin
14
 */
15
class Config extends AdminController
16
{
17
18
    use StringFunctions;
19
20
    private $configModel;
21
22
    public function __construct(Container $container)
23
    {
24
        parent::__construct($container);
25
        $this->configModel = new ConfigModel($this->container);
26
    }
27
28
    /**
29
     * Shows the config page with all of the config options
30
     * @throws \Twig_Error_Loader
31
     * @throws \Twig_Error_Runtime
32
     * @throws \Twig_Error_Syntax
33
     * @throws \ReflectionException
34
     */
35
    public function index()
36
    {
37
        $this->onlyAdmin();
38
39
        $this->data['configList'] = $this->configModel->getAllConfigOrdered();
40
        $this->renderView('Admin/Config');
41
    }
42
43
44
    /**
45
     * gets the information in post and sets the corresponding site config details
46
     * this is pure php with no ajax
47
     * @throws \Exception
48
     */
49
    public function updateConfig()
50
    {
51
        //Security checks
52
        $this->onlyAdmin();
53
        if (!$this->container->getRequest()->isPost()) {
54
            $this->alertBox->setAlert('Only post messages allowed', 'error');
55
            $this->container->getResponse()->redirect('admin');
56
        }
57
58
        $posts = $this->container->getRequest()->getDataFull();
59
        $success = true;
60
61
        foreach ($posts as $key => $config) {
62
            if (!$this->configModel->updateConfig($key, $config)) {
63
                $success = false;
64
            }
65
        }
66
        if ($success) {
67
            $this->alertBox->setAlert('Configuration updates successfully');
68
        } else {
69
            $this->alertBox->setAlert('error in configuration update', 'error');
70
        }
71
        $this->container->getResponse()->redirect('admin/config');
72
    }
73
}
74