11. จับมารวมกัน
Submitted by wd on Wed, 2008-01-16 12:19
ได้ดังนี้
$ cd /var/www/drupal
$ mkdir -p sites/all/module/onthisdate
ไฟล์ 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. core = 6.x
ไฟล์ module
$ vi onthisdate.module
<?php /** * Display help and module information * @param path which path of the site we're displaying help * @param arg array that holds the current path as would be returned from arg() function * @return help text for the path */ function onthisdate_help($path, $arg) { $output = ''; switch ($path) { case "admin/help#onthisdate": $output = '<p>'. t("Displays links to nodes created on this date") .'</p>'; break; } return $output; } // function onthisdate_help /** * Valid permissions for this module * @return array An array of valid permissions for the onthisdate module */ function onthisdate_perm() { return array('access onthisdate content', '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 // Get today's date $today = getdate(); // calculate midnight one week ago $start_time = mktime(0, 0, 0, $today['mon'], ($today['mday'] - 7), $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 } //We'll use db_query() to get the records (i.e. the database rows) with our SQL query $limitnum = variable_get("onthisdate_maxdisp", 3); $query = "SELECT nid, title, created FROM " . "{node} WHERE created >= %d " . "AND created <= %d"; $queryResult = db_query_range($query, $start_time, $end_time, 0, $limitnum); // content variable that will be returned for display $block_content = '<ul>'; while ($links = db_fetch_object($queryResult)) { $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 == '<ul>') { /* No content from a week ago. If we return nothing, the block * doesn't show, which is what we want. */ return; } $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'; $block['content'] = $block_content; return $block; } // end onthisdate_block 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."), '#required' => TRUE, ); return system_settings_form($form); } function onthisdate_menu() { $items = array(); //this was created earlier in tutorial 7. $items['admin/settings/onthisdate'] = array( 'title' => 'On this date module settings', 'description' => 'Description of your On this date settings control', 'page callback' => 'drupal_get_form', 'page arguments' => array('onthisdate_admin'), 'access arguments' => array('access administration pages'), 'type' => MENU_NORMAL_ITEM, ); //this is added in tutorial 9. $items['onthisdate'] = array( 'title' => 'On this date', 'page callback' => 'onthisdate_all', 'access arguments' => array('access onthisdate content'), 'type' => MENU_CALLBACK, ); return $items; } function onthisdate_admin_validate($form, &$form_state) { $maxdisp = $form_state['values']['onthisdate_maxdisp']; if (!is_numeric($maxdisp)) { form_set_error('onthisdate_maxdisp', t('You must select a number for the maximum number of links.')); } else if ($maxdisp <= 0) { form_set_error('onthisdate_maxdisp', t('Maximum number of links must be positive.')); } } 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']); // 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 $query = "SELECT nid, title, created FROM " . "{node} WHERE created >= '%d' " . " AND created <= '%d'"; // get the links (no range limit here) $queryResult = db_query($query, $start_time, $end_time); while ($links = db_fetch_object($queryResult)) { $page_content .= l($links->title, 'node/'.$links->nid).'<br />'; } // 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."; } return $page_content; } ?>
ติดตั้ง
ติดอยู่แล้ว
เปิดใช้
- URL
admin/build/module
กาถูกหน้า onthisdate - URL
admin/user/permissions
เลือกกาถูกข้ออนุญาตที่เกี่ยวข้องกับ onthisdate
ตั้งค่า
- URL
admin/settings/onthisdate
ตั้งจำนวนหัวข้อ - ทำเป็นบล๊อกผ่าน URL
admin/build/block
เลือกกาถูก On this date
ทดสอบ
ดูที่บล๊อก On this date ตามต้องการ
- Printer-friendly version
- Log in or register to post comments
- 3903 reads
Recent comments