array('name' => 'Announcement', 'description' => 'An Announcement. Use this page to add an announcement page.', 'module' => 'announcement')); } /** * Implementation of hook_access(). * * This function allows the announcement module to limit access to the node type * it defines depending on the operation currently being performed and the * permissions defined in the Drupal user permissions administration page. * * @param $op * The Drupal operation currently being performed. Here we want to control access * for the ‘create’, ‘view’, ‘update’ and ‘delete’ operations. * @param $node * The node object on which this operation is being performed. * @return * A boolean value depending on whether the operation can be performed or not. * * More detail at http://api.drupal.org/api/HEAD/function/hook_access */ function announcement_access($op, $node) { global $user; if ($op == 'create') { return user_access('create announcement'); } else if ($op == 'view') { return user_access('access content'); } else if ($op == 'update' || $op == 'delete') { if($user->uid == $node->uid || user_access('edit announcement')) { return true; } else { return false; } } else { return false; } } /** * Implementation of hook_menu(). * * This function allows the announcement module to register URL paths and * determine how these requests are to be handled. Depending on the * registration a link may be placed in a menu or as a tab at the top of * the page. * * NOTE: For version 5.*, you need to register the URL path, * admin/settings/announcement, to map to a callback function to create * a form to manipulate its settings. * * @param $may_cache * This is a boolean used to determine if a registered URL path should * be cached or not. Usually, those URL path that include some dynamic * value should not be cached. * @return * An array of registered URL path objects. These contain at least the * registered URL path, a string of text used as a title for these paths, * an access flag built by testing the access list, a type to determine * how this registration be used, and the name of the callback function * that should be called when this URL path is requested. * * More detail at http://api.drupal.org/api/HEAD/function/hook_menu * */ function announcement_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array('path' => 'announcements/add', 'title' => t('Add a new Announcement'), 'access' => node_access('create', 'announcement'), 'type' => MENU_CALLBACK, 'callback arguments' => array('announcement'), 'callback' => 'node_add'); $items[] = array('path' => 'announcements', 'title' => t('Announcements'), 'access' => user_access('access content'), 'type' => MENU_CALLBACK, 'callback' => 'announcement_all'); } else { $items[] = array('path' => 'announcements/pager', 'title' => t('Announcements Pager Example'), 'access' => user_access('administer site'), 'type' => MENU_CALLBACK, 'callback' => 'announcement_pager'); if(is_numeric(arg(1))) { $node = node_load(arg(1)); $items[] = array('path' => 'announcements/' . arg(1), 'title' => t('View an Announcement'), 'access' => node_access('view', $node), 'type' => MENU_CALLBACK, 'callback' => 'node_page'); $items[] = array('path' => 'announcements/' . arg(1) . '/view', 'title' => t('View an Announcement'), 'access' => node_access('view', $node), 'type' => MENU_CALLBACK, 'callback' => 'node_page'); $items[] = array('path' => 'announcements/' . arg(1) . '/edit', 'title' => t('Edit an Announcement'), 'access' => node_access('edit', $node), 'type' => MENU_CALLBACK, 'callback' => 'node_page'); $items[] = array('path' => 'announcements/' . arg(1) . '/delete', 'access' => node_access('delete', $node), 'type' => MENU_CALLBACK, 'callback' => 'node_delete_confirm'); } } return $items; } /** * * This function builds a themed set of links, known as the pager, to each * page of a paginated list of announcements. This is usually placed at the * bottom of each of these paginated pages. * * @return * A string of XHTML used to render the pager. * */ function announcement_pager() { $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)); while ($announcement = db_fetch_object($result)) { $output .= node_view(node_load($announcement->nid), 1); } $output .= theme('pager', NULL, variable_get('default_nodes_main', 10)); return $output; } /** * Implementation of hook_cron(). * * This function allows the announcement module to insert its own actions when * the cron.php script is run – usually by the system cron system. This is useful * when performing periodic asynchronous tasks like, as in this case, checking to * see if any announcements have expired. * * @return * Nothing. * * More detail at http://api.drupal.org/api/HEAD/function/hook_cron * */ function announcement_cron() { // Mark expired announcements as unpublished so that non-administrative // users cannot see them on the site through standard node mechanisms $queryResult = db_query("UPDATE {node} AS n INNER JOIN {announcement} AS a ON n.nid = a.nid " . "SET n.status = 0 WHERE n.type='announcement' AND n.status = 1 AND a.expiration_date < %d", time()); } /** * Implementation of hook_link(). * * This function allows the announcement module to insert its own links * into certain parts of Drupal generated content. In this case add, edit * and delete links are added to Drupal rendered announcements depending * on the access permissions of the current user. * * @param $type * A string identifying the type of link being requested. In this case, * the links placed below an announcement node. * @param $node * The node object currently being requested. * @param $teaser * A boolean indicating if the full announcement is being displayed or * just its teaser. * @return * An array of link objects as created by the l() function. * * More detail at http://api.drupal.org/api/HEAD/function/hook_link * */ function announcement_link($type, $node = NULL, $teaser = FALSE) { global $user; $links = array(); if($type == 'node' && $node->type == 'announcement') { if(node_access('create', 'announcement')) { $links[] = l(t('Add'), "node/add/announcement", array('title' => t('Add a new announcement'))); } if(node_access('update', $node)) { $links[] = l(t('Edit'), "announcements/$node->nid/edit", array('title' => t('Edit Announcement ') . $node->title)); $links[] = l(t('Delete'), "announcements/$node->nid/delete", array('title' => t('Delete Announcement ') . $node->title)); } } return $links; } /** * Implementation of hook_settings(). * * This function provides an administrative interface for controlling * various settings for the announcement module. * * NOTE: This hook is not used in Drupal v5.* * (http://drupal.org/node/64279#hook-settings) * * @return * An array description, in the Drupal Forms API format, of the elements * to render the settings interface. * * More detail at http://api.drupal.org/api/4.7/function/hook_settings * */ function announcement_settings() { $form = array(); $form['announcement_block_max_list_count'] = array('#type' => 'textfield', '#title' => t('Maximum number of block announcements'), '#default_value' => variable_get('announcement_block_max_list_count', 3), '#description' => t('The maximum number of items listed in the announcement block'), '#required' => FALSE, '#weight' => 0 ); $form['announcement_display_classification'] = array('#type' => 'checkbox', '#title' => t('Display additional announcement classification'), '#default_value' => variable_get('announcement_display_classification', 1), '#description' => t('Insert the additional classification in the announcement modules'), '#required' => FALSE, '#weight' => 0 ); return $form; } /** * This function is called to retrieve the form that is displayed when one attempts * to "create" an announcement. */ function announcement_form(&$node) { if ($node->expiration_date == NULL) { $node->expiration_date = time() + (365 * 86400); } if ($node->publish_date == NULL) { $node->publish_date = time(); } $form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => $node->title, '#description' => t('Title of the announcement'), '#required' => TRUE, '#weight' => 1 ); $form['publication'] = array('#type'=> 'fieldset', '#collapsible' => FALSE, '#title' => t('Publication dates'), '#weight' => 5 ); $form['publication']['publish_date'] = array( '#prefix' => '
', '#type' => 'date', '#title' => t('Publication date'), '#default_value' => _announcement_unixtime2drupaldate($node->publish_date) ); $form['publication']['expiration_date'] = array( '#prefix' => '', '#type' => 'date', '#title' => t('Expiration date'), '#default_value' => _announcement_unixtime2drupaldate($node->expiration_date) ); $form['abstract'] = array('#type' => 'textarea', '#title' => t('Abstract'), '#default_value' => $node->abstract, '#rows' => 3, '#description' => t('Short summary of the full announcement'), '#required' => TRUE, '#weight' => 9 ); $form['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => $node->body, '#description' => t('Full content for the announcement which is shown with the abstract on the details page'), '#required' => TRUE, '#weight' => 10 ); return $form; } /** * Only show those announcements that have not expired for the average user. * If they have access to edit, show all announcements */ function announcement_all() { if (user_access('edit announcement')) { $queryResult = db_query("SELECT n.nid FROM node n INNER JOIN announcement a ON n.nid = a.nid " . "WHERE n.type='announcement' ORDER BY n.sticky DESC, A.publish_date DESC"); } else { $queryResult = db_query("SELECT n.nid FROM node n INNER JOIN announcement a ON n.nid = a.nid " . "WHERE n.type='announcement' AND a.expiration_date > %d ORDER BY n.sticky DESC, a.publish_date DESC", date("U")); } $page_content = array(); $page_content[] = "