第一步,设计新插件,保存后
第二步,修改模块增加一个class类型,对于文件夹名,可以保持默认(跟id相同),保存.
第三步,在./source/plugin目录下建立跟上面的目录名相同的目录.然后把xml导出到那个目录中.
第四步,在本插件目录中创建随便.class.php文件,内容如下;作用见代码中说明也可以到官方的帮助中看一下.但是官方的说明有一个点我看得不太明白.不得已直接瞧代码才明白.
<?php
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
/*class名由plugin_id名组成*/
class plugin_recommend_article {
/*初始化*/
function plugin_recommend_article(){
global $_G;
//$this->pluginPath = $_G['siteurl']. 'source/plugin/recommend_article/';
}
/*
* add by qidizi
* 输出推荐下拉
* 内部使用公用方法,建议增加_线
*/
function _recommendSelect($vars = array()){
global $_G;
if (empty($_G['uid']) || empty($vars) ) return;//未登录
$res = DB::query("SELECT p_a_id, name FROM ".DB::table('chrd_common_recommend_area'));
//注意这里为了代码独立出来,方便升级,把所有的功能代码都放到这个目录来了,所以js也放这里了.
//然后使用php输出js代码方式.也可以使用上面的 //$this->pluginPath来输出一个xxx.js
$sel = "
<script type=\"text/javascript\" src=\"plugin.php?id=recommend_article:ajaxrecommed&action=loadJs\"></script>
<select οnchange=\"ajaxRecommend({pid:this.value, type:{$vars['type']}, aid:{$vars['aid']}, path:'{$vars['path']}' });\">
<option value=0>请选择推荐位置</option>";
while ($tmp = DB::fetch($res)){
$sel .="<option value=\"{$tmp['p_a_id']}\">{$tmp['name']}</option>";
}
$sel .="</select>";
return array('select'=>$sel);
}
}
/* 子类,由 plugin_id名_需要出现的对象脚本名 组成
* 脚本对象在discuz的根目录下,
* 有home.php,即home
* 有forum.php即forum
*反正你看到 地址上是http://chrd.q/discuz/forum.php,想出现在这个页面就得写一个plugin_id名_forum子类;
*/
class plugin_recommend_article_forum extends plugin_recommend_article {
/*在帖子内容页输出推荐下拉
* 方法名字组成,define('CURMODULE', $mod)定义的$mod,表示在那个模块中可以出现,配合类名中forum,就能达到某个限定_自定名字_output这个位置还有其它定义,需要看官方说明
*
*/
function viewthread_showSelect_output(){
global $_G;
$vars = array('type'=>1, 'aid'=>$_G['tid'], 'path'=>$_G['fid']);
return $this->_recommendSelect($vars);
}
}
/* plugin_是固定需要的,recommend_article是插件id,home是根目录下home.php的名字*/
class plugin_recommend_article_home extends plugin_recommend_article {
/*在日志内容页输出推荐下拉
* 要在home模块内出现,必须加上do的类型,http://chrd.q/discuz/home.php?mod=space&uid=1&do=blog&id=1
* 所以需要space_showSelect_output 写成 space_blog_showSelect_output
*我就是因为只看官方说明.发现这个东东怎么总是出不来,
*在function_core.php中的hookscript中有一句说明了它在这个时候改了规则....
if($hscript == 'home') {
if($script != 'spacecp') {
$script = 'space_'.(!empty($_G['gp_do']) ? $_G['gp_do'] : (!empty($_GET['do']) ? $_GET['do'] : ''));
} else {
$script .= !empty($_G['gp_ac']) ? '_'.$_G['gp_ac'] : (!empty($_GET['ac']) ? '_'.$_GET['ac'] : '');
}
}
*/
function space_blog_showSelect_output(){//array('type'=>2, 'id'=>$blog[blogid], 'path'=>''
global $_G;
$vars = array('type'=>2, 'aid'=>$_G['gp_id'], 'path'=>'');
return $this->_recommendSelect($vars);
}
}
插件数据生成是在./function/cache/cache_setting.php中的get_cachedata_setting_plugin($method = '')生成的.
放在模板中的hook代码只是在对应的位置输出对应的$_G数组中的数据而已;
在模板中的home/space_blog_view.html某处显示space_blog_showSelect_output输出的内容,这样写
<!--{hook/space_blog_showSelect 'select'}-->
效果
如果安装时候需要用到install或是uninstall.php文件,需要手动向xml中增加(xml中有很item,注意item放的树序),关于这个xml的解释,看后台的admin中的plugin的脚本代码.
<item id="installfile"><![CDATA[install.php]]></item>
<item id="uninstallfile"><![CDATA[uninstall.php]]></item>
</item>
</root>
安装脚本install.php可以这样写,uninstall差不多意思了.
<?php
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
$dir = $_G['gp_dir'];//获取本插件的目录名
if (empty($_G['gp_installing'])){//第一次,进入本文件时,先进行提示,忘记写取消按钮了.第二次,即按下确定安装再进行安装
echo '<div class="infobox"><h4 class="infotitle2">安装前提示</h4>'
.'<div style="text-align:left;line-height:25px;">'
."
安装前说明:<br />
重新安装将会把旧的推荐信息数据库的表全部删除,所以.以前的信息将会丢失.请慎重操作.
"
.'</div><br /><br /><center>'
.'<button οnclick="location.href=\''.ADMINSCRIPT.'?action=plugins&operation=plugininstall&dir='.$dir.'&installtype=&installing=1\'">'
.'知道了,开始安装</button></center></div>';
exit;//注意别直接就安装了.退出先
}
$sid = 1;//服务器id
$tabPre = $_G['config']['db'][$sid]['tablepre'];//获取配置中的表前缀
$sql = <<<EOF
--
-- 表的结构 `{$tabPre}chrd_common_recommend_area`
--
DROP TABLE IF EXISTS `{$tabPre}chrd_common_recommend_area`;
CREATE TABLE IF NOT EXISTS `{$tabPre}chrd_common_recommend_area` (
`p_a_id` int(4) NOT NULL COMMENT '页序号+位置序号数字组合',
`name` varchar(250) NOT NULL COMMENT '位置名称',
PRIMARY KEY (`p_a_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='文章推荐位置表';
-- --------------------------------------------------------
--
-- 表的结构 `{$tabPre}chrd_common_recommend_article`
--
DROP TABLE IF EXISTS `{$tabPre}chrd_common_recommend_article`;
CREATE TABLE IF NOT EXISTS `{$tabPre}chrd_common_recommend_article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL COMMENT '首个推荐用户id',
`users` int(11) NOT NULL DEFAULT '0' COMMENT '推荐总数',
`type` int(11) NOT NULL COMMENT '文章类别(1帖子2日志)',
`path` varchar(250) NOT NULL COMMENT '文章分类路径,用.分隔',
`aid` int(11) NOT NULL COMMENT '文章id',
`p_a_id` int(11) NOT NULL COMMENT '推荐位置id',
`yes` int(11) NOT NULL DEFAULT '0' COMMENT '同意人数',
`no` int(11) NOT NULL DEFAULT '0' COMMENT '不同意人数',
`pass` int(1) NOT NULL DEFAULT '0' COMMENT '通过审核',
`time` int(11) NOT NULL COMMENT '推荐时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='推荐文章表' AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- 表的结构 `{$tabPre}chrd_common_recommend_grade_log`
--
DROP TABLE IF EXISTS `{$tabPre}chrd_common_recommend_grade_log`;
CREATE TABLE IF NOT EXISTS `{$tabPre}chrd_common_recommend_grade_log` (
`rid` int(11) NOT NULL COMMENT '推荐id',
`uid` int(11) NOT NULL COMMENT '用户id',
`time` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='推荐文章审核记录';
-- --------------------------------------------------------
--
-- 表的结构 `{$tabPre}chrd_common_recommend_log`
--
DROP TABLE IF EXISTS `{$tabPre}chrd_common_recommend_log`;
CREATE TABLE IF NOT EXISTS `{$tabPre}chrd_common_recommend_log` (
`rid` int(11) NOT NULL COMMENT '推荐id',
`uid` int(11) NOT NULL COMMENT '推荐用户',
`time` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='推荐记录';
EOF;
runquery($sql);//执行sql语句
$finish = TRUE;//安装完成了,必须出现这个变量.
-------
安装后需要启用才能在模板中显示.
-------------
使用<script type=\"text/javascript\" src=\"plugin.php?id=recommend_article:ajaxrecommed&action=loadJs\"></script>调用本目录中的php代码编写.
文件名为 随便.inc.php
ajaxrecommed.inc.php
内容就简单的
<?php
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
switch($_G['gp_action']) {
case 'loadJs':
就可以了.