summaryrefslogtreecommitdiff
path: root/src/bdrem/Source/Sql.php
blob: 81ce5beb8ed3d772ba99443f886f9d90c274f51e (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
 * Part of bdrem
 *
 * PHP version 5
 *
 * @category  Tools
 * @package   Bdrem
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @link      http://cweiske.de/bdrem.htm
 */
namespace bdrem;

/**
 * Fetch data from an SQL database
 *
 * @category  Tools
 * @package   Bdrem
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @link      http://cweiske.de/bdrem.htm
 */
class Source_Sql
{
    /**
     * PDO data source description
     * @var string
     */
    protected $dsn;

    /**
     * Database user name
     * @var string
     */
    protected $user;

    /**
     * Database password
     * @var string
     */
    protected $password;

    /**
     * Database table with event data
     * @var string
     */
    protected $table;

    /**
     * Field configuration
     * Keys:
     * - date - array of columns with dates and their event title,
     *          e.g. 'c_birthday' => 'Birthday'
     * - name - array of name columns
     * - nameFormat - sprintf-compatible name formatting instruction,
     *                uses name columns
     *
     * @var array
     */
    protected $fields;

    /**
     * Set SQL server configuration
     *
     * @param array $config SQL server configuration with keys:
     *                      dsn, user, password, table and fields
     */
    public function __construct($config)
    {
        $this->dsn      = $config['dsn'];
        $this->user     = $config['user'];
        $this->password = $config['password'];
        $this->table    = $config['table'];
        $this->fields   = $config['fields'];
    }

    /**
     * Return all events for the given date range
     *
     * @param string  $strDate       Date the events shall be found for,
     *                               YYYY-MM-DD
     * @param integer $nDaysPrevious Include number of days before $strDate
     * @param integer $nDaysNext     Include number of days after $strDate
     *
     * @return Event[] Array of matching event objects
     */
    public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
    {
        $dbh = new \PDO(
            $this->dsn, $this->user, $this->password,
            array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)
        );
        $arDays = $this->getDates($strDate, $nDaysPrevious, $nDaysNext);
        $arEvents = array();

        foreach ($this->fields['date'] as $field => $typeName) {
            if (substr($this->dsn, 0, 5) == 'dblib') {
                //MS SQL Server does of course cook its own sht
                $sqlMonth = 'DATEPART(month, ' . $field . ')';
                $sqlDay = 'DATEPART(day, ' . $field . ')';
            } else {
                $sqlMonth = 'EXTRACT(MONTH FROM ' . $field . ')';
                $sqlDay = 'EXTRACT(DAY FROM ' . $field . ')';
            }

            $parts = array();
            foreach ($arDays as $month => $days) {
                $parts[] = '('
                    . $sqlMonth . ' = ' . $dbh->quote($month, \PDO::PARAM_INT)
                    . ' AND ' . $sqlDay . ' >= '
                    . $dbh->quote(min($days), \PDO::PARAM_INT)
                    . ' AND ' . $sqlDay . ' <= '
                    . $dbh->quote(max($days), \PDO::PARAM_INT)
                    . ')';
            }
            $sql = 'SELECT ' . $field . ' AS e_date'
                . ', ' . implode(', ', (array) $this->fields['name'])
                . ' FROM ' . $this->table
                . ' WHERE '
                . implode(' OR ', $parts);

            $res = $dbh->query($sql);
            while ($row = $res->fetchObject()) {
                $arNameData = array();
                foreach ((array) $this->fields['name'] as $fieldName) {
                    $arNameData[] = $row->$fieldName;
                }
                $name = call_user_func_array(
                    'sprintf',
                    array_merge(
                        array($this->fields['nameFormat']),
                        $arNameData
                    )
                );
                $event = new Event(
                    $name, $typeName,
                    str_replace('0000', '????', $row->e_date)
                );
                if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
                    $arEvents[] = $event;
                }
            }
        }
        return $arEvents;
    }

    /**
     * Create an array of dates that are included in the given range.
     *
     * @param string  $strDate       Date the events shall be found for,
     *                               YYYY-MM-DD
     * @param integer $nDaysPrevious Include number of days before $strDate
     * @param integer $nDaysNext     Include number of days after $strDate
     *
     * @return array Key is the month, value an array of days
     */
    protected function getDates($strDate, $nDaysPrevious, $nDaysNext)
    {
        $ts = strtotime($strDate) - 86400 * $nDaysPrevious;
        $numDays = $nDaysPrevious + $nDaysNext;

        $arDays = array();
        do {
            $arDays[(int) date('n', $ts)][] = (int) date('j', $ts);
            $ts += 86400;
        } while (--$numDays >= 0);
        return $arDays;
    }
}
?>