<?php namespace Phpcmf;
class Table extends \Phpcmf\Common
{
public $dfield; public $init; public $is_get_catid;
protected $field; protected $sys_field; protected $form_rule; protected $is_data; protected $is_post_code; protected $is_module_index; protected $is_category_data_field;
protected $edit_where; protected $delete_where;
protected $name; protected $tpl_name; protected $tpl_prefix; protected $list_pagesize;
protected $auto_save; protected $replace_id;
protected $url_params;
public function __construct(...$params) {
parent::__construct(...$params);
$this->is_data = 0;
$this->tpl_name = '';
$this->auto_save = 1;
$this->tpl_prefix = \Phpcmf\Service::L('Router')->class.'_';
$this->delete_where = '';
$this->is_module_index = 0;
$this->is_category_data_field = 0;
}
protected function _init($data) {
!$data['show_field'] && $data['show_field'] = 'id';
$this->field = $data['field'] ? $data['field'] : $this->field;
$this->sys_field = $data['sys_field'] ? \Phpcmf\Service::L('Field')->sys_field($data['sys_field']) : [];
$data['field'] = $this->sys_field && $this->field ? $this->field + $this->sys_field : ($this->field ? $this->field : $this->sys_field);
$this->init = $data;
return $this;
}
protected function _field_save($catid) {
$field = $this->sys_field ? dr_array22array($this->sys_field, $this->field) : $this->field;
$this->is_category_data_field && $catid
&& $this->module['category'][$catid]['field']
&& $field = dr_array22array($field, $this->module['category'][$catid]['field']);
if (!IS_ADMIN && $field) {
foreach ($field as $i => $t) {
if (!$t['ismember']) {
unset($field[$i]);
}
}
}
return $field;
}
protected function _field_group($data) {
$field = $this->field;
$my_field = $sys_field = $diy_field = $cat_field = [];
$this->is_category_data_field && $data['catid']
&& $this->module['category'][$data['catid']]['field']
&& $field = dr_array22array($field, $this->module['category'][$data['catid']]['field']);
uasort($field, function($a, $b){
if($a['displayorder'] == $b['displayorder']){
return 0;
}
return($a['displayorder']<$b['displayorder']) ? -1 : 1;
});
foreach ($field as $i => $t) {
if ($t['setting']['is_right'] == 1) {
if (IS_ADMIN) {
$sys_field[$i] = $t;
} else {
$my_field[$i] = $t;
}
} elseif ($t['setting']['is_right'] == 2) {
$diy_field[$i] = $t;
} else {
$my_field[$i] = $t;
}
}
$this->sys_field && $sys_field = $this->sys_field + $sys_field ;
return [$my_field, $sys_field, $diy_field, $cat_field];
}
protected function _Data($id = 0) {
if (!$id) {
return [];
}
$row = \Phpcmf\Service::M()->init($this->init)->get($id);
if (!$row) {
return [];
}
if ($this->is_data) {
$r = \Phpcmf\Service::M()->table($this->init['table'] . '_data_'.intval($row['tablieid']))->get($id);
$row = $r ? $r + $row : $row;
}
return $row;
}
protected function _Display_Order($id, $value, $after = null) {
$row = \Phpcmf\Service::M()->init($this->init)->get($id);
if (!$row) {
$this->_json(0, dr_lang('数据%s不存在', $id));
}
$rt = \Phpcmf\Service::M()->init($this->init)->save($id, 'displayorder', $value, $this->edit_where);
if (!$rt['code']) {
$this->_json(0, $rt['msg']);
}
\Phpcmf\Service::L('input')->system_log($this->name.':修改('.$row[$this->init['show_field']].')排序值为'.$value);
IS_ADMIN && \Phpcmf\Service::M('cache')->sync_cache();
if ($after) {
call_user_func_array($after, [$row]);
}
$this->_json(1, dr_lang('操作成功'));
}
protected function _Format_Data($id, $data, $old) {
return $data;
}
protected function _Save($id = 0, $data = [], $old = [], $before = null, $after = null) {
$this->is_data && $tid = intval($old['tableid']);
if ($before) {
$rt = call_user_func_array($before, [$id, $data, $old]);
if (!$rt['code']) {
return $rt;
}
$data = $rt['data'];
}
if ($this->is_module_index) {
$rt = $this->content_model->save($id, $data, $old);
if (!$rt['code']) {
return $rt;
}
$data = $rt['data'];
$data[1]['id'] = $data[0]['id'] = $id = $rt['code'];
} else {
$main = isset($data[1]) ? $data[1] : $data;
if ($id) {
$rt = \Phpcmf\Service::M()->table($this->init['table'])->update($id, $main, $this->edit_where);
if (!$rt['code']) {
return $rt;
}
} else {
$rt = \Phpcmf\Service::M()->table($this->init['table'])->replace($main);
if (!$rt['code']) {
return $rt;
}
$_id = $rt['code'];
if ($this->is_data) {
$tid = floor($_id / 50000);
\Phpcmf\Service::M()->table($this->init['table'])->update($_id, ['tableid' => $tid], $this->edit_where);
}
}
if ($this->is_data) {
\Phpcmf\Service::M()->is_data_table($this->init['table'].'_data_', $tid);
$table = $this->init['table'].'_data_'.$tid;
if ($id) {
if ($data[0]) {
$rt = \Phpcmf\Service::M()->table($table)->update($id, $data[0], $this->edit_where);
if ($rt['msg']) {
\Phpcmf\Service::M()->table($this->init['table'])->delete($id);
$this->is_module_index && \Phpcmf\Service::M()->table($this->init['table'].'_index')->delete($id);
return $rt;
}
} else {
}
} else {
$data[0]['id'] = $_id; $rt = \Phpcmf\Service::M()->table($table)->replace($data[0]);
if ($rt['msg']) {
\Phpcmf\Service::M()->table($this->init['table'])->delete($_id);
$this->is_module_index && \Phpcmf\Service::M()->table($this->init['table'].'_index')->delete($_id);
return $rt;
}
}
}
$data[1]['id'] = $data[0]['id'] = $id = $id ? $id : $_id;
}
if ($after) {
$rt = call_user_func_array($after, [$id, $data, $old]);
$rt && $data = $rt;
}
return dr_return_data($id, 'ok', $data);
}
protected function _Post($id = 0, $draft = [], $is_data = 0, $is_post = 0) {
$uri =\Phpcmf\Service::L('Router')->uri();
$name = md5($id.$uri);
\Phpcmf\Service::L('Form')->id($id);
$data = $this->_Data($id);
$this->replace_id && $id = $this->replace_id;
\Phpcmf\Service::L('Field')->app(APP_DIR);
if (IS_AJAX_POST || $is_post) {
if (!$data && $id) {
$this->_json(0, dr_lang('数据#%s不存在', $id));
} elseif ($this->is_post_code && !\Phpcmf\Service::L('Form')->check_captcha('code')) {
$this->_json(0, dr_lang('图片验证码不正确'), ['field' => 'code']);
}
\Phpcmf\Service::L('field')->value = $data;
$post = \Phpcmf\Service::L('input')->post('data', false);
list($post, $return, $attach) = \Phpcmf\Service::L('Form')->validation(
$post,
$this->form_rule,
$this->_field_save(intval(\Phpcmf\Service::L('input')->post('catid'))),
$data
);
if ($return) {
$this->_json(0, $return['error'], ['field' => $return['name']]);
}
$post = $this->_Format_Data($id, $post, $data);
$rt = $this->_Save($id, $post, $data);
if (!$rt['code']) {
$this->_json(0, $rt['msg'], $rt['data']);
}
$post['id'] = $rt['code'];
$logname = isset($post[$this->init['show_field']]) && $post[$this->init['show_field']] ? $post[$this->init['show_field']] : $data[$this->init['show_field']];
!$logname && $logname = $post['id'];
$id ? \Phpcmf\Service::L('input')->system_log($this->name.':修改('.$logname.')') : \Phpcmf\Service::L('input')->system_log($this->name.':新增('.$logname.')');
$id = $rt['code'];
SYS_ATTACHMENT_DB && $attach && \Phpcmf\Service::M('Attachment')->handle(isset($data['uid']) ? $data['uid'] : $this->member['id'], \Phpcmf\Service::M()->dbprefix($this->init['table']).'-'.$id, $attach);
\Phpcmf\Service::L('Form')->auto_form_data_delete($name);
$this->_Call_Post($rt['data']);
}
if (!$data && $id) {
IS_ADMIN ? $this->_admin_msg(0, dr_lang('数据#%s不存在', $id)) : $this->_msg(0, dr_lang('数据#%s不存在', $id));
return [null, null];
}
if (defined('SYS_AUTO_FORM') && SYS_AUTO_FORM && !$id && $this->auto_save) {
$data = \Phpcmf\Service::L('Form')->auto_form_data($name, $data);
}
$draft && $data = $draft;
$mydata = $data;
$this->is_get_catid && $data['catid'] = $mydata['catid'] = $this->is_get_catid;
$is_data && $data['data'] = $mydata;
list($my_field, $sys_field, $diy_field, $cat_field) = $this->_field_group($mydata);
$data['myfield'] = \Phpcmf\Service::L('Field')->toform($id, $my_field, $mydata);
$data['sysfield'] = \Phpcmf\Service::L('Field')->toform($id, $sys_field, $mydata);
$data['diyfield'] = \Phpcmf\Service::L('Field')->toform($id, $diy_field, $mydata);
$data['catfield'] = \Phpcmf\Service::L('Field')->toform($id, $cat_field, $mydata);
if (defined('SYS_AUTO_FORM') && SYS_AUTO_FORM && !$id && $this->auto_save) {
$data['auto_form_data_ajax'] = \Phpcmf\Service::L('Form')->auto_form_data_ajax($name);
}
$data['form'] = dr_form_hidden([
'id' => $id,
'table' => IS_ADMIN ? $this->init['table'] : '',
]);
$data['post_url'] = IS_MEMBER ? \Phpcmf\Service::L('Router')->member_url(\Phpcmf\Service::L('Router')->uri('add'), $this->url_params) : \Phpcmf\Service::L('Router')->url(\Phpcmf\Service::L('Router')->uri('add'), $this->url_params);
$data['reply_url'] = \Phpcmf\Service::L('Router')->get_back(\Phpcmf\Service::L('Router')->uri('index'), $this->url_params);
$data['uriprefix'] = trim(APP_DIR.'/'.\Phpcmf\Service::L('Router')->class, '/');
$data['is_edit'] = $id;
\Phpcmf\Service::V()->assign($data);
return [$this->_tpl_filename('post'), $data];
}
protected function _Call_Post($data) {
$this->_json(1, dr_lang('操作成功'));
}
protected function _Show($id) {
$data = $this->_Data($id);
if (!$data) {
return [null, null];
}
\Phpcmf\Service::L('Field')->app(APP_DIR);
list($my_field, $sys_field, $diy_field, $cat_field) = $this->_field_group($data);
$data['myfield'] = \Phpcmf\Service::L('Field')->toform($id, $my_field, $data, 1);
$data['sysfield'] = \Phpcmf\Service::L('Field')->toform($id, $sys_field, $data, 1);
$data['diyfield'] = \Phpcmf\Service::L('Field')->toform($id, $diy_field, $data, 1);
$data['catfield'] = \Phpcmf\Service::L('Field')->toform($id, $cat_field, $data, 1);
$fields = $this->field;
$fields['inputtime'] = ['fieldtype' => 'Date'];
$fields['updatetime'] = ['fieldtype' => 'Date'];
$page = max(1, (int)\Phpcmf\Service::L('input')->get('page'));
$data = \Phpcmf\Service::L('Field')->format_value($fields, $data, $page);
$data['reply_url'] = \Phpcmf\Service::L('Router')->get_back(\Phpcmf\Service::L('Router')->uri('index'), $this->url_params);
$data['uriprefix'] = trim(APP_DIR.'/'.\Phpcmf\Service::L('Router')->class, '/');
\Phpcmf\Service::V()->assign($data);
return [$this->_tpl_filename('show'), $data];
}
protected function _Del($ids, $before = null, $after = null, $attach = 0) {
if (!$ids) {
$this->_json(0, dr_lang('所选数据不存在'));
}
$rows = \Phpcmf\Service::M()->init($this->init)->where_in('id', $ids)->getAll();
if (!$rows) {
$this->_json(0, dr_lang('所选数据不存在2'));
}
if ($before) {
$rt = call_user_func_array($before, [$rows]);
if (!$rt['code']) {
$this->_json(0, $rt['msg']);
}
$rt['data'] && $rows = $rt['data'];
}
$ids = [];
foreach ($rows as $t) {
$id = intval($t['id']);
$rt = \Phpcmf\Service::M()->init($this->init)->delete($id, $this->delete_where);
if (!$rt['code']) {
$this->_json(0, $rt['msg']);
}
if ($this->is_data) {
$rt = \Phpcmf\Service::M()->init($this->init)->table($this->init['table'].'_data_'.intval($t['tableid']))->delete($id, $this->delete_where);
if (!$rt['code']) {
$this->_json(0, $rt['msg']);
}
}
SYS_ATTACHMENT_DB && $attach && \Phpcmf\Service::M('Attachment')->cid_delete((int)$t['uid'], $id, $attach);
$ids[] = $id;
}
$after && call_user_func_array($after, [$rows]);
\Phpcmf\Service::L('input')->system_log($this->name.':删除('.implode(', ', $ids).')');
$this->_json(1, dr_lang('操作成功'));
}
protected function _List($p = [], $size = 0) {
if (!$this->list_pagesize) {
if (!$size) {
if (IS_ADMIN) {
$size = (int)SYS_ADMIN_PAGESIZE;
} else {
$size = (int)$this->member_cache['config']['pagesize'];
if (IS_API_HTTP) {
$size = (int)$this->member_cache['config']['pagesize_api'];
} elseif (\Phpcmf\Service::IS_MOBILE()) {
$size = (int)$this->member_cache['config']['pagesize_mobile'];
}
}
}
!$size && $size = 10;
} else {
$size = $this->list_pagesize;
}
list($list, $total, $param) = \Phpcmf\Service::M()->init($this->init)->limit_page($size);
$p && $param = $p + $param;
$sql = \Phpcmf\Service::M()->get_sql_query();
$this->url_params && $param = dr_array22array($param, $this->url_params);
$uri =\Phpcmf\Service::L('Router')->uri();
$url = IS_ADMIN ?\Phpcmf\Service::L('Router')->url($uri, $param) :\Phpcmf\Service::L('Router')->member_url($uri, $param);
$url = $url.'&page={page}';
if (IS_ADMIN) {
$config = require CMSPATH.'Config/Apage.php';
} else {
$file = 'config/page/'.(\Phpcmf\Service::IS_PC() ? 'pc' : 'mobile').'/member.php';
if (is_file(WEBPATH.$file)) {
$config = require WEBPATH.$file;
} elseif (is_file(ROOTPATH.$file)) {
$config = require ROOTPATH.$file;
} else {
exit('无法找到分页配置文件【'.$file.'】');
}
}
\Phpcmf\Service::L('Router')->set_back(\Phpcmf\Service::L('Router')->uri(), $param);
$list_field = [];
if ($this->init['list_field']) {
foreach ($this->init['list_field'] as $i => $t) {
$t['use'] && $list_field[$i] = $t;
}
}
!$list_field && $this->init['show_field'] && $list_field = [
$this->init['show_field'] => [
'name' => dr_lang('主题'),
'func' => 'title',
'width' => 0,
],
];
$list_table = \Phpcmf\Service::M()->dbprefix($this->init['table']);
if (isset($this->init['join_list'][0]) && $this->init['join_list'][0]) {
$list_table.= ','.\Phpcmf\Service::M()->dbprefix($this->init['join_list'][0]);
}
$data = [
'list' => $list,
'total' => $total,
'param' => $param,
'mypages' => \Phpcmf\Service::L('input')->table_page($url, $total, $config, $size),
'my_file' => $this->_tpl_filename('table'),
'uriprefix' => trim(APP_DIR.'/'.\Phpcmf\Service::L('Router')->class, '/'), 'list_field' => $list_field, 'list_query' => urlencode(dr_authcode($sql, 'ENCODE')), 'list_table' => $list_table, ];
$field = \Phpcmf\Service::V()->get_value('field');
if ($field) {
foreach ($field as $i => $t) {
if (!$t['fieldtype']) {
continue;
} elseif (!in_array($t['fieldtype'], [
'Text',
'Textarea',
'Textbtn',
'Ueditor',
'Select',
'Radio',
])) {
unset($field[$i]);
}
}
$data['field'] = $field;
}
\Phpcmf\Service::V()->assign($data);
return [$this->_tpl_filename('list'), $data];
}
public function _tpl_filename($name) {
if (IS_ADMIN) {
$my_file = is_file(APPPATH.'Views/'.$this->tpl_name.'_'.$name.'.html') ? $this->tpl_name.'_'.$name.'.html' : $this->tpl_prefix.$name.'.html';
} else {
$my_file = is_file(dr_tpl_path().$this->tpl_name.'_'.$name.'.html') ? $this->tpl_name.'_'.$name.'.html' : $this->tpl_prefix.$name.'.html';
}
return $my_file;
}
}