ajax实时任务提示功能的实现代码第1/2页
程序员文章站
2022-03-07 10:18:24
项目代码结构见 我之前写的[ext/fckeditor 集成 -- ajax ui -- 一种web开发的新的思维,要及时转换思想]一文. 中的 ├─taskofpig │...
项目代码结构见 我之前写的[ext/fckeditor 集成 -- ajax ui -- 一种web开发的新的思维,要及时转换思想]一文.
中的
├─taskofpig
│ ├─controller
│ ├─dao
│ ├─js
│ ├─music
│ ├─tpl
│ ├─tpl_c
│ └─_log
项目代码如下:
db.sql
set foreign_key_checks=0;
-- ----------------------------
-- table structure for task
-- ----------------------------
create table `task` (
`id` int(11) not null,
`title` varchar(100) collate utf8_unicode_ci not null,
`desc` text collate utf8_unicode_ci,
`date` datetime not null,
`created` int(11) default null,
`updated` int(11) default null,
primary key (`id`)
) engine=myisam default charset=utf8 collate=utf8_unicode_ci;
-- ----------------------------
-- table structure for task_seq
-- ----------------------------
create table `task_seq` (
`id` int(11) not null
) engine=myisam default charset=utf8 collate=utf8_unicode_ci;
/ucren/taskofpig/index.php
<?php
//设置正确的时区
date_default_timezone_set("asia/shanghai");
define('taskofpig_dir',dirname(__file__)) ;
require('../phplibs/flea/flea.php');
// 对$globals[g_flea_var]['class_path'] 进行配置
flea::import(taskofpig_dir); //将当前目录加入到环境变量中
flea::loadappinf('appconfig.php') ; //将配置文件单独分出来,容易维护
flea::init();
// 由于 flea_db_tabledatagateway 并不是自动载入的,因此需要明确载入
flea::loadclass('flea_db_tabledatagateway');
flea::runmvc();
?>
/ucren/taskofpig/appconfig.php
<?php
// 对 $globals[g_flea_var]['app_inf'] 进行配置
return array(
'dispatcher' => 'flea_dispatcher_simple' , //定制调度器 flea_dispatcher_auth
'controlleraccessor' => 'ctl' ,
'actionaccessor' => 'act' ,
'view' => 'flea_view_smarty', //定制视图
'viewconfig' => array(
'smartydir' => '../phplibs/smarty',
'template_dir' => './tpl',
'compile_dir' => './tpl_c',
'left_delimiter' => '<%',
'right_delimiter' => '%>',
'debugging' => false
),
'dbdsn' => array( //定制数据库连接参数
'driver' => 'mysql',
'host' => 'localhost',
'login' => 'dbuser',
'password' => 'dbpass',
'database' => 'dbname' ,
'charset ' => 'utf8'
) ,
'logfiledir' => './log' , //定制日志
'logfilename' => 'task_admin.log'
);
?>
/ucren/taskofpig/dao/table.php
<?php
//生气猪的任务计划表
class dao_tasktable extends flea_db_tabledatagateway
{
// 指定数据表名称
var $tablename = 'task';
// 指定主键字段名
var $primarykey = 'id';
}
?>
/ucren/taskofpig/controller/default.php
<?php
flea::loadfile('dao_table.php',true) ;
flea::loadfile('flea_ajax_json.php',true) ;
class controller_default extends flea_controller_action
{
var $smarty ;
function controller_default()
{
$this->smarty = $this->_getview();
$this->smarty->assign('sitename','任务计划表 -- 生气猪') ;
$this->smarty->assign('opname','任务列表') ;//缺省应该在子模块中更改值
}
function actionindex()
{
$this->tomodulepage(); //缺省显示任务列表页
}
//定义一个函数用于调用fckeditor
function call_fck($input_name,$input_value,$w='800',$h='400')
{
include_once '../fckeditor/fckeditor.php';
$fcked = new fckeditor($input_name) ;
$fcked->basepath = '../fckeditor/';
$fcked->toolbarset = 'default' ; //工具栏设置
$fcked->instancename = $input_name ;
$fcked->width = $w;
$fcked->height = $h;
$fcked->value = $input_value;
$fck_area = $fcked->createhtml();
$this->smarty->assign('fck_area',$fck_area);
unset($fck_area) ;
unset($fcked) ;
}
function _showpage($tpl='taskofpig.main.html')
{
$this->smarty->display($tpl);
}
function actionadd()
{
$this->addtask();
}
function actionupdate()
{
$this->updatetask();
}
function deletetask($id){
$row = array('id'=>$id);
$thisdao = & new dao_tasktable() ;
$status = $thisdao->remove($row); //返回boolean值
unset($thisdao);
return $status ;
}
function listtask()
{
$thisdao = & new dao_tasktable() ;
$rows = $thisdao->findall(); //二维数组
foreach($rows as &$row) //注意这里要传引用
{
$row['desc'] = mb_substr($row['desc'],0,40,'utf-8');
}
$this->smarty->assign('rowset',$rows);
$this->_showpage();
}
function addtask()
{
$thisdao = & new dao_tasktable() ;
$row = array(
'title' => $_request['title'],
'desc' => $_request['desc'],
'date' => $_request['date']
);
$commitid = $thisdao->create($row);
unset($thisdao);
echo "成功添加新任务";
redirect( url("default"),1) ;
}
function updatetask()
{
$thisdao = & new dao_tasktable() ;
$row = array(
'id' => $_request['id'],
'title' => $_request['title'],
'desc' => $_request['desc'],
'date' => $_request['date']
);
$commitid = $thisdao->update($row);
unset($thisdao);
echo "成功更新任务";
redirect( url("default"),1) ;
}
function querytask($id){
$thisdao = & new dao_tasktable() ;
$row = $thisdao->find(array('id'=>$id));
unset($thisdao);
return $row ;
}
function querytaskfordate($date=null)
{
$thisdao = & new dao_tasktable() ; //'2008-08-17 07:42:29'
$row = $thisdao->find(array('date'=>date('y-m-d h:i:s')));
unset($thisdao);
if (!empty($row))
{
$jsonobj = new services_json();
echo $jsonobj->encode($row);
}
else
die(date('y-m-d h:i:s'));
}
//任务流转控制方法
function tomodulepage()
{
if ($_request['op'] == 'search') {
$this->querytaskfordate();
}
else if ($_request['op'] == 'add') {
$this->smarty->assign('opname','添加新任务') ;
$this->smarty->assign('tasktime',date('y-m-d h:i:s')) ;
$this->call_fck('desc','');
$this->_showpage('taskofpig.add.html');
}
else if ($_request['op'] == 'del') {
if ( isset($_request['id']) && is_numeric($_request['id']) )
$status = $this->deletetask($_request['id']) ;
$this->listtask();
}
else if ($_request['op'] == 'edit') {
if ( isset($_request['id']) && is_numeric($_request['id']) ){
$row = $this->querytask($_request['id']) ;
}
$this->call_fck('desc',$row['desc']);
unset($row['desc']) ;
$this->smarty->assign('rowset',$row);
$this->smarty->assign('opname','修改任务') ;
$this->_showpage('taskofpig.edit.html');
}
else { //列表
$this->listtask();
}
}
}
?>
中的
├─taskofpig
│ ├─controller
│ ├─dao
│ ├─js
│ ├─music
│ ├─tpl
│ ├─tpl_c
│ └─_log
项目代码如下:
db.sql
set foreign_key_checks=0;
-- ----------------------------
-- table structure for task
-- ----------------------------
create table `task` (
`id` int(11) not null,
`title` varchar(100) collate utf8_unicode_ci not null,
`desc` text collate utf8_unicode_ci,
`date` datetime not null,
`created` int(11) default null,
`updated` int(11) default null,
primary key (`id`)
) engine=myisam default charset=utf8 collate=utf8_unicode_ci;
-- ----------------------------
-- table structure for task_seq
-- ----------------------------
create table `task_seq` (
`id` int(11) not null
) engine=myisam default charset=utf8 collate=utf8_unicode_ci;
/ucren/taskofpig/index.php
<?php
//设置正确的时区
date_default_timezone_set("asia/shanghai");
define('taskofpig_dir',dirname(__file__)) ;
require('../phplibs/flea/flea.php');
// 对$globals[g_flea_var]['class_path'] 进行配置
flea::import(taskofpig_dir); //将当前目录加入到环境变量中
flea::loadappinf('appconfig.php') ; //将配置文件单独分出来,容易维护
flea::init();
// 由于 flea_db_tabledatagateway 并不是自动载入的,因此需要明确载入
flea::loadclass('flea_db_tabledatagateway');
flea::runmvc();
?>
/ucren/taskofpig/appconfig.php
<?php
// 对 $globals[g_flea_var]['app_inf'] 进行配置
return array(
'dispatcher' => 'flea_dispatcher_simple' , //定制调度器 flea_dispatcher_auth
'controlleraccessor' => 'ctl' ,
'actionaccessor' => 'act' ,
'view' => 'flea_view_smarty', //定制视图
'viewconfig' => array(
'smartydir' => '../phplibs/smarty',
'template_dir' => './tpl',
'compile_dir' => './tpl_c',
'left_delimiter' => '<%',
'right_delimiter' => '%>',
'debugging' => false
),
'dbdsn' => array( //定制数据库连接参数
'driver' => 'mysql',
'host' => 'localhost',
'login' => 'dbuser',
'password' => 'dbpass',
'database' => 'dbname' ,
'charset ' => 'utf8'
) ,
'logfiledir' => './log' , //定制日志
'logfilename' => 'task_admin.log'
);
?>
/ucren/taskofpig/dao/table.php
<?php
//生气猪的任务计划表
class dao_tasktable extends flea_db_tabledatagateway
{
// 指定数据表名称
var $tablename = 'task';
// 指定主键字段名
var $primarykey = 'id';
}
?>
/ucren/taskofpig/controller/default.php
<?php
flea::loadfile('dao_table.php',true) ;
flea::loadfile('flea_ajax_json.php',true) ;
class controller_default extends flea_controller_action
{
var $smarty ;
function controller_default()
{
$this->smarty = $this->_getview();
$this->smarty->assign('sitename','任务计划表 -- 生气猪') ;
$this->smarty->assign('opname','任务列表') ;//缺省应该在子模块中更改值
}
function actionindex()
{
$this->tomodulepage(); //缺省显示任务列表页
}
//定义一个函数用于调用fckeditor
function call_fck($input_name,$input_value,$w='800',$h='400')
{
include_once '../fckeditor/fckeditor.php';
$fcked = new fckeditor($input_name) ;
$fcked->basepath = '../fckeditor/';
$fcked->toolbarset = 'default' ; //工具栏设置
$fcked->instancename = $input_name ;
$fcked->width = $w;
$fcked->height = $h;
$fcked->value = $input_value;
$fck_area = $fcked->createhtml();
$this->smarty->assign('fck_area',$fck_area);
unset($fck_area) ;
unset($fcked) ;
}
function _showpage($tpl='taskofpig.main.html')
{
$this->smarty->display($tpl);
}
function actionadd()
{
$this->addtask();
}
function actionupdate()
{
$this->updatetask();
}
function deletetask($id){
$row = array('id'=>$id);
$thisdao = & new dao_tasktable() ;
$status = $thisdao->remove($row); //返回boolean值
unset($thisdao);
return $status ;
}
function listtask()
{
$thisdao = & new dao_tasktable() ;
$rows = $thisdao->findall(); //二维数组
foreach($rows as &$row) //注意这里要传引用
{
$row['desc'] = mb_substr($row['desc'],0,40,'utf-8');
}
$this->smarty->assign('rowset',$rows);
$this->_showpage();
}
function addtask()
{
$thisdao = & new dao_tasktable() ;
$row = array(
'title' => $_request['title'],
'desc' => $_request['desc'],
'date' => $_request['date']
);
$commitid = $thisdao->create($row);
unset($thisdao);
echo "成功添加新任务";
redirect( url("default"),1) ;
}
function updatetask()
{
$thisdao = & new dao_tasktable() ;
$row = array(
'id' => $_request['id'],
'title' => $_request['title'],
'desc' => $_request['desc'],
'date' => $_request['date']
);
$commitid = $thisdao->update($row);
unset($thisdao);
echo "成功更新任务";
redirect( url("default"),1) ;
}
function querytask($id){
$thisdao = & new dao_tasktable() ;
$row = $thisdao->find(array('id'=>$id));
unset($thisdao);
return $row ;
}
function querytaskfordate($date=null)
{
$thisdao = & new dao_tasktable() ; //'2008-08-17 07:42:29'
$row = $thisdao->find(array('date'=>date('y-m-d h:i:s')));
unset($thisdao);
if (!empty($row))
{
$jsonobj = new services_json();
echo $jsonobj->encode($row);
}
else
die(date('y-m-d h:i:s'));
}
//任务流转控制方法
function tomodulepage()
{
if ($_request['op'] == 'search') {
$this->querytaskfordate();
}
else if ($_request['op'] == 'add') {
$this->smarty->assign('opname','添加新任务') ;
$this->smarty->assign('tasktime',date('y-m-d h:i:s')) ;
$this->call_fck('desc','');
$this->_showpage('taskofpig.add.html');
}
else if ($_request['op'] == 'del') {
if ( isset($_request['id']) && is_numeric($_request['id']) )
$status = $this->deletetask($_request['id']) ;
$this->listtask();
}
else if ($_request['op'] == 'edit') {
if ( isset($_request['id']) && is_numeric($_request['id']) ){
$row = $this->querytask($_request['id']) ;
}
$this->call_fck('desc',$row['desc']);
unset($row['desc']) ;
$this->smarty->assign('rowset',$row);
$this->smarty->assign('opname','修改任务') ;
$this->_showpage('taskofpig.edit.html');
}
else { //列表
$this->listtask();
}
}
}
?>
1