<?php
namespace application\diyform\controller;
use application\admin\controller\auth as auth;
use ticky\request;
use ticky\response;
use ticky\schema;
class diyform extends auth {
public function index() {
$search = array('type' => 1);
$ret = $this->db->page('model', $search, 'modelid', $this->p);
$this->assign('page', $ret['page']);
$this->assign('items', $ret['items']);
$this->display('diyform_list');
}
public function manage() {
$modelid = request::get('modelid', 0);
if ($modelid <= 0) {
$this->assign('postUrl', '/diyform/diyform/add');
$this->assign('action', '添加');
$item['modelid'] = $modelid;
$item['show_template'] = 'show_diyform';
$item['allowvisitor'] = '0';
$item['check_code'] = '0';
} else {
$item = $this->db->table('model')->where(['modelid' => $modelid])->find();
$setting = json_decode($item['setting'], true);
$item = array_merge($item, $setting);
$this->assign('postUrl', '/diyform/diyform/update');
$this->assign('action', '修改');
}
$show_temp = select_template('show_temp', 'show_');
$this->assign('show_temp', $show_temp);
$this->assign('item', $item);
$this->display('diyform_manage');
}
public function add() {
$data = $this->post_frm();
if ($this->db->table_exists($data['tablename'])) {
showmsg("表名已存在!");
}
$this->db->table('model')->add($data);
schema::sql_create($data['tablename']);
if (request::isAjax()) {
response::ajax(['code' => 200, 'msg' => '添加成功']);
} else {
showmsg('添加成功', '/diyform/diyform');
}
}
public function update() {
$modelid = request::get('modelid', 0);
$model = db('model')->where(['modelid' => $modelid])->find();
$data = $this->post_frm();
$data['tablename'] = $model['tablename'];
$this->db->table('model')->where(['modelid' => $modelid])->update($data);
if (request::isAjax()) {
response::ajax(['code' => 200, 'msg' => '更新成功']);
} else {
showmsg('更新成功', '/diyform/diyform');
}
}
public function delete() {
$modelid = request::get('modelid', 0);
$r = db('model')->field('tablename')->where(array('modelid' => $modelid))->find();
if ($r) {
schema::sql_delete($r['tablename']);
}
db('model')->where(['modelid' => $modelid])->delete(); db('model_field')->where(['modelid' => $modelid])->delete(); showmsg('删除成功', '/diyform/diyform');
}
private function post_frm() {
$arr = array();
$arr['siteid'] = $_COOKIE['ticky_tickysite'];
$arr['name'] = request::post('name', '');
$arr['tablename'] = request::post('tablename', '');
$arr['description'] = request::post('description', '');
$arr['inputtime'] = NOW_TIME;
$arr['items'] = request::post('items', '');
$arr['disabled'] = request::post('disabled', '1');
$arr['type'] = 1;
$arr['sort'] = request::post('sort', '0');
$arr['issystem'] = 0;
$arr['setting'] = json_encode(array(
'show_template' => request::post('show_template', ''),
'check_code' => request::post('check_code', '0'),
'sendmail' => request::post('sendmail', '0'),
'allowvisitor' => request::post('allowvisitor', '0')
));
return $arr;
}
}