summaryrefslogtreecommitdiffstats
path: root/SchedulerHelperConnector.php
blob: 0f96ae668fa15ee0e691127a8f640b9c3ea57c58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

namespace DHTMLX_Scheduler;
use PDO, Exception;

class SchedulerHelperConnector
{
    private $_dbsm, $_host, $_db_name, $_user, $_password, $_table_name;
    private $_PDO;

    protected function __construct($configs = array())
    {
        $this->_dbsm = isset($configs["dbsm"]) ? $configs["dbsm"] : "mysql";
        $this->_host = isset($configs["host"]) ? $configs["host"] : "localhost";
        $this->_db_name = $configs["db_name"];
        $this->_user = $configs["user"];
        $this->_password = $configs["password"];
        $this->_table_name = $configs["table_name"];
    }

    protected function getTableName()
    {
        return $this->_table_name;
    }

    private function getConfigStringPDO()
    {
        return "{$this->_dbsm}:host={$this->_host};dbname={$this->_db_name}";
    }

    public function getConnection()
    {
        $PDO_options = array(
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        );

        $this->_PDO = ($this->_PDO) ? $this->_PDO : new PDO($this->getConfigStringPDO(), $this->_user, $this->_password, $PDO_options);
        return $this->_PDO;
    }

    public function closeConnection()
    {
        unset($this->_PDO);
        $this->_PDO = null;
    }

    protected function save()
    {
        if(!is_null($this->getFieldsValues(Helper::FLD_ID)))
            $this->update();
        else
            $this->insert();
    }

    protected function update()
    {
        if(is_null($this->getFieldsValues(Helper::FLD_ID)))
            throw new Exception("For updating data needs value of field Helper::FLD_ID");

        $fieldsValues = $this->getFieldsValues();
        $sqlSetPart = [];
        foreach($fieldsValues as $field => $value)
            array_push($sqlSetPart, "{$field}='{$value}'");

        $sql = "
            UPDATE
              {$this->_table_name}
            SET
              ".join("," ,$sqlSetPart)."
            WHERE
                ".$this->getIdFieldName()." = ".$this->getFieldsValues(Helper::FLD_ID)."
        ";

        if($this->config["debug"])
            echo("Update operation sql: ".$sql."<BR>");

        $this->getConnection()->prepare($sql)->execute();
        $this->closeConnection();
    }

    protected function insert()
    {
        $fieldsValues = $this->getFieldsValues();
        $sqlFields = join(",", array_keys($fieldsValues));
        $sqlValues = "'".join("','", array_values($fieldsValues))."'";
        $sql = "INSERT INTO {$this->_table_name} ({$sqlFields}) values ({$sqlValues})";

        if($this->config["debug"])
            echo("Insert operation sql: ".$sql."<BR>");

        $this->getConnection()->prepare($sql)->execute();
        $this->closeConnection();
    }

    protected function delete()
    {
        $dataId = $this->getFieldsValues(Helper::FLD_ID);
        if(is_null($dataId))
            throw new Exception("For deleting needs value of FLD_ID");

        $sql = "
            DELETE FROM
                ".$this->_table_name."
            WHERE
                ".$this->getIdFieldName()." = '{$dataId}'";

        if($this->config["debug"])
            echo("Delete operation sql: ".$sql."<BR>");

        $this->getConnection()->prepare($sql)->execute();
        $this->closeConnection();
    }
}