<?php
/**
* +----------------------------------------------------------------------
* | TickyPHP [ This is a freeware ]
* +----------------------------------------------------------------------
* | Copyright (c) 2015 All rights reserved.
* +----------------------------------------------------------------------
* | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
* +----------------------------------------------------------------------
* | SVN: $Id: update_urls.php 09594 2018-09-04 09:29:53 luomingui $
* +----------------------------------------------------------------------
* | 文件功能:批量更新URL
* +----------------------------------------------------------------------
*/
namespace application\admin\controller;
class update_urls extends auth {
public function index() {
$modelid = 0;
$modelinfo = get_modelinfo();
$select = select_category('catids[]', '0', '≡ 所有栏目 ≡', 0, 'multiple="multiple" style="height:200px;width:140px;"', false, false);
$this->assign('title', '批量更新URL');
$this->assign('modelid', $modelid);
$this->assign('modelinfo', $modelinfo);
$this->assign('select', $select);
$this->display('update_urls_list');
}
public function change_model() {
$modelid = isset($_POST['modelid']) ? intval($_POST['modelid']) : 0;
$modelinfo = get_modelinfo();
$select = select_category('catids[]', '0', '≡ 所有栏目 ≡', 0, 'multiple="multiple" style="height:200px;width:140px;"', false, false, $modelid);
$this->assign('title', '批量更新URL');
$this->assign('modelid', $modelid);
$this->assign('modelinfo', $modelinfo);
$this->assign('select', $select);
$this->display('update_urls_list');
}
public function update_category_url() {
if (isset($_POST['dosubmit'])) {
$catids = isset($_POST['catids']) && is_array($_POST['catids']) ? $_POST['catids'] : array('0');
$url_rule = get_config('url_rule');
if (!$catids[0]) {
$catinfo = get_category();
} else {
$catids = join(',', array_map('intval', $catids));
$catinfo = db('category')->field('catid,catname,type,category_urlrule,catdir')->where("catid IN ($catids)")->select();
}
foreach ($catinfo as $val) {
if ($val['type'] == 2) {
continue; }
if ($url_rule) {
$pclink = URL_MODEL == 1 ? 'index.php?s=/' . $val['catdir'] . '/' : '/'. $val['catdir'] . '/';
} else {
$pclink = url('index/index/lists', 'catid=' . $val['catid']);
}
db('category')->where(['catid' => $val['catid']])->update(array('pclink' => $pclink));
}
cache('categoryinfo', null);
showmsg(L('operation_success'), '', 1);
}
}
public function update_content_url() {
$modelid = isset($_POST['modelid']) ? intval($_POST['modelid']) : (isset($_GET['modelid']) ? intval($_GET['modelid']) : 0);
$modelinfo = get_modelinfo();
$url_rule = get_config('url_rule');
if (!$modelid) {
$i = isset($_GET['i']) ? intval($_GET['i']) : 0;
$num = count($modelinfo);
if ($i >= $num) {
showmsg('更新完成!', '/admin/update_urls', 1);
}
$r = db('content')->field('catid,contentid as id')->limit('1000')->select(); foreach ($r as $val) {
$url = $this->get_url($url_rule, $val['catid'], $val['id']);
db('content')->where(['contentid' => $val['id']])->update(array('url' => $url));
}
showmsg($modelinfo[$i]['name'] . '更新完成...', url('/admin/update_urls/update_content_url', array('i' => ++$i, 'modelid' => 0)), 1);
} else {
$modelarr = array();
foreach ($modelinfo as $val) {
$modelarr[$val['modelid']] = $val;
}
if (!isset($modelarr[$modelid])) {
showmsg('模型不存在!');
}
$r = db('content')->field('catid,contentid as id')->where(['modelid' => $modelid])->limit('1000')->select(); foreach ($r as $val) {
$url = $this->get_url($url_rule, $val['catid'], $val['id']);
db('content')->where(['contentid' => $val['id']])->update(array('url' => $url));
}
showmsg($modelarr[$modelid]['name'] . '更新完成!', '/admin/update_urls', 1);
}
}
private function get_url($url_rule, $catid, $id) {
if ($url_rule) {
$catinfo = get_category($catid);
$ext = config('http', 'default_extension');
$url = URL_MODEL == 1 ? 'index.php?s=/' . $catinfo['catdir'] . '/' . $id . $ext :
'/' . $catinfo['catdir'] . '/' . $id . $ext;
} else {
$url = url('/home/index/show', array('catid' => $catid, 'id' => $id));
}
return $url;
}
}