给dedeCMS自定义模型添加图片集字段
1、先找到dedecms图片集模型的templets生成图片集的html代码(album_add.htm)
<tr>
<td height="24" bgcolor="#F9FCEF" class="bline2"> <strong>图集选项:</strong></td>
</tr>
<tr>
<td height="24" colspan="4" class="bline" style="padding-top:6px">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="80"> <b>表现方式:</b></td>
<td>
<input name="pagestyle" class="np" type="radio" id="pagestyle1" onClick="checkMuList()" value="1"<?php if($cfg_album_style==1) echo " checked='checked'"; ?> />
<img src='images/alb2.gif' alt='单页多图样式' title='单页多图样式' />
<input name="pagestyle" class="np" type="radio" id="pagestyle2" onClick="checkMuList()" value="2"<?php if($cfg_album_style==2) echo " checked='checked'"; ?> />
<img src='images/alb1.gif' alt='幻灯片样式' title='幻灯片样式' />
<input name="pagestyle" class="np" type="radio" id="pagestyle3" onClick="checkMuList()" value="3"<?php if($cfg_album_style==3) echo " checked='checked'"; ?>/>
<img src='images/alb3.gif' alt='多缩略图样式' title='多缩略图样式' />
</td>
</tr>
</table></td>
</tr>
<tr id='cfgmulist' style='display:none'>
<td height="24" colspan="4" class="bline">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="26"> 多列式参数:</td>
<td>行
<input name="row" type="text" id="row" value="<?php echo $cfg_album_row; ?>" size="6">
列
<input name="col" type="text" id="col" value="<?php echo $cfg_album_col; ?>" size="6">
缩略图宽度限制:
<input name="ddmaxwidth" type="text" id="ddmaxwidth" value="<?php echo $cfg_album_ddwidth; ?>" size="6">
像素</td>
</tr>
<tr>
<td width="80" height="26"> </td>
<td><strong>(系统仅是每页输出"行x列"张图片,实现需自行控制样式表)</strong></td>
</tr>
</table></td>
</tr>
<tr id='spagelist' style='display:none'>
<td height="24" colspan="4" class="bline">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="95"> 每页图片数:</td>
<td>
<input name='pagepicnum' type='text' id='pagpicnum' size='10' value='<?php echo $cfg_album_pagesize; ?>' />
单页多图显示需要设置此参数,这种模式下系统不会为每张图单独生成缩略图。
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="24" colspan="4" class="bline"><table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="90"> <b>上传方式:</b></td>
<td>
<input name="isrm" class="np" type="hidden" id="isrm" value="1" />
<input name="byhand" class="np" type="checkbox" id="byhand" value="1" disabled="ture" checked />
手工上传
<input name="formzip" class="np" type="checkbox" id="formzip" value="1" onClick="showZipField(this,'zipff','htmlfield');" />
从ZIP压缩包中解压图片
<input name="formhtml" class="np" type="checkbox" id="formhtml" value="1" onClick="showHtmlField(this,'htmlfield','zipff');" />
网上复制图片
</td>
</tr>
</table></td>
</tr>
<tr id="zipff" style="display:none">
<td colspan="4" class="bline">
<table width="800" border="0" cellspacing="0" cellpadding="0" height="100">
<tr>
<td width="90"> 压缩包文件:</td>
<td><input type='text' name='zipfile' id='zipfile' style='width:300px'>
<input name='addon_bt' type='button' class='inputbut' value='浏览...' onClick="SelectSoft('form1.zipfile')">
<input name="delzip" type="checkbox" id="delzip" value="1" checked="checked" />
处理后删除压缩文件 </td>
</tr>
</table></td>
</tr>
<tr id="htmlfield" style="display:none">
<td height="24" colspan="4" class="bline"><table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="90"> 网上复制:</td>
<td> 复制的源网址:
<input name='copysource' type='text' value='http://' style='width:250px'/>
(防盗链网站需填写您复制图片的其中一个网页网址) <br />
把图片从别的网页复制,在这里用Ctrl+V粘贴,提交后程序可直接获取这个区域的所有图片
<input type="button" name="button" id="button" value="预览" onClick="LoadTestDiv()">
<br />
<div id='copyhtml' style='margin-top:6px;margin-bottom:6px;border:solid 1px #CCCCCC;width:96%;padding:6px;height:250px;width:100%' contenteditable="true">粘贴到这里...</div> </td>
</tr>
</table></td>
</tr>
<tr>
<td width="100%" height="24" colspan="4" class="bline">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="80" height="40"> <b>手工上传:</b></td>
<td>
<div style="display: inline; border: solid 1px #7FAAFF; padding: 2px;">
<span id="spanButtonPlaceholder"></span>
</div>
</td>
</tr>
</table></td>
</tr>
<tr id="handfield">
<td colspan="4" class="bline" style="background:url(images/albviewbg.gif) #fff 0 20px no-repeat;">
<table width='100%' height='160' style="margin:0 0 20px 0">
<tr>
<td>
<div id="divFileProgressContainer" style="height:75px;"></div>
<div id="thumbnails"></div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="24" colspan="4">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="90"> 图集内容:</td>
<td> <?php GetEditor("body","",450,"Diy"); ?></td>
</tr>
</table>
</td>
</tr>
将html代码放入自定义模型关联文档templets,然后一个一个解决关联后台数据库的问题
上传方式选择效果失效(js问题无疑)
查看了下调试器报错
showZipField is not defined
解决办法:自然就是找到相应js文件并且引入->album.js
手工上传行显示不正常(应该是读取后台信息出错)
查看源代码,发现该处引用了一个swfUpload的object,推测是先找到执行函数然后引入swfUpload的js文件
执行函数在album_add.htm处找到
window.onload = function () { swfu = new SWFUpload( { // Backend Settings upload_url: "swfupload.php", post_params: {"PHPSESSID": "<?php echo session_id(); ?>", "dopost" : ""}, // File Upload Settings file_size_limit : "2 MB", // 2MB file_types : "*.jpg; *.gif; *.png", file_types_description : "选择 JPEG/GIF/PNG 格式图片", file_upload_limit : "0", file_queue_error_handler : fileQueueError, file_dialog_complete_handler : fileDialogComplete, upload_progress_handler : uploadProgress, upload_error_handler : uploadError, upload_success_handler : uploadSuccess, upload_complete_handler : uploadComplete, button_image_url : "../images/SmallSpyGlassWithTransperancy_17x18.png", button_placeholder_id : "spanButtonPlaceholder", button_width: 250, button_height: 18, button_text : '<span class="button">选择本地图片 <span class="buttonSmall">(单图最大为 2 MB,支持多选)</span></span>', button_text_style : '.button { font-family: Helvetica, Arial, sans-serif; font-size: 12pt; } .buttonSmall { font-size: 10pt; }', button_text_top_padding: 0, button_text_left_padding: 18, button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT, button_cursor: SWFUpload.CURSOR.HAND, // Flash Settings flash_url : "../images/swfupload/swfupload.swf", custom_settings : { upload_target : "divFileProgressContainer" }, // Debug Settings debug: false }); };
将执行函数和库文件引入之后报错
fileQueueError is not defined
继续找在handle.js里面有定义 直接引入该文件
引入该文件后,内容显示出来了但是样式不正常,控制台里面没有报错
查看样式来源swfUpload.css文件,直接引用,显示正常,问题解决,继续找下一个问题。
好像前端显示与图片集基本一致,没有什么问题,那就进入后台逻辑修改。
2、完善图片集上传逻辑
后台上传主要涉及到
-
- 文件上传,保存在什么地方
- 数据库保存,写入在什么表什么字段
首先分析图片集模型的上传流程
我本机图片集上传就有问题。。。。弹出了302和Error #2038alert框,先解决该问题吧
首先找alert框出处,在代码中没找到,因为是swfUpload flash插件,所以直接百度了,第一个推测原因是因为没有带上sessionID造成的,直接在post参数里面带上sessionID(找到上传成功时候参数形式)
post_params: {"PHPSESSID": "<?php echo session_id(); ?>", "dopost" : ""},
在参数设置里面直接找到了这个参数..说明不是这个问题,找其他原因吧
https://www.genban.org/news/dedecms-7582.html这里出现一种解决办法,增加http GET获取session的方式
if (isset($_POST["PHPSESSID"])) { session_id($_POST["PHPSESSID"]); } else if(isset($_GET["PHPSESSID"])) { session_id($_GET["PHPSESSID"]); }
这里先留个坑吧。。。。。
先回到图片集模型的上传流程吧
两个问题:1、图片保存在uploads 文件夹 allimg里面
2、数据保存在dede_addimg数据表里面
先解决图片上传问题(上传直接没问题),那就剩数据保存问题了。
先给自定义模型添加图集相关字段pagestyle,maxwidth,imgurls,row,col,isrm,ddmaxwidth,pagepicnum这里也涉及一个问题,怎么添加这些字段呢?如果在模型字段里面手动添加的话,添加文章页面就会渲染这些字段,当然这不是我想要的,所以 先把不需要出现的属性直接暴力添加到数据表里吧
添加好数据表字段之后,就是获取和插入数据了
还是先分析原逻辑,找到这些字段的获取处理和插入操作
<?php /** * 文档发布 * * @version $Id: archives_add.php 1 8:26 2010年7月12日Z tianya $ * @package DedeCMS.Administrator * @copyright Copyright (c) 2007 - 2010, DesDev, Inc. * @license http://help.dedecms.com/usersguide/license.html * @link http://www.dedecms.com */ require_once(dirname(__FILE__).'/config.php'); CheckPurview('a_New,a_AccNew'); require_once(DEDEINC.'/customfields.func.php'); require_once(DEDEADMIN.'/inc/inc_archives_functions.php'); if(empty($dopost)) $dopost = ''; if($dopost != 'save') { require_once(DEDEINC.'/dedetag.class.php'); require_once(DEDEADMIN.'/inc/inc_catalog_options.php'); ClearMyAddon(); $channelid = empty($channelid) ? 0 : intval($channelid); $cid = empty($cid) ? 0 : intval($cid); //获得频道模型ID if($cid > 0 && $channelid == 0) { $row = $dsql->GetOne("SELECT channeltype FROM `#@__arctype` WHERE id='$cid'; "); $channelid = $row['channeltype']; } else { if($channelid==0) { ShowMsg('无法识别模型信息,因此无法操作!', '-1'); exit(); } } //获得频道模型信息 $cInfos = $dsql->GetOne(" SELECT * FROM `#@__channeltype` WHERE id='$channelid' "); $channelid = $cInfos['id']; //获取文章最大id以确定当前权重 $maxWright = $dsql->GetOne("SELECT COUNT(*) AS cc FROM #@__archives"); include DedeInclude('templets/archives_add.htm'); exit(); } /*-------------------------------- function __save(){ } -------------------------------*/ else if($dopost=='save') { require_once(DEDEINC.'/image.func.php'); require_once(DEDEINC.'/oxwindow.class.php'); $flag = isset($flags) ? join(',',$flags) : ''; $notpost = isset($notpost) && $notpost == 1 ? 1: 0; if(empty($click)) $click = ($cfg_arc_click=='-1' ? mt_rand(50, 200) : $cfg_arc_click); if(empty($typeid2)) $typeid2 = 0; if(!isset($autokey)) $autokey = 0; if(!isset($remote)) $remote = 0; if(!isset($dellink)) $dellink = 0; if(!isset($autolitpic)) $autolitpic = 0; // 图集相关--------------------- if(!isset($formhtml)) $formhtml = 0; if(!isset($formzip)) $formzip = 0; if(!isset($ddisfirst)) $ddisfirst = 0; if(!isset($delzip)) $delzip = 0; // 图集相关--------------------- if(empty($click)) $click = ($cfg_arc_click=='-1' ? mt_rand(50, 200) : $cfg_arc_click); if($typeid==0) { ShowMsg('请指定文档的栏目!', '-1'); exit(); } if(empty($channelid)) { ShowMsg('文档为非指定的类型,请检查你发布内容的表单是否合法!', '-1'); exit(); } if(!CheckChannel($typeid,$channelid) ) { ShowMsg('你所选择的栏目与当前模型不相符,请选择白色的选项!', '-1'); exit(); } if(!TestPurview('a_New')) { CheckCatalog($typeid, "对不起,你没有操作栏目 {$typeid} 的权限!"); } //对保存的内容进行处理 if(empty($writer)) $writer = $cuserLogin->getUserName(); if(empty($source)) $source = '未知'; $pubdate = GetMkTime($pubdate); $senddate = time(); $sortrank = AddDay($pubdate,$sortup); $ismake = $ishtml == 0 ? -1 : 0; $title = preg_replace("#\"#", '"', $title); $title = cn_substrR($title,$cfg_title_maxlen); $shorttitle = cn_substrR($shorttitle,36); $color = cn_substrR($color,7); $writer = cn_substrR($writer,20); $source = cn_substrR($source,30); $description = cn_substrR($description,$cfg_auot_description); $keywords = cn_substrR($keywords,60); $filename = trim(cn_substrR($filename,40)); $userip = GetIP(); $isremote = (empty($isremote)? 0 : $isremote); $voteid = (empty($voteid)? 0 : $voteid); $serviterm=empty($serviterm)? "" : $serviterm; if(!TestPurview('a_Check,a_AccCheck,a_MyCheck')) { $arcrank = -1; } $adminid = $cuserLogin->getUserID(); //处理上传的缩略图 if(empty($ddisremote)) { $ddisremote = 0; } $litpic = GetDDImage('none',$picname,$ddisremote); // 图集相关--------------------- //使用第一张图作为缩略图 if($ddisfirst==1 && $litpic=='') { if(isset($imgurl1)) { $litpic = GetDDImage('ddfirst', $imgurl1, $isrm); } } // 图集相关--------------------- //生成文档ID $arcID = GetIndexKey($arcrank,$typeid,$sortrank,$channelid,$senddate,$adminid); if(empty($arcID)) { ShowMsg("无法获得主键,因此无法进行后续操作!","-1"); exit(); } // 图集相关--------------------- $imgurls = "{dede:pagestyle maxwidth='$maxwidth' pagepicnum='$pagepicnum' ddmaxwidth='$ddmaxwidth' row='$row' col='$col' value='$pagestyle'/}\r\n"; $hasone = FALSE; //处理并保存从网上复制的图片 /*--------------------- function _getformhtml() ------------------*/ if($formhtml==1) { $imagebody = stripslashes($imagebody); $imgurls .= GetCurContentAlbum($imagebody,$copysource,$litpicname); if($ddisfirst==1 && $litpic=='' && !empty($litpicname)) { $litpic = $litpicname; $hasone = TRUE; } } /*--------------------- function _getformzip() 处理从ZIP中解压的图片 ---------------------*/ if($formzip==1) { include_once(DEDEINC."/zip.class.php"); include_once(DEDEADMIN."/file_class.php"); $zipfile = $cfg_basedir.str_replace($cfg_mainsite,'',$zipfile); $tmpzipdir = DEDEDATA.'/ziptmp/'.cn_substr(md5(ExecTime()),16); $ntime = time(); if(file_exists($zipfile)) { @mkdir($tmpzipdir,$GLOBALS['cfg_dir_purview']); @chmod($tmpzipdir,$GLOBALS['cfg_dir_purview']); $z = new zip(); $z->ExtractAll($zipfile,$tmpzipdir); $fm = new FileManagement(); $imgs = array(); $fm->GetMatchFiles($tmpzipdir,"jpg|png|gif",$imgs); $i = 0; foreach($imgs as $imgold) { $i++; $savepath = $cfg_image_dir."/".MyDate("Y-m",$ntime); CreateDir($savepath); $iurl = $savepath."/".MyDate("d",$ntime).dd2char(MyDate("His",$ntime).'-'.$adminid."-{$i}".mt_rand(1000,9999)); $iurl = $iurl.substr($imgold,-4,4); $imgfile = $cfg_basedir.$iurl; copy($imgold,$imgfile); unlink($imgold); if(is_file($imgfile)) { $litpicname = $pagestyle > 2 ? GetImageMapDD($iurl,$cfg_ddimg_width) : $iurl; //指定了提取第一张为缩略图的情况强制使用第一张缩略图 if($i=='1') { if(!$hasone && $ddisfirst==1 && $litpic=='' && empty($litpicname)) { $litpicname = GetImageMapDD($iurl,$cfg_ddimg_width); } } $info = ''; $imginfos = GetImageSize($imgfile,$info); $imgurls .= "{dede:img ddimg='$litpicname' text='' width='".$imginfos[0]."' height='".$imginfos[1]."'} $iurl {/dede:img}\r\n"; //把图片信息保存到媒体文档管理档案中 $inquery = " INSERT INTO #@__uploads(title,url,mediatype,width,height,playtime,filesize,uptime,mid) VALUES ('{$title}','{$iurl}','1','".$imginfos[0]."','".$imginfos[1]."','0','".filesize($imgfile)."','".$ntime."','$adminid'); "; $dsql->ExecuteNoneQuery($inquery); $fid = $dsql->GetLastID(); AddMyAddon($fid, $iurl); WaterImg($imgfile, 'up'); if(!$hasone && $ddisfirst==1 && $litpic=='') { if(empty($litpicname)) { $litpicname = $iurl; $litpicname = GetImageMapDD($iurl, $cfg_ddimg_width); } $litpic = $litpicname; $hasone = TRUE; } } } if($delzip==1) unlink($zipfile); $fm->RmDirFiles($tmpzipdir); } } /*--------------------- function _getformupload() 通过swfupload正常上传的图片 ---------------------*/ if(is_array($_SESSION['bigfile_info'])) { foreach($_SESSION['bigfile_info'] as $k=>$v) { $truefile = $cfg_basedir.$v; if(strlen($v)<2 || !file_exists($truefile)) continue; $info = ''; $imginfos = GetImageSize($truefile, $info); $litpicname = $pagestyle > 2 ? GetImageMapDD($v, $cfg_ddimg_width) : ''; if(!$hasone && $ddisfirst==1 && $litpic=='') { $litpic = empty($litpicname) ? GetImageMapDD($v, $cfg_ddimg_width) : $litpicname; $hasone = TRUE; } $imginfo = !empty(${'picinfook'.$k}) ? ${'picinfook'.$k} : ''; $imgurls .= "{dede:img ddimg='$v' text='$imginfo' width='".$imginfos[0]."' height='".$imginfos[1]."'} $v {/dede:img}\r\n"; } } $imgurls = addslashes($imgurls); //处理body字段自动摘要、自动提取缩略图等 $body = AnalyseHtmlBody($body,$description,$litpic,$keywords,'htmltext'); // 图集相关--------------------- //分析处理附加表数据 $inadd_f = $inadd_v = ''; if(!empty($dede_addonfields)) { $addonfields = explode(';', $dede_addonfields); if(is_array($addonfields)) { foreach($addonfields as $v) { if($v=='') continue; $vs = explode(',', $v); if($vs[1]=='htmltext' || $vs[1]=='textdata') { ${$vs[0]} = AnalyseHtmlBody(${$vs[0]}, $description, $litpic, $keywords, $vs[1]); } else { if(!isset(${$vs[0]})) ${$vs[0]} = ''; ${$vs[0]} = GetFieldValueA(${$vs[0]}, $vs[1], $arcID); } $inadd_f .= ','.$vs[0]; $inadd_v .= " ,'".${$vs[0]}."' "; } } } //处理图片文档的自定义属性 if($litpic!='' && !preg_match("#p#", $flag)) { $flag = ($flag=='' ? 'p' : $flag.',p'); } if($redirecturl!='' && !preg_match("#j#", $flag)) { $flag = ($flag=='' ? 'j' : $flag.',j'); } //跳转网址的文档强制为动态 if(preg_match("#j#", $flag)) $ismake = -1; //保存到主表 $query = "INSERT INTO `#@__archives`(id,typeid,typeid2,sortrank,flag,ismake,channel,arcrank,click,money,title,shorttitle, color,writer,source,litpic,pubdate,senddate,mid,voteid,notpost,description,keywords,filename,dutyadmin,weight) VALUES ('$arcID','$typeid','$typeid2','$sortrank','$flag','$ismake','$channelid','$arcrank','$click','$money','$title','$shorttitle', '$color','$writer','$source','$litpic','$pubdate','$senddate','$adminid','$voteid','$notpost','$description','$keywords','$filename','$adminid','$weight');"; if(!$dsql->ExecuteNoneQuery($query)) { $gerr = $dsql->GetError(); $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$arcID'"); ShowMsg("把数据保存到数据库主表 `#@__archives` 时出错,请把相关信息提交给DedeCms官方。".str_replace('"','',$gerr),"javascript:;"); exit(); } //保存到附加表 $cts = $dsql->GetOne("SELECT addtable FROM `#@__channeltype` WHERE id='$channelid' "); $addtable = trim($cts['addtable']); if(!empty($addtable)) { $useip = GetIP(); // $query = "INSERT INTO `{$addtable}`(aid,typeid,redirecturl,userip{$inadd_f}) Values('$arcID','$typeid','$redirecturl','$useip'{$inadd_v})"; // 图集相关--------------------- $query = "INSERT INTO `{$addtable}`(aid,typeid,redirecturl,userip{$inadd_f},pagestyle,maxwidth,imgurls,row,col,isrm,ddmaxwidth,pagepicnum) Values('$arcID','$typeid','$redirecturl','$useip'{$inadd_v},'$pagestyle','$maxwidth','$imgurls','$row','$col','$isrm','$ddmaxwidth','$pagepicnum')"; // 图集相关--------------------- if(!$dsql->ExecuteNoneQuery($query)) { $gerr = $dsql->GetError(); $dsql->ExecuteNoneQuery("DELETE FROM `#@__archives` WHERE id='$arcID'"); $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$arcID'"); ShowMsg("把数据保存到数据库附加表 `{$addtable}` 时出错,请把相关信息提交给DedeCms官方。".str_replace('"','',$gerr),"javascript:;"); exit(); } } //生成HTML InsertTags($tags, $arcID); if($cfg_remote_site=='Y' && $isremote=="1") { if($serviterm!="") { list($servurl,$servuser,$servpwd) = explode(',',$serviterm); $config=array( 'hostname' => $servurl, 'username' => $servuser, 'password' => $servpwd,'debug' => 'TRUE'); } else { $config=array(); } if(!$ftp->connect($config)) exit('Error:None FTP Connection!'); } $artUrl = MakeArt($arcID, true, true,$isremote); if($artUrl=='') { $artUrl = $cfg_phpurl."/view.php?aid=$arcID"; } ClearMyAddon($arcID, $title); //返回成功信息 $msg = " 请选择你的后续操作: <a href='archives_add.php?cid=$typeid'><u>继续发布文档</u></a> <a href='$artUrl' target='_blank'><u>查看文档</u></a> <a href='archives_do.php?aid=".$arcID."&dopost=editArchives'><u>更改文档</u></a> <a href='catalog_do.php?cid=$typeid&dopost=listArchives'><u>已发布文档管理</u></a> $backurl "; $msg = "<div style=\"line-height:36px;height:36px\">{$msg}</div>".GetUpdateTest(); $wintitle = '成功发布文档!'; $wecome_info = '文档管理::发布文档'; $win = new OxWindow(); $win->AddTitle('成功发布文档:'); $win->AddMsgItem($msg); $winform = $win->GetWindow('hand', ' ', false); $win->Display(); }
还是贴整个代码吧修改的部分在注释(// 图集相关---------------------)之间
查询数据库,发现上传成功。
后面的一些优化就根据自己的需求进行调整了!