programming4:module:rss
Mục lục
Hướng dẫn chức năng RSS của module
Tổng quan
Một module có chức năng RSS cần bổ sung đủ hai file
- modules/module-name/rssdata.php
- modules/module-name/funcs/rss.php
Trong đó:
rssdata.php
là file tạo danh mục (chủ đề) rss cho module feeds của hệ thốngrss.php
là file xử lý rss chính của module.
Cách viết file rssdata.php
File này cần trả về một mảng có dạng
$rssarray[] = array('catid' => 0, 'parentid' => 0, 'title' => '', 'link' => '');
Trong đó:
catid
là ID của chuyên mục, chủ đề, nhóm …parentid
là ID của chuyên mục, chủ đề, nhóm … cha (Nếu có đa cấp)title
là tiêu đềlink
là đường dẫn tới phần xử lý RSS của module (funcs rss)
Ví dụ về file ''rssdata.php'' không có đa cấp
- rssdata.php
<?php /** * @Project EVENTS 4.X * @Author PHAN TAN DUNG (phantandung92@gmail.com) * @Copyright (C) 2016 PHAN TAN DUNG. All rights reserved * @License GNU/GPL version 2 or any later version * @Createdate Sun, 12 Jun 2016 05:02:54 GMT */ if (!defined('NV_IS_MOD_RSS')) die('Stop!!!'); $rssarray = array(); $sql = "SELECT catid, title, alias FROM " . NV_PREFIXLANG . "_" . $mod_data . "_cat ORDER BY weight ASC"; //$rssarray[] = array('catid' => 0, 'parentid' => 0, 'title' => '', 'link' => ''); $list = $nv_Cache->db($sql, '', $mod_name); foreach ($list as $value) { $value['parentid'] = 0; $value['link'] = NV_BASE_SITEURL . "index.php?" . NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&" . NV_NAME_VARIABLE . "=" . $mod_name . "&" . NV_OP_VARIABLE . "=" . $mod_info['alias']['rss'] . "/" . $value['alias']; $rssarray[] = $value; }
Ví dụ về file ''rssdata.php'' đa cấp
- rssdata.php
<?php /** * @Project NUKEVIET 4.x * @Author VINADES (contact@vinades.vn) * @Copyright (C) 2014 VINADES. All rights reserved * @License GNU/GPL version 2 or any later version * @Createdate Apr 20, 2010 10:47:41 AM */ if (! defined('NV_IS_MOD_RSS')) { die('Stop!!!'); } $rssarray = array(); $sql = "SELECT catid, parentid, title, alias FROM " . NV_PREFIXLANG . "_" . $mod_data . "_cat ORDER BY weight, sort"; //$rssarray[] = array('catid' => 0, 'parentid' => 0, 'title' => '', 'link' => ''); $list = $nv_Cache->db($sql, '', $mod_name); foreach ($list as $value) { $value['link'] = NV_BASE_SITEURL . "index.php?" . NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&" . NV_NAME_VARIABLE . "=" . $mod_name . "&" . NV_OP_VARIABLE . "=" . $mod_info['alias']['rss'] . "/" . $value['alias']; $rssarray[] = $value; }
Trong ví dụ trên giá trịparentid
do sự quyết định của CSDL.
Chú ý
Nếu module có chức năng RSS và không phân theo chủ đề hay chuyên mục gì thì vẫn phải có file rssdata.php như sau
- rssdata.php
<?php /** * @Project NUKEVIET 4.x * @Author VINADES (contact@vinades.vn) * @Copyright (C) 2014 VINADES. All rights reserved * @License GNU/GPL version 2 or any later version * @Createdate Apr 20, 2010 10:47:41 AM */ if (! defined('NV_IS_MOD_RSS')) { die('Stop!!!'); } $rssarray = array();
Cách viết file rss.php
File rss.php cần đưa được dữ liệu hợp chuẩn vào hai mảng
$channel = array(); $items = array();
Mảng $channel
có dạng
$channel['title'] = $module_info['custom_title']; $channel['link'] = NV_MY_DOMAIN . NV_BASE_SITEURL . 'index.php?' . NV_LANG_VARIABLE . '=' . NV_LANG_DATA . '&' . NV_NAME_VARIABLE . '=' . $module_name; $channel['description'] = !empty($module_info['description']) ? $module_info['description'] : $global_config['site_description'];
Trong đó:
title
là tiêu đề nguồn cấp tinlink
là đường dẫn đến nguồn cấp tindescription
là mô tả nguồn cấp tin
Mảng $items
có dạng
$items[] = array( 'title' => $title, 'link' => NV_MY_DOMAIN . NV_BASE_SITEURL . 'index.php?' . NV_LANG_VARIABLE . '=' . NV_LANG_DATA . '&' . NV_NAME_VARIABLE . '=' . $module_name . '&' . NV_OP_VARIABLE . '=' . $alias . $global_config['rewrite_exturl'], // 'guid' => $module_name . '_' . $id, 'description' => $time . (!empty($location) ? '. ' . $location : ''), 'pubdate' => $publtime );
Trong đó:
title
là tiêu đề bài viếtlink
là đường dẫn đến bài viếtguid
là khóa của tin (không quan trọng)description
mô tả tin (có thể sử dụng html vào đây)pubdate
là thời gian xuất bản tin (kiểu số nguyên)
Ví dụ
- rss.php
<?php /** * @Project EVENTS 4.X * @Author PHAN TAN DUNG (phantandung92@gmail.com) * @Copyright (C) 2016 PHAN TAN DUNG. All rights reserved * @License GNU/GPL version 2 or any later version * @Createdate Sun, 12 Jun 2016 05:02:54 GMT */ if (!defined('NV_MOD_EVENTS')) die('Stop!!!'); $channel = array(); $items = array(); $channel['title'] = $module_info['custom_title']; $channel['link'] = NV_MY_DOMAIN . NV_BASE_SITEURL . 'index.php?' . NV_LANG_VARIABLE . '=' . NV_LANG_DATA . '&' . NV_NAME_VARIABLE . '=' . $module_name; $channel['description'] = !empty($module_info['description']) ? $module_info['description'] : $global_config['site_description']; $db->sqlreset()->select('id, time_start, time_end, title, alias, location, addtime')->order('id DESC')->limit(30); $where = 'status = 1'; if (isset($array_op[1])) { $_catid = 0; foreach ($global_array_cat as $cat) { if ($cat['alias'] == $array_op[1]) { $_catid = $cat['catid']; break; } } if (!empty($_catid)) { $where .= ' AND catids LIKE \'%,' . $_catid . ',%\''; } else { header('location:' . nv_url_rewrite(NV_BASE_SITEURL . 'index.php?' . NV_LANG_VARIABLE . '=' . NV_LANG_DATA . '&' . NV_NAME_VARIABLE . '=' . $module_name . '&' . NV_OP_VARIABLE . '=' . $op, true)); die(); } } $db->from(NV_PREFIXLANG . '_' . $module_data . '_rows')->where($where); if ($module_info['rss']) { $result = $db->query($db->sql()); while (list($id, $time_start, $time_end, $title, $alias, $location, $publtime) = $result->fetch(3)) { $h = nv_date('H', $time_start); $m = nv_date('i', $time_start); $d = nv_date('d/m/Y', $time_start); $time = ($h != '00' ? $h . ':' . $m . ' ' : '') . $d; if (!empty($time_end)) { $h = nv_date('H', $time_end); $m = nv_date('i', $time_end); $d = nv_date('d/m/Y', $time_end); $time .= ' - ' . ($h != '00' ? $h . ':' . $m . ' ' : '') . $d; } $items[] = array( 'title' => $title, 'link' => NV_MY_DOMAIN . NV_BASE_SITEURL . 'index.php?' . NV_LANG_VARIABLE . '=' . NV_LANG_DATA . '&' . NV_NAME_VARIABLE . '=' . $module_name . '&' . NV_OP_VARIABLE . '=' . $alias . $global_config['rewrite_exturl'], // 'guid' => $module_name . '_' . $id, 'description' => $time . (!empty($location) ? '. ' . $location : ''), 'pubdate' => $publtime ); } } nv_rss_generate($channel, $items); die();
Chú ý
Các đường dẫn trong RSS đều phải là đường dẫn tuyệt đối.
programming4/module/rss.txt · Sửa đổi lần cuối: 2016/06/21 08:48 bởi hoaquynhtim99