📄 正在查看:admin/control/product_control.class.php
1<?php
2/**
3 * (C)2012-2014 twcms.com TongWang Inc.
4 * Author: wuzhaohuan <kongphp@gmail.com>
5 */
6
7defined('TWCMS_PATH') or exit;
8
9class product_control extends admin_control {
10 // 内容管理
11 public function index() {
12 // hook admin_product_control_index_before.php
13
14 $cid = intval(R('cid'));
15 $keyword = empty($_POST) ? R('keyword') : R('keyword', 'P');
16 if($keyword) {
17 $keyword = urldecode($keyword);
18 $keyword = safe_str($keyword);
19 }
20 $this->assign('keyword', $keyword);
21
22 // 获取分类下拉框
23 $cidhtml = $this->category->get_cidhtml_by_mid(3, $cid, '所有产品');
24 $this->assign('cidhtml', $cidhtml);
25
26 // 初始模型表名
27 $this->cms_content->table = 'cms_product';
28
29 // 初始分页
30 $pagenum = 20;
31 if($keyword) {
32 $where = array('title'=>array('LIKE'=>$keyword));
33 $total = $this->cms_content->find_count($where);
34 $urlstr = '-keyword-'.urlencode($keyword);
35 }elseif($cid) {
36 $where = array('cid' => $cid);
37 $categorys = $this->category->read($cid);
38 $total = isset($categorys['count']) ? $categorys['count'] : 0;
39 $urlstr = '-cid-'.$cid;
40 }else{
41 $where = array();
42 $total = $this->cms_content->count();
43 $urlstr = '';
44 }
45 $maxpage = max(1, ceil($total/$pagenum));
46 $page = min($maxpage, max(1, intval(R('page'))));
47 $pages = pages($page, $maxpage, 'index.php?u=product-index'.$urlstr.'-page-{page}');
48 $this->assign('total', $total);
49 $this->assign('pages', $pages);
50
51 // 读取内容列表
52 $flag_arr = array(1=>'推荐', 2=>'热点', 3=>'头条', 4=>'精选', 5=>'幻灯');
53 $cms_product_arr = $this->cms_content->list_arr($where, 'id', -1, ($page-1)*$pagenum, $pagenum, $total);
54 foreach($cms_product_arr as &$v) {
55 $this->cms_content->format($v, 2);
56
57 // 属性
58 $v['flagstr'] = '';
59 if(!empty($v['imagenum'])) {
60 $v['flagstr'] .= ' [图片]';
61 }
62 if(!empty($v['flags'])) {
63 $flags = explode(',', $v['flags']);
64 foreach($flags as $flag) {
65 $flag = intval($flag);
66 if($flag) $v['flagstr'] .= ' ['.$flag_arr[$flag].']';
67 }
68 }
69 if($v['flagstr']) $v['flagstr'] = '<font color="BC0B0B">'.$v['flagstr'].'</font>';
70 }
71 $this->assign('cms_product_arr', $cms_product_arr);
72
73 // hook admin_product_control_index_after.php
74
75 $this->display();
76 }
77
78 // 发布产品
79 public function add() {
80 // hook admin_product_control_add_before.php
81
82 $uid = $this->_user['uid'];
83 if(empty($_POST)) {
84 $this->_pkey = 'content';
85 $this->_ukey = 'product-add';
86 $this->_title = '发布产品';
87 $this->_place = '内容 » 内容管理 » 发布产品';
88
89 $habits = (array)$this->kv->get('user_habits_uid_'.$uid);
90 $cid = isset($habits['last_add_cid']) ? (int)$habits['last_add_cid'] : 0;
91
92 $data = $this->kv->get('auto_save_product_uid_'.$uid);
93 if($data) {
94 !empty($data['cid']) && $cid = $data['cid'];
95 $data['pic_src'] = empty($data['pic']) ? '../static/img/nopic.gif' : '../'.$data['pic'];
96 empty($data['author']) && $data['author'] = $this->_user['username'];
97 $data['flags'] = empty($data['flag']) ? array() : $data['flag'];
98 !empty($data['images']) && $data['images'] = (array)$data['images'];
99 $data['content'] = htmlspecialchars($data['content']);
100 }else{
101 $data['flags'] = array();
102 $data['pic_src'] = '../static/img/nopic.gif';
103 $data['author'] = $this->_user['username'];
104 $data['views'] = 0;
105 }
106 $this->assign('data', $data);
107
108 $cidhtml = $this->category->get_cidhtml_by_mid(3, $cid);
109 $this->assign('cidhtml', $cidhtml);
110
111 $edit_cid_id = '&mid=3';
112 $this->assign('edit_cid_id', $edit_cid_id);
113
114 $this->display('product_set.htm');
115 }else{
116 $cid = intval(R('cid', 'P'));
117 $title = trim(strip_tags(R('title', 'P')));
118 $alias = trim(R('alias', 'P'));
119 $flags = (array)R('flag', 'P');
120 $views = intval(R('views', 'P'));
121 $images = (array)R('images', 'P');
122 $contentstr = trim(R('content', 'P'));
123 $intro = trim(R('intro', 'P'));
124 $dateline = trim(R('dateline', 'P'));
125 $isremote = intval(R('isremote', 'P'));
126 $pic = trim(R('pic', 'P'));
127
128 empty($cid) && E(1, '亲,您没有选择分类哦!');
129 empty($title) && E(1, '亲,您的标题忘了填哦!');
130 empty($images) && E(1, '亲,您的产品忘上传图片了!');
131 if(strlen($contentstr) < 5) E(1, '亲,您的内容字数太少了哦!');
132
133 $categorys = $this->category->read($cid);
134 if(empty($categorys)) E(1, '分类ID不存在!');
135
136 $mid = $this->category->get_mid_by_cid($cid);
137 $table = $this->models->get_table($mid);
138
139 // 防止提交到其他模型的分类
140 if($table != 'product') E(1, '分类ID非法!');
141
142 // 检测别名是否能用
143 if($alias && $err_msg = $this->only_alias->check_alias($alias)) {
144 E(1, $err_msg);
145 }
146
147 // 标签预处理,最多支持5个标签
148 $tags = trim(R('tags', 'P'), ", \t\n\r\0\x0B");
149 $tags_arr = explode(',', $tags);
150 $this->cms_content_tag->table = 'cms_'.$table.'_tag';
151 $tagdatas = $tags = array();
152 for($i=0; isset($tags_arr[$i]) && $i<5; $i++) {
153 $name = trim($tags_arr[$i]);
154 if($name) {
155 $tagdata = $this->cms_content_tag->find_fetch(array('name'=>$name), array(), 0, 1);
156 if($tagdata) {
157 $tagdata = current($tagdata);
158 }else{
159 $tagid = $this->cms_content_tag->create(array('name'=>$name, 'count'=>0, 'content'=>''));
160 if(!$tagid) E(1, '写入标签表出错');
161 $tagdata = $this->cms_content_tag->get($tagid);
162 }
163
164 $tagdata['count']++;
165 $tagdatas[] = $tagdata;
166 $tags[$tagdata['tagid']] = $tagdata['name'];
167 }
168 }
169
170 // 远程图片本地化
171 $endstr = '';
172 $this->cms_content_attach->table = 'cms_'.$table.'_attach';
173 if($isremote) {
174 $endstr .= $this->get_remote_img($table, $contentstr, $uid);
175 }
176
177 // 计算图片数,和非图片文件数
178 $imagenum = $this->cms_content_attach->find_count(array('id'=>0, 'uid'=>$uid, 'isimage'=>1));
179 $filenum = $this->cms_content_attach->find_count(array('id'=>0, 'uid'=>$uid, 'isimage'=>0));
180
181 // 如果缩略图为空,并且内容含有图片,则将第一张图片设置为缩略图
182 if(empty($pic) && $imagenum) {
183 $pic = $this->auto_pic($table, $uid);
184 }
185
186 // 如果摘要为空,自动生成摘要
187 $intro = $this->auto_intro($intro, $contentstr);
188
189 // 写入内容表
190 $data = array(
191 'cid' => $cid,
192 'title' => $title,
193 'color' => trim(R('color', 'P')),
194 'alias' => $alias,
195 'tags' => _json_encode($tags),
196 'intro' => $intro,
197 'pic' => $pic,
198 'uid' => $uid,
199 'author' => trim(R('author', 'P')), // 可以不等于发布用户
200 'source' => trim(R('source', 'P')),
201 'dateline' => $dateline ? strtotime($dateline) : $_ENV['_time'],
202 'lasttime' => $_ENV['_time'],
203 'ip' => ip2long($_ENV['_ip']),
204 'iscomment' => intval(R('iscomment', 'P')),
205 'comments' => 0,
206 'imagenum' => $imagenum,
207 'filenum' => $filenum,
208 'flags' => implode(',', $flags),
209 'seo_title' => trim(strip_tags(R('seo_title', 'P'))),
210 'seo_keywords' => trim(strip_tags(R('seo_keywords', 'P'))),
211 'seo_description' => trim(strip_tags(R('seo_description', 'P'))),
212 );
213 $this->cms_content->table = 'cms_'.$table;
214 $id = $this->cms_content->create($data);
215 if(!$id) {
216 E(1, '写入内容表出错');
217 }
218
219 // 写入全站唯一别名表
220 if($alias && !$this->only_alias->set($alias, array('mid' => $mid, 'cid' => $cid, 'id' => $id))) {
221 E(1, '写入全站唯一别名表出错');
222 }
223
224 // 写入内容数据表
225 $this->cms_content_data->table = 'cms_'.$table.'_data';
226 if(!$this->cms_content_data->set($id, array('content' => $contentstr, 'images' => json_encode($images)))) {
227 E(1, '写入内容数据表出错');
228 }
229
230 // 写入内容查看数表
231 $this->cms_content_views->table = 'cms_'.$table.'_views';
232 if(!$this->cms_content_views->set($id, array('cid' => $cid, 'views' => $views))) {
233 E(1, '写入内容查看数表出错');
234 }
235
236 // 更新附件归宿 cid 和 id
237 if($imagenum || $filenum) {
238 if(!$this->cms_content_attach->find_update(array('id'=>0, 'uid'=>$uid), array('cid'=>$cid, 'id'=>$id))) {
239 E(1, '更新内容附件表出错');
240 }
241 }
242
243 // 写入内容属性标记表
244 $this->cms_content_flag->table = 'cms_'.$table.'_flag';
245 foreach($flags as $flag) {
246 if(!$this->cms_content_flag->set(array($flag, $id), array('cid'=>$cid))) {
247 E(1, '写入内容属性标记表出错');
248 }
249 }
250
251 // 如果内容含有图片附件,则标记图片属性
252 if($imagenum && !$this->cms_content_flag->set(array(0, $id), array('cid'=>$cid))) {
253 E(1, '写入内容属性标记表出错');
254 }
255
256 // 写入内容标签表
257 $this->cms_content_tag_data->table = 'cms_'.$table.'_tag_data';
258 foreach($tagdatas as $tagdata) {
259 $this->cms_content_tag->update($tagdata);
260 $this->cms_content_tag_data->set(array($tagdata['tagid'], $id), array('id'=>$id));
261 }
262
263 // 更新用户发布的内容条数
264 $this->_user['contents']++;
265 $this->user->update($this->_user);
266
267 // 更新分类的内容条数
268 $categorys['count']++;
269 $this->category->update($categorys);
270 $this->category->update_cache($cid);
271
272 $data = $this->kv->delete('auto_save_product_uid_'.$uid);
273
274 // 记住最后一次发布的分类ID,感觉这样人性化一些吧。
275 $habits = (array) $this->kv->get('user_habits_uid_'.$uid);
276 $habits['last_add_cid'] = $cid;
277 $habits = $this->kv->set('user_habits_uid_'.$uid, $habits);
278
279 // hook admin_product_control_add_after.php
280
281 E(0, '发布完成'.$endstr);
282 }
283 }
284
285 // 编辑产品
286 public function edit() {
287 // hook admin_product_control_edit_before.php
288
289 if(empty($_POST)) {
290 $id = intval(R('id'));
291 $cid = intval(R('cid'));
292
293 $this->_pkey = 'content';
294 $this->_ukey = 'product-edit-cid-'.$cid.'-id-'.$id;
295 $this->_title = '编辑产品';
296 $this->_place = '内容 » 内容管理 » 编辑产品';
297
298 $cidhtml = $this->category->get_cidhtml_by_mid(3, $cid);
299 $this->assign('cidhtml', $cidhtml);
300
301 $table = 'product';
302
303 // 读取内容
304 $this->cms_content->table = 'cms_'.$table;
305 $this->cms_content_data->table = 'cms_'.$table.'_data';
306 $this->cms_content_views->table = 'cms_'.$table.'_views';
307 $data = $this->cms_content->get($id);
308 if(empty($data)) $this->message(0, '内容不存在!', -1);
309
310 $data2 = $this->cms_content_data->get($id);
311 $data3 = $this->cms_content_views->get($id);
312 $data = array_merge($data, $data2, $data3);
313 $data['images'] = (array)_json_decode($data['images']);
314 $data['content'] = htmlspecialchars($data['content']);
315 $data['tags'] = implode(',', (array)_json_decode($data['tags']));
316 $data['intro'] = str_replace('<br />', "\n", strip_tags($data['intro'], '<br>'));
317 $data['pic_src'] = empty($data['pic']) ? '../static/img/nopic.gif' : '../'.$data['pic'];
318 $data['flags'] = explode(',', $data['flags']);
319 $data['dateline'] = date('Y-m-d H:i:s', $data['dateline']);
320 $this->assign('data', $data);
321
322 $edit_cid_id = '&mid=3&cid='.$data['cid'].'&id='.$data['id'];
323 $this->assign('edit_cid_id', $edit_cid_id);
324
325 $this->display('product_set.htm');
326 }else{
327 $id = intval(R('id', 'P'));
328 $cid = intval(R('cid', 'P'));
329 $title = trim(strip_tags(R('title', 'P')));
330 $alias = trim(R('alias', 'P'));
331 $flags = (array)R('flag', 'P');
332 $views = intval(R('views', 'P'));
333 $images = (array)R('images', 'P');
334 $contentstr = trim(R('content', 'P'));
335 $intro = trim(R('intro', 'P'));
336 $isremote = intval(R('isremote', 'P'));
337 $pic = trim(R('pic', 'P'));
338 $uid = $this->_user['uid'];
339
340 empty($id) && E(1, 'ID不能为空!');
341 empty($cid) && E(1, '亲,您没有选择分类哦!');
342 empty($title) && E(1, '亲,您的标题忘了填哦!');
343 empty($images) && E(1, '亲,您的产品忘上传图片了!');
344 if(strlen($contentstr) < 5) E(1, '亲,您的内容字数太少了哦!');
345
346 $categorys = $this->category->read($cid);
347 if(empty($categorys)) E(1, '分类ID不存在!');
348
349 $mid = $this->category->get_mid_by_cid($cid);
350 $table = $this->models->get_table($mid);
351
352 // 防止提交到其他模型的分类
353 if($table != 'product') E(1, '分类ID非法!');
354
355 $this->cms_content->table = 'cms_'.$table;
356 $data = $this->cms_content->get($id);
357 if(empty($data)) E(1, '内容不存在!');
358
359 // 检测别名是否能用
360 $alias_old = $data['alias'];
361 if($alias && $alias != $alias_old && $err_msg = $this->only_alias->check_alias($alias)) {
362 E(1, $err_msg);
363 }
364
365 // 比较属性变化
366 $flags_old = array();
367 if($data['flags']) {
368 $flags_old = explode(',', $data['flags']);
369 foreach($flags as $flag) {
370 $key = array_search($flag, $flags_old);
371 if($key !== false) unset($flags_old[$key]);
372 }
373 }
374
375 // 比较标签变化
376 $tags = trim(R('tags', 'P'), ", \t\n\r\0\x0B");
377 $tags_new = explode(',', $tags);
378 $tags_old = (array)_json_decode($data['tags']);
379 $tags_arr = $tags = array();
380 foreach($tags_new as $tagname) {
381 $key = array_search($tagname, $tags_old);
382 if($key === false) {
383 $tags_arr[] = $tagname;
384 }else{
385 $tags[$key] = $tagname;
386 unset($tags_old[$key]);
387 }
388 }
389
390 // 标签预处理,最多支持5个标签
391 $this->cms_content_tag->table = 'cms_'.$table.'_tag';
392 $tagdatas = array();
393 for($i=0; isset($tags_arr[$i]) && $i<5; $i++) {
394 $name = trim($tags_arr[$i]);
395 if($name) {
396 $tagdata = $this->cms_content_tag->find_fetch(array('name'=>$name), array(), 0, 1);
397 if($tagdata) {
398 $tagdata = current($tagdata);
399 }else{
400 $tagid = $this->cms_content_tag->create(array('name'=>$name, 'count'=>0, 'content'=>''));
401 if(!$tagid) E(1, '写入标签表出错');
402 $tagdata = $this->cms_content_tag->get($tagid);
403 }
404
405 $tagdata['count']++;
406 $tagdatas[] = $tagdata;
407 $tags[$tagdata['tagid']] = $tagdata['name'];
408 }
409 }
410
411 // 远程图片本地化
412 $endstr = '';
413 $this->cms_content_attach->table = 'cms_'.$table.'_attach';
414 if($isremote) {
415 $endstr .= $this->get_remote_img($table, $contentstr, $uid, $cid, $id);
416 }
417
418 // 计算图片数,和非图片文件数
419 $imagenum = $this->cms_content_attach->find_count(array('id'=>$id, 'uid'=>$uid, 'isimage'=>1));
420 $filenum = $this->cms_content_attach->find_count(array('id'=>$id, 'uid'=>$uid, 'isimage'=>0));
421
422 // 如果缩略图为空,并且内容含有图片,则将第一张图片设置为缩略图
423 if(empty($pic) && $imagenum) {
424 $pic = $this->auto_pic($table, $uid, $id);
425 }
426
427 // 如果摘要为空,自动生成摘要
428 $intro = $this->auto_intro($intro, $contentstr);
429
430 // 如果分类ID发生变化,更新分类内容数
431 if($cid != $data['cid']) {
432 // 旧的分类内容数减1
433 $categorys_old = $this->category->read($data['cid']);
434 $categorys_old['count'] = max(0, $categorys_old['count']-1);
435 $this->category->update($categorys_old);
436
437 // 新的分类内容数加1
438 $categorys['count']++;
439 $this->category->update($categorys);
440
441 $this->category->delete_cache();
442 }
443
444 // 编辑时,别名有三种情况需要处理
445 if($alias && $alias_old && $alias != $alias_old) {
446 // 写入新别名
447 if(!$this->only_alias->set($alias, array('mid' => $mid, 'cid' => $cid, 'id' => $id))) {
448 E(1, '写入全站唯一别名表出错');
449 }
450
451 // 删除旧别名
452 if(!$this->only_alias->delete($alias_old)) {
453 E(1, '删除别名表数据时出错');
454 }
455 }elseif($alias && empty($alias_old)) {
456 // 写入新别名
457 if(!$this->only_alias->set($alias, array('mid' => $mid, 'cid' => $cid, 'id' => $id))) {
458 E(1, '写入全站唯一别名表出错');
459 }
460 }elseif(empty($alias) && $alias_old) {
461 // 删除旧别名
462 if(!$this->only_alias->delete($alias_old)) {
463 E(1, '删除别名表数据时出错');
464 }
465 }
466
467 // 写入内容表
468 $data['cid'] = $cid;
469 $data['id'] = $id;
470 $data['title'] = $title;
471 $data['color'] = trim(R('color', 'P'));
472 $data['alias'] = $alias;
473 $data['tags'] = _json_encode($tags);
474 $data['intro'] = $intro;
475 $data['pic'] = $pic;
476 $data['uid'] = $uid;
477 $data['author'] = trim(R('author', 'P')); // 可以不等于发布用户
478 $data['source'] = trim(R('source', 'P'));
479 $data['dateline'] = strtotime(trim(R('dateline', 'P')));
480 $data['lasttime'] = $_ENV['_time'];
481 $data['iscomment'] = intval(R('iscomment', 'P'));
482 $data['imagenum'] = $imagenum;
483 $data['filenum'] = $filenum;
484 $data['flags'] = implode(',', $flags);
485 $data['seo_title'] = trim(strip_tags(R('seo_title', 'P')));
486 $data['seo_keywords'] = trim(strip_tags(R('seo_keywords', 'P')));
487 $data['seo_description'] = trim(strip_tags(R('seo_description', 'P')));
488 if(!$this->cms_content->update($data)) {
489 E(1, '更新内容表出错');
490 }
491
492 // 写入内容数据表
493 $this->cms_content_data->table = 'cms_'.$table.'_data';
494 if(!$this->cms_content_data->set($id, array('content' => $contentstr, 'images' => json_encode($images)))) {
495 E(1, '写入内容数据表出错');
496 }
497
498 // 写入内容查看数表
499 $this->cms_content_views->table = 'cms_'.$table.'_views';
500 if(!$this->cms_content_views->set($id, array('cid' => $cid, 'views' => $views))) {
501 E(1, '写入内容查看数表出错');
502 }
503
504 // 写入内容属性标记表
505 $this->cms_content_flag->table = 'cms_'.$table.'_flag';
506 foreach($flags as $flag) {
507 if(!$this->cms_content_flag->set(array($flag, $id), array('cid'=>$cid))) {
508 E(1, '写入内容属性标记表出错');
509 }
510 }
511
512 // 如果内容含有图片附件,则标记图片属性,否则删除图片属性
513 if($imagenum) {
514 $this->cms_content_flag->set(array(0, $id), array('cid'=>$cid));
515 }else{
516 $this->cms_content_flag->delete(0, $id);
517 }
518
519 // 删除去掉的属性
520 foreach($flags_old as $flag) {
521 $flag = intval($flag);
522 if($flag) $this->cms_content_flag->delete($flag, $id);
523 }
524
525 // 写入内容标签表
526 $this->cms_content_tag_data->table = 'cms_'.$table.'_tag_data';
527 foreach($tagdatas as $tagdata) {
528 $this->cms_content_tag->update($tagdata);
529 $this->cms_content_tag_data->set(array($tagdata['tagid'], $id), array('id'=>$id));
530 }
531
532 // 删除不用的标签
533 foreach($tags_old as $tagid => $tagname) {
534 $tagdata = $this->cms_content_tag->get($tagid);
535 $tagdata['count']--;
536 $this->cms_content_tag->update($tagdata);
537 $this->cms_content_tag_data->delete($tagid, $id);
538 }
539
540 // hook admin_product_control_edit_after.php
541
542 E(0, '编辑完成'.$endstr);
543 }
544 }
545
546 // 删除产品
547 public function del() {
548 // hook admin_product_control_del_before.php
549
550 $id = (int) R('id', 'P');
551 $cid = (int) R('cid', 'P');
552
553 empty($id) && E(1, '内容ID不能为空!');
554 empty($cid) && E(1, '分类ID不能为空!');
555
556 // hook admin_product_control_del_after.php
557
558 $err = $this->cms_content->xdelete('product', $id, $cid);
559 if($err) {
560 E(1, $err);
561 }else{
562 E(0, '删除成功!');
563 }
564 }
565
566 // 批量删除产品
567 public function batch_del() {
568 // hook admin_product_control_batch_del_before.php
569
570 $id_arr = R('id_arr', 'P');
571 if(!empty($id_arr) && is_array($id_arr)) {
572 $err_num = 0;
573 foreach($id_arr as $v) {
574 $err = $this->cms_content->xdelete('product', $v[0], $v[1]);
575 if($err) $err_num++;
576 }
577
578 if($err_num) {
579 E(1, $err_num.' 篇产品删除失败!');
580 }else{
581 E(0, '删除成功!');
582 }
583 }else{
584 E(1, '参数不能为空!');
585 }
586 }
587
588 // 删除单个附件
589 public function del_attach() {
590 // hook admin_product_control_del_attach_before.php
591
592 $aid = (int) R('aid', 'P');
593
594 empty($aid) && E(1, 'AID不能为空!');
595
596 // hook admin_product_control_del_attach_after.php
597
598 $this->cms_content_attach->table = 'cms_product_attach';
599 if($this->cms_content_attach->xdelete($aid)) {
600 E(0, '删除成功!');
601 }else{
602 E(1, '删除失败!');
603 }
604 }
605
606 // 单独保存图集
607 public function save_images() {
608 $id = intval(R('id', 'P'));
609 $images = (array)R('images', 'P');
610
611 empty($id) && E(1, 'ID不能为空!');
612 empty($images) && E(1, '亲,您的产品忘上传图片了!');
613
614 // 写入内容数据表
615 $this->cms_content_data->table = 'cms_product_data';
616 $data = $this->cms_content_data->read($id);
617 if(empty($data)) {
618 E(1, '内容不存在!');
619 }
620 $data['images'] = json_encode($images);
621 if($this->cms_content_data->set($id, $data)) {
622 E(0, '保存成功!');
623 }else{
624 E(1, '写入内容数据表出错!');
625 }
626 }
627
628 // 自动保存产品
629 public function auto_save() {
630 $this->kv->set('auto_save_product_uid_'.$this->_user['uid'], $_POST) ? E(0, '自动保存成功!') : E(1, '自动保存失败!');
631 }
632
633 // 自动生成摘要
634 private function auto_intro($intro, &$content) {
635 if(empty($intro)) {
636 $intro = preg_replace('/\s{2,}/', ' ', strip_tags($content));
637 return trim(utf8::cutstr_cn($intro, 255, ''));
638 }else{
639 return str_replace(array("\r\n", "\r", "\n"), '<br />', strip_tags($intro));
640 }
641 }
642
643 // 自动生成缩略图
644 private function auto_pic($table, $uid, $id = 0) {
645 $pic_arr = $this->cms_content_attach->find_fetch(array('id'=>$id, 'uid'=>$uid, 'isimage'=>1), array(), 0, 1);
646 $pic_arr = current($pic_arr);
647 $cfg = $this->runtime->xget();
648 $path = 'upload/'.$table.'/'.$pic_arr['filepath'];
649 $pic = image::thumb_name($path);
650 $src_file = TWCMS_PATH.$path;
651 $dst_file = TWCMS_PATH.$pic;
652 if(!is_file($dst_file)) {
653 image::thumb($src_file, $dst_file, $cfg['thumb_'.$table.'_w'], $cfg['thumb_'.$table.'_h'], $cfg['thumb_type'], $cfg['thumb_quality']);
654 }
655 return $pic;
656 }
657
658 // 获取远程图片
659 private function get_remote_img($table, &$content, $uid, $cid = 0, $id = 0) {
660 function_exists('set_time_limit') && set_time_limit(0);
661 $cfg = $this->runtime->xget();
662 $updir = 'upload/'.$table.'/';
663 $_ENV['_prc_err'] = 0;
664 $_ENV['_prc_arg'] = array(
665 'hosts'=>array('127.0.0.1', 'localhost', $_SERVER['HTTP_HOST'], $cfg['webdomain']),
666 'uid'=>$uid,
667 'cid'=>$cid,
668 'id'=>$id,
669 'maxSize'=>10000,
670 'upDir'=>TWCMS_PATH.$updir,
671 'preUri'=>$cfg['weburl'].$updir,
672 'cfg'=>$cfg,
673 );
674 $content = preg_replace_callback('#\<img [^\>]*src=["\']((?:http|ftp)\://[^"\']+)["\'][^\>]*\>#iU', array($this, 'img_replace'), $content);
675 unset($_ENV['_prc_arg']);
676 return $_ENV['_prc_err'] ? ',但远程抓取图片失败 '.$_ENV['_prc_err'].' 张!' : '';
677 }
678
679 // 远程图片处理 (如果抓取失败则不替换)
680 // $conf 用到4个参数 hosts preUri cfg upDir
681 private function img_replace($mat) {
682 static $uris = array();
683 $uri = $mat[1];
684 $conf = &$_ENV['_prc_arg'];
685
686 // 排除重复保存相同URL图片
687 if(isset($uris[$uri])) return str_replace($uri, $uris[$uri], $mat[0]);
688
689 // 根据域名排除本站图片
690 $urls = parse_url($uri);
691 if(in_array($urls['host'], $conf['hosts'])) return $mat[0];
692
693 $file = $this->cms_content_attach->remote_down($uri, $conf);
694 if($file) {
695 $uris[$uri] = $conf['preUri'].$file;
696 $cfg = $conf['cfg'];
697
698 // 是否添加水印
699 if(!empty($cfg['watermark_pos'])) {
700 image::watermark($conf['upDir'].$file, TWCMS_PATH.'static/img/watermark.png', null, $cfg['watermark_pos'], $cfg['watermark_pct']);
701 }
702
703 return str_replace($uri, $uris[$uri], $mat[0]);
704 }else{
705 $_ENV['_prc_err']++;
706 return $mat[0];
707 }
708 }
709
710 // hook admin_product_control_after.php
711}
712