Article模块功能相当强大,在它的基础上增加需要的字段,就可以快速实现很多特定的应用。
本例是一个培训信息站点,每一个培训有开始日期和结束日期,以及培训地点,其余字段与article相同。 采用article模块,开始日期和结束日期显然与文章的提交日期有别,不可以用原有的日期字段。增加以上三个字段就可以实现。
article的主要流程:
* 添加/编辑文章 edit.article.php 无模板,引用表单
* 保存 action.article.php 无模板
* 查看 view.article.php article_article.html
* 分类 view_category.php article_category.html
* 区块 block_news.php article_block_news.html
* block_feature.php article_block_feature.html
article的结构:
* 数据库定义:mysql.sql
* 类: class/article.php
article的表单主要由三个文件组成:
* include/form.article.php 表头及按钮
* form.article.config.php 定义表单的“简单/完全/定制”三种模式所显示的字段,默认为定制,可由用户或cookie选择。
* form.article.elements.php 定义各表单元素
在熟悉article的基本结构之后,要修改的文件已呼之欲出。
mysql.sql
在article表中增加:
`start_date` int(11) unsigned NOT NULL default ‘0′, #开始日期
`end_date` int(11) unsigned NOT NULL default ‘0′, #结束日期
`location` varchar(64) NOT NULL default ”, #地点
以及增加键:
KEY `start_date` (`start_date`), #常要以开始日期排序或分组
class/article.php
在 function Article() 中增加:
// hack by bitshine ================================>
//增加三个字段
$this->initVar(’start_date’, XOBJ_DTYPE_INT, 0);
$this->initVar(’end_date’, XOBJ_DTYPE_INT, 0);
$this->initVar(’location’, XOBJ_DTYPE_TXTBOX, 0);
// end of hack <=====================================
include/form.article.config.php
在$form_art_elements[”custom”] 和 [“full”]中增加:
// hack by bitshine ================================>
“start_date”,
“end_date”,
“location”,
// end of hack <=====================================
form.article.elements.php
在//summary之前增加:
// hack by bitshine ================================>
//开始日期
$form_element[”active”][”start_date”] = new XoopsFormText(’开始日期格式:<br />’.date(’Y-m-d’), “start_date”, 10, 10, $start_date);
$form_element[”active”][”end_date”] = new XoopsFormText(’结束日期 格式:<br />’.date(’Y-m-d’), “end_date”, 10, 10, $end_date);
$form_element[”active”][”location”] = new XoopsFormText(’地点’, “location”, 64, 64, $location);
// end of hack <=====================================
edit.article.php
在//summary之前增加:
// hack by bitshine ================================>
$start_date = $article_obj->getVar(”start_date”)? date(’Y-m-d’, $article_obj->getVar(”start_date”)) : “”;
$end_date = $article_obj->getVar(”end_date”) ? date(’Y-m-d’, $article_obj->getVar(”end_date”)) : “”;
$location = $article_obj->getVar(”location”);
// end of hack <=====================================
action.article.php
在 if($article_isNew){ 之前增加:
// hack by bitshine ================================>
$start_date = strtotime($_POST[’start_date’]);
if ($start_date != $article_obj->getVar(’start_date’)) {
$article_obj->setVar(’start_date’, $start_date);
}
$end_date = strtotime($_POST[’end_date’]);
if ($end_date != $article_obj->getVar(’end_date’)) {
$article_obj->setVar(’end_date’, $end_date);
}
$location = $_POST[’location’];
if ($location != $article_obj->getVar(’location’)) {
$article_obj->setVar(’location’, $location);
}
// end of hack <=====================================
view.article.php
在//summary之前增加:
// hack by bitshine ================================>
$article_data[”start_date”] = $article_obj->getVar(”start_date”)? date(’m月d日’, $article_obj->getVar(”start_date”)) : “”;
$article_data[”end_date”] = $article_obj->getVar(”end_date”)? date(’m月d日’, $article_obj->getVar(”end_date”)) : “”;
$article_data[”location”] = $article_obj->getVar(”location”);
// end of hack <=====================================
view.category.php
修改:
$tags = array(”uid”, “writer_id”, “art_title”, “art_summary”, “art_image”, “art_pages”, “art_categories”, “art_time_publish”, “art_counter”, “art_comments”, “art_trackbacks”);
==>
$tags = array(”uid”, “writer_id”, “art_title”, “start_date”, “end_date”, “location”, “art_summary”, “art_image”, “art_pages”, “art_categories”, “art_time_publish”, “art_counter”, “art_comments”, “art_trackbacks”);
blocks_news.php
修改:
$sql = “ SELECT art_id, art_title, art_time_publish, cat_id, ( art_comments + art_trackbacks ) AS comments “.
==>
$sql = “ SELECT art_id, art_title, art_time_publish, cat_id, start_date, end_date, location, ( art_comments + art_trackbacks ) AS comments “.
修改:
$sql = “ SELECT art_id, art_title, art_time_publish, ( art_comments + art_trackbacks ) AS comments “.
==>
$sql = “ SELECT art_id, art_title, start_date, end_date, location, art_time_publish, ( art_comments + art_trackbacks ) AS comments “.
在两个while ($row = $xoopsDB->fetchArray($result)) {中分别增加:
//hack by bitshine
$_art[”start_date”] = date(”m月d日”, $row[”start_date”]);
$_art[”end_date”] = date(”m月d日”, $row[”end_date”]);
$_art[”location”] = $row[”location”];
//hack end
//hack by bitshine
$_art[”start_date”] = $row[”start_date”] ? date(”m月d日”, $row[”start_date”]) : “”;
$_art[”end_date”] = $row[”end_date”] ? date(”m月d日”, $row[”end_date”]) : “”;
$_art[”location”] = $row[”location”];
//hack end
blocks_feature.php
修改:
$select .= “, a.cat_id, art_title, a.uid, writer_id, art_time_publish”;
==>
$select .= “, a.cat_id, art_title, a.uid, writer_id, art_time_publish, start_date, end_date, location”; //hack by bitshine
在$_art[”time”] = $article->getTime($options[4]);后增加:
//hack by bitshine
$_art[”start_date”] = $article->getVar(”start_date”)? date(”m月d日”, $article->getVar(”start_date”)) : “”;
$_art[”end_date”] = $article->getVar(”end_date”)? date(”m月d日”, $article->getVar(”end_date”)) : “”;
$_art[”location”] = $article->getVar(”location”);
//hack end
在模板文件 article_article.html、article_category.html、article_block_news.html、 article_block_feature.html相应的位置加上:
<!– hack start by bitshine –>
<div style=”color: red”>
<{$article.location}> <{$article.start_date}>
<{if $article.end_date}> – <{$article.end_date}> <{/if}>
</div>
<!– hack end by bitshine –>
经过以上修改,增加字段得到的基本功能已经可以使用了。如果需要其它一些管理和显示页面也可以进行类似的修改。