This repository has been archived by the owner on May 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
libhours.module
156 lines (145 loc) · 4.83 KB
/
libhours.module
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
<?php
module_load_include('inc', 'libhours', 'libhours.admin');
// add zend gdata library directory to the php include path
$gdata_library_path = dirname(__FILE__) . '/ZendGdata/library';
set_include_path(get_include_path() . PATH_SEPARATOR . $gdata_library_path);
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Uri_Http');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar_EventQuery');
/**
* Implementation of hook_menu()
*/
function libhours_menu() {
$items['admin/settings/libhours'] = array(
'title' => 'Library Hours',
'description' => t('Configure the library hours module.'),
'page callback' => 'drupal_get_form',
'access callback' => 'user_access',
'access arguments' => array('administer libhours'),
'page arguments' => array('libhours_admin_settings'),
'type' => MENU_NORMAL_ITEM
);
$items['libhours'] = array(
'title' => t('Library Hours'),
'description' => t('Library Hours'),
'page callback' => 'libhours_page',
'access callback' => 'user_access',
'access arguments' => array('view libhours'),
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Implementation of hook_perm()
*/
function libhours_perm() {
return array('view libhours', 'administer libhours');
}
/**
* Implementation of hook_theme()
*/
function libhours_theme() {
return array(
'libhours_block' => array(
'arguments' => array(
'items' => NULL
)
),
'libhours_page' => array(
'arguments' => array(
'items' => NULL
),
'template' => 'libhours_page'
)
);
}
/**
* Authenticate with Google
*/
function libhours_gdata_authenticate() {
$user = variable_get('libhours_user', '');
if (empty($user)) {
watchdog('libhours', 'Google Calendar user is not set. Please set it in the Libhours settings page.');
}
$pass = variable_get('libhours_pass', '');
if (empty($pass)) {
watchdog('libhours', 'Google Calendar password is not set. Please set it in the Libhours settings page.');
}
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
return Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
}
/**
* Return events
* @param $startMin an RFC 3339 timestamp for the start of the date range to retrieve
* @param $startMax an RFC 3339 timestamp for the end of the date range to retrieve
* @param $text a string which will be used in a fulltext search of the calendar
*/
function libhours_get_events($startMin = null, $startMax = null, $text = '') {
$calendar = new Zend_Gdata_Calendar(libhours_gdata_authenticate());
$query = new Zend_Gdata_Calendar_EventQuery();
$query->setUser(variable_get('libhours_id', 'default'));
$query->setVisibility('public');
$query->setProjection('full');
$query->setOrderBy('starttime');
$query->setSortOrder('ascending');
$query->setStartMin($startMin);
$query->setStartMax($startMax);
// allow for fulltext queries
if (!empty($text)) {
$query->setQuery($text);
}
$eventFeed = $calendar->getCalendarEventFeed($query);
return $eventFeed;
}
/**
* Display hours in a page
*/
function libhours_page() {
$events = libhours_get_events(date(DATE_ATOM, time() - 86400), date(DATE_ATOM, time() + 86400*6));
foreach($events as $event) {
$items[$event->title->text][date('Y-m-d', strtotime($event->when[0]->startTime))] = array(
'isodate' => date('Y-m-d', strtotime($event->when[0]->startTime)),
'dayOfWeek' => date('l', strtotime($event->when[0]->startTime)),
'timestamp' => strtotime($event->when[0]->startTime),
'startTime' => date('g:i A', strtotime($event->when[0]->startTime)),
'endTime' => date('g:i A', strtotime($event->when[0]->endTime))
);
}
return theme('libhours_page', $items);
}
/**
* Implementation of hook_block()
*/
function libhours_block($op = 'list', $delta = 0, $edit = array()) {
switch($op) {
case "list":
$blocks[0]['info'] = t('Libhours block');
$blocks[0]['cache'] = BLOCK_NO_CACHE;
return $blocks;
case "view":
$events = libhours_get_events(date(DATE_ATOM, time() - 86400), date(DATE_ATOM, time() + 86400));
$items = array();
foreach($events as $event) {
$items[$event->title->text][] = array(
'startTime' => date('g:i A', strtotime($event->when[0]->startTime)),
'endTime' => date('g:i A', strtotime($event->when[0]->endTime))
);
}
$block['subject'] = t('Hours for '. date("D. F j"));
$block['content'] = theme('libhours_block', $items);
return $block;
}
}
/**
* Theme the library hours block
*/
function theme_libhours_block($items) {
foreach($items as $key => $item) {
$output .= "<h2>". $key ."</h2>";
$output .= $item[0]['startTime'] ." - ". $item[0]['endTime'] ."<br />";
}
return $output;
}