/[drupal]/contributions/modules/bookroll/bookroll.module
ViewVC logotype

Contents of /contributions/modules/bookroll/bookroll.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Wed Nov 30 01:44:24 2005 UTC (3 years, 11 months ago) by eaton
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-4-7
Changes since 1.2: +16 -2 lines
File MIME type: text/x-php
Minor changes to the themed bookroll page.
1 <?php
2 // $Id: bookroll.module,v 1.1 2005/11/26 20:14:39 eaton Exp $
3
4 /**
5 * @file
6 * Enables users to maintain a reading list of books.
7 */
8
9 /**
10 * Implementation of hook_help().
11 *
12 * @param $section
13 * provides help for /admin/modules and /admin/help and admin/settings/bookroll.
14 *
15 * @return
16 * A brief message for administrators to explain what this module does
17 */
18 function bookroll_help($section) {
19 switch ($section) {
20 case 'admin/modules#description':
21 return t('Provides contributors with a list of books they\'re currently reading.');
22 case 'admin/help#bookroll':
23 return '<p>'. t('The Amazon Associate Tools module must be installed.') .'</p>';
24 case 'admin/settings/bookroll':
25 return t('The bookroll module allows users to maintain lists of the books they\'re currently reading, or wish to read.');
26 }
27 }
28
29 /**
30 * Implementation of hook_menu().
31 *
32 * @return
33 * Arrays containing paths to be accessed.
34 */
35 function bookroll_menu($may_cache) {
36 global $user;
37 $items = array();
38
39 if ($may_cache) {
40 $items[] = array('path' => 'bookroll/edit', 'title' => t('edit bookroll'),
41 'callback' => 'bookroll_edit_page',
42 'access' => user_access('maintain bookroll'), // depends on blog.module
43 'type' => MENU_CALLBACK);
44 $items[] = array('path' => 'bookroll', 'title' => variable_get('bookroll_title', t('Bookroll')),
45 'callback' => 'bookroll_page',
46 'access' => TRUE,
47 'type' => MENU_CALLBACK);
48 }
49 return $items;
50 }
51
52 /**
53 * Implementation of hook_perm()
54 */
55 function bookroll_perm() {
56 return array('maintain bookroll');
57 }
58
59 /**
60 * Implementation of hook_settings()
61 *
62 * @return
63 * The settings controls
64 */
65 function bookroll_settings() {
66 $form = array();
67 $form['bookroll']['bookroll_title'] = array(
68 '#type' => 'textfield',
69 '#title' => t('The title used for the bookroll'),
70 '#default_value' => variable_get('bookroll_title', 'Reading List'),
71 );
72 $form['bookroll']['bookroll_description'] = array(
73 '#type' => 'textarea',
74 '#title' => t('The description of the bookroll, for use on the bookroll page'),
75 '#default_value' => variable_get('bookroll_description', t('A community reading list is a run-down of all the books members of this site are currently reading or want to read.')),
76 );
77 $form['bookroll']['bookroll_block_num'] = array(
78 '#type' => 'select',
79 '#title' => t('Number of books listed in sidebar'),
80 '#default_value' => variable_get('bookroll_block_num', '10'),
81 '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30)),
82 );
83 return $form;
84 }
85
86 /**
87 * Menu callback; displays the bookroll editing page
88 */
89 function bookroll_edit_page() {
90 global $user;
91 $output = '';
92
93 if ($user->uid) {
94 $output = '<p>'. t('Your bookroll is shown on your personal blog\'s page.') .'</p>';
95 $output .= bookroll_edit_form($user->uid);
96 $output .= bookroll_edit_add_form();
97 }
98
99 print theme('page', $output);
100 }
101
102 /**
103 * Prepare the bookroll display portion of the page
104 */
105 function bookroll_edit_form($uid) {
106 $output = '<p>'. t('After making changes click \'Update list.\'.') .'</p>';
107
108 // display roll_form with it's table
109 $rolls = db_query("SELECT b.uid, b.asin, b.notes, b.status, b.weight FROM {bookroll} b WHERE b.uid = %d ORDER BY b.timestamp DESC, b.weight", $uid);
110 while ($roll = db_fetch_object($rolls)) {
111 $amazon = _amazon_product_db_data_from_asin($roll->asin);
112
113 $form['old'][$roll->asin]['delete'] = array(
114 '#type' => 'checkbox',
115 '#default_value' => 0,
116 '#return_value' => 1,
117 );
118
119 $form['old'][$roll->asin]['update'] = array(
120 '#type' => 'checkbox',
121 '#default_value' => 0,
122 '#return_value' => 1,
123 );
124
125 $form['old'][$roll->asin]['title'] = array(
126 '#type' => 'markup',
127 '#value' => $amazon->title,
128 );
129
130 $form['old'][$roll->asin]['notes'] = array(
131 '#type' => 'textfield',
132 '#maxlength' => 255,
133 '#size' => 40,
134 '#default_value' => $roll->notes,
135 );
136
137 $form['old'][$roll->asin]['status'] = array(
138 '#type' => 'select',
139 '#default_value' => $roll->status,
140 '#options' => bookroll_status_options(),
141 );
142
143 $form['old'][$roll->asin]['weight'] = array(
144 '#type' => 'weight',
145 '#default_value' => $roll->weight,
146 );
147 }
148
149 $form['#tree'] = true;
150
151 $form['buttons']['submit'] = array(
152 '#type' => 'submit',
153 '#value' => t('Update list'),
154 );
155
156 return drupal_get_form('bookroll_edit', $form);
157 }
158
159 /**
160 * Prepare the bookroll adding portion of the page
161 */
162 function bookroll_edit_add_form() {
163 $form = array();
164
165 $form['new']['asin'] = array(
166 '#type' => 'textfield',
167 '#maxlength' => 20,
168 '#size' => 10,
169 );
170
171 $form['new']['notes'] = array(
172 '#type' => 'textfield',
173 '#maxlength' => 255,
174 '#size' => 40,
175 );
176
177 $form['new']['status'] = array(
178 '#type' => 'select',
179 '#default_value' => 1,
180 '#options' => bookroll_status_options(),
181 );
182
183 $form['new']['weight'] = array(
184 '#type' => 'weight',
185 );
186
187 $form['buttons']['submit'] = array(
188 '#type' => 'submit',
189 '#value' => t('Add book'),
190 );
191
192 return drupal_get_form('bookroll_edit_add', $form);
193 }
194
195 function bookroll_status_options() {
196 return array(0 => 'Wish list', 1 => 'In the queue', 2 => 'In Progress', 3 => 'Finished');
197 }
198
199 function bookroll_edit_add_validate($form_id, $edit) {
200 if (!$edit['asin']) {
201 form_set_error('asin', t('You must enter an Amazon product ID.'));
202 }
203 else {
204 $saved = save_amazon_data($edit['asin']);
205 if (!$saved) {
206 form_set_error('asin', t('No data is available for ') . $edit['asin']);
207 }
208 }
209 }
210
211 function bookroll_edit_add_execute($form_id, $edit) {
212 global $user;
213 $uid = $user->uid;
214 // process post data
215 if ($edit['asin'] and $edit['notes']) {
216 // if ! 404 $edit[url] {};
217 db_query("INSERT into {bookroll} values (%d, '%s', '%s', %d, %d, %d)", $uid, $edit['asin'], $edit['notes'], $edit['status'], $edit['weight'], time());
218
219 drupal_set_message( t('The book was added.'));
220 cache_clear_all();
221 drupal_goto('bookroll/edit');
222 }
223 }
224
225 function bookroll_edit_execute($form_id, $edit) {
226 global $user;
227 $uid = $user->uid;
228 // process post data
229 foreach ($edit['old'] as $asin => $roll) {
230 if ($roll['delete'] == 1) {
231 db_query("DELETE FROM {bookroll} WHERE uid = '%d' AND asin= '%s'", $uid, $asin);
232 } else if ($roll['update'] == 1) {
233 db_query("UPDATE {bookroll} b SET b.notes = '%s', b.status = %d, b.weight = %d, b.timestamp = %d WHERE b.uid = '%d' AND b.asin = '%s'", $roll['notes'], $roll['status'], $roll['weight'], time(), $uid, $asin);
234 }
235 };
236 drupal_set_message( t('Your reading list has been updated.'));
237 cache_clear_all();
238 drupal_goto('bookroll/edit');
239 }
240
241 function theme_bookroll_edit_add($form) {
242 $output = '<br /><p>'. t('Add a new book to your reading list:') .'</p>';
243
244 $header = array(t('ASIN'), t('Notes'), t('Status'), t('Weight'));
245
246 $row = array();
247 $row[] = form_render($form['new']['asin']);
248 $row[] = form_render($form['new']['notes']);
249 $row[] = form_render($form['new']['status']);
250 $row[] = form_render($form['new']['weight']);
251
252 $rows = array($row);
253
254 $output .= theme('table', $header, $rows);
255 $output .= form_render($form);
256 return $output;
257 }
258
259 function theme_bookroll_edit($form) {
260 $header = array(t('Delete'), t('Update'), t('Title'), t('Notes'), t('Status'), t('Weight'));
261 foreach (element_children($form['old']) as $key) {
262 $row = array();
263 $row[] = form_render($form['old'][$key]['delete']);
264 $row[] = form_render($form['old'][$key]['update']);
265 $row[] = form_render($form['old'][$key]['title']);
266 $row[] = form_render($form['old'][$key]['notes']);
267 $row[] = form_render($form['old'][$key]['status']);
268 $row[] = form_render($form['old'][$key]['weight']);
269 $rows[] = $row;
270 }
271
272 if (!$rows) {
273 $rows[] = array(
274 array('data' => t('No books listed.'), 'class' => 'no_bookrolls', 'colspan' => '5')
275 );
276 }
277
278 $output .= theme('table', $header, $rows);
279 $output .= form_render($form);
280 return $output;
281 }
282
283
284 /**
285 * Implementation of hook_block();
286 *
287 * @param $op
288 * Operation 'list' or 'view'. If omitted 'list' is used.
289 * @param $delta
290 * If omitted, 0 is used.
291 *
292 * @return a block containing the bookroll
293 */
294 function bookroll_block($op = 'list', $delta = 0) {
295 global $user;
296 switch($op) {
297 case ('list'):
298 $block[0]['info'] = t('Bookroll');
299 return $block;
300 case('view'):
301 if (user_access('access content')) {
302 $output = '<ul>';
303 $block['subject'] = variable_get('bookroll_title', t('Bookroll'));
304 $rolls = db_query_range('SELECT b.* FROM {bookroll} b ORDER BY b.timestamp DESC', 0, variable_get('bookroll_block_num', 5));
305 while ($roll = db_fetch_object($rolls)) {
306 $output .= theme('bookroll_item', $roll);
307 }
308 $output .= '</ul>';
309 }
310 $output .= '<div class="more-link">';
311 if (user_access('maintain bookroll')) {
312 $output .= l('edit', 'bookroll/edit', array('title' => t('Edit'))) . ' | ';
313 }
314 $output .= l('more', 'bookroll', array('title' => t('more')));
315 $output .= '</div>';
316
317 $block['content'] = $output;
318 }
319 return $block;
320 }
321
322 /**
323 * Menu callback; displays a Drupal page containing all bookrolled blogs
324 */
325 function bookroll_page($uid = 0) {
326 $output = '<p>'. variable_get('bookroll_description', '') . '</p>';
327 $output .= '<ul>';
328 $rolls = db_query('SELECT b.* FROM {bookroll} b ORDER BY b.timestamp DESC');
329 while ($roll = db_fetch_object($rolls)) {
330 $output .= theme('bookroll_page_item', $roll);
331 }
332 $output .= '</ul>';
333 print theme('page', $output);
334 }
335
336 /**
337 * Inserts an amazon-node record for a given ASIN.
338 */
339 function save_amazon_data($asin) {
340 $exists_query = db_query("SELECT asin FROM {amazonitem} WHERE asin = '$asin'");
341 $exists = db_num_rows($exists_query);
342 if (!$exists) {
343 $ASIN_data = _amazon_product_data_from_Amazon($asin);
344 foreach ($ASIN_data as $product) {
345 _to_amazonnode($product);
346 }
347 $exists = 1;
348 }
349 return $exists;
350 }
351
352 function _amazon_product_db_data_from_asin($asin) {
353 $amazondata = db_fetch_object(db_query("SELECT * FROM {amazonitem} a WHERE a.asin = '$asin'"));
354 if ($amazondata) {
355 $amazondata->author = unserialize($amazondata->author);
356 }
357 return $amazondata;
358 }
359
360 function theme_bookroll_item($roll) {
361 $open = '/' .drupal_get_path('module', 'bookroll') . '/open-book.png';
362 $closed = '/' . drupal_get_path('module', 'bookroll') . '/closed-book.png';
363
364 if ($roll->status == 2) {
365 $style = 'style="list-style-image: url(' . $open . '); margin-left: 10px;" ';
366 }
367 else {
368 $style = 'style="list-style-image: url(' . $closed . '); margin-left: 10px;" ';
369 }
370
371 $amazon = _amazon_product_db_data_from_asin($roll->asin);
372 $output .= '<li ' . $style .'><a href="'. $amazon->detailpageurl .'" title="' . $roll->notes . '"><i>'. $amazon->title . '</i></a></li>';
373 return $output;
374 }
375
376 function theme_bookroll_page_item($roll) {
377 $open = '/' .drupal_get_path('module', 'bookroll') . '/open-book.png';
378 $closed = '/' . drupal_get_path('module', 'bookroll') . '/closed-book.png';
379 $user_object = user_load(array('uid' => $roll->uid));
380
381 if ($roll->status == 2) {
382 $style = 'style="list-style-image: url(' . $open . '); margin-left: 10px;" ';
383 }
384 else {
385 $style = 'style="list-style-image: url(' . $closed . '); margin-left: 10px;" ';
386 }
387
388 $amazon = _amazon_product_db_data_from_asin($roll->asin);
389 $output .= '<li ' . $style .'><a href="'. $amazon->detailpageurl .'" title="' . $roll->notes . '"><i>'. $amazon->title . '</i></a> by '. implode(', ',$amazon->author);
390 $output .= '<br>' . theme('username', $user_object) . ': ' . $roll->notes . '</li>';
391 return $output;
392 }
393

  ViewVC Help
Powered by ViewVC 1.1.2