您的位置:首页 > Web前端

Joomla模块学习之后台mod_feed模块

2011-02-26 09:50 489 查看
Joomla模块学习之后台mod_feed模块

后台管理模块效果图




原理:

后台mod_feed模块通过在后台配置相应的信息.

如rssurl链接,rss结果数量,缓存时间等等,聚合相应的信息,达到用户配置的要求





配置界面:










mod_feed模块结构图:








关键代码

(modFeedHelper代码)
        $rssurl				= $params->get('rssurl', '');  //RSS/RDF URL
		$rssitems			= $params->get('rssitems', 5);  //RSS显示数量
		$rssdesc		    = $params->get('rssdesc', 1);  //RSS描述
		$rssimage		    = $params->get('rssimage', 1);  //RSS图片
		$rssitemdesc		= $params->get('rssitemdesc', 1);//条目描述
		$words				= $params->def('word_count', 0);  //限制字符长度
		$rsstitle			= $params->get('rsstitle', 1);  //RSS标题
		$rssrtl			    = $params->get('rssrtl', 0);  //从右到左显示来源
		$moduleclass_sfx	= $params->get('moduleclass_sfx', '');
		//  get RSS parsed object
		//  缓存时间设置
		$options = array();
		$options['rssUrl'] 		= $rssurl;
		
		// 判断是否缓存
		if ($params->get('cache')) {
			$options['cache_time']  = $params->get('cache_time', 15) ;
			$options['cache_time']	*= 60;
		} else {
			$options['cache_time'] = null;
		}
		// Rss聚合
		$rssDoc =& JFactory::getXMLparser('RSS', $options);


(JFactory代码)
function &getXMLParser( $type = 'DOM', $options = array())
	 {
		$doc = null;

		switch (strtolower( $type ))
		{
			case 'rss' :
			case 'atom' :
			{
				if (!is_null( $options['rssUrl'] ))
				{
					jimport ('simplepie.simplepie');
					if(!is_writable(JPATH_BASE.DS.'cache')) {
						$options['cache_time'] = 0;
					}
					//真正聚合需要传递参数为rssUrl,缓存目录,缓存时间
					$simplepie = new SimplePie(
						$options['rssUrl'],
						JPATH_BASE.DS.'cache',
						isset( $options['cache_time'] ) ? $options['cache_time'] : 0
					);
					$simplepie->force_feed(true);
					$simplepie->handle_content_type();
					if ($simplepie->init()) {
						$doc = $simplepie;
					} else {
						JError::raiseWarning( 'SOME_ERROR_CODE', JText::_('ERROR LOADING FEED DATA') );
					}
				}
			}	break;
    ...
    return $doc;
    }






根据后台mod_feed设置,处理聚合返回结果



if ($rssDoc != false)
		{
			// channel header and link
			// 通道标题和链接
			$channel['title'] = $rssDoc->get_title();
			$channel['link'] = $rssDoc->get_link();
			$channel['description'] = $rssDoc->get_description();

			// channel image if exists
			// 通道图片
			$image['url'] = $rssDoc->get_image_url();
			$image['title'] = $rssDoc->get_image_title();

			//image handling
			$iUrl 	= isset($image['url']) ? $image['url'] : null;
			$iTitle = isset($image['title']) ? $image['title'] : null;

			// items
			// 聚合内容列表
			$items = $rssDoc->get_items();

			// feed elements
			// 取出用户指定数量的items
			$items = array_slice($items, 0, $rssitems);
			?>
			<table cellpadding="0" cellspacing="0" class="moduletable<?php echo $params->get('moduleclass_sfx'); ?>">
			<?php
			// feed description
			if (!is_null( $channel['title'] ) && $rsstitle) {
			?>
				<tr>
				<td>
					<strong>
						<a href="<?php echo str_replace( '&', '&', $channel['link']); ?>" target="_blank">
						<?php echo $channel['title']; ?>
						</a>
					</strong>
				</td>
				</tr>
			<?php
			}

			// feed description
			if ($rssdesc) {
			?>
				<tr>
					<td>
						<?php echo $channel['description']; ?>
					</td>
				</tr>
			<?php
			}

			// feed image
			if ($rssimage && $iUrl) {
			?>
				<tr>
					<td align="center">
						<img src="<?php echo $iUrl; ?>" alt="<?php echo @$iTitle; ?>"/>
					</td>
				</tr>
			<?php
			}

			$actualItems = count( $items );
			$setItems = $rssitems;

			if ($setItems > $actualItems) {
				$totalItems = $actualItems;
			} else {
				$totalItems = $setItems;
			}
			?>
			<tr>
			<td>
				<ul class="newsfeed<?php echo $moduleclass_sfx; ?>"  >
				<?php
				for ($j = 0; $j < $totalItems; $j ++)
				{
					$currItem = & $items[$j];
					// item title
					?>
					<li>
					<?php
					if ( !is_null( $currItem->get_link() ) ) {
					?>
						<a href="<?php echo $currItem->get_link(); ?>" target="_child">
						<?php echo $currItem->get_title(); ?></a>
					<?php
					}

					// item description
					if ($rssitemdesc)
					{
						// item description
						$text = html_entity_decode($currItem->get_description());
						$text = str_replace(''', "'", $text);

						// word limit check
						// 返回指定字节长度
						if ($words) {
							$texts = explode(' ', $text);
							$count = count($texts);
							if ($count > $words) {
								$text = '';
								for ($i = 0; $i < $words; $i ++)
								{
									$text .= ' '.$texts[$i];
								}
								$text .= '...';
							}
						}
						?>
						<div style="text-align: <?php echo $rssrtl ? 'right': 'left'; ?> ! important">
							<?php echo $text; ?>
						</div>
						<?php
					}
					?>
					</li>
					<?php
				}
				?>
				</ul>
			</td>
			</tr>
		</table>
		<?php
		}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: