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

Contents of /contributions/modules/thisdayinhistory/thisdayinhistory.module

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


Revision 1.2 - (show annotations) (download) (as text)
Tue Feb 10 19:40:01 2009 UTC (9 months, 2 weeks ago) by dmcgladdery
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1, DRUPAL-6--2, DRUPAL-7--1
Changes since 1.1: +196 -409 lines
File MIME type: text/x-php
Initial commit of the thisdayinhistory module Drupal 6 version
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Main module file for This Day in History module.
7 */
8
9 /**
10 * Implementation of hook_help().
11 */
12 function thisdayinhistory_help($path, $arg) {
13 switch ($path) {
14 case 'admin/help#thisdayinhistory':
15 // This description shows up when users click "create content."
16 $help_text = '<p>The <em>This Day in History</em> module allows users with the appropriate permissions to enter historical events. The historical events can be displayed in any number of Administrator defined blocks. Blocks can be configured to display historical events based on different criteria, such as taxonomy terms.</p>';
17 return t($help_text);
18 break;
19 }
20 }
21
22
23 /**
24 * Implementation of hook_node_info().
25 */
26 function thisdayinhistory_node_info() {
27 return array(
28 'thisdayinhistory' => array(
29 'name' => t('Historical event'),
30 'module' => 'thisdayinhistory',
31 'description' => t("An interesting or historical event from the past."),
32 )
33 );
34 }
35
36
37 /**
38 * Implementation of hook_perm().
39 */
40 function thisdayinhistory_perm() {
41 return array(
42 'administer this day in history',
43 'create historical events',
44 'edit own historical events',
45 'promote historical events to block'
46 );
47 }
48
49
50 /**
51 * Implementation of hook_access().
52 */
53 function thisdayinhistory_access($op, $node, $account) {
54 // Admins can do anything
55 if (user_access('administer this day in history', $account)) return TRUE;
56
57 if ($op == 'create') {
58 return user_access('create historical events', $account);
59 }
60
61 if ($op == 'update' || $op == 'delete') {
62 if (user_access('edit own historical events', $account) && ($account->uid == $node->uid)) {
63 return TRUE;
64 }
65 }
66 }
67
68
69 /**
70 * Implementation of hook_menu().
71 */
72 function thisdayinhistory_menu() {
73 $items = array();
74
75 $items['admin/settings/thisdayinhistory'] = array(
76 'title' => 'This Day in History',
77 'description' => 'Create and configure This Day in History blocks and configure options.',
78 'access callback' => 'user_access',
79 'access arguments' => array('administer this day in history'),
80 'page callback' => 'thisdayinhistory_admin_overview',
81 //'page arguments' => array('_thisdayinhistory_admin_settings'),
82 'file' => 'thisdayinhistory.admin.inc',
83 'type' => MENU_NORMAL_ITEM,
84 );
85 $items['admin/settings/thisdayinhistory/overview'] = array(
86 'title' => 'Overview',
87 'description' => 'Create and configure This Day in History blocks and configure options.',
88 'access callback' => 'user_access',
89 'access arguments' => array('administer this day in history'),
90 'file' => 'thisdayinhistory.admin.inc',
91 'type' => MENU_DEFAULT_LOCAL_TASK,
92 'weight' => -2
93 );
94 $items['admin/settings/thisdayinhistory/blocks'] = array(
95 'title' => 'Configure blocks',
96 'description' => 'Configure blocks.',
97 'page callback' => 'drupal_get_form',
98 'page arguments' => array('thisdayinhistory_blocks_configure'),
99 'access callback' => 'user_access',
100 'access arguments' => array('administer this day in history'),
101 'file' => 'thisdayinhistory.admin.inc',
102 'type' => MENU_LOCAL_TASK,
103 'weight' => -1
104 );
105 $items['admin/settings/thisdayinhistory/blocks/delete'] = array(
106 'title' => 'Delete TDIH block',
107 'page callback' => 'drupal_get_form',
108 'page arguments' => array('thisdayinhistory_block_delete_confirmation'),
109 'access callback' => 'user_access',
110 'access arguments' => array('administer this day in history'),
111 'file' => 'thisdayinhistory.admin.inc',
112 'type' => MENU_CALLBACK
113 );
114
115 return $items;
116 }
117
118
119 /**
120 * Implementation of hook_form().
121 */
122 function thisdayinhistory_form(&$node) {
123 $type = node_get_types('type', $node);
124 drupal_add_css(drupal_get_path('module', 'thisdayinhistory') .'/thisdayinhistory.css');
125
126 $form['title'] = array(
127 '#type' => 'textfield',
128 '#title' => check_plain($type->title_label),
129 '#required' => TRUE,
130 '#default_value' => $node->title,
131 '#weight' => -5
132 );
133
134 // Put the body and its associated filter selection form into the same sub-array
135 // so they don't get split up if someone plays around with weights
136 $form['body_filter']['body'] = array(
137 '#type' => 'textarea',
138 '#title' => t('Event'),
139 '#default_value' => $node->body,
140 '#required' => FALSE
141 );
142 $form['body_filter']['filter'] = filter_form($node->format);
143
144 // Now we define the form elements specific to our node type.
145 $form['tdih_date'] = array(
146 '#attributes' => array('class' => 'tdih-date'),
147 '#weight' => -1
148 );
149 $form['tdih_date']['year'] = array(
150 '#type' => 'textfield',
151 '#title' => t('Year'),
152 '#required' => TRUE,
153 '#default_value' => $node->year,
154 '#size' => 4,
155 '#maxlength' => 4,
156 '#field_suffix' => ' - ',
157 '#attributes' => array('class' => 'tdih-datefield'),
158 '#prefix' => '<div class="tdih-date form-item"><label>Date:</label>',
159 );
160 $form['tdih_date']['month'] = array(
161 '#type' => 'textfield',
162 '#title' => t('Month'),
163 '#required' => TRUE,
164 '#default_value' => $node->month,
165 '#size' => 4,
166 '#maxlength' => 2,
167 '#field_suffix' => ' - ',
168 );
169 $form['tdih_date']['day'] = array(
170 '#type' => 'textfield',
171 '#title' => t('Day'),
172 '#required' => TRUE,
173 '#default_value' => $node->day,
174 '#size' => 4,
175 '#maxlength' => 2,
176 '#suffix' => '</div><div style="clear:both;"></div>'
177 );
178
179 if (user_access('promote quotes to block')) {
180 $form['blockdisplay'] = array(
181 '#type' => 'checkbox',
182 '#title' => t('Display in TDiH blocks'),
183 '#default_value' => (isset($node->blockdisplay) ? $node->blockdisplay : 1)
184 );
185 }
186
187 return $form;
188 }
189
190
191 /**
192 * Implementation of hook_validate().
193 */
194 function thisdayinhistory_validate($node, &$form) {
195 $texterror = FALSE;
196 if (!is_numeric($node->year)) {
197 form_set_error('year', t('Year value must be entered as a integer'));
198 $texterror = TRUE;
199 }
200 if (!is_numeric($node->month)) {
201 form_set_error('month', t('Month value must be entered as a integer'));
202 $texterror = TRUE;
203 }
204 if (!is_numeric($node->day)) {
205 form_set_error('day', t('Day value must be entered as a integer'));
206 $texterror = TRUE;
207 }
208 if ($texterror) return;
209
210 if ($node->month < 1) {
211 form_set_error('month', t('Month value can not be set less than 1'));
212 return;
213 }
214 if ($node->month > 12) {
215 form_set_error('month', t('Month value can not be set larger than 12'));
216 return;
217 }
218
219 $days_in_month = array(0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
220 if ($node->day < 1) {
221 form_set_error('day', t('Day value can not be set less than 1'));
222 return;
223 }
224 if ($node->day > $days_in_month[(int)$node->month]) {
225 form_set_error('day', t('Day value is too large'));
226 return;
227 }
228 }
229
230
231 /**
232 * Implementation of hook_insert().
233 */
234 function thisdayinhistory_insert($node) {
235 $isodate = $node->year .'-'. $node->month .'-'. $node->day;
236 db_query("INSERT INTO {thisdayinhistory}
237 (vid, nid, month, day, year, isodate, blockdisplay)
238 VALUES (%d, %d, '%d', %d, %d, '%s', %d)",
239 $node->vid, $node->nid, $node->month, $node->day, $node->year, $isodate, $node->blockdisplay);
240 }
241
242
243 /**
244 * Implementation of hook_update().
245 */
246 function thisdayinhistory_update($node) {
247 // if this is a new node or we're adding a new revision,
248 if ($node->revision) {
249 thisdayinhistory_insert($node);
250 }
251 else {
252 $isodate = $node->year .'-'. $node->month .'-'. $node->day;
253 db_query("UPDATE {thisdayinhistory}
254 SET month = %d, day = %d, year = %d, isodate = '%s', blockdisplay = %d WHERE vid = %d",
255 $node->month, $node->day, $node->year, $isodate, $node->blockdisplay, $node->vid);
256 }
257 }
258
259
260 /**
261 * Implementation of hook_delete().
262 */
263 function thisdayinhistory_delete($node) {
264 // When a node is deleted, we need to remove all related records from out table.
265 // Notice that we're matching all revision, by using the node's nid.
266 db_query('DELETE FROM {thisdayinhistory} WHERE nid = %d', $node->nid);
267 }
268
269
270 /**
271 * Implementation of hook_load().
272 */
273 function thisdayinhistory_load($node) {
274 $additions = db_fetch_object(
275 db_query('SELECT tdih.month, tdih.day, tdih.year, DATE(tdih.isodate) as isodate, tdih.blockdisplay
276 FROM {thisdayinhistory} tdih
277 WHERE vid = %d', $node->vid)
278 );
279 return $additions;
280 }
281
282
283 /**
284 * Implementation of hook_view().
285 */
286 function thisdayinhistory_view($node, $teaser = FALSE, $page = FALSE) {
287 $node = node_prepare($node, $teaser);
288 if (!$node->isodate) $node->isodate = $node->year .'-'. $node->month .'-'. $node->day;
289 $node->content['thisdayinhistory'] = array(
290 '#value' => theme('thisdayinhistory_node_field', $node),
291 '#weight' => -1,
292 );
293
294 return $node;
295 }
296
297
298 /**
299 * Implementation of hook_block().
300 */
301 function thisdayinhistory_block($op = 'list', $delta = 0, $edit = array()) {
302 switch ($op) {
303 //
304 // List
305 //
306 case 'list':
307 $blocks = array();
308 $block_list = db_query('SELECT tdihb.bid, tdihb.name FROM {thisdayinhistory_blocks} tdihb');
309 while ($block = db_fetch_object($block_list)) {
310 $blocks[$block->bid] = array('info' => t('TDiH') .': '. $block->name);
311 }
312 return $blocks;
313
314 //
315 // View
316 //
317 case 'view':
318 return thisdayinhistory_get_block($delta);
319
320 //
321 // Configure
322 //
323 case 'configure':
324 $block = db_fetch_object(
325 db_query("SELECT tdihb.* FROM {thisdayinhistory_blocks} tdihb WHERE bid = %d", $delta)
326 );
327 $block->uid_filter = explode(',', $block->uid_filter);
328 $block->tid_filter = explode(',', $block->tid_filter);
329 $block->rid_filter = explode(',', $block->rid_filter);
330
331 // get roles
332 $roles = user_roles(FALSE, 'create historical events');
333 foreach (user_roles(FALSE, 'edit own historical events') as $rid => $role) {
334 $roles[$rid] = $role;
335 }
336 foreach (user_roles(FALSE, 'administer this day in history') as $rid => $role) {
337 $roles[$rid] = $role;
338 }
339
340 // Get users
341 $users = array();
342 if ($roles[DRUPAL_ANONYMOUS_RID]) {
343 $users[0] = variable_get('anonymous', t('Anonymous'));
344 }
345 $result = db_query(
346 "SELECT DISTINCT u.uid, u.name
347 FROM {users} u
348 LEFT JOIN {users_roles} ur ON ur.uid = u.uid
349 WHERE u.uid = 1 OR ur.rid
350 IN (%s)",
351 implode(',', (count($roles) ? array_keys($roles) : array(0)))
352 );
353 while ($row = db_fetch_object($result)) {
354 $users[$row->uid] = ($row->uid ? $row->name : variable_get('anonymous', t('Anonymous')));
355 }
356
357 $form = array();
358
359 if ($delta) {
360 $form['bid'] = array(
361 '#type' => 'value',
362 '#value' => $delta
363 );
364 }
365
366 $form['name'] = array(
367 '#type' => 'textfield',
368 '#title' => t('Name'),
369 '#required' => TRUE,
370 '#default_value' => $block->name,
371 '#description' => t('Enter a unique name for this block. This will identify the block on the !block administration page and be used for the default block title.', array('!block administration page' => l(t('block administration page'), 'admin/build/block'), '%title' => '%title'))
372 );
373 $form['header']['header_field'] = array(
374 '#type' => 'textarea',
375 '#title' => t('Header'),
376 '#default_value' => $block->header,
377 );
378 $form['header']['header_filter'] = filter_form($block->header_format, NULL, array('header_filter'));
379 $form['block_type'] = array(
380 '#type' => 'radios',
381 '#title' => t('Type'),
382 '#required' => TRUE,
383 '#default_value' => (($block->block_type == 1) ? 1 : 0),
384 '#options' => array(t('All'), t('Random'))
385 );
386
387 if (count($roles)) {
388 $form['rid_filter'] = array(
389 '#type' => 'select',
390 '#title' => t('Role filter'),
391 '#multiple' => TRUE,
392 '#default_value' => $block->rid_filter,
393 '#options' => $roles,
394 '#description' => t('To restrict this block to display only quotes submitted by users in specific roles, select the roles here.')
395 );
396 }
397 else {
398 $form['rid_filter'] = array(
399 '#type' => 'item',
400 '#title' => t('Role filter'),
401 '#description' => t('There are no roles configured with the %create historical events, %administer this day in history permissions, or %edit own historical events, so no roles are available. To filter by role, please assign this permission to at least one role on the !access control page.', array('%create historical events' => t('create historical events'), '%administer this day in history' => t('administer this day in history'), '%edit own historical events' => t('edit own historical events'), '!access control page' => l(t('access control page'), 'admin/user/access')))
402 );
403 }
404
405 $form['uid_filter'] = array(
406 '#type' => 'select',
407 '#title' => t('User filter'),
408 '#multiple' => TRUE,
409 '#default_value' => $block->uid_filter,
410 '#options' => $users,
411 '#description' => t('To restrict this block to display only This Day in History events submitted by specific users, select the users here.')
412 );
413
414 if (function_exists('taxonomy_form_all')) {
415 $form['tid_filter'] = array(
416 '#type' => 'select',
417 '#title' => t('Category filter'),
418 '#multiple' => TRUE,
419 '#default_value' => $block->tid_filter,
420 '#options' => taxonomy_form_all(TRUE),
421 '#description' => t('To restrict this block to display only This Day in History events in specific categories, select the categories here.')
422 );
423 }
424
425 return $form;
426
427 case 'save':
428 $uids = implode(',', (array) $edit['uid_filter']);
429 $tids = implode(',', (array) $edit['tid_filter']);
430 $rids = implode(',', (array) $edit['rid_filter']);
431
432 db_query(
433 "UPDATE {thisdayinhistory_blocks}
434 SET name = '%s', block_type = %d, uid_filter = '%s', tid_filter = '%s', rid_filter = '%s', header='%s', header_format = %d
435 WHERE bid = %d",
436 $edit['name'], (($edit['block_type'] == 1) ? 1 : 0), $uids, $tids, $rids, $edit['header_field'], $edit['header_filter'], $delta
437 );
438 }
439 }
440
441 /**
442 * Returns a list of This Day in History nodes based on the option list passed to it.
443 * The option list is designed to accept a Block array retrieved from the database.
444 *
445 * @param $options
446 * The array specifying options and filter criteria
447 *
448 * @return
449 * A list of nodes matching the specified options and criteria.
450 */
451 function thisdayinhistory_get_events($options = array()) {
452 // Default values
453 $month = date('n');
454 $day = date('j');
455 $promote = '1';
456 $type = 0;
457
458 // Evaluate $options
459 $added_joins = '';
460 $additional_conditions = '';
461 foreach ($options as $name => $value) {
462 switch ($name) {
463 case 'uid_filter':
464 if ($value) {
465 $additional_conditions .= " AND n.uid IN ($value)";
466 }
467 break;
468 case 'tid_filter':
469 if ($value) {
470 $added_joins .= "INNER JOIN {term_node} tn ON tn.nid = n.nid ";
471 $additional_conditions .= " AND tn.tid IN ($value)";
472 }
473 break;
474 case 'rid_filter':
475 if ($value) {
476 $added_joins .= "LEFT JOIN {users_roles} ur ON ur.uid = n.uid ";
477 $additional_conditions .= " AND ur.rid IN ($value) OR (". DRUPAL_ANONYMOUS_RID ." IN ($value) AND n.uid = 0)";
478 }
479 break;
480 case 'promoted_only':
481 $promote = ($value) ? 1 : 0;
482 case 'month':
483 $month = $value;
484 case 'day':
485 $day = $value;
486 case 'block_type':
487 switch ($value) {
488 case 0:
489 $type = 'all';
490 break;
491 case 1:
492 $type = 'random';
493 break;
494 }
495 break;
496 }
497 }
498
499 $query = db_rewrite_sql(
500 "SELECT n.nid FROM {thisdayinhistory} tdih
501 INNER JOIN {node} n ON n.vid = tdih.vid
502 $added_joins
503 WHERE tdih.month = %d AND tdih.day = %d
504 AND n.status = 1
505 AND n.type = 'thisdayinhistory'
506 AND tdih.blockdisplay = $promote
507 $additional_conditions
508 ORDER BY tdih.year"
509 );
510
511 $queryresult = db_query($query, $month, $day);
512
513 $nodelist = array();
514 while ($entry = db_fetch_object($queryresult)) {
515 $nodelist[] = $entry->nid;
516 }
517
518 if ($type == 'random') {
519 $count = count($nodelist);
520 if ($count > 0) {
521 $randomnode = $nodelist[rand(0, $count - 1)];
522 $nodelist = array($randomnode);
523 }
524 }
525
526 $renderednodes = array();
527 foreach ($nodelist as $node_id) {
528 $renderednodes[] = node_prepare(node_load($node_id), TRUE);
529 }
530
531 return $renderednodes;
532 }
533
534 function thisdayinhistory_get_block($delta) {
535 $block = db_fetch_array(
536 db_query('SELECT tdihb.* FROM {thisdayinhistory_blocks} tdihb WHERE tdihb.bid = %d', $delta)
537 );
538 if (!$block) {
539 return NULL;
540 }
541
542 $eventnodes = thisdayinhistory_get_events($block);
543
544 if (count($eventnodes) == 0) {
545 return NULL;
546 }
547 else {
548 return array(
549 'subject' => $block['name'],
550 'content' => theme('thisdayinhistory_block_content', $block, $eventnodes)
551 );
552 }
553 }
554
555
556 /**
557 *
558 * Theme Functions
559 *
560 */
561
562 /**
563 * Implementation of hook_theme().
564 */
565 function thisdayinhistory_theme() {
566 return array(
567 'thisdayinhistory_blocks_configure' => array(
568 'arguments' => array('form' => NULL),
569 ),
570 'thisdayinhistory_node_field' => array(
571 'file' => 'thisdayinhistory.module',
572 'arguments' => array(
573 'node' => NULL,
574 ),
575 ),
576 'thisdayinhistory_block_content' => array(
577 'template' => 'thisdayinhistory-block-content',
578 'path' => drupal_get_path('module', 'thisdayinhistory') .'/theme',
579 'arguments' => array(
580 'block' => NULL,
581 'node_list' => NULL,
582 ),
583 ),
584 );
585 }
586
587
588 /**
589 * Default theme function for TDiH date field displayed with node
590 */
591 function theme_thisdayinhistory_node_field($node) {
592 $output = '<div class="thisdayinhistory-date">';
593 $output .= t('Date: %isodate', array('%isodate' => check_plain($node->isodate)));
594 $output .= '</div>';
595 return $output;
596 }
597
598
599 /**
600 *
601 * Import Node Module Hooks
602 *
603 */
604
605 /**
606 * Implementation of Node Import's hook_node_import_fields().
607 */
608 function thisdayinhistory_node_import_fields($type) {
609 if ($type == 'node:thisdayinhistory') {
610 return array(
611 'iso_date' => array(
612 'title' => t('TDiH Event Date, formatted: yyyy-mm-dd'),
613 'is_mappable' => TRUE,
614 'map_required' => FALSE,
615 ),
616 'european_date' => array(
617 'title' => t('TDiH Event Date, formatted: dd/mm/yyyy'),
618 'is_mappable' => TRUE,
619 'map_required' => FALSE,
620 ),
621 'american_date' => array(
622 'title' => t('TDiH Event Date, formatted: mm/dd/yyyy'),
623 'is_mappable' => TRUE,
624 'map_required' => FALSE,
625 ),
626 'text_date' => array(
627 'title' => t('TDiH Event Date, formatted: mmm d, yyyy'),
628 'is_mappable' => TRUE,
629 'map_required' => FALSE,
630 ),
631 'blockdisplay' => array(
632 'title' => t('TDiH: Display in Block'),
633 'is_mappable' => TRUE,
634 'map_required' => FALSE,
635 ),
636 );
637 }
638 }
639
640
641 /**
642 * Implementation of Node Import's hook_node_import_values_alter().
643 */
644 function thisdayinhistory_node_import_values_alter(&$values, $type, $options, $preview) {
645 if ($values['iso_date']) {
646 $dateparts = explode('-', check_plain($values['iso_date']));
647 $values['year'] = $dateparts[0];
648 $values['month'] = $dateparts[1];
649 $values['day'] = $dateparts[2];
650 }
651 else if ($values['text_date']) {
652 $monthmap = array(t('jan') => 1, t('feb') => 2, t('mar') => 3, t('apr') => 4, t('may') => 5, t('jun') => 6,
653 t('jul') => 7, t('aug') => 8, t('sep') => 9, t('oct') => 10, t('nov') => 11, t('dec') => 12 );
654 $dateparts = explode(' ', check_plain($values['text_date']));
655 $values['year'] = $dateparts[2];
656 $values['month'] = $monthmap[strtolower($dateparts[0])];
657 $values['day'] = str_replace(',', '', $dateparts[1]);
658 }
659 else if ($values['european_date']) {
660 $dateparts = explode('/', check_plain($values['european_date']));
661 $values['year'] = $dateparts[2];
662 $values['month'] = $dateparts[1];
663 $values['day'] = $dateparts[0];
664 }
665 else if ($values['american_date']) {
666 $dateparts = explode('/', check_plain($values['american_date']));
667 $values['year'] = $dateparts[2];
668 $values['month'] = $dateparts[0];
669 $values['day'] = $dateparts[1];
670 }
671 $values['isodate'] = $node->year .'-'. $node->month .'-'. $node->day;
672
673 if ($values['blockdisplay'] == NULL) {
674 $values['blockdisplay'] = 1;
675 }
676 }
677
678 /**
679 * Implementation of Node Import's hook_node_import_fields_alter().
680 */
681 function thisdayinhistory_node_import_fields_alter(&$fields, $type) {
682 $fields['body']['title'] = 'Event Description';
683 }

  ViewVC Help
Powered by ViewVC 1.1.2