一个自动回调+可动态配置php参数+可暂停/继续的框架
2013-04-09 21:08
323 查看
思路:
1. 通过php的array形式进行配置各项参数,因为php的array可以转成str,及str转成array对象.
2. 通过在配置中指定类的名字和方法名,能动态调用不同的功能块.进行流程选择.
3. 回调完.把配置对象输出,备复制保存下次使用.也让下轮回调更加简单,且回调时无需在前台动态处理参数.交给php处理更加方便.
4. 选择iframe代替ajax好处是出错或是其它的提示信息更加方便查看.
5. 没提供配置时可以在php中初始化后再提示修改后继续.且保证不同功能块能使用不同的参数配置.
设计它的用处就是为了能够把配置信息保存,给下次继续使用.
主php代码:
------------
<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('import', 'import', 0);
class index extends import{//默认控制器
public function init () {//默认动作
//载入操作面板
echo '当前站点id是'.$this->getSiteId();
?><br />
php配置信息(格式为 key=>array('值', '说明'))
<form target="hideWin" id="hideForm" name="hideForm" action="?m=import&c=index&a=doing&pc_hash=<?php echo $_GET['pc_hash'];?>" method="POST" >
<textarea id="config" name="config" style="width:100%; height:300px;"></textarea>
</form>
<a href="javascript:void(0);" onclick="this.childNodes[0].click();"><input type=checkbox id="pauseCall" />暂停回调下个地址(直到取消回调才能继续)</a>
<br />
<input type=button value="开始" onclick="startImport(this);" id="startBt"><br />
<fieldset>
<legend>
导入进度信息 <a href="javascript:void(0);" onclick="getObj('importTip').innerHTML = '';">清屏</a>
<input type=checkbox checked id="autoScroll" />自动滚屏
</legend>
<div id="importTip" style="height:300px;overflow:auto;">
</div>
</fieldset>
<script>
var pcHash = '<?php echo $_GET['pc_hash'];?>';
var site = location.href.replace(/index\.php.*$/i, 'index.php');
function getObj(id){
return document.getElementById(id);
}
function showTip(str){
var div = document.createElement('DIV');
div.innerHTML = str.replace(/\{\*\/}/g, "'");
div = getObj('importTip').appendChild(div);
if (getObj('autoScroll').checked){
getObj('importTip').scrollTop = getObj('importTip').scrollHeight;
}
}
function startImport(obj){
if (obj.value == '开始'){
if (! /^\s*array\s*\([\s\S]*\)\s*$|^\s*$/img.test(getObj('config').value)) {
return alert('php配置格式有误, 或许清空后让php初始化.');
}
if (! confirm('你确定配置正确了吗?')){
return 0;
}
obj.value = '进行中...';
callUrl();
}else {
stopImport();
}
}
function stopImport(){
showTip('处理结束');
getObj('startBt').value = '开始';
}
function showConfig(str) {
getObj('config').value = decodeURIComponent(str);
}
function callUrl() {
if (getObj('startBt').value == '开始') {
showTip('回调取消');
return;
}
if (getObj('pauseCall').checked){
showTip('回调暂停,5秒再试是否允许回调');
return setTimeout(callUrl, 5000);
}
showTip('正在回调');
getObj('hideForm').submit();
}
function obj2str(obj){
var a = [];
for (var tmp in obj){
a.push(tmp + '=' + encodeURIComponent(obj[tmp]));
}
return a.join('&');
}
window.onbeforeunload = function () {
event.returnValue = "你确定要退出, 你是否需要保存配置参数等还有记录?";
}
document.write('<iframe id="hideWin" name="hideWin" style="width:100%;height:300px;" src="about:blank"></iframe>');
</script>
<?php
}
function doing() {
if (! getCfg('classFunc')) setCfg('classFunc', array('init', '需要执行的流程类中的动作, 默认是init'));
if (! getCfg('className')) {
setCfg('className', array('category', '需要执行的流程,目前有category:创建工具分类;tool:导入工具文章'));
runJs('showConfig', array(cfg2str()));
exitJs('初始化流程,请配置参数');
}
if (! pc_base::load_app_class(getCfg('className'), 'import')) exitJs(getCfg('className'). ' 类不存在,请重新配置');
if (! method_exists (getCfg('className'), getCfg('classFunc'))) exitJs(getCfg('className'). '类不存在方法'. getCfg('classFunc').',请重新配置');
$co = getCfg('className');
$cf = getCfg('classFunc');
$co = new $co();
$co->$cf();
}
}
------------------
基础类import.class.php
_---------------
<?php
header('Content-Type: text/html; charset=utf-8');
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_app_func('public','import');
//pc_base::load_app_func('db', 'import');//外接数据库类
class import extends admin{//默认控制器
function __construct() {
parent::__construct();
$this->initConfig();
}
function getSiteId(){
$cId = param::get_cookie('siteid');
$sId = get_siteid();
if ($cId != $sId) {
return $cId;
}
return $sId;
}
function initConfig() { //初始化参数成array对象
global $gCfg;
$config = $_POST['config'];
if(!get_magic_quotes_gpc()) {//反pc_base过滤
$config = new_stripslashes($config);
}
$config = urldecode($config);
$config = empty($config) ? 'array()' : $config;
$phpCode = "\$gCfg = $config;";
eval($phpCode);
if (!(int)getCfg('siteId')) setCfg('siteId', array(0, '站点id,注意非0值'));
}
}
---------------------
共用func
public.func.php
---------
<?php
/*
* 获取配置中的值/说明
*/
function getCfg($key, $index = 0){
global $gCfg;
if (empty($key) || empty($gCfg) || !isset($gCfg[$key]) ) return null;
return trim($gCfg[$key][$index]);
}
/*
* 设置配置中的值
*/
function setCfg($key, $val = array(null, '')) {
global $gCfg;
if (isset($gCfg[$key])) {//用新值reset相同位置上的旧值
array_splice($gCfg[$key], 0, count($val), $val);
}else {
$gCfg[$key] = $val;
}
}
function cfg2str() {
global $gCfg;
return "'".rawurlencode (var_export($gCfg, 1))."'";
}
function runJs($fun, $var){
echo "<script > parent.{$fun}(" .implode(',', $var). ");</script>";
}
function exitJs($str){
echoJs($str);
endImport();
exit;
}
function endImport(){
echo "<script > parent.stopImport();</script>";
}
function echoJs($str){
$str = str_replace("'", "{*/}", $str);
$str = preg_replace("/[\n\r]+/", "<br />", $str);
echo "<script >try{ parent.showTip('{$str}');}catch(e){alert('{$str}');}</script>";
}
function getPinYin($str){
$str = str2array($str);
$str = array_map('first_pinyin', $str);
return implode('', $str);
}
---------
其中一个功能块类
category.class.php
----------------
<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('import', 'import', 0);
class category extends import{//默认控制器
private $db;
function __construct() {
parent::__construct();
$this->checkConfig() ;
$this->db = pc_base::load_model('category_model');
}
/*
* 检测配置是否适用/并初始化默认值
*/
function checkConfig() {
if (!getCfg('catPDir')) setCfg('catPDir', array('', '工具上级的英文目录名,顶级时为空'));
if (!getCfg('catPId')) setCfg('catPId', array(0, '工具上级的Id,顶级时为0'));
if (!getCfg('catDir')) setCfg('catDir', array('tools', '工具英文目录,非空'));
if (!getCfg('catId')) setCfg('catId', array(0, '工具catid, 非0,在栏目管理中'));
if (!getCfg('catMId')) setCfg('catMId', array(0, '工具moudle id, 非0, 在模型管理中'));
if (!getCfg('privRoleid')) setCfg('privRoleid', array(null, '角色权限,默认'));
if (!getCfg('privGroupid')) setCfg('privGroupid', array(null, '会员组权限,默认'));
if (!getCfg('catTpl')) setCfg('catTpl', array('category_download', '栏目模板'));
if (!getCfg('listTpl')) setCfg('listTpl', array('list_download', '列表模板'));
if (!getCfg('showTpl')) setCfg('showTpl', array('show_download', '页面模板'));
if (!getCfg('cat2Htm')) setCfg('cat2Htm', array(1, '栏目生成Html,可保留默认'));
if (!getCfg('content2Html')) setCfg('content2Html', array(1, '内容页生成Html,可保留默认'));
if (!getCfg('catHtm2root')) setCfg('catHtm2root', array(0, '分类下的文章是否生成html到root,可保留默认'));
if (!getCfg('isMenu')) setCfg('isMenu', array(1, '是否在导航显示,可保留默认'));
if (!getCfg('catUrlRuleId')) setCfg('catUrlRuleId', array(0, '必填,栏目页URL规则id,在新建栏目时的生成html选项中有,如果自定义,需要先写好规则再设置'));
if (!getCfg('contentUrlRuleId')) setCfg('contentUrlRuleId', array(0, '必填,内容页URL规则id,同栏目页url规则说明'));
if (
!getCfg('catDir') //工具目录
|| ((int)getCfg('catId') < 1) //工具id
|| ((int)getCfg('siteId') < 1) //站点id
|| ((int)getCfg('catUrlRuleId') < 1) //栏目页URL规则
|| ((int)getCfg('contentUrlRuleId') < 1) //内容页URL规则id
) {
runJs('showConfig', array(cfg2str()));
exitJs('请配置分类参数');
}
}
//获取旧站的工具下面的子分类,导入
public function init(){
$subs = dpSql ("SELECT d.name, d.tid FROM {pre/}term_data d LEFT JOIN {pre/}vocabulary v ON d.vid=v.vid WHERE v.name='工具类别' order by d.tid asc");
while($row = getRow($subs)){
$this->joinCatInfo($row['name']);
}
$this->cache();//更新栏目缓存,否则后面增加文章时会出现问题
runJs('showConfig', array(cfg2str()));
exitJs('导入栏目完成');
}
//人工组织栏目的数据
private function joinCatInfo($catName){
if ((int)$this->db->count("catname = '{$catName}' and parentid = '" .getCfg('catId')."' and siteid = '". getCfg('siteId'). "' ")){//如果已经增加过,就不再增加栏目
return echoJs("{$catName} 栏目已存在.跳过添加栏目");
}
//设置数组,人工设定
$setting = array (
'workflowid' => '',
'ishtml' => getCfg('cat2Htm'),
'content_ishtml' => getCfg('content2Html'),
'create_to_html_root' => getCfg('catHtm2root'),
'template_list' => 'default',
'category_template' => getCfg('catTpl'),//栏目模板
'list_template' => getCfg('listTpl'),//列表模板
'show_template' => getCfg('showTpl'),//页面模板
'meta_title' => '',
'meta_keywords' => '',
'meta_description' => '',
'presentpoint' => '1',
'defaultchargepoint' => '0',
'paytype' => '0',
'repeatchargedays' => '1',
'category_ruleid' => getCfg('catUrlRuleId'),
'show_ruleid' => getCfg('contentUrlRuleId'),
);
//==============设置结束==================
pc_base::load_sys_func('iconv');
$enCatDir = iconv('utf-8','gbk',$catName);//把utf8转gb才能取拼音
$enCatDir = gbk_to_pinyin($catName);
$enCatDir = preg_replace("/\W/", '_', $enCatDir);//移除非法目录字符
$enCatDir = strtolower(implode('', $enCatDir));
$insert = array();
$insert['siteid'] = (int)getCfg('siteId');
$insert['module'] = 'content';
$insert['type'] = 0;
$insert['modelid'] = getCfg('catMId');//模型id
$insert['parentid'] = getCfg('catId');//工具的id,上级
$insert['arrparentid'] = implode(',', array('0', getCfg('catPId'), getCfg('catId')) );//父级id路径
$insert['child'] = 0;//无下级
$insert['description'] = '';
$insert['image'] = '';
$insert['catname'] = $catName;
$insert['parentdir'] = implode('/', array(getCfg('catPDir'), getCfg('catDir'))) .'/';
$insert['catdir'] = $enCatDir;
$insert['url'] = '/'. implode('/', array('html', getCfg('catPDir'), getCfg('catDir'), $enCatDir)) .'/';
$insert['ismenu'] = getCfg('isMenu');
$insert['letter'] = $enCatDir;
$insert['sethtml'] = getCfg('catHtm2root');//$setting['create_to_html_root']; 是否生成到根目录
$insert['setting'] = array2string($setting);
$catid = $this->db->insert($insert, true);
$this->update_priv($catid, getCfg('privRoleid'));
$this->update_priv($catid, getCfg('privGroupid'),0);
echoJs('完成栏目添加:' .$catName);
}
/**
* 更新权限
* @param $catid
* @param $priv_datas
* @param $is_admin
*/
private function update_priv($catid,$priv_datas,$is_admin = 1) {
$this->priv_db = pc_base::load_model('category_priv_model');
$this->priv_db->delete(array('catid'=>$catid,'is_admin'=>$is_admin));
if(is_array($priv_datas) && !empty($priv_datas)) {
foreach ($priv_datas as $r) {
$r = explode(',', $r);
$action = $r[0];
$roleid = $r[1];
$this->priv_db->insert(array('catid'=>$catid,'roleid'=>$roleid,'is_admin'=>$is_admin,'action'=>$action,'siteid'=>getCfg('siteId')));
}
}
}
/**
* 更新缓存
*/
public function cache() {
$categorys = array();
$models = getcache('model','commons');
foreach ($models as $modelid=>$model) {
$datas = $this->db->select(array('modelid'=>$modelid),'catid,type,items',10000);
$array = array();
foreach ($datas as $r) {
if($r['type']==0) $array[$r['catid']] = $r['items'];
}
setcache('category_items_'.$modelid, $array,'commons');
}
$array = array();
$categorys = $this->db->select('`module`=\'content\'','catid,siteid',20000,'listorder ASC');
foreach ($categorys as $r) {
$array[$r['catid']] = $r['siteid'];
}
setcache('category_content',$array,'commons');
$categorys = $this->categorys = array();
$this->categorys = $this->db->select(array('siteid'=>getCfg('siteId'), 'module'=>'content'),'*',10000,'listorder ASC');
foreach($this->categorys as $r) {
unset($r['module']);
$setting = string2array($r['setting']);
$r['create_to_html_root'] = $setting['create_to_html_root'];
$r['ishtml'] = $setting['ishtml'];
$r['content_ishtml'] = $setting['content_ishtml'];
$r['category_ruleid'] = $setting['category_ruleid'];
$r['show_ruleid'] = $setting['show_ruleid'];
$r['workflowid'] = $setting['workflowid'];
$r['isdomain'] = '0';
if(!preg_match('/^(http|https):\/\//', $r['url'])) {
$r['url'] = siteurl($r['siteid']).$r['url'];
} elseif ($r['ishtml']) {
$r['isdomain'] = '1';
}
$categorys[$r['catid']] = $r;
}
setcache('category_content_'.getCfg('siteId'),$categorys,'commons');
return true;
}
}
相关文章推荐
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- 分享一个小东西,自动到指定的地方下载动态版本的命令,然后根据配置循环执行调用.
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- eclipse中配置mybatis框架的自动提示
- 一个用C#编写的自动读写配置文件的开源组件--V1.0.2版本
- 如何开发一个java开源框架-----Jvn框架之实现自动生成在线文档(第七讲)
- Java日志框架——一个经典的Logback配置实例
- Android 下拉刷新框架实现、仿新浪微博、QQ好友动态滑到底部自动加载
- 配置ssm框架遇到的一个坑(<context:property-placeholder>占位符配置失效)
- 一个通用的单元测试框架的思考和设计04-实现篇-自动管理测试数据
- 关于PHP-Zend framework2 框架 学习过程。 阅前须知: ZF2中的配置文件是可以静态文件配置来注册和通过相关函数动态注册。 1.EventManager(事件驱动),关于事件驱动,在ZF2相关资料没有详细说明,可以参考ANDROID的事件驱动,MFC的消息响应/事件驱动。
- 《转》struts2动态方法配置 Action,使一个Action可处理多请求
- 练习springMVC的一个小例子,为了方便以后继续学习springmvc和别的框架整合
- Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块