Passed
Pull Request — dev (#47)
by Stone
02:52
created

ConfigModel::getAllConfigOrdered()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 0
dl 0
loc 23
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Core\Model;
6
7
/**
8
 * Class ConfigModel
9
 * @package App\Models
10
 */
11
class ConfigModel extends Model
12
{
13
    /**
14
     * Returns all the configs orderd with their class names
15
     * @return array
16
     * @throws \Exception
17
     */
18
    public function getAllConfigOrdered(): array
19
    {
20
        $returnData = [];
21
        //getting our tables
22
        $configsTbl = $this->getTablePrefix('configs');
23
        $configsClassTbl = $this->getTablePrefix('configs_class');
24
        $sql = "SELECT idconfigs, configs_name, configs_type, configs_value, class FROM  $configsTbl 
25
                INNER JOIN $configsClassTbl ON $configsTbl.configs_class_idconfigsclass = $configsClassTbl.idconfigsclass
26
                ORDER BY class;";
27
28
        $this->query($sql);
29
        $this->execute();
30
        $configClass = $this->fetchAll();
31
        foreach ($configClass as $class) {
32
            //we remove the first 3 characters as they are used for ordering (10_global_site_configuration)
33
            $className = substr($class->class, 3);
34
35
            if (!isset($returnData[$className])) {
36
                $returnData[$className] = [];
37
            }
38
            $returnData[$className][] = $class;
39
        }
40
        return $returnData;
41
    }
42
43
    public function getAllConfig()
44
    {
45
        return $this->getResultSet();
46
    }
47
48
    /**
49
     * updates the site config table
50
     * @param int $id id of the config
51
     * @param string $param paramter to update
52
     * @return bool update success
53
     * @throws \Exception error
54
     */
55
    public function updateConfig(int $id, string $param)
56
    {
57
        $configsTbl = $this->getTablePrefix('configs');
58
        $sql = "UPDATE $configsTbl SET configs_value = :param WHERE idconfigs = :id";
59
        $this->query($sql);
60
        $this->bind(':param', $param);
61
        $this->bind(':id', $id);
62
        return $this->execute();
63
64
    }
65
}