drupal: แปลงโมดูล On This Date เป็น Recent 3 Days
Submitted by wd on Tue, 2007-01-23 14:44
ทดลองแปลงโมดูล On This Date ของ Drupal-5 Modules Tutorial
โมดูลในรุ่น 5 ควรเขียนในไดเรกทอรี่ ./sites/all/modules/
ดังนั้นต้องสร้างไดเรกทอรี่ให้โมดูลเราก่อน โดยใช้ชื่อ onthisdate เหมือนเดิม
$ cd /var/www/drupal
$ mkdir -p sites/all/modules/onthisdate
$ cd sites/all/modules/onthisdate
สำหรับรุ่น 5 ต้องมีไฟล์ info
$ vi onthisdate.info
; $Id$ name = On this date description = "A block module that lists links to content such as blog entries or forum discussions that were created one week ago. (wd's: Modify to recent 3 days post)"
คราวนี้ก็สร้างไฟล์โมดูลจริง ๆ ได้แล้ว
$ vi onthisdate.module
<?php /** * @file: onthisdate.module.5.txt Drupal tutorial on how to write modules * @author: Kitt Hodsden (http://kitt.hodsden.org/) * @license: GPL:v2, CC:by-nc-sa, contact for commercial uses */ /** * Display help and module information * @param section which section of the site we're displaying help * @return help text for section */ function onthisdate_help($section='') { $output = ''; switch ($section) { case "admin/help#onthisdate": $output = '<p>'. t("Displays links to nodes created on this date"). '</p>'; break; } return $output; } // function onthisdate_help /** * menu hook * @return array of menu items */ function onthisdate_menu() { $items = array(); //this was created earlier in tutorial 7. $items[] = array( 'path' => 'admin/settings/onthisdate', 'title' => t('On this date module settings'), 'callback' => 'drupal_get_form', 'callback arguments' => 'onthisdate_admin', 'access' => user_access('access administration pages'), 'type' => MENU_NORMAL_ITEM, ); //this is added for this current tutorial. $items[] = array( 'path' => 'onthisdate', 'title' => t('on this date'), 'callback' => 'onthisdate_all', 'access' => user_access('access content'), 'type' => MENU_CALLBACK ); return $items; } function onthisdate_admin() { $form['onthisdate_maxdisp'] = array( '#type' => 'textfield', '#title' => t('Maximum number of links'), '#default_value' => variable_get('onthisdate_maxdisp', 3), '#size' => 2, '#maxlength' => 2, '#description' => t("The maximum number of links to display in the block.") ); return system_settings_form($form); } /** * Valid permissions for this module * @return An array of valid permissions for the onthisdate module */ function onthisdate_perm() { return array('access onthisdate', 'administer onthisdate'); } // function onthisdate_perm() /** * Generate HTML for the onthisdate block * @param op the operation from the URL * @param delta offset * @returns block HTML */ function onthisdate_block($op='list', $delta=0) { // listing of blocks, such as on the admin/block page if ($op == "list") { $block[0]["info"] = t("On This Date"); return $block; } else if ($op == 'view') { // our block content // content variable that will be returned for display $block_content = ''; // Get today's date $today = getdate(); // calculate midnight one week ago //$start_time = mktime(0, 0, 0,$today['mon'], // ($today['mday'] - 7), $today['year']); //wd's: MODIFY TO LAST 3 DAYS $start_time = mktime(0, 0, 0,$today['mon'], ($today['mday'] - 3) , $today['year']); // we want items that occur only on the day in question, so //calculate 1 day //$end_time = $start_time + 86400; // 60 * 60 * 24 = 86400 seconds in a day //wd's EXPAND TO 3 DAYS $end_time = $start_time + 259200; $limitnum = variable_get("onthisdate_maxdisp", 3); $query = "SELECT nid, title, created FROM " . "{node} WHERE created >= %d " . "AND created <= %d ORDER BY created DESC"; // get the links $queryResult = db_query_range($query, $start_time, $end_time, 0, $limitnum); while ($links = db_fetch_object($queryResult)) { //wd's: MODIFY TO LIST //$block_content .= l($links->title, 'node/'.$links->nid) . '<br />'; $block_content .= '<li>'.l($links->title, 'node/'.$links->nid) . '</li>'; } // check to see if there was any content before setting up the block if ($block_content == '') { // no content from a week ago, return nothing. return; } else { //wd's: ADD <ul> $block_content = '<ul>'.$block_content.'</ul>'; } // add a more link to our page that displays all the links $block_content .= "<div class=\"more-link\">". l( t("more"), "onthisdate", array( "title" => t("More events on this day.")))."</div>"; // set up the block //$block['subject'] = 'On This Date'; //wd's EXPAND TO 3 DAYS $block['subject'] = 'These 3 Days'; $block['content'] = $block_content; return $block; } } // end onthisdate_block /** * Settings for the onthisdate module * @return form contents for this module */ function onthisdate_settings() { // only administrators can access this function if (!user_access('access administration pages')) { return message_access(); } // 4.7 forms API $form['onthisdate_maxdisp'] = array('#type' => 'textfield', '#title' => t('Maximum number of links'), '#default_value' => variable_get('onthisdate_maxdisp', 3), '#description' => t("The maximum number of links to display in the block."), '#maxlength' => '2', '#size' => '2'); return $form; } /** * Render a page listing links to all the content from a week ago */ function onthisdate_all() { // content variable that will be returned for display $page_content = ''; // Get today's date $today = getdate(); // calculate midnight one week ago //$start_time = mktime(0, 0, 0, $today['mon'], ($today['mday'] - 7), $today['year']); //wd's: CHANGE TO LAST 3 DAYS $start_time = mktime(0, 0, 0, $today['mon'], ($today['mday'] - 3), $today['year']); // we want items that occur only on the day in question, // so calculate 1 day //$end_time = $start_time + 86400; // 60 * 60 * 24 = 86400 seconds in a day //wd's EXPAND TO 3 DAYS $end_time = $start_time + 259200; $query = "SELECT nid, title, created FROM " . "{node} WHERE created >= '" . $start_time . "' AND created <= '". $end_time . " ORDER BY created DESC'"; // get the links (no range limit here) $queryResult = db_query($query); while ($links = db_fetch_object($queryResult)) { //$page_content .= l($links->title, 'node/'.$links->nid).'<br />'; //wd's: ADD LIST $page_content .= '<li>'.l($links->title, 'node/'.$links->nid).'</li>'; } // check to see if there was any content before // setting up the block if ($page_content == '') { // no content from a week ago, let the user know $page_content = "No events occurred on this site on this date in history."; } else { //wd's: ADD <ul> $page_content = '<ul>'.$page_content.'</ul>'; } print theme("page", $page_content); } ?>
เสร็จแล้ว
ต่อไปก็ทำตามขั้นตอนคือ
- ให้โมดูลทำงาน
admin/build/module -> enable onthisdate
- ให้ผู้ใช้สามารถใช้งานโมดูลได้
admin/user/access -> เปิดให้ทำงานตาม roles
- เปิดให้ใช้งานบล๊อก
admin/build/block -> enable onthisdate
- Printer-friendly version
- Log in or register to post comments
- 6171 reads
Recent comments