Joomla模块学习之后台mod_latest模块
显示最新文章
后台管理模块效果图
原理:
后台mod_latest模块通过在后台配置相应的信息.
如排序方式,采集文章作者等等,返回最新文章列表
配置界面:
mod_feed模块结构图:
关键代码
(mod_latest.php代码) // Get the user object for the logged in user // 获得当前用户的对象实例 $db =& JFactory::getDBO(); $user =& JFactory::getUser(); $userId = (int) $user->get('id'); $where = 'WHERE a.state <> -2'; // User Filter // 文章作者过滤参 switch ($params->get( 'user_id' )) { case 'by_me': $where .= ' AND (created_by = ' . (int) $userId . ' OR modified_by = ' . (int) $userId . ')'; break; case 'not_me': $where .= ' AND (created_by <> ' . (int) $userId . ' AND modified_by <> ' . (int) $userId . ')'; break; } // Ordering // 排序 switch ($params->get( 'ordering' )) { case 'm_dsc': $ordering = 'modified DESC, created DESC'; $dateProperty = 'modified'; break; case 'c_dsc': default: $ordering = 'created DESC'; $dateProperty = 'created'; break; } $query = 'SELECT a.id, a.sectionid, a.title, a.created, a.modified, u.name, a.created_by_alias, a.created_by' . ' FROM #__content AS a' . ' LEFT JOIN #__users AS u ON u.id = a.created_by' . ' '. $where . ' ORDER BY '. $ordering ; $db->setQuery( $query, 0, 10 ); $rows = $db->loadObjectList();
if (count( $rows )) { foreach ($rows as $row) { $link = 'index.php?option=com_content&task=edit&id='. $row->id; if ( $user->authorize( 'administration', 'manage', 'components', 'com_users' ) ) { if ( $row->created_by_alias ) { $author = $row->created_by_alias; } else { $linkA = 'index.php?option=com_users&task=edit&cid[]='. $row->created_by; $author = '<a href="'. $linkA .'" mce_href="'. $linkA .'" title="'. JText::_( 'Edit User' ) .'">'. htmlspecialchars( $row->name, ENT_QUOTES, 'UTF-8' ) .'</a>'; } } else { if ( $row->created_by_alias ) { $author = $row->created_by_alias; } else { $author = htmlspecialchars( $row->name, ENT_QUOTES, 'UTF-8' ); } } ?> <tr> <td> <a href="<?php echo $link; ?>"> <?php echo htmlspecialchars($row->title, ENT_QUOTES, 'UTF-8');?></a> </td> <td> <?php echo JHTML::_('date', $row->$dateProperty, '%Y-%m-%d %H:%M:%S'); ?> </td> <td> <?php echo $author;?> </td> </tr> <?php } }