Files
orico-official-website-old/app/admin/controller/Msgform.php
2024-10-29 14:04:59 +08:00

241 lines
9.0 KiB
PHP
Executable File

<?php
namespace app\admin\controller;
use think\Lang;
use think\Loader;
use think\Config;
class Msgform extends BaseController {
public function index() {
$this->redirect('/admin/msgform/lists');
}
public function lists() {
$skeyword = $this->request->get('skeyword', '', 'urldecode');
$arg_where = ['stat' => ['eq', 0]];
$arg_order = ['id' => 'desc'];
$arg_field = ['*'];
if (!empty($skeyword)) {
$skeyword = trim($skeyword);
$arg_where['cname|content'] = ['like', '%' . $skeyword . '%'];
$search['skeyword'] = $skeyword;
Config::set('paginate.query', ['skeyword' => $skeyword]); //分页参数
} else {
$search['skeyword'] = '';
}
$dataObject = Loader::model('Msgform')->getPageList($arg_where, $arg_order, $arg_field, 12);
$value = [
'list' => $dataObject->isEmpty() ? null : $dataObject->items(), //$dataObject->getCollection()->toArray()
'page' => $dataObject->render(),
//'page_previous' => $dataObject->getUrl($dataObject->currentPage() - 1),
//'page_next' => $dataObject->getUrl($dataObject->currentPage() + 1),
'search' => $search,
];
$this->assign($value);
return $this->fetch();
}
public function modalview($id) {
$id = intval($id);
if ($id > 0) {
$msgform = Loader::model('Msgform')->getRow(['id' => $id, 'stat' => ['gt', -1]]);
$value['msgform'] = $msgform;
}
$this->assign($value);
Config::set('default_ajax_return', 'html');
$this->view->engine(['type' => 'php', 'view_suffix' => 'html']);
return $this->fetch();
}
public function view($id) {
$id = intval($id);
if ($id > 0) {
$msgform = Loader::model('Msgform')->getRow(['id' => $id, 'stat' => ['gt', -1]]);
if (empty($msgform)) {
return $this->error(Lang::get('incorrect operation'));
}
$value['msgform'] = $msgform;
}
if($msgform['attachment']) {
$attachment = json_decode($msgform['attachment'], true);
}
else{
$attachment = '';
}
$this->assign('attachment',$attachment);
//echo "<pre>=="; print_r($attachment); die;
//echo "<pre>=="; print_r($msgform['attachment']);die;
$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',
'tel' => ['regex' => '^1[345789]\d{9}$|^([0-9]{3,4}-?)?[0-9]{7,8}$'],
//'content' => 'require',
];
$validatemsg = [
'pid.require' => 'p不能为空',
'tel.regex' => '电话格式错误',
//'content.require' => '内容不能为空',
];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$addtime = date('Y-m-d H:i:s');
$set = [
'name' => isset($data['name']) ? $data['name'] : 'Name',
'type' => isset($data['type']) ? $data['type'] : 'Article',
'msg' => isset($data['msg']) ? $data['msg'] : '',
'content' => isset($data['content']) ? $data['content'] : '',
'content' => $data['content'],
'addtime' => $addtime,
];
$model = Loader::model('Msgform')->insertRow($set);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/msgform/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function edit($id) {
$id = intval($id);
if ($id > 0) {
$msgform = Loader::model('Msgform')->getRow(['id' => $id, 'stat' => ['gt', -1]]);
if (empty($msgform)) {
return $this->error(Lang::get('incorrect operation'));
}
$value['msgform'] = $msgform;
}
$value['feedback_type'] = (array) Config::get('website_feedback_type');
$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',
];
$validatemsg = [
'id.require' => 'id不能为空',
];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$model = Loader::model('Msgform')->updateRow($data);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/msgform/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function togglestat() {
$id = $this->request->get('id', 0);
$flag = $this->request->get('flag', 0);
if ($this->request->isGet() && $id) {
$model = Loader::model('Msgform')->updateRow(['id' => $id, 'stat' => $flag]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/msgform/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function toggleishot() {
$id = $this->request->param('id', 0);
$flag = $this->request->param('flag', 0);
$id = intval($id);
if ($id > 0) {
$model = Loader::model('Msgform')->updateRow(['id' => $id, 'ishot' => $flag]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/msgform/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function checked() {
$ids = $this->request->post('ids');
if ($this->request->isPost() && $ids) {
$result = Loader::model('Msgform')->updateRow(['display' => 1], ['id' => ['in', $ids]]);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/msgform/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function unchecked() {
$ids = $this->request->post('ids');
if ($this->request->isPost() && $ids) {
$result = Loader::model('Msgform')->updateRow(['display' => 0], ['id' => ['in', $ids]]);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/msgform/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) {
$result = Loader::model('Msgform')->deleteRow($id);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/msgform/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function deletes() {
$ids = $this->request->post('ids');
if ($this->request->isPost() && $ids) {
$result = Loader::model('Msgform')->deleteRows($ids);
//$result = Loader::model('Msgform')->updateRow(['stat' => -1], ['id' => ['in', $ids]]);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/msgform/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
}