5 * Fetch data from an SQL database
15 public function __construct($config)
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'];
25 * @param string $strDate Date the events shall be found for, YYYY-MM-DD
27 public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
30 $this->dsn, $this->user, $this->password,
31 array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)
33 $arDays = $this->getDates($strDate, $nDaysPrevious, $nDaysNext);
36 foreach ($this->fields['date'] as $field => $typeName) {
37 if (substr($this->dsn, 0, 5) == 'dblib') {
38 //MS SQL Server does of course cook its own sht
39 $sqlMonth = 'DATEPART(month, ' . $field . ')';
40 $sqlDay = 'DATEPART(day, ' . $field . ')';
42 $sqlMonth = 'EXTRACT(MONTH FROM ' . $field . ')';
43 $sqlDay = 'EXTRACT(DAY FROM ' . $field . ')';
47 foreach ($arDays as $month => $days) {
49 . $sqlMonth . ' = ' . $dbh->quote($month, \PDO::PARAM_INT)
50 . ' AND ' . $sqlDay . ' >= '
51 . $dbh->quote(min($days), \PDO::PARAM_INT)
52 . ' AND ' . $sqlDay . ' <= '
53 . $dbh->quote(max($days), \PDO::PARAM_INT)
56 $sql = 'SELECT ' . $field . ' AS e_date'
57 . ', ' . implode(', ', (array) $this->fields['name'])
58 . ' FROM ' . $this->table
60 . implode(' OR ', $parts);
62 $res = $dbh->query($sql);
63 while ($row = $res->fetchObject()) {
64 $arNameData = array();
65 foreach ((array) $this->fields['name'] as $fieldName) {
66 $arNameData[] = $row->$fieldName;
68 $name = call_user_func_array(
71 array($this->fields['nameFormat']),
77 str_replace('0000', '????', $row->e_date)
79 if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
88 * @return array Key is the month, value an array of days
90 protected function getDates($strDate, $nDaysPrevious, $nDaysNext)
92 $ts = strtotime($strDate) - 86400 * $nDaysPrevious;
93 $numDays = $nDaysPrevious + $nDaysNext;
97 $arDays[(int) date('n', $ts)][] = (int) date('j', $ts);
99 } while (--$numDays >= 0);