开发ecshop大概有半年多时间了,每次改的功能也没有做总结和记录,今天添加完一个功能,记录一下吧,省得以后忘记又要麻烦去查找。
需求:在后台菜单-报表统计-下面添加-用户情况-的功能,包括:每周新增注册用户,每周累积注册用户,活跃用户-周内至少登陆1次,每月访问量等等。
第一步:建立数据库的表
说明:个人本地数据库是:eshop,新建表名:ecs_user_situation。
第二步:建立存储过程
DELIMITER $$
DROP PROCEDURE IF EXISTS `eshop`.`pro_users`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `pro_users`()
BEGIN
DECLARE week_count INT;
DECLARE user_count INT;
DECLARE over_count INT;
DECLARE month_count INT;
-- 每周注册用户
SELECT COUNT(*) INTO week_count FROM ecs_users WHERE reg_time>(UNIX_TIMESTAMP(NOW())-7*24*60*60);
-- 总注册用户
SELECT COUNT(*) INTO user_count FROM ecs_users;
-- 每周的活跃用户
SELECT COUNT(*) INTO over_count FROM ecs_users WHERE last_login>(UNIX_TIMESTAMP(NOW())-7*24*60*60);
-- 每月的访问量
SELECT COUNT(*) INTO month_count FROM ecs_stats WHERE access_time>UNIX_TIMESTAMP(DATE_SUB(DATE_SUB(DATE_FORMAT(NOW(),'%y-%m-%d 00:00:00'),INTERVAL EXTRACT(DAY FROM NOW())-1 DAY),INTERVAL 0 MONTH));
INSERT INTO ecs_user_situation(week_count,user_count,over_count,month_count,week_time) VALUES (week_count,user_count,over_count,month_count,NOW());
END$$
DELIMITER ;
说明:个人本地存储过程名字:pro_users。
第三步:建立触发器
DELIMITER $$
ALTER EVENT `eve_users` ON SCHEDULE EVERY 1 HOUR STARTS '2013-09-02 12:11:14' ON COMPLETION PRESERVE ENABLE DO BEGIN
CALL pro_users();
END$$
DELIMITER ;
说明:个人本地触发器名字:eve_users。
第四步:添加后台菜单
1.添加菜单URL
在目录admin/includes/inc_menu.php的-报表统计-的那部分代码下面添加:
$modules['06_stats']['user_situation'] = 'user_situation.php?act=list';
在目录languages/zh_cn/admin/common.php的/* 报表统计 */的那部分代码下面添加:
$_LANG['user_situation'] = '用户情况';
在目录admin/includes/inc_priv.php的-报表统计权限-的那部分代码下面添加:
$purview['user_situation'] = 'user_situation';
4.添加管理权限
在目录languages/zh_cn/admin/priv_action.php添加下面的代码:
$_LANG['user_situation'] = '用户情况';
INSERT INTO ecs_admin_action(parent_id,action_code) VALUES(6,'user_situation');
第五步:业务逻辑代码
在目录admin下面添加user_situation.php,代码如下:
<?php
/**
* add by zbl 2013-08-29
* 新增注册用户,累积注册用户,活跃用户-周内至少登陆1次,每月访问量
* @var unknown_type
*/
define('IN_ECS', true);
//页面引用
require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/lib_goods.php');
if($_REQUEST['act']=='list'){
$user_situation=get_user_situation();
$smarty->assign('user_situation_list', $user_situation['user_situation_list']);
$smarty->assign('filter', $user_situation['filter']);
$smarty->assign('record_count', $user_situation['record_count']);
$smarty->assign('page_count', $user_situation['page_count']);
$smarty->assign('full_page', 1);//解决分页查询出现页面重复情况
$smarty->assign('ur_here', $_LANG['user_situation']);
/* 在页脚显示内存信息 */
assign_query_info();
$smarty->display('user_situation.htm');
}elseif($_REQUEST['act']=='query'){
$user_situation=get_user_situation();
$smarty->assign('user_situation_list', $user_situation['user_situation_list']);
$smarty->assign('filter', $user_situation['filter']);
$smarty->assign('record_count', $user_situation['record_count']);
$smarty->assign('page_count', $user_situation['page_count']);
$smarty->assign('ur_here', $_LANG['user_situation']);
$tpl = 'user_situation.htm';
make_json_result($smarty->fetch($tpl), '',array('filter' => $user_situation['filter'], 'page_count' => $user_situation['page_count']));
}
?>
说明:这里有两个方法,一个是列表数据查询,另外一个是分页查询,每行的代码解析就不详细说。
第六步:操作数据库的方法
在目录admin/includes/lib_goods.php里面添加get_user_situation()方法:
/**
* zbl add 20130902
* Enter description here ...
*/
function get_user_situation(){
//分页
$filter['record_count']=$GLOBALS['db']->GetOne('SELECT count(*) FROM '.$GLOBALS['ecs']->table('user_situation'));
$filter = page_and_size($filter);
//排序
$filter['sort_by'] = empty($_REQUEST['sort_by']) ? 'week_time' : trim($_REQUEST['sort_by']);
$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
//查询
$sql='SELECT week_count,user_count,over_count,month_count,week_time FROM '.$GLOBALS['ecs']->table('user_situation').' order by '.$filter[sort_by].' '.$filter[sort_order].' LIMIT ' . $filter['start'] . ',' . $filter[page_size];
//echo $sql;
$row = $GLOBALS['db']->getAll($sql);
//返回数据
return array('user_situation_list' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}
说明:这里的代码顺序不能放错,$sql的where后面查询条件必须加上才可以实现分页功能,所以$filter['sort_by']和$filter['sort_order']这两个参数不能缺少,详细请查看/admin/js/listtable.js的listTable.sort()方法。
第七步:添加显示页面
在目录/admin/templates下面添加user_situation.htm代码如下:
<!-- $Id: user_situation.htm 16752 2013-08-28 zbl $ -->
{if $full_page}
{include file="pageheader.htm"}
{insert_scripts files="../js/utils.js,listtable.js"}
<form method="post" action="" name="listForm" οnsubmit="return confirmSubmit(this)">
<div class="list-div" id="listDiv">
{/if}
<table cellpadding="3" cellspacing="1">
<tr>
<th><a href="javascript:listTable.sort('week_time'); ">{$lang.week_time}</a></th>
<th><a href="javascript:listTable.sort('week_count'); ">{$lang.week_count}</a></th>
<th><a href="javascript:listTable.sort('user_count'); ">{$lang.user_count}</a></th>
<th><a href="javascript:listTable.sort('over_count'); ">{$lang.over_count}</a></th>
<th><a href="javascript:listTable.sort('month_count'); ">{$lang.month_count}</a></th>
<tr>
{foreach from=$user_situation_list item=user_situation}
<tr>
<td align="center">{$user_situation.week_time}</td>
<td align="center">{$user_situation.week_count}</td>
<td align="center">{$user_situation.user_count}</td>
<td align="center">{$user_situation.over_count}</td>
<td align="center">{$user_situation.month_count}</td>
</tr>
{foreachelse}
<tr><td class="no-records" colspan="10">{$lang.no_records}</td></tr>
{/foreach}
</table>
<!-- 分页 -->
<table id="page-table" cellspacing="0">
<tr>
<td align="right" nowrap="true">
{include file="page.htm"}
</td>
</tr>
</table>
{if $full_page}
</div>
</form>
<script type="text/javascript">
listTable.recordCount = {$record_count};
listTable.pageCount = {$page_count};
{foreach from=$filter item=item key=key}
listTable.filter.{$key} = '{$item}';
{/foreach}
</script>
{include file="pagefooter.htm"}
{/if}
说明:html页面注意引入js文件和用{if $full_page}{/if}来包含首位代码来解决分页查询出现的重复页面。
第八步:添加显示页面的列表名字
在目录languages/zh_cn/admin/下面添加user_situation.php代码如下:
<?php
/**
* add by zbl 2013-08-30
* Enter description here ...
* @var unknown_type
*/
$_LANG['week_time'] = '时间';
$_LANG['week_count'] = '每周注册用户数';
$_LANG['user_count'] = '累积注册用户数';
$_LANG['over_count'] = '每周活跃用户数';
$_LANG['month_count'] = '每月访问量';
?>
第九步:测试运行
说明:本人这里都是修改触发器出现的当天数据。
这个流程从修改数据库到顶层页面都有说明,ecshop的二次开发按照这个流程基本能开发所有功能了。详细的功能实现代码再慢慢研究就可以了。