Đăng ký học joomla

Tin tức mới nhất

Thống kê truy cập

Truy cập trong ngày:255
Truy cập hôm qua:473
Tổng số truy cập:536452
Đang online: 3 Khách 

Module cần có 2 file là :

- .info

- .module

Vậy đầu tiên, ta cần tạo file info. Tôi viết ứng dụng get file XML từ trang

http://www.goodreads.com/review/list_rss/398385?shelf=history-of-philosophy.

Và đưa về module trang web của mình. Đầu tiên tạo thư mục goodreads .

Trong đó tạo 2 file goodreads.info và goodreads.module . 2 File có nội dung như sau :

File goodreads.info có nội dung như sau :

;$Id$
name = "Goodreads Bookshelf"
description = "Displays items from a Goodreads Bookshelf"
core = 6.x

php = 5.1


name và description chỉ đơn giản là mô tả môdule .
File con chính : goodreads.module có nội dung :

<?php
// $Id$
/**
 * @file
 * Module for fetching data from Goodreads.com.
 * This module provides block content retrieved from a
 * Goodreads.com bookshelf.
 * @see http://www.goodreads.com
 */
 /**
 * Implementation of hook_block()
 */
function goodreads_block($op='list' , $delta=0, $edit=array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Goodreads Bookshelf');
      return $blocks;
    case 'view':
      $url = 'http://www.goodreads.com/review/list_rss/'
            .'398385'
            .'?shelf='
            .'history-of-philosophy';
      $blocks['subject'] = t('On the Bookshelf');
      $blocks['content'] = _goodreads_fetch_bookshelf($url);
      return $blocks;
  }
}
/**
 * Retrieve information from the Goodreads bookshelp XML API.
 * 
 * This makes an HTTP connection to the given URL, and 
 * retrieves XML data, which it then attempts to format 
 * for display.
 *
 * @param $url
 * URL to the goodreads bookshelf.
 * @param $num_items
 * Number of items to include in results.
 * @return 
 * String containing the bookshelf.
 */
function _goodreads_fetch_bookshelf($url, $num_items=3) {
  $http_result = drupal_http_request($url);
    if ($http_result->code == 200) {
    $doc = simplexml_load_string($http_result->data);
    if ($doc === false) {
      $msg = "Error parsing bookshelf XML for %url: %msg.";
      $vars = array('%url'=>$url, '%msg'=>$e->getMessage());
      watchdog('goodreads', $msg, $vars, WATCHDOG_WARNING);
      return t("Getting the bookshelf resulted in an error.");
    }     
    return _goodreads_block_content($doc, $num_items);
    // Otherwise we don't have any data
  } 
  else {
    $msg = 'No content from %url.';
    $vars = array('%url' => $url);
    watchdog('goodreads', $msg, $vars, WATCHDOG_WARNING);
    return t("The bookshelf is not accessible.");
  }
}
/**
 * Generate the contents of a block from a SimpleXML object.
 * Given a SimpleXML object and the maximum number of 
 * entries to be displayed, generate some content.
 *
 * @param $doc
 * SimpleXML object containing Goodreads XML.
 * @param $num_items
 * Number of items to format for display.
 * @return
 * Formatted string. 
 */
function _goodreads_block_content($doc, $num_items=3) {   
  $items = $doc->channel->item;
  $count_items = count($items);
  $len = ($count_items < $num_items) ? $count_items : $num_items;   
  
  $template = '<div>'
            .'<img src="/%s"/>
%s
by %s</div>';
  // Default image: 'no cover'
  $default_img = 'http://www.goodreads.com/images/nocover-60x80.jpg';
  $default_link = 'http://www.goodreads.com';   
  $out = '';  
  foreach ($items as $item) {  
      $author = check_plain($item->author_name);
    $title = strip_tags($item->title);
    $link = check_url(trim($item->link));
    $img = check_url(trim($item->book_image_url));     
    if (empty($author)) $author = '';
    if (empty($title)) $title = '';
    if (empty($link) !== 0) $link = $default_link;
    if (empty($img)) $img = $default_img;     
    $book_link = l($title, $link);
    $out .= sprintf($template, $img, $book_link, $author);
  }
  $out .= '
<div>'
         . l('Goodreads.com', 'http://www.goodreads.com')
         .'</div>';
  return $out;
}

Giải thích :1 : Code trên không cần dấu tag đóng ?> vì khi có nó thì nếu kết quả

trả về có khoản trắng sẽ không tốt cho website lắm .
2 : Ta cần chú ý biến $op . $op là biến quan trọng của drupal, nó có các

tham số quan trọng là :
+ Configure .
+ List .
+ View ...
Đầu tiên :

case 'list':
      $blocks[0]['info'] = t('Goodreads Bookshelf');
      return $blocks;


Nếu $op là list thì $blocks[0]['info'] có giá trị là "Goodreads Bookshelf" .

Và giá trị này được dùng khi cài module .
Sau đó :

case 'view':
      $url = 'http://www.goodreads.com/review/list_rss/'
            .'398385'
            .'?shelf='
            .'history-of-philosophy';
      $blocks['subject'] = t('On the Bookshelf');
      $blocks['content'] = _goodreads_fetch_bookshelf($url);
      return $blocks;


Nếu $op là view thì sẽ gọi
$blocks['subject'] : là tựa đề module chính.
$blocks['content'] : là nội dung hiện ra .
Sau đó module sẽ gọi function _goodreads_fetch_bookshelf để xử lý .

Hàm watchdog():
Hàm thường có dạng như sau : watchdog('goodreads', $msg, $vars, WATCHDOG_WARNING);
- Tham số thứ 1 : là tên module .
- Thứ 2 và 3 : chỉ đơn giản là truyền biến var vào trong biến $msg .
- Tham số thứ 4 chỉ độ quan trọng :

WATCHDOG_EMERG: The system is now in an unusable state.
WATCHDOG_ALERT: Something must be done immediately.
WATCHDOG_CRITICAL: The application is in a critical state.
WATCHDOG_ERROR: An error occurred.
WATCHDOG_WARNING: Something unexpected (and negative) 
happened, but didn't cause any serious problems. WATCHDOG_NOTICE: Something significant (but not bad) happened. WATCHDOG_INFO: Information can be logged. WATCHDOG_DEBUG: Debugging information can be logged.



Hook_Help trong module :

/**
 * Implementation of hook_help()
 */
function goodreads_help($path, $arg) {   
  if ($path == 'admin/help#goodreads' || $path == 'admin/help') {
    $txt = 'The Goodreads module uses the !goodreads_url XML '
      .'API to retrieve a list of books and display it as block '
      .'content.';
    $link = l('Goodreads.com', 'http://www.goodreads.com');
    $replace = array(
      '!goodreads_url' => $link
    );
    return '<p>'. t($txt, $replace) .'</p>';
  }
}


Chỉ đơn thuần là file help, hướng dẫn người dùng sử dụng module .

 

Add comment


Security code
Refresh

thiet ke web chuan w3c

Dịch vụ seo website

seo website