Joomla! 中文开发

2010-03-12 星期五
  • 登录
  • 注册新帐户
    Registration
    *
    *
    *
    *
    *
    带星号(*)的项目为必填项!
    • 放大字号
    • 默认字号
    • 缩小字号
    •  color
    • cyan color
    • deepblue color
    • green color
    • lime color
    • orange color
    • pink color
    • red color
    首页 Joomla!相关 个人体会 Joomap组件的扩展应用
    Joomap组件的扩展应用
      站点地图(sitemap)是面向搜索引擎的一个重要工具,通常会是一个XML文件,这个文件里罗列出您网站中的哪些网址希望被搜索引擎抓取。在Joomla!中可以使用Joomap组件来完成这项工作。
      默认的情况下,Joomap只适合于Joomla!中已经创建的菜单、Content组件、VirtueMart组件。这样,当我们安装了其它组件的话,它并不能自动的根据组件的内容进行更新。还好,Joomap提供了开发接口以供我们使用。这里,贴出它的一个示例,列出最新文章的一个Plugin:
    1. <?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); ?>
    2. <?php
    3.  
    4. /**
    5. * @author Daniel Grothe
    6. * @package Plugins
    7. */
    8.  
    9. // Register with the Plugin Manager
    10. $tmp = new Joomap_content;
    11. JoomapPlugins::addPlugin( $tmp );
    12.  
    13. /** Handles standard Joomla Content */
    14. class Joomap_content {
    15.  
    16.  function isOfType(&$joomap, &$parent) {
    17.   switch( $parent->type ) {
    18.   case 'content_blog_category':
    19.   case 'content_category':
    20.   case 'content_section':
    21.   case 'content_blog_section':
    22.   case 'content_typed':
    23.    return true;
    24.   }
    25.   return false;
    26.  }
    27.  
    28.  /** return a node-tree */
    29.  function &getTree(&$joomap, &$parent) {
    30.  
    31.   $result = null;
    32.  
    33.   switch( $parent->type ) {
    34.   case 'content_blog_category':
    35.   case 'content_category':
    36.    if( $joomap->config->expand_category ) {
    37.     $params = $this->_paramsToArray( $parent->params );
    38.     $result = $this->getContentCategory($joomap, $parent, $parent->componentid, $params);
    39.    }
    40.    break;
    41.   case 'content_section':
    42.    if( $joomap->config->expand_section ) {
    43.     $params = $this->_paramsToArray( $parent->params );
    44.     $result = $this->getContentSection($joomap, $parent, $parent->componentid, $params);
    45.    }
    46.    break;
    47.   case 'content_blog_section':
    48.    if( $joomap->config->expand_section ) {
    49.     $params = $this->_paramsToArray( $parent->params );
    50.     $result = $this->getContentBlogSection($joomap, $parent, $parent->componentid, $params);
    51.    }
    52.    break;
    53.   case 'content_typed':
    54.    global $database;
    55.    $database->setQuery("SELECT modified, created FROM content WHERE id=". $parent->componentid);
    56.    $database->loadObject( $item );
    57.    if( $item->modified == '0000-00-00 00:00:00' )
    58.     $item->modified = $item->created;
    59.    $parent->modified = $this->_toTimestamp( $item->modified );
    60.    break;
    61.   }
    62.   return $result;
    63.  }
    64.  
    65.  /** Get all content items within a content category.
    66.   * Returns an array of all contained content items. */
    67.  function &getContentCategory(&$joomap, &$parent, $catid, &$params) {
    68.   global $database;
    69.  
    70.   $orderby = isset($params['orderby']) && !empty($params['orderby']) ? $params['orderby'] : 'rdate';
    71.   $orderby = $this->_orderby_sec( $orderby );
    72.  
    73.   $query =
    74.     "SELECT a.id, a.title, a.modified, a.created"
    75.   . "\n FROM content AS a"
    76.   . "\n WHERE a.catid='".$catid."'"
    77.   . "\n AND a.state='1'"
    78.   . "\n AND ( a.publish_up = '0000-00-00 00:00:00' OR a.publish_up <= '". $joomap->now ."' )"
    79.   . "\n AND ( a.publish_down = '0000-00-00 00:00:00' OR a.publish_down >= '". $joomap->now ."' )"
    80.   . ( $joomap->noauth ? '' : "\n AND a.access<='". $joomap->gid ."'" ) // authentication required ?
    81.   . "\n ORDER BY ". $orderby .""
    82.   ;
    83.   $database->setQuery( $query );
    84.   $items = $database->loadObjectList();
    85.  
    86.   $content = array();
    87.  
    88.   foreach($items as $item) {
    89.    $node = new stdclass();
    90.    $node->id = $parent->id;
    91.    $node->browserNav = $parent->browserNav;
    92.    $node->name = $item->title;
    93.    
    94.    if( $item->modified == '0000-00-00 00:00:00' )
    95.     $item->modified = $item->created;
    96.    $node->modified = $this->_toTimestamp( $item->modified );
    97.    
    98.    $node->link = 'index.php?option=com_content&task=view&id='.$item->id;
    99.  
    100.    $content[] = $node;             // add this content item as a node to the list
    101.      }
    102.      return $content;
    103.  }
    104.  
    105.  /** Get all Categories within a Section.
    106.   * Also call getCategory() for each Category to include it's items */
    107.  function &getContentSection(&$joomap, &$parent, $secid, &$params ) {
    108.   global $database;
    109.  
    110.   $orderby = isset($params['orderby']) ? $params['orderby'] : '';
    111.   $orderby = $this->_orderby_sec( $orderby );
    112.  
    113.   $query =
    114.     "SELECT a.id, a.title, a.name, a.params"
    115.   . "\n FROM categories AS a"
    116.   . "\n LEFT JOIN content AS b ON b.catid = a.id "
    117.   . "\n AND b.state = '1'"
    118.   . "\n AND ( b.publish_up = '0000-00-00 00:00:00' OR b.publish_up <= '". $joomap->now ."' )"
    119.   . "\n AND ( b.publish_down = '0000-00-00 00:00:00' OR b.publish_down >= '". $joomap->now ."' )"
    120.   . ( $joomap->noauth ? '' : "\n AND b.access <= ". $joomap->gid )  // authentication required ?
    121.   . "\n WHERE a.section = '". $secid ."'"
    122.   . "\n AND a.published = '1'"
    123.   . ( $joomap->noauth ? '' : "\n AND a.access <= ". $joomap->gid )  // authentication required ?
    124.   . "\n GROUP BY a.id"
    125.   . ( @$params['empty_cat'] ? '' : "\n HAVING COUNT( b.id ) > 0" ) // hide empty categories ?
    126.   . "\n ORDER BY ". $orderby
    127.   ;
    128.   $database->setQuery( $query );
    129.   $items = $database->loadObjectList();
    130.  
    131.   $content = array();
    132.   foreach($items as $item) {
    133.    $node = new stdclass();
    134.    $node->id = $parent->id;
    135.    $node->name = $item->name;
    136.    $node->browserNav = $parent->browserNav;
    137.    $node->link = 'index.php?option=com_content&task=category§ionid='.$secid.'&id='.$item->id;
    138.    if( $joomap->config->expand_category )
    139.     $node->tree = $this->getContentCategory($joomap, $parent, $item->id, $params);
    140.     
    141.    $content[] = $node;
    142.      }
    143.      return $content;
    144.  }
    145.  
    146.  /** Return an array with all Items in a Section */
    147.  function &getContentBlogSection(&$joomap, &$parent, $secid, &$params ) {
    148.   global $database;
    149.  
    150.   $order_pri = isset($params['orderby_pri']) ? $params['orderby_pri'] : '';
    151.   $order_sec = isset($params['orderby_sec']) && !empty($params['orderby_sec']) ? $params['orderby_sec'] : 'rdate';
    152.   $order_pri = $this->_orderby_pri( $order_pri );
    153.   $order_sec = $this->_orderby_sec( $order_sec );
    154.   $where  = $this->_where( 1, $joomap->access, $joomap->noauth, $joomap->gid, $secid, $joomap->now );
    155.  
    156.   $query =
    157.     "SELECT a.id, a.title, a.modified, a.created"
    158.   . "\n FROM content AS a"
    159.   . "\n INNER JOIN categories AS cc ON cc.id = a.catid"
    160.   . "\n LEFT JOIN users AS u ON u.id = a.created_by"
    161.   . "\n LEFT JOIN content_rating AS v ON a.id = v.content_id"
    162.   . "\n LEFT JOIN sections AS s ON a.sectionid = s.id"
    163.   . "\n LEFT JOIN groups AS g ON a.access = g.id"
    164.   . "\n WHERE ". implode( "\n AND ", $where )
    165.   . "\n AND s.access <= ".$joomap->gid
    166.   . "\n AND cc.access <= ".$joomap->gid
    167.   . "\n AND s.published = 1"
    168.   . "\n AND cc.published = 1"
    169.   . "\n ORDER BY $order_pri $order_sec";
    170.  
    171.   $database->setQuery( $query );
    172.   $items = $database->loadObjectList();
    173.  
    174.   $content = array();
    175.   foreach($items as $item) {
    176.    $node = new stdclass();
    177.    $node->id = $parent->id;
    178.    $node->browserNav = $parent->browserNav;
    179.    $node->name = $item->title;
    180.  
    181.    if( $item->modified == '0000-00-00 00:00:00' )
    182.     $item->modified = $item->created;
    183.    $node->modified = $this->_toTimestamp( $item->modified );
    184.    
    185.    $node->link = 'index.php?option=com_content&task=view&id='.$item->id;
    186.  
    187.    $content[] = $node;
    188.      }
    189.      return $content;
    190.  }
    191.  
    192.  /***************************************************/
    193.  /* copied from /components/com_content/content.php */
    194.  /***************************************************/
    195.  
    196.  /** convert a menuitem's params field to an array */
    197.  function _paramsToArray( &$params ) {
    198.   $tmp = explode("\n", $params);
    199.   $res = array();
    200.   foreach($tmp AS $a) {
    201.    @list($key, $val) = explode('=', $a, 2);
    202.    $res[$key] = $val;
    203.   }
    204.   return $res;
    205.  }
    206.  /** Translate Joomla datestring to timestamp */
    207.  function _toTimestamp( &$date ) {
    208.   if ( $date && ereg( "([0-9]{4})-([0-9]{2})-([0-9]{2})[ ]([0-9]{2}):([0-9]{2}):([0-9]{2})", $date, $regs ) ) {
    209.    return mktime( $regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1] );
    210.   }
    211.   return FALSE;
    212.  }
    213.  
    214.  /** translate primary order parameter to sort field */
    215.  function _orderby_pri( $orderby ) {
    216.   switch ( $orderby ) {
    217.    case 'alpha':
    218.     $orderby = 'cc.title, ';
    219.     break;
    220.  
    221.    case 'ralpha':
    222.     $orderby = 'cc.title DESC, ';
    223.     break;
    224.  
    225.    case 'order':
    226.     $orderby = 'cc.ordering, ';
    227.     break;
    228.  
    229.    default:
    230.     $orderby = '';
    231.     break;
    232.   }
    233.  
    234.   return $orderby;
    235.  }
    236.  
    237.  /** translate secondary order parameter to sort field */
    238.  function _orderby_sec( $orderby ) {
    239.   switch ( $orderby ) {
    240.    case 'date':
    241.     $orderby = 'a.created';
    242.     break;
    243.  
    244.    case 'rdate':
    245.     $orderby = 'a.created DESC';
    246.     break;
    247.  
    248.    case 'alpha':
    249.     $orderby = 'a.title';
    250.     break;
    251.  
    252.    case 'ralpha':
    253.     $orderby = 'a.title DESC';
    254.     break;
    255.  
    256.    case 'hits':
    257.     $orderby = 'a.hits';
    258.     break;
    259.  
    260.    case 'rhits':
    261.     $orderby = 'a.hits DESC';
    262.     break;
    263.  
    264.    case 'order':
    265.     $orderby = 'a.ordering';
    266.     break;
    267.  
    268.    case 'author':
    269.     $orderby = 'a.created_by_alias, u.name';
    270.     break;
    271.  
    272.    case 'rauthor':
    273.     $orderby = 'a.created_by_alias DESC, u.name DESC';
    274.     break;
    275.  
    276.    case 'front':
    277.     $orderby = 'f.ordering';
    278.     break;
    279.  
    280.    default:
    281.     $orderby = 'a.ordering';
    282.     break;
    283.   }
    284.  
    285.   return $orderby;
    286.  }
    287.  /** @param int 0 = Archives, 1 = Section, 2 = Category */
    288.  function _where( $type=1, &$access, &$noauth, $gid, $id, $now=NULL, $year=NULL, $month=NULL ) {
    289.   global $database;
    290.  
    291.   $nullDate = $database->getNullDate();
    292.   $where = array();
    293.  
    294.   // normal
    295.   if ( $type > 0) {
    296.    $where[] = "a.state = '1'";
    297.    if ( !$access->canEdit ) {
    298.     $where[] = "( a.publish_up = '$nullDate' OR a.publish_up <= '$now' )";
    299.     $where[] = "( a.publish_down = '$nullDate' OR a.publish_down >= '$now' )";
    300.    }
    301.    if ( $noauth ) {
    302.     $where[] = "a.access <= $gid";
    303.    }
    304.    if ( $id > 0 ) {
    305.     if ( $type == 1 ) {
    306.      $where[] = "a.sectionid IN ( $id ) ";
    307.     } else if ( $type == 2 ) {
    308.      $where[] = "a.catid IN ( $id ) ";
    309.     }
    310.    }
    311.   }
    312.  
    313.   // archive
    314.   if ( $type < 0 ) {
    315.    $where[] = "a.state='-1'";
    316.    if ( $year ) {
    317.     $where[] = "YEAR( a.created ) = '$year'";
    318.    }
    319.    if ( $month ) {
    320.     $where[] = "MONTH( a.created ) = '$month'";
    321.    }
    322.    if ( $noauth ) {
    323.     $where[] = "a.access <= $gid";
    324.    }
    325.    if ( $id > 0 ) {
    326.     if ( $type == -1 ) {
    327.      $where[] = "a.sectionid = $id";
    328.     } else if ( $type == -2) {
    329.      $where[] = "a.catid = $id";
    330.     }
    331.    }
    332.   }
    333.  
    334.   return $where;
    335.  }
    336. }
    337. ?>
    参照这个示例,我们可以这样来做:
    1. <?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); ?>
    2. <?php
    3.  
    4. /**
    5. * @author Daniel Grothe
    6. * @module Joomap
    7. */
    8.  
    9. $tmp = new 插件名称;  //请将插件名称改为您所需要的名字
    10. JoomapPlugins::addPlugin( $tmp );
    11.  
    12. /** Adds support for Phpshop and Virtuemart categories to Joomap */
    13. class 插件名称 {
    14.  
    15.  /** Return true if this plugin handles this content */
    16.  function isOfType( &$joomap, &$parent ) {
    17.   if( $parent->type == 'components') {
    18.    switch( $parent->component ) {
    19.     case '组件名称'//这里的组件名称指的是您所安装在Joomla!中的组件的名称,这所以这样写,是想告诉Joomap,以下所列出的内容是当作该菜单的子项来进行地。呵呵,当然,这里就要求:该组件应该在Joomap所使用的菜单类型里发布了至少一个菜单,而且,类型为“components”。
    20.      return true;
    21.    }
    22.   }
    23.   return false;
    24.  }
    25.  
    26.  /** Get the content tree for this kind of content */
    27.  function &getTree( &$joomap, &$parent ) {
    28.   $tree = $this->getList($joomap, $parent);
    29.   return $tree;
    30.  }
    31.  
    32.  /** Virtuemart support */
    33.  function &getList( &$joomap, &$parent ) {
    34.   global $database;
    35. //定义一个数组,写一些自定义的链接。
    36.   $ax[]=array("link"=>"index.php?option=您的组件&task=您的组件里的事件","name"=>"此项菜单的名称");
    37.   $list=array();
    38.   foreach($ax as $k=>$v){
    39.    unset($node);
    40.    $node->link=$v['link'];
    41.    $node->name=$v['name'];
    42.    $node->id=$parent->id;
    43.    $list[] = $node;
    44.   }
    45.   return $list;
    46.  }
    47. }?>

    怎么样,Joomap扩展起来是不是也很容易啊?

    评论

    姓名 *
    Email (用于验证及回复)
    验证码   
    ChronoComments by Joomla Professional Solutions
    提交评论
    最后更新 ( 2008-05-18 18:52 )