-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooklibrary.module
306 lines (247 loc) · 8.56 KB
/
booklibrary.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
// $Id$
/**
* bookLibrary Module.
* Module that creates a personal library of books
* Currently only a latest read block is implemented
* TODO create a recommended readings block
* TODO convert books to a content type
* TODO create a listing of books
* TODO add amazon support for retrieving book information
* TODO clean up code
*
* @file
*/
/**
* Implementation of hook_menu().
* Adds a menu item to the admin site
* to add an isbn number.
*
* @return $items
*/
function bookLibrary_menu() {
$items['admin/bookLibrary/add'] = array(
'title' => 'bookLibrary add isbn',
'description' => 'add book to bookLibrary module',
'page callback' => 'bookLibrary_add_page',
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Menu callback
* Callback from the menu item, creates
* a page and gets the form for the
* insertion of the isbn number.
*/
function bookLibrary_add_page() {
$output = t('Add book to recommended reading');
$output .= drupal_get_form('bookLibrary_addform');
return $output;
}
/**
* add isbn number form
* simple form with one field for
* the isbn number
*/
function bookLibrary_addform() {
$form['isbn'] = array(
'#title' => t('Isbn Book number'),
'#type' => 'textfield',
'#description' => t('Add the isbn number for the book that will be displayed in the recommended reading'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Enter'),
);
return $form;
}
/**
* Implementation of hook_submit
* On submit the isbn number gets stored locally
* the function get_bookdata is called with the
* isbn number and retrieved data from that function
* gets stored in $book_data.
* This object gets saved through the saveBook function
* that gets this object as an argument.
*/
function bookLibrary_addform_submit($form, &$form_state) {
$isbn = $form_state['values']['isbn'];
$book_data = get_bookdata($isbn);
saveBook($book_data);
//echo $isbn;
//echo var_dump($book_data);
//exit;
}
/**
* function get_bookdata
* this function retrieves the bookdata when it's given
* a isbn number. The information is retrieved from the
* openLibrary site.
* The isbn number gets cleaned up and checked if it's in
* the isbn-10 format or isbn-13 format.
* The resulting book information is returned by this function
* @param $isbn
* @return array
*/
function get_bookdata($isbn) {
//clean up isbn number
$isbn = str_replace('-', '', $isbn);
$isbn = str_replace(' ', '', $isbn);
//echo var_dump($isbn);
//check if the isbn number is available
if(!is_null($isbn)) {
switch (strlen($isbn)) {
case 10:
// if isbn-10 format this is the url to get the versions from openLibrary
$url_bookkeys = "http://openlibrary.org/api/things?&query=".urlencode('{"type":"/type/edition", "isbn_10":"'.$isbn.'"}');
break;
case 13:
//if isbn-13 format this is the url to get the versions from openLibrary
$url_bookkeys = "http://openlibrary.org/api/things?&query=".urlencode('{"type":"/type/edition", "isbn_13":"'.$isbn.'"}');
break;
}
}
//query openLibrary
//echo var_dump($url_bookkeys);
$bookkeys = get_url_contents($url_bookkeys, $curltimeout);
//echo var_dump($bookkeys);
$obj = json_decode($bookkeys);
$bookkeyresult = $obj->{'result'};
//echo var_dump($bookkeyresult);
//determine number of versions and choose first available version if none specified
$bookversioncount = count($bookkeyresult);
if($bookversion == '') $bookversion = 1;
elseif ($bookversion > $bookversioncount) $bookversion = $bookversioncount;
$bookversion = $bookversion-1;
$bookkey = $bookkeyresult[$bookversion];
$bookpage = 'http://openlibrary.org' .$bookkey;
$curltimeout = 10;
$url = 'http://openlibrary.org/api/get?key='.$bookkey.'&text=true';
$bookdata = get_url_contents($url, $curltimeout);
$obj = json_decode($bookdata);
$bookdataresult = $obj->{'result'};
//echo var_dump($bookdataresult);
//isbn
$book_info['isbn'] = $isbn;
//title
$book_info['title'] = $bookdataresult->{'title'};
//subtitle
$book_info['subtitle'] = $bookdataresult->{'subtitle'};
//authors -- handle multiple
$book_info['authors'] = $bookdataresult->{'subjects'};
//publishers
$book_info['publishers'] = $bookdataresult->{'publishers'};
//coverimage
$olnumber_begin = stripos($bookkey, "/b/") + 3;
$olnumber = substr($bookkey, $olnumber_begin);
$size = getimagesize("http://covers.openlibrary.org/b/olid/$olnumber-M.jpg");
if($size[0] == 1){
//TODO code for covers when no cover exists
} else {
$img_small = array('URL' => 'http://covers.openlibrary.org/b/olid/'.$olnumber.'-S.jpg?default=http://wiki-beta.us.archive.org/static/images/blank.book.png');
$img_medium = array('URL' => 'http://covers.openlibrary.org/b/olid/'.$olnumber.'-M.jpg?default=http://wiki-beta.us.archive.org/static/images/blank.book.png');
$img_large = array('URL' => 'http://covers.openlibrary.org/b/olid/'.$olnumber.'-L.jpg?default=http://wiki-beta.us.archive.org/static/images/blank.book.png');
$book_info['images'] = array('small' => $img_small,
'medium' => $img_medium,
'large' => $img_large);
}
//pagination
$book_info['pages_total'] = $bookdataresult->{'number_of_pages'};
//publication date
$book_info['public_date'] = $bookdataresult->{'publish_date'};
//link to book
$book_info['book_link'] = 'http://openlibrary.org/b/'.$olnumber;
return $book_info;
}
/**
* function get_url_contents
* this function gets the content from the openLibrary site
* with cURL.
* TODO support when cURL not available
* @param $url
* @param $curltimeout
* @return unknown_type
*/
function get_url_contents($url, $curltimeout) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $curltimeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $curltimeout);
//execute request
$output = curl_exec($ch);
//close cURL session
curl_close($ch);
if($output == '') throw new Exception("open library data unavailable");
return $output;
}
/**
* function saveBook
*
* function to save the book data to the database.
*
* TODO save book data to a book node instead
*
* @param $bookdata
* @return unknown_type
*/
function saveBook($bookdata) {
$book_info = $bookdata;
//echo var_dump($book_info);
//TODO save the book to the database ..
//TODO check if the record allready exists
$table = 'bookLibrary';
$record = new stdClass();
$record->isbn = $book_info['isbn'];
$record->title = $book_info['title'];
$record->subtitle = $book_info['subtitle'];
$record->author = $book_info['authors'][0];
$record->publishers = $book_info['publishers'][0];
$record->image_small = $book_info['images']['small']['URL'];
$record->image_medium = $book_info['images']['medium']['URL'];
$record->image_large = $book_info['images']['large']['URL'];
$record->pages_total = $book_info['pages_total'];
$record->publication_date = $book_info['publication_date'];
$record->book_link = $book_info['book_link'];
//echo var_dump($book_info['authors'][0]);
//echo var_dump($book_info['images']);
drupal_write_record($table, $record);
}
/**
* Implementation of hook_block()
*
* this creates a block with the latest added book.
*
* TODO create a block with recommended books, books can be flagged as recommended
*
* @param $op
* @param $delta
* @param $edit
* @return unknown_type
*/
function bookLibrary_block($op ='list', $delta = 0, $edit=array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Last read');
$blocks[0]['cache'] = BLOCK_NO_CACHE;
return $blocks;
case 'view':
$sql = "SELECT title, author, image_medium, book_link, bid FROM {bookLibrary} ORDER BY bid DESC LIMIT 1";
$result = db_query($sql);
$book = db_fetch_object($result);
$book_info[] = $book->title;
$book_info[] = $book->author;
$book_info[] = $book->image_medium;
$book_info[] = $book->book_link;
//echo var_dump($book_info);
//echo var_dump($book);
//exit;
$block['subject'] = t('Last read');
$block['content'] .= '<h4>'.$book->title.'</h4>';
$block['content'] .= '<img src="'.$book->image_medium.'"/>';
$block['content'] .= '<br /><a href="'.$book->book_link.'">meer</a>';
return $block;
}
}