Files
2024-10-29 14:04:59 +08:00

400 lines
16 KiB
PHP
Executable File

<?php
namespace app\admin\controller;
use think\Lang;
use think\Loader;
use think\Config;
class Ad extends BaseController {
public function lists() {
$skeyword = $this->request->get('skeyword', '', 'urldecode');
$arg_where = ['b.siteid' => $this->siteid, 'b.country_code' => $this->country_code];
$arg_order = ['b.id' => 'desc'];
$arg_field = ['b.*', 'bt.id' => 'typeid', 'bt.name' => 'typename'];
if (!empty($skeyword)) {
$skeyword = trim($skeyword);
$arg_where['b.name'] = ['like', '%' . $skeyword . '%'];
$search['skeyword'] = $skeyword;
Config::set('paginate.query', ['skeyword' => $skeyword]); //分页参数
} else {
$search['skeyword'] = '';
}
$dataObject = model('ad')->getAdLists($arg_where, $arg_order, $arg_field);
$value = [
'list' => $dataObject->isEmpty() ? null : $dataObject->items(),
'page' => $dataObject->render(),
'search' => $search,
];
$this->assign($value);
return $this->fetch();
}
public function add($typeid = 0) {
$typeid = is_numeric($typeid) ? intval($typeid) : 0;
$arg_where = array('pid' => 0, 'stat' => 0, 'country_code' => $this->country_code);
$arg_order = array('id' => 'desc');
$arg_field = array('id', 'pid', 'name');
$typeOption = model('ad_type')->getOption($typeid, $arg_where, $arg_order, $arg_field, 100);
$value = ['typeOption' => $typeOption];
$this->assign($value);
return $this->fetch();
}
public function create() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
$validaterule = ['name' => 'require', 'typeid' => 'number|between:0,2147483647',];
$validatemsg = ['name.require' => '名称不能为空', 'typeid.between' => '所属上级值无效',];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$href = isset($data['ad_normbody']['href']) ? addslashes($data['ad_normbody']['href']) : '/';
if ($data['normbody']['style'] == 'htmlcode') {
$data['normbody'] = addslashes($data['normbody']['htmlcode']);
} else if ($data['normbody']['style'] == 'text') {
$style = "style=\"";
if (!empty($data['normbody']['size'])) {
$style .= "font-size:{$data['normbody']['size']};";
}
if (!empty($data['normbody']['color'])) {
$style.= "color:{$data['normbody']['color']};";
}
$style.="\"";
$data['normbody'] = "<a href=\"{$href}\" {$style}>{$data['normbody']['text']}</a>";
} else if ($data['normbody']['style'] == 'image') {
$style = "style=\"";
if (!empty($data['normbody']['width'])) {
$style .= "width:{$data['normbody']['width']};";
}
if (!empty($data['normbody']['height'])) {
$style.= "height:{$data['normbody']['height']};";
}
$style.="border:0px;\"";
$data['normbody'] = "<a href=\"{$href}\" target=\"_blank\"><img src=\"{$data['normbody']['src']}\" {$style}/></a>";
} else {
$data['normbody'] = addslashes($data['normbody']['flash']);
}
if (($timestamp = strtotime($data['starttime'])) === false) {
$data['starttime'] = time();
} else {
$data['starttime'] = $timestamp;
}
if (($timestamp = strtotime($data['endtime'])) === false) {
$data['endtime'] = time();
} else {
$data['endtime'] = $timestamp;
}
$set = array(
'typeid' => $data['typeid'],
'name' => $data['name'],
'sort' => intval($data['sort']),
'tags' => trim($data['tags']),
'timeset' => $data['timeset'],
'starttime' => $data['starttime'],
'endtime' => $data['endtime'],
'normbody' => $data['normbody'],
'expbody' => $data['expbody'],
'siteid' => $this->siteid,
'country_code' => $this->country_code
);
$model = model('ad')->insertRow($set);
if ($model && $model->getData('id')) {
$this->cacheClear('adTag');
return $this->redirect(url('/admin/ad/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function edit($id = 0) {
$id = intval($id);
if ($id > 0) {
$ad = model('ad')->getRow($id);
if (empty($ad)) {
return $this->error(Lang::get('incorrect operation'));
}
$value['ad'] = $ad;
} else {
return $this->error(Lang::get('incorrect operation'));
}
$typeid = isset($ad['typeid']) ? $ad['typeid'] : 0;
$arg_where = array('pid' => 0, 'stat' => 0, 'country_code' => $this->country_code);
$arg_order = array('id' => 'desc');
$arg_field = array('id', 'pid', 'name');
$typeOption = model('ad_type')->getOption($typeid, $arg_where, $arg_order, $arg_field, 100);
$value['typeOption'] = $typeOption;
$value['typeid'] = $typeid;
$this->assign($value);
return $this->fetch();
}
public function update() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
$validaterule = ['id' => 'require', 'name' => 'require', 'typeid' => 'number|between:0,2147483647',];
$validatemsg = ['id.require' => 'ID不能为空', 'name.require' => '名称不能为空', 'typeid.between' => '所属上级值无效',];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
if (($timestamp = strtotime($data['starttime'])) === false) {
$data['starttime'] = time();
} else {
$data['starttime'] = $timestamp;
}
if (($timestamp = strtotime($data['endtime'])) === false) {
$data['endtime'] = time();
} else {
$data['endtime'] = $timestamp;
}
$set = array(
'typeid' => $data['typeid'],
'name' => $data['name'],
'sort' => intval($data['sort']),
'tags' => trim($data['tags']),
'timeset' => $data['timeset'],
'starttime' => $data['starttime'],
'endtime' => $data['endtime'],
'normbody' => $data['normbody'],
'expbody' => $data['expbody'],
'id' => $data['id'],
);
$model = model('ad')->updateRow($set);
if ($model && $id = $model->getData('id')) {
$this->cacheClear('adTag');
return $this->redirect(url('/admin/ad/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function preview() {
$id = $this->request->param('id', 0);
$id = intval($id);
if ($this->request->isAjax() && $id > 0) {
$jscode = '<script src="' . url('index/ad/index', ['id' => $id]) . '" type="text/javascript"></script>';
$showhtml = "<xmp style='color:#333333;background-color:#ffffff'>\r\n\r\n$jscode\r\n\r\n</xmp>";
$iframesrc = url('/index/ad/previewjs', ['id' => $id]);
$value = ['showhtml' => $showhtml, 'iframesrc' => $iframesrc,];
$this->assign($value);
Config::set('default_ajax_return', 'html');
$this->view->engine(['type' => 'php', 'view_suffix' => 'html', 'tpl_replace_string' => [],]);
return $this->fetch();
}
return $this->error(Lang::get('incorrect operation'));
}
public function clear_cache() {
$this->cacheClear('adTag');
return $this->success(Lang::get('operation successed'));
}
public function togglerecommend() {
$id = $this->request->get('id', 0);
$flag = $this->request->get('flag', 0);
$id = intval($id);
if ($this->request->isGet() && $id) {
$model = model('ad')->updateRow(['id' => $id, 'recommend' => $flag]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/ad/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function updatesort() {
$id = $this->request->param('id', 0);
$sort = $this->request->param('sort', 0);
$sort = intval($sort);
$id = intval($id);
if ($this->request->isAjax() && $id) {
$model = model('ad')->updateRow(['id' => $id, 'sort' => $sort]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/ad/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function delete($id = 0) {
$id = intval($id);
if ($id > 0) {
$model = model('ad')->deleteRow($id);
if ($model && $model->getData('id')) {
$this->cacheClear('adTag');
return $this->success(Lang::get('operation successed'), url('/admin/ad/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function deletes() {
if ($this->request->isPost()) {
$ids = $this->request->post('ids');
$in_ids = explode(',', trim($ids, ','));
//echo json_encode(['code' => false, 'msg' => print_r($in_ids, true)]);exit;
$result = model('ad')->deleteRows($in_ids);
if ($result) {
$this->cacheClear('adTag');
return $this->success(Lang::get('operation successed'), url('/admin/ad/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function recommends() {
$ids = $this->request->post('ids');
$in_ids = explode(',', trim($ids, ','));
if ($this->request->isPost() && $in_ids) {
$result = model('ad')->updateRow(['recommend' => 1], ['id' => ['in', $in_ids]]);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/ad/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
//类别
public function typelists() {
$skeyword = $this->request->get('skeyword', '', 'urldecode');
$arg_where = array('pid' => 0, 'stat' => 0, 'siteid' => $this->siteid, 'country_code' => $this->country_code);
$arg_order = array('id' => 'desc');
$arg_field = array('id', 'name', 'description', 'createtime');
if (!empty($skeyword)) {
$skeyword = trim($skeyword);
$arg_where['name'] = ['like', '%' . $skeyword . '%'];
$search['skeyword'] = $skeyword;
Config::set('paginate.query', ['skeyword' => $skeyword]); //分页参数
} else {
$search['skeyword'] = '';
}
$dataObject = model('ad_type')->getPageList($arg_where, $arg_order, $arg_field);
$value = [
'list' => $dataObject->isEmpty() ? null : $dataObject->items(),
'page' => $dataObject->render(),
'search' => $search,
];
$this->assign($value);
return $this->fetch();
}
public function typeedit($id = 0) {
$id = intval($id);
if ($id > 0) {
$adtype = model('ad_type')->getRow($id);
if (empty($adtype)) {
return $this->error(Lang::get('incorrect operation'));
}
$value['adtype'] = $adtype;
} else {
return $this->error(Lang::get('incorrect operation'));
}
$this->assign($value);
return $this->fetch();
}
public function typeupdate() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
$validaterule = ['name' => 'require'];
$validatemsg = ['name.require' => '名称不能为空'];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$model = model('ad_type')->updateRow($data);
if ($model && $model->getData('id')) {
return $this->redirect(url('/admin/ad/typelists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function typeadd() {
return $this->fetch();
}
public function typecreate() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
$validaterule = ['name' => 'require'];
$validatemsg = ['name.require' => '名称不能为空'];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$data['siteid'] = $this->siteid;
$data['country_code'] = $this->country_code;
$model = model('ad_type')->insertRow($data);
if ($model && $model->getData('id')) {
return $this->redirect(url('/admin/ad/typelists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function typedelete($id = 0) {
$id = intval($id);
if ($id > 0) {
$result = model('ad_type')->deleteRow($id);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/ad/typelists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function typedeletes() {
$ids = $this->request->post('ids');
if ($this->request->isPost() && $ids) {
$result = model('ad_type')->deleteRows($ids);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/ad/typelists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
}