drupal6: แปลงมอดูล onthisdate เป็น recentweek

จะแปลงมอดูลจากตัวอย่าง คือ On This Date ซึ่งดู "วันนี้ในอาทิตย์ก่อน" มาเป็น Recent Week คือดูหัวข้อใหม่ในสัปดาห์นี้ (เหมือนกับ tracker แต่ทำเป็นบล๊อกได้)

เริ่มเลย
$ cd /var/www/drupal/sites/all/modules
$ cp -xa onthisdate recentweek
$ cd recentweek
$ mv onthisdate.info recentweek.info
$ mv onthisdate.module recentweek.module
$ sed -i "s/onthisdate/recentweek/g" *
$ sed -i "s/on this date/recent week/g" *
$ sed -i "s/On this date/Recent week/g" *
$ sed -i "s/On This Date/Recent Week/g" *
$ vi recentweek.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 recentweek_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#recentweek":
      $output = '<p>'.  t("Displays links to nodes created recent week") .'</p>';
      break;
  }
  return $output;
} // function recentweek_help

/**
* Valid permissions for this module
* @return array An array of valid permissions for the recentweek module
*/
function recentweek_perm() {
  return array('access recentweek content', 'administer recentweek');
} // function recentweek_perm

/**
* Generate HTML for the recentweek block
* @param op the operation from the URL
* @param delta offset
* @returns block HTML
*/
function recentweek_block($op='list', $delta=0) {
  // listing of blocks, such as on the admin/block page
  if ($op == "list") {
    $block[0]["info"] = t('Recent Week');
    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']);

/* wd's mod
    // 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#// delete parameter $end_time, $query changed
  }

  //We'll use db_query() to get the records (i.e. the database rows) with our SQL query
  $limitnum = variable_get("recentweek_maxdisp", 3);

/* wd's mod
  $query = "SELECT nid, title, created FROM " .
           "{node} WHERE created >= %d " .
           "AND created <= %d";
  $queryResult = db_query_range($query, $start_time, $end_time, $limitnum);
*/
  #wd#// delete parameter $end_time, $query changed
  $query = "SELECT nid, title, created FROM " .
           "{node} WHERE created >= %d ORDER BY created DESC" ;
  $queryResult = db_query_range($query, $start_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"),
      "recentweek",
      array(
        "title" => t("More events on this day.")
      )
    )."</div>";
  
  // set up the block
  $block['subject'] = 'Recent Week'; 
  $block['content'] = $block_content;
  return $block;
} // end recentweek_block

function recentweek_admin() {
  $form['recentweek_maxdisp'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum number of links'),
    '#default_value' => variable_get('recentweek_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 recentweek_menu() {
  $items = array();

  //this was created earlier in tutorial 7.
  $items['admin/settings/recentweek'] = array(
    'title' => 'Recent week module settings',
    'description' => 'Description of your Recent week settings control',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('recentweek_admin'),
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
   );

  //this is added in tutorial 9.
  $items['recentweek'] = array(
    'title' => 'Recent week',
    'page callback' => 'recentweek_all',
    'access arguments' => array('access recentweek content'),
    'type' => MENU_CALLBACK,
  );
  
  return $items;
}   
    
function recentweek_admin_validate($form, &$form_state) {
  $maxdisp = $form_state['values']['recentweek_maxdisp'];
  if (!is_numeric($maxdisp)) {
    form_set_error('recentweek_maxdisp', t('You must select a number for the maximum number of links.'));
  }
  else if ($maxdisp <= 0) {
    form_set_error('recentweek_maxdisp', t('Maximum number of links must be positive.'));
  }
} 

function recentweek_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
  // 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#// delete parameter $end_time, $query changed
  $query = "SELECT nid, title, created FROM " .
           "{node} WHERE created >= '%d' " .
           " AND created <= '%d'";
  $queryResult =  db_query($query, $start_time, $end_time);
*/

  $query = "SELECT nid, title, created FROM " .
           "{node} WHERE created >= '%d' ORDER BY created DESC";

  // get the links (no range limit here)
  $queryResult =  db_query($query, $start_time);
  $page_content = '<ul>';
   while ($links = db_fetch_object($queryResult)) {
    $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 == '<ul>') {
    // no content from a week ago, let the user know
    $page_content = "No events occurred on this site recent week in history.";
  } else $page_content .= '</ul>';
  return $page_content;
}
?>

เสร็จแล้ว

เพื่อความสุขสวัสดี อย่าลืมรัน update.php ด้วย ไม่งั้นอาจมีปัญหาเรื่อง HTTP request failed

Topic: