| 1 |
<?php
|
| 2 |
// $Id: announcements.module,v 1.9 2008/05/09 01:15:02 nancyw Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Module used to present Announcements
|
| 6 |
* (C) Copyright IBM Corp. 2005-2006
|
| 7 |
*
|
| 8 |
* @file
|
| 9 |
* Enables the creation of announcement pages with a title, abstract, body, author and date.
|
| 10 |
* @link http://www-128.ibm.com/developerworks/ibm/library/i-osource6/ Original module source @endlink
|
| 11 |
*/
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_help().
|
| 15 |
*
|
| 16 |
* This function allows the announcement module to make documentation available
|
| 17 |
* to the Drupal interface.
|
| 18 |
*
|
| 19 |
* @param $section
|
| 20 |
* This determines which part of the interface is requesting help content.
|
| 21 |
* Here we return content for the module description under the administration
|
| 22 |
* interface, 'admin/modules#description', and at the top of the form to add a
|
| 23 |
* new announcement, node/add#announcements.
|
| 24 |
* @return
|
| 25 |
* A string containing the help content.
|
| 26 |
*
|
| 27 |
* More detail at @link http://api.drupal.org/api/HEAD/function/hook_help API page @endlink .
|
| 28 |
*/
|
| 29 |
function announcements_help($section) {
|
| 30 |
switch ($section) {
|
| 31 |
case 'admin/modules#description':
|
| 32 |
return t('Enables the creation of announcement pages that are presented on the home page.');
|
| 33 |
|
| 34 |
case 'node/add#announcements':
|
| 35 |
return t('An announcement is anything worthy of special note on the site. It may be displayed in a block or page, or both.');
|
| 36 |
|
| 37 |
case 'announcements/view':
|
| 38 |
case 'announcements':
|
| 39 |
$links = array();
|
| 40 |
if (user_access('create announcement')) {
|
| 41 |
$links[] = l(t('Add a new announcement'), 'node/add/announcements');
|
| 42 |
}
|
| 43 |
if (user_access('administer site configuration')) {
|
| 44 |
$links[] = l(t('Announcement settings'), 'admin/settings/announcements', array(), drupal_get_destination());
|
| 45 |
}
|
| 46 |
|
| 47 |
return '<div class="announcement-links">'.
|
| 48 |
implode(' | ', $links)
|
| 49 |
.'</div><div class="clear-block"></div>';
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
| 53 |
/**
|
| 54 |
* Implementation of hook_perm().
|
| 55 |
*
|
| 56 |
* This function supplies the permissions that the announcement module defines.
|
| 57 |
* These can then be selected on the Drupal user permissions administration page
|
| 58 |
* and used to restrict access to actions the announcement module performs.
|
| 59 |
*
|
| 60 |
* @return
|
| 61 |
* An array of strings used to identify permissible actions.
|
| 62 |
*
|
| 63 |
* More detail at @link http://api.drupal.org/api/HEAD/function/hook_perm API page @endlink .
|
| 64 |
*/
|
| 65 |
function announcements_perm() {
|
| 66 |
return array('create announcement', 'edit announcement');
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* Implementation of hook_node_info().
|
| 71 |
*
|
| 72 |
* This function is required for modules to define one or more node types. It
|
| 73 |
* allows Drupal to determine the names and the attributes of the announcement
|
| 74 |
* module node type.
|
| 75 |
*
|
| 76 |
* @return
|
| 77 |
* An array of information on the module's node types. The minimum required
|
| 78 |
* information that needs to be returned is the modules human readable name,
|
| 79 |
* the Drupal module name used to build hook function names and short description
|
| 80 |
* of the node type.
|
| 81 |
*
|
| 82 |
* More detail at @link http://api.drupal.org/api/HEAD/function/hook_node_info API page @endlink .
|
| 83 |
*/
|
| 84 |
function announcements_node_info() {
|
| 85 |
return array('announcements' => array(
|
| 86 |
'name' => 'Announcements',
|
| 87 |
'description' => t('An announcement is anything worthy of special note on the site. It may be displayed in a block or page, or both.'),
|
| 88 |
'module' => 'announcements'));
|
| 89 |
}
|
| 90 |
|
| 91 |
/**
|
| 92 |
* Implementation of hook_access().
|
| 93 |
*
|
| 94 |
* This function allows the announcement module to limit access to the node type
|
| 95 |
* it defines depending on the operation currently being performed and the
|
| 96 |
* permissions defined in the Drupal user permissions administration page.
|
| 97 |
*
|
| 98 |
* @param $op
|
| 99 |
* The Drupal operation currently being performed. Here we want to control access
|
| 100 |
* for the create, view, update and delete operations.
|
| 101 |
* @param $node
|
| 102 |
* The node object on which this operation is being performed.
|
| 103 |
* @return
|
| 104 |
* A boolean value depending on whether the operation can be performed or not.
|
| 105 |
*
|
| 106 |
* More detail at @link http://api.drupal.org/api/HEAD/function/hook_access API page @endlink .
|
| 107 |
*/
|
| 108 |
function announcements_access($op, $node) {
|
| 109 |
global $user;
|
| 110 |
|
| 111 |
switch ($op) {
|
| 112 |
case 'create':
|
| 113 |
return user_access('create announcement');
|
| 114 |
|
| 115 |
case 'view':
|
| 116 |
return user_access('access content');
|
| 117 |
|
| 118 |
case 'update':
|
| 119 |
case 'delete':
|
| 120 |
// 'Edit own' is by default.
|
| 121 |
if ($user->uid == $node->uid || user_access('edit announcement')) {
|
| 122 |
return true;
|
| 123 |
}
|
| 124 |
else {
|
| 125 |
return false;
|
| 126 |
}
|
| 127 |
|
| 128 |
default:
|
| 129 |
return false;
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
| 133 |
/**
|
| 134 |
* Implementation of hook_menu().
|
| 135 |
*
|
| 136 |
* This function allows the announcement module to register URL paths and
|
| 137 |
* determine how these requests are to be handled. Depending on the
|
| 138 |
* registration a link may be placed in a menu or as a tab at the top of
|
| 139 |
* the page.
|
| 140 |
*
|
| 141 |
* NOTE: For version 5.*, you need to register the URL path,
|
| 142 |
* admin/settings/announcement, to map to a callback function to create
|
| 143 |
* a form to manipulate its settings.
|
| 144 |
*
|
| 145 |
* @param $may_cache
|
| 146 |
* This is a boolean used to determine if a registered URL path should
|
| 147 |
* be cached or not. Usually, those URL path that include some dynamic
|
| 148 |
* value should not be cached.
|
| 149 |
* @return
|
| 150 |
* An array of registered URL path objects. These contain at least the
|
| 151 |
* registered URL path, a string of text used as a title for these paths,
|
| 152 |
* an access flag built by testing the access list, a type to determine
|
| 153 |
* how this registration be used, and the name of the callback function
|
| 154 |
* that should be called when this URL path is requested.
|
| 155 |
*
|
| 156 |
* More detail at @link http://api.drupal.org/api/HEAD/function/hook_menu API page @endlink .
|
| 157 |
*/
|
| 158 |
function announcements_menu($may_cache) {
|
| 159 |
$items = array();
|
| 160 |
|
| 161 |
if ($may_cache) {
|
| 162 |
$items[] = array(
|
| 163 |
'path' => 'node/add/announcements',
|
| 164 |
'title' => t('Announcement'),
|
| 165 |
'access' => user_access('create announcement'),
|
| 166 |
);
|
| 167 |
|
| 168 |
$items[] = array(
|
| 169 |
'path' => 'admin/settings/announcements',
|
| 170 |
'title' => t('Announcement settings'),
|
| 171 |
'access' => user_access('administer site configuration'),
|
| 172 |
'type' => MENU_NORMAL_ITEM,
|
| 173 |
'callback arguments' => array('announcements_settings'),
|
| 174 |
'callback' => 'drupal_get_form',
|
| 175 |
);
|
| 176 |
|
| 177 |
$items[] = array(
|
| 178 |
'path' => 'announcements',
|
| 179 |
'title' => t('Announcements'),
|
| 180 |
'access' => user_access('access content'),
|
| 181 |
'callback' => 'announcements_all',
|
| 182 |
);
|
| 183 |
}
|
| 184 |
else {
|
| 185 |
drupal_add_css(drupal_get_path('module', 'announcements') .'/announcements.css');
|
| 186 |
|
| 187 |
$items[] = array(
|
| 188 |
'path' => 'announcements/pager',
|
| 189 |
'title' => t('Announcements Pager Example'),
|
| 190 |
'access' => user_access('administer site'),
|
| 191 |
'type' => MENU_CALLBACK,
|
| 192 |
'callback' => 'announcements_pager',
|
| 193 |
);
|
| 194 |
}
|
| 195 |
|
| 196 |
return $items;
|
| 197 |
}
|
| 198 |
|
| 199 |
/**
|
| 200 |
*
|
| 201 |
* This function builds a themed set of links, known as the pager, to each
|
| 202 |
* page of a paginated list of announcements. This is usually placed at the
|
| 203 |
* bottom of each of these paginated pages.
|
| 204 |
*
|
| 205 |
* @return
|
| 206 |
* A string of XHTML used to render the pager.
|
| 207 |
*/
|
| 208 |
function announcements_pager() {
|
| 209 |
$result = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE type = 'announcement' ORDER BY n.created DESC"), variable_get('default_nodes_main', 10));
|
| 210 |
while ($announcement = db_fetch_object($result)) {
|
| 211 |
$output .= node_view(node_load($announcement->nid), 1);
|
| 212 |
}
|
| 213 |
|
| 214 |
$output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
|
| 215 |
|
| 216 |
return $output;
|
| 217 |
}
|
| 218 |
|
| 219 |
/**
|
| 220 |
* Implementation of hook_cron().
|
| 221 |
*
|
| 222 |
* This function allows the announcement module to insert its own actions when
|
| 223 |
* the cron.php script is run usually by the system cron system. This is useful
|
| 224 |
* when performing periodic asynchronous tasks like, as in this case, checking to
|
| 225 |
* see if any announcements have expired.
|
| 226 |
*
|
| 227 |
* @return
|
| 228 |
* Nothing.
|
| 229 |
*
|
| 230 |
* More detail at @link http://api.drupal.org/api/HEAD/function/hook_cron API page @endlink .
|
| 231 |
*/
|
| 232 |
function announcements_cron() {
|
| 233 |
|
| 234 |
// Mark expired announcements as unpublished so that non-administrative
|
| 235 |
// users cannot see them on the site through standard node mechanisms.
|
| 236 |
$query_result = db_query("UPDATE {node} AS n INNER JOIN {announcements} AS a ON n.nid = a.nid SET n.status = 0 WHERE n.type='announcements' AND n.status = 1 AND a.expiration_date < %d", time());
|
| 237 |
}
|
| 238 |
|
| 239 |
/**
|
| 240 |
* Implementation of hook_link().
|
| 241 |
*
|
| 242 |
* This function allows the announcement module to insert its own links
|
| 243 |
* into certain parts of Drupal generated content. In this case add, edit
|
| 244 |
* and delete links are added to Drupal rendered announcements depending
|
| 245 |
* on the access permissions of the current user.
|
| 246 |
*
|
| 247 |
* @param $type
|
| 248 |
* A string identifying the type of link being requested. In this case,
|
| 249 |
* the links placed below an announcement node.
|
| 250 |
* @param $node
|
| 251 |
* The node object currently being requested.
|
| 252 |
* @param $teaser
|
| 253 |
* A boolean indicating if the full announcement is being displayed or
|
| 254 |
* just its teaser.
|
| 255 |
* @return
|
| 256 |
* An array of link objects as created by the l() function.
|
| 257 |
*
|
| 258 |
* More detail at @link http://api.drupal.org/api/HEAD/function/hook_link API page @endlink .
|
| 259 |
*/
|
| 260 |
/* function announcements_link($type, $node = NULL, $teaser = FALSE) {
|
| 261 |
global $user;
|
| 262 |
$links = array();
|
| 263 |
|
| 264 |
if ($type == 'node' && $node->type == 'announcement') {
|
| 265 |
if (node_access('create', 'announcement')) {
|
| 266 |
$links[t('Add')] = array(
|
| 267 |
'href' => 'node/add/announcement',
|
| 268 |
'title' => t('Add a new announcement'),
|
| 269 |
);
|
| 270 |
}
|
| 271 |
|
| 272 |
if (node_access('update', $node)) {
|
| 273 |
$links[t('Edit')] = array(
|
| 274 |
'href' => "announcements/$node->nid/edit",
|
| 275 |
'title' => t('Edit Announcement ') . $node->title,
|
| 276 |
);
|
| 277 |
|
| 278 |
$links[t('Delete')] = array(
|
| 279 |
'href' => "announcements/$node->nid/delete",
|
| 280 |
'title' => t('Delete Announcement ') . $node->title,
|
| 281 |
);
|
| 282 |
}
|
| 283 |
}
|
| 284 |
|
| 285 |
return $links;
|
| 286 |
} */
|
| 287 |
|
| 288 |
/**
|
| 289 |
* Implementation of hook_settings().
|
| 290 |
*
|
| 291 |
* This function provides an administrative interface for controlling
|
| 292 |
* various settings for the announcement module.
|
| 293 |
*
|
| 294 |
* NOTE: This hook is not used in Drupal v5 - converted to regular form.
|
| 295 |
* (@link http://drupal.org/node/64279#hook-settings http://drupal.org/node/64279#hook-settings @endlink .)
|
| 296 |
*
|
| 297 |
* @return
|
| 298 |
* An array description, in the Drupal Forms API format, of the elements
|
| 299 |
* to render the settings interface.
|
| 300 |
*
|
| 301 |
* More detail at @link http://api.drupal.org/api/4.7/function/hook_settings API page @endlink .
|
| 302 |
*/
|
| 303 |
function announcements_settings() {
|
| 304 |
$form = array();
|
| 305 |
$form['announcements_block_max_list_count'] = array(
|
| 306 |
'#type' => 'textfield',
|
| 307 |
'#title' => t('Maximum number of block announcements'),
|
| 308 |
'#default_value' => variable_get('announcements_block_max_list_count', 3),
|
| 309 |
'#description' => t('The maximum number of items listed in the announcement block'),
|
| 310 |
'#required' => FALSE,
|
| 311 |
'#size' => 6,
|
| 312 |
);
|
| 313 |
|
| 314 |
$form['announcements_display_classification'] = array(
|
| 315 |
'#type' => 'checkbox',
|
| 316 |
'#title' => t('Display additional announcement classification'),
|
| 317 |
'#default_value' => variable_get('announcements_display_classification', true),
|
| 318 |
'#description' => t('Insert the additional classification in the announcement modules'),
|
| 319 |
'#required' => FALSE,
|
| 320 |
);
|
| 321 |
|
| 322 |
$form['announcements_body_required'] = array(
|
| 323 |
'#type' => 'checkbox',
|
| 324 |
'#title' => t('Require a body'),
|
| 325 |
'#default_value' => variable_get('announcements_body_required', false),
|
| 326 |
'#description' => t('In addition to the abstract, do you require a body?'),
|
| 327 |
'#required' => FALSE,
|
| 328 |
);
|
| 329 |
|
| 330 |
$intervals = array(
|
| 331 |
'+1 day' => '1 day',
|
| 332 |
'+1 week' => '1 week',
|
| 333 |
'+2 week' => '2 weeks',
|
| 334 |
'+1 month' => '1 month',
|
| 335 |
'+2 month' => '2 months',
|
| 336 |
'+3 month' => '3 months',
|
| 337 |
'+6 month' => '6 months',
|
| 338 |
'+1 year' => '1 year',
|
| 339 |
);
|
| 340 |
$form['announcements_interval'] = array(
|
| 341 |
'#type' => 'radios',
|
| 342 |
'#options' => $intervals,
|
| 343 |
'#title' => t('Default duration of announcement'),
|
| 344 |
'#default_value' => variable_get('announcements_interval', '+1 month'),
|
| 345 |
'#description' => t('This will be added to the current date to set the default ending date.'),
|
| 346 |
'#required' => FALSE,
|
| 347 |
'#prefix' => '<div class="announcements-radios">',
|
| 348 |
'#suffix' => '</div><div class="clear-block"></div>',
|
| 349 |
);
|
| 350 |
|
| 351 |
return system_settings_form($form);
|
| 352 |
}
|
| 353 |
|
| 354 |
/**
|
| 355 |
* This function is called to retrieve the form that is displayed when one attempts
|
| 356 |
* to "create" an announcement.
|
| 357 |
*/
|
| 358 |
function announcements_form(&$node) {
|
| 359 |
$type = node_get_types('type', $node);
|
| 360 |
|
| 361 |
if ($node->expiration_date == NULL) {
|
| 362 |
$node->expiration_date = strtotime(variable_get('announcements_interval', '+1 month'));
|
| 363 |
}
|
| 364 |
|
| 365 |
if ($node->publish_date == NULL) {
|
| 366 |
$node->publish_date = time();
|
| 367 |
}
|
| 368 |
|
| 369 |
$form = node_content_form($node);
|
| 370 |
// $form['title'] = array('#type' => 'textfield',
|
| 371 |
// '#title' => t('Title'),
|
| 372 |
// '#default_value' => $node->title,
|
| 373 |
// '#description' => t('Title of the announcement'),
|
| 374 |
// '#required' => TRUE,
|
| 375 |
// '#weight' => -5
|
| 376 |
// );
|
| 377 |
$form['body_filter']['body']['#description'] = t('The full text of the announcement.');
|
| 378 |
$form['body_filter']['body']['#required'] = variable_get('announcements_body_required', false);
|
| 379 |
|
| 380 |
$form['publication'] = array('#type' => 'fieldset',
|
| 381 |
'#collapsible' => false,
|
| 382 |
'#title' => t('Publication dates'),
|
| 383 |
'#weight' => -4
|
| 384 |
);
|
| 385 |
|
| 386 |
$form['publication']['publish_date'] = array(
|
| 387 |
'#type' => 'date',
|
| 388 |
'#title' => t('Publication date'),
|
| 389 |
'#required' => true,
|
| 390 |
'#default_value' => _announcements_unixtime2drupaldate($node->publish_date),
|
| 391 |
'#prefix' => '<div class="date_widget">',
|
| 392 |
'#suffix' => '</div>',
|
| 393 |
);
|
| 394 |
|
| 395 |
$form['publication']['expiration_date'] = array(
|
| 396 |
'#type' => 'date',
|
| 397 |
'#title' => t('Expiration date'),
|
| 398 |
'#required' => true,
|
| 399 |
'#default_value' => _announcements_unixtime2drupaldate($node->expiration_date),
|
| 400 |
'#prefix' => '<div class="date_widget">',
|
| 401 |
'#suffix' => '</div>',
|
| 402 |
);
|
| 403 |
|
| 404 |
$form['abstract'] = array('#type' => 'textarea',
|
| 405 |
'#title' => t('Abstract'),
|
| 406 |
'#default_value' => $node->abstract,
|
| 407 |
'#rows' => 3,
|
| 408 |
'#description' => t('Short summary of the announcement'),
|
| 409 |
'#required' => true,
|
| 410 |
'#weight' => -3,
|
| 411 |
);
|
| 412 |
|
| 413 |
// $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
|
| 414 |
|
| 415 |
return $form;
|
| 416 |
}
|
| 417 |
|
| 418 |
/**
|
| 419 |
* Implementation of hook_validate()
|
| 420 |
* This just checks to ensure that the expiration date is after the publish date.
|
| 421 |
* No node attributes are set.
|
| 422 |
*/
|
| 423 |
function announcements_validate($node) {
|
| 424 |
if ($node) {
|
| 425 |
$publish_date = _announcements_drupaldate2unixtime($node->publish_date);
|
| 426 |
$expiration_date = _announcements_drupaldate2unixtime($node->expiration_date);
|
| 427 |
if ($publish_date >= $expiration_date) {
|
| 428 |
form_set_error('publish_date', t('The publish date of an announcement must be before its expiration date.'));
|
| 429 |
}
|
| 430 |
}
|
| 431 |
}
|
| 432 |
|
| 433 |
/**
|
| 434 |
* Implementation of hook_submit()
|
| 435 |
* Prepare the node for submission into the database.
|
| 436 |
* Update the date values to integer values
|
| 437 |
*/
|
| 438 |
function announcements_submit(&$node) {
|
| 439 |
$node->publish_date = _announcements_drupaldate2unixtime($node->publish_date);
|
| 440 |
$node->expiration_date = _announcements_drupaldate2unixtime($node->expiration_date);
|
| 441 |
|
| 442 |
$now = time();
|
| 443 |
if ($now > $node->publish_date && $now < $node->expiration_date) {
|
| 444 |
$node->status = 1;
|
| 445 |
}
|
| 446 |
else {
|
| 447 |
$node->status = 0;
|
| 448 |
}
|
| 449 |
}
|
| 450 |
|
| 451 |
/**
|
| 452 |
* Only show those announcements that have not expired for the average user.
|
| 453 |
* If they have access to edit, show all announcements.
|
| 454 |
*/
|
| 455 |
function announcements_all($option = null) {
|
| 456 |
$output = '<div class="announcements">';
|
| 457 |
if (user_access('edit announcement')) {
|
| 458 |
$query_result = db_query("SELECT n.nid FROM {node} n INNER JOIN {announcements} a ON n.nid = a.nid " .
|
| 459 |
"WHERE n.type='announcements' ORDER BY n.sticky DESC, a.publish_date DESC");
|
| 460 |
}
|
| 461 |
else {
|
| 462 |
$query_result = db_query("SELECT n.nid FROM {node} n INNER JOIN {announcements} a ON n.nid = a.nid " .
|
| 463 |
"WHERE n.type='announcements' AND a.expiration_date > %d ORDER BY n.sticky DESC, a.publish_date DESC", date("U"));
|
| 464 |
}
|
| 465 |
|
| 466 |
$page_content = array();
|
| 467 |
while ($nid = db_fetch_object($query_result)) {
|
| 468 |
$announcement = node_load($nid->nid);
|
| 469 |
$announcement->url = url('announcements/'. $announcement->nid);
|
| 470 |
if ($option == 'view') {
|
| 471 |
$output .= theme('announcements', $announcement);
|
| 472 |
}
|
| 473 |
else {
|
| 474 |
$output .= theme('announcements_compact', $announcement);
|
| 475 |
}
|
| 476 |
}
|
| 477 |
|
| 478 |
$output .= '</div>';
|
| 479 |
return $output;
|
| 480 |
}
|
| 481 |
|
| 482 |
/**
|
| 483 |
* Database hooks when loading, inserting, updating or deleting an announcement
|
| 484 |
*/
|
| 485 |
|
| 486 |
/**
|
| 487 |
* Implementation of hook_view()
|
| 488 |
* This function is called to allow the module a chance to format extra information to display.
|
| 489 |
*/
|
| 490 |
function announcements_view($node, $teaser = false, $page = false) {
|
| 491 |
$node = node_prepare($node, $teaser);
|
| 492 |
$node->content['dates'] = array(
|
| 493 |
'#value' => theme('announcements_dates', $node),
|
| 494 |
'#weight' => -4,
|
| 495 |
);
|
| 496 |
$node->content['abstract'] = array(
|
| 497 |
'#value' => theme('announcements_abstract', $node),
|
| 498 |
'#weight' => -2,
|
| 499 |
);
|
| 500 |
return $node;
|
| 501 |
}
|
| 502 |
|
| 503 |
/**
|
| 504 |
* Implementation of hook_load()
|
| 505 |
* This function is called to allow the module a chance to load extra information that
|
| 506 |
* it stores about an announcement.
|
| 507 |
*/
|
| 508 |
function announcements_load($node) {
|
| 509 |
$additions = db_fetch_object(db_query('SELECT * FROM {announcements} WHERE nid = %d', $node->nid));
|
| 510 |
return $additions;
|
| 511 |
}
|
| 512 |
|
| 513 |
/**
|
| 514 |
* Implementation of hook_insert()
|
| 515 |
* This function is called to allow the module to take action when a new node is
|
| 516 |
* being inserted in the database.
|
| 517 |
*/
|
| 518 |
function announcements_insert($node) {
|
| 519 |
db_query("INSERT INTO {announcements} (nid, abstract, publish_date, expiration_date) VALUES (%d, '%s', '%d', '%d')", $node->nid, $node->abstract, $node->publish_date, $node->expiration_date);
|
| 520 |
}
|
| 521 |
|
| 522 |
/**
|
| 523 |
* Implementation of hook_update()
|
| 524 |
* As an existing node is being updated in the database, we need to do our own
|
| 525 |
* database updates, i.e., put info into announcement table.
|
| 526 |
*/
|
| 527 |
function announcements_update($node) {
|
| 528 |
db_query("UPDATE {announcements} SET abstract='%s', publish_date = '%s', expiration_date = '%s' WHERE nid = %d", $node->abstract, $node->publish_date, $node->expiration_date, $node->nid);
|
| 529 |
}
|
| 530 |
|
| 531 |
/**
|
| 532 |
* Implementation of hook_delete()
|
| 533 |
* This function is called to allow the module to take action when a node is
|
| 534 |
* being deleted from the database.
|
| 535 |
*/
|
| 536 |
function announcements_delete($node) {
|
| 537 |
db_query('DELETE FROM {announcements} WHERE nid = %d', $node->nid);
|
| 538 |
}
|
| 539 |
|
| 540 |
/**
|
| 541 |
* Implementation of hook_block().
|
| 542 |
*/
|
| 543 |
function announcements_block($op = 'list', $delta = 0, $edit = array()) {
|
| 544 |
global $user;
|
| 545 |
$vids = _announcements_get_vocabularies();
|
| 546 |
if ($op == 'list') {
|
| 547 |
$blocks[0]['info'] = t('Announcements: Recent');
|
| 548 |
if ($vids) {
|
| 549 |
foreach ($vids as $vid => $name) {
|
| 550 |
$blocks[$vid]['info'] = t('Announcements: In !name vocabulary', array('!name' => $name));
|
| 551 |
}
|
| 552 |
}
|
| 553 |
return $blocks;
|
| 554 |
}
|
| 555 |
else if ($op == 'view') {
|
| 556 |
$block = array();
|
| 557 |
//$output = '';
|
| 558 |
|
| 559 |
switch ($delta) {
|
| 560 |
case 0:
|
| 561 |
$announcement_items = array();
|
| 562 |
|
| 563 |
if (user_access('access content')) {
|
| 564 |
$now = time();
|
| 565 |
$qargs = array($now, $now);
|
| 566 |
|
| 567 |
$q = 'SELECT n.nid FROM {node} n JOIN {announcements} a USING(nid) '.
|
| 568 |
"WHERE n.type='announcements' AND n.status=1 ".
|
| 569 |
'AND a.publish_date < %d AND a.expiration_date > %d ORDER BY a.publish_date ASC ';
|
| 570 |
|
| 571 |
$result = db_query_range($q, $qargs, 0, variable_get('announcement_block_max_list_count', 3));
|
| 572 |
while ($announcement = db_fetch_object($result)) {
|
| 573 |
$announcement_items[] = node_load($announcement->nid);
|
| 574 |
}
|
| 575 |
}
|
| 576 |
if ($announcement_items) {
|
| 577 |
// $block['subject'] = t('Announcements');
|
| 578 |
$block['content'] = theme('announcements_block_list', $announcement_items);
|
| 579 |
}
|
| 580 |
break;
|
| 581 |
|
| 582 |
case 1:
|
| 583 |
if (user_access("access content")) {
|
| 584 |
$vocabulary = taxonomy_get_vocabulary($delta);
|
| 585 |
$block['subject']= $vocabulary->name;
|
| 586 |
$block['content']= announcements_vocab_vert($vocabulary->vid);
|
| 587 |
}
|
| 588 |
}
|
| 589 |
return $block;
|
| 590 |
}
|
| 591 |
}
|
| 592 |
|
| 593 |
/**
|
| 594 |
* Adapated from the taxonomy_dhtml module.
|
| 595 |
*/
|
| 596 |
function announcements_vocab_vert($vocabulary_id, $op = NULL) {
|
| 597 |
$tree = taxonomy_get_tree($vocabulary_id);
|
| 598 |
// Build an array which holds all children of current term.
|
| 599 |
// Necessary to build a proper 'or' value in the HREF
|
| 600 |
foreach ($tree as $term) {
|
| 601 |
//$url = "taxonomy/term/$term->tid/9";
|
| 602 |
$url = "taxonomy/term/$term->tid";
|
| 603 |
if ($op) {
|
| 604 |
$url .= "/$op";
|
| 605 |
}
|
| 606 |
$link = l($term->name, $url, array("title" => $term->description));
|
| 607 |
$out .= _taxonomy_depth($term->depth, " ") ."- $link";
|
| 608 |
$count = taxonomy_term_count_nodes($term->tid);
|
| 609 |
if ($count) {
|
| 610 |
$out .= " ($count)";
|
| 611 |
$out .= _announcements_by_terms($term->tid);
|
| 612 |
}
|
| 613 |
else {
|
| 614 |
$out .= " (0)";
|
| 615 |
}
|
| 616 |
$out .= "<br />";
|
| 617 |
}
|
| 618 |
return $out;
|
| 619 |
}
|
| 620 |
|
| 621 |
/**
|
| 622 |
* Show all the announcements classified by this term.
|
| 623 |
*/
|
| 624 |
function _announcements_by_terms($tid) {
|
| 625 |
$result = '';
|
| 626 |
$tids = array( $tid );
|
| 627 |
$nodes = taxonomy_select_nodes($tids, 'or', 0, FALSE);
|
| 628 |
while ($r = db_fetch_object($nodes)) {
|
| 629 |
$url = "announcements/". $r->nid;
|
| 630 |
$result .= "<br/> - " . l($r->title, $url, array("title" => $r->title));
|
| 631 |
}
|
| 632 |
return $result;
|
| 633 |
}
|
| 634 |
|
| 635 |
/**
|
| 636 |
* Implementation of hook_nodeapi().
|
| 637 |
* If the operation is update index, we want to add the abstract
|
| 638 |
* of this announcement to the search index.
|
| 639 |
*/
|
| 640 |
function announcements_nodeapi(&$node, $op) {
|
| 641 |
switch ($op) {
|
| 642 |
// Add abstract field from announcement table to the search index
|
| 643 |
case 'update index':
|
| 644 |
if ($node->type == 'announcements') {
|
| 645 |
$text = '';
|
| 646 |
$q = db_query(
|
| 647 |
'SELECT a.abstract FROM {node} n LEFT JOIN {announcements} a ON n.nid = a.nid '.
|
| 648 |
'WHERE n.nid = %d', $node->nid);
|
| 649 |
if ($r = db_fetch_object($q)) {
|
| 650 |
$text = $r->abstract;
|
| 651 |
}
|
| 652 |
return $text;
|
| 653 |
}
|
| 654 |
}
|
| 655 |
}
|
| 656 |
|
| 657 |
/*
|
| 658 |
* Theme functions
|
| 659 |
*
|
| 660 |
* There are two theme functions:
|
| 661 |
*
|
| 662 |
* theme_announcement - Generic theme function that will work for all themes and theme engines
|
| 663 |
*
|
| 664 |
* phptemplate_annoucement - Theme function that will be used by the phptemplate engine
|
| 665 |
*/
|
| 666 |
function theme_announcements($announcement) {
|
| 667 |
// return '';
|
| 668 |
return node_view($announcement, true, false, variable_get('announcement_display_classification', true));
|
| 669 |
}
|
| 670 |
|
| 671 |
function theme_announcements_dates($announcement) {
|
| 672 |
$output = '<div class="announcement-dates">'. t('Starting') .' ';
|
| 673 |
$output .= '<span class="announcement-start">'. format_date($announcement->publish_date, 'custom', 'F j, Y') .'</span>';
|
| 674 |
$output .= ' - '. t('Ending') .' ';
|
| 675 |
$output .= '<span class="announcement-end">'. format_date($announcement->expiration_date, 'custom', 'F j, Y') .'</span>';
|
| 676 |
$output .= '</div>';
|
| 677 |
return $output;
|
| 678 |
}
|
| 679 |
|
| 680 |
function theme_announcements_compact($announcement) {
|
| 681 |
// return '';
|
| 682 |
$path = drupal_get_path_alias('node/'. $announcement->nid);
|
| 683 |
$output = '<h3>'. l($announcement->title, $path) .'</h3>';
|
| 684 |
$output .= theme('announcements_dates', $announcement);
|
| 685 |
$output .= theme('announcements_abstract', $announcement);
|
| 686 |
return $output;
|
| 687 |
}
|
| 688 |
|
| 689 |
function theme_announcements_block_list($announcement_list) {
|
| 690 |
// return '';
|
| 691 |
$output = null;
|
| 692 |
foreach ($announcement_list as $announcement) {
|
| 693 |
$output .= '<h3>'. l($announcement->title, 'node/'. $announcement->nid) .'</h3>';
|
| 694 |
$output .= theme('announcements_abstract', $announcement);
|
| 695 |
}
|
| 696 |
return $output;
|
| 697 |
}
|
| 698 |
|
| 699 |
function theme_announcements_abstract($announcement) {
|
| 700 |
return '<div class="announcement-abstract">'. check_markup($announcement->abstract, $announcement->format) .'</div>';
|
| 701 |
}
|
| 702 |
|
| 703 |
//function phptemplate_announcement($announcement) {
|
| 704 |
// return _theme_phptemplate_announcement($announcement, 'announcement');
|
| 705 |
//}
|
| 706 |
|
| 707 |
//function phptemplate_announcement_compact($announcement) {
|
| 708 |
// return _theme_phptemplate_announcement($announcement, 'announcement_compact');
|
| 709 |
//}
|
| 710 |
|
| 711 |
//function phptemplate_announcement_block_list($announcement_list) {
|
| 712 |
// global $user;
|
| 713 |
// return _phptemplate_callback('announcement_block_list', array('announcements' => $announcement_list, 'user' => $user));
|
| 714 |
//}
|
| 715 |
|
| 716 |
function _theme_phptemplate_announcements($announcement, $announcement_template) {
|
| 717 |
$expired = FALSE;
|
| 718 |
if ($announcement->expiration_date < time()) {
|
| 719 |
$expired = TRUE;
|
| 720 |
}
|
| 721 |
$variables = array(
|
| 722 |
'title' => $announcement->title,
|
| 723 |
'body' => $announcement->body,
|
| 724 |
'links' => $announcement->links ? theme('links', $announcement->links) : '',
|
| 725 |
'abstract' => $announcement->abstract,
|
| 726 |
'published' => format_date($announcement->publish_date, 'custom', 'j M, Y'),
|
| 727 |
'expires' => format_date($announcement->expiration_date, 'custom', 'j M, Y'),
|
| 728 |
'expired' => $expired,
|
| 729 |
'node' => $announcement
|
| 730 |
);
|
| 731 |
return _phptemplate_callback($announcement_template, $variables);
|
| 732 |
}
|
| 733 |
|
| 734 |
function _announcements_drupaldate2unixtime($drupal_date) {
|
| 735 |
$year = $drupal_date["year"];
|
| 736 |
$month = $drupal_date["month"];
|
| 737 |
$day = $drupal_date["day"];
|
| 738 |
|
| 739 |
// Making the hour 12 helps prevent the date from adjusting due to GMT offset.
|
| 740 |
return mktime(12, 0, 0, (int)$month, (int)$day, (int)$year);
|
| 741 |
}
|
| 742 |
|
| 743 |
function _announcements_unixtime2drupaldate($unixtime) {
|
| 744 |
return array('day' => date('j', $unixtime),
|
| 745 |
'month' => date('n', $unixtime),
|
| 746 |
'year' => date('Y', $unixtime));
|
| 747 |
}
|
| 748 |
|
| 749 |
/**
|
| 750 |
* Function to get array of vocabularies that are sey up for the announcements type.
|
| 751 |
*/
|
| 752 |
function _announcements_get_vocabularies() {
|
| 753 |
$vids = array();
|
| 754 |
$result = db_query("SELECT t.vid, v.name FROM {vocabulary_node_types} t JOIN {vocabulary} v USING (vid) WHERE t.type='announcements'");
|
| 755 |
while ($voc = db_fetch_object($result)) {
|
| 756 |
$vids[$voc->vid] = $voc->name;
|
| 757 |
}
|
| 758 |
return $vids;
|
| 759 |
}
|