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

ConfigModel::getAllConfigOrdered()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 0
dl 0
loc 25
rs 9.7333
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
        $configTypeTbl = $this->getTablePrefix('configs_type');
25
        $sql = "SELECT idconfigs, configs_name, configs_type_name, configs_value, class FROM  $configsTbl 
26
                INNER JOIN $configsClassTbl ON $configsTbl.configs_class_idconfigsclass = $configsClassTbl.idconfigsclass 
27
                INNER JOIN $configTypeTbl ON $configsTbl.configs_type_idconfigs_type = $configTypeTbl.idconfigs_type
28
                ORDER BY class, configs_name;";
29
30
        $this->query($sql);
31
        $this->execute();
32
        $configClass = $this->fetchAll();
33
        foreach ($configClass as $class) {
34
            //we remove the first 3 characters as they are used for ordering (10_global_site_configuration)
35
            $className = substr($class->class, 3);
36
37
            if (!isset($returnData[$className])) {
38
                $returnData[$className] = [];
39
            }
40
            $returnData[$className][] = $class;
41
        }
42
        return $returnData;
43
    }
44
45
    /**
46
     * gets the entire config table
47
     * @return array
48
     * @throws \ReflectionException
49
     */
50
    public function getAllConfig()
51
    {
52
        return $this->getResultSet();
53
    }
54
55
    /**
56
     * updates the site config table
57
     * @param int $idTable
58
     * @param string $param parameter to update
59
     * @return bool update success
60
     * @throws \Exception error
61
     */
62
    public function updateConfig(int $idTable, string $param): bool
63
    {
64
        $configsTbl = $this->getTablePrefix('configs');
65
        $sql = "UPDATE $configsTbl SET configs_value = :param WHERE idconfigs = :id";
66
        $this->query($sql);
67
        $this->bind(':param', $param);
68
        $this->bind(':id', $idTable);
69
        return $this->execute();
70
71
    }
72
}