escape strings
[bdrem.git] / src / bdrem / Source / Sql.php
1 <?php
2 namespace bdrem;
3
4 /**
5  * Fetch data from an SQL database
6  */
7 class Source_Sql
8 {
9     protected $dsn;
10     protected $user;
11     protected $password;
12     protected $table;
13     protected $fields ;
14
15     public function __construct($config)
16     {
17         $this->dsn      = $config['dsn'];
18         $this->user     = $config['user'];
19         $this->password = $config['password'];
20         $this->table    = $config['table'];
21         $this->fields   = $config['fields'];
22     }
23
24     /**
25      * @param string $strDate Date the events shall be found for, YYYY-MM-DD
26      */
27     public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
28     {
29         $dbh = new \PDO($this->dsn, $this->user, $this->password);
30         $arDays = $this->getDates($strDate, $nDaysPrevious, $nDaysNext);
31         $arEvents = array();
32
33         foreach ($this->fields['date'] as $field => $typeName) {
34             $sqlMonth = 'EXTRACT(MONTH FROM ' . $field . ')';
35             $sqlDay = 'EXTRACT(DAY FROM ' . $field . ')';
36
37             $parts = array();
38             foreach ($arDays as $month => $days) {
39                 $parts[] = '('
40                     . $sqlMonth . ' = ' . $dbh->quote($month, \PDO::PARAM_INT)
41                     . ' AND ' . $sqlDay . ' >= '
42                     . $dbh->quote(min($days), \PDO::PARAM_INT)
43                     . ' AND ' . $sqlDay . ' <= '
44                     . $dbh->quote(max($days), \PDO::PARAM_INT)
45                     . ')';
46             }
47             $sql = 'SELECT ' . $field . ' AS e_date'
48                 . ', ' . $this->fields['name'] . ' AS e_name'
49                 . ' FROM ' . $this->table
50                 . ' WHERE '
51                 . implode(' OR ', $parts);
52
53             $res = $dbh->query($sql);
54             if ($res === false) {
55                 $errorInfo = $dbh->errorInfo();
56                 throw new \Exception(
57                     'SQL error #' . $errorInfo[0]
58                     . ': ' . $errorInfo[1]
59                     . ': ' . $errorInfo[2],
60                     (int) $errorInfo[1]
61                 );
62             }
63             while ($row = $res->fetchObject()) {
64                 $event = new Event(
65                     $row->e_name, $typeName, 
66                     str_replace('0000', '????', $row->e_date)
67                 );
68                 if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
69                     $arEvents[] = $event;
70                 }
71             }
72         }
73         return $arEvents;
74     }
75     
76     /**
77      * @return array Key is the month, value an array of days
78      */
79     protected function getDates($strDate, $nDaysPrevious, $nDaysNext)
80     {
81         $ts = strtotime($strDate) - 86400 * $nDaysPrevious;
82         $numDays = $nDaysPrevious + $nDaysNext;
83
84         $arDays = array();
85         do {
86             $arDays[(int) date('n', $ts)][] = (int) date('j', $ts);
87             $ts += 86400;
88         } while (--$numDays >= 0);
89         return $arDays;
90     }
91 }
92 ?>