<?php namespace Phpcmf\Model;
class Table extends \Phpcmf\Model
{
public function cache($siteid = SITE_ID, $module = null) {
$cache = [];
$paytable = []; !$module && $module = $this->table('module')->getAll();
if ($module) {
foreach ($module as $t) {
$table = dr_module_table_prefix($t['dirname'], $siteid);
$prefix = $this->dbprefix($table);
if (!$this->db->tableExists($prefix)) {
continue;
}
$main_field = $this->db->getFieldNames($prefix);
if ($main_field) {
$paytable['module-'.$t['id']] = [
'table' => $table,
'name' => 'title',
'thumb' => 'thumb',
'url' => '/index.php?s='.$t['dirname'].'&c=show&id=',
'username' => 'author',
];
$cache[$prefix] = $main_field;
$table = $prefix.'_data_0';
$this->db->tableExists($table) && $cache[$table] = $this->db->getFieldNames($table);
$table = $prefix.'_category_data';
$this->db->tableExists($table) && $cache[$table] = $this->db->getFieldNames($table);
$table = $prefix.'_category_data_0';
$this->db->tableExists($table) && $cache[$table] = $this->db->getFieldNames($table);
$table = $prefix.'_hits';
$this->db->tableExists($table) && $cache[$table] = $this->db->getFieldNames($table);
$table = $prefix.'_comment';
$this->db->tableExists($table) && $cache[$table] = $this->db->getFieldNames($table);
$form = $this->table('module_form')->where('module', $t['dirname'])->order_by('id ASC')->getAll();
if ($form) {
foreach ($form as $f) {
$table = $prefix.'_form_'.$f['table'];
$this->db->tableExists($table) && $cache[$table] = $this->db->getFieldNames($table);
$paytable['mform-'.$t['dirname'].'-'.$f['id']] = [
'table' => $table,
'name' => 'title',
'thumb' => 'thumb',
'url' => '/index.php?s='.$t['dirname'].'&c='.$f['table'].'&m=show&id=',
'username' => 'author',
];
}
}
}
}
}
$form = $this->table($siteid.'_form')->getAll();
if ($form) {
foreach ($form as $t) {
$table = $siteid.'_form_'.$t['table'];
$prefix = $this->dbprefix($table);
$cache[$prefix] = $this->db->getFieldNames($prefix);
$paytable['form-'.$siteid.'-'.$t['id']] = [
'table' => $table,
'name' => 'title',
'thumb' => 'thumb',
'url' => '/index.php?s=form&c='.$t['table'].'&m=show&id=',
'username' => 'author',
];
}
}
$table = $this->dbprefix('member');
$cache[$table] = $this->db->getFieldNames($table);
$table = $this->dbprefix('member_data');
$cache[$table] = $this->db->getFieldNames($table);
\Phpcmf\Service::L('cache')->set_file('table-'.$siteid, $cache);
$cache = [];
$field = $this->db->table('field')->where('disabled', 0)->orderBy('id ASC')->get()->getResultArray();
if ($field) {
foreach ($field as $f) {
$f['setting'] = dr_string2array($f['setting']);
$cache[$f['id']] = $f;
}
}
\Phpcmf\Service::L('cache')->set_file('table-field', $cache);
\Phpcmf\Service::L('cache')->set_file('table-pay-'.$siteid, $paytable);
}
public function get_field($table) {
return $this->db->getFieldNames($this->dbprefix($table));
}
public function get_cache_field($table) {
$tableinfo = \Phpcmf\Service::L('cache')->get('table-'.SITE_ID);
if (!$tableinfo) {
return [];
}
return isset($tableinfo[$this->dbprefix($table)]) ? $tableinfo[$this->dbprefix($table)] : [];
}
public function _query($sql, $replace = []) {
$replace[0][] = '{dbprefix}';
$replace[1][] = $this->db->DBPrefix;
$todo = [];
$count = 0;
$sql_data = explode(';SQL_FINECMS_EOL', trim(str_replace(array(PHP_EOL, chr(13), chr(10)), 'SQL_FINECMS_EOL', str_replace($replace[0], $replace[1], $sql))));
if ($sql_data) {
foreach($sql_data as $query){
if (!$query) {
continue;
}
$ret = '';
$queries = explode('SQL_FINECMS_EOL', trim($query));
foreach($queries as $query) {
$ret.= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query;
}
$ret = trim($ret);
if (!$ret) {
continue;
}
if ($this->db->simpleQuery(dr_format_create_sql($ret))) {
$todo[] = $ret;
$count++;
} else {
$rt = $this->db->error();
return dr_return_data(0, $rt['message'].'<br> '.$ret);
}
}
}
return dr_return_data(1, '', [$count, $todo]);
}
public function create_form($data) {
$data['name'] = dr_safe_filename($data['name']);
$pre = $this->dbprefix(SITE_ID.'_form');
$sql = [
"
CREATE TABLE IF NOT EXISTS `".$pre.'_'.$data['table']."` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uid` int(10) unsigned DEFAULT 0 COMMENT '录入者uid',
`author` varchar(100) DEFAULT NULL COMMENT '录入者账号',
`title` varchar(255) DEFAULT NULL COMMENT '主题',
`inputip` varchar(50) DEFAULT NULL COMMENT '录入者ip',
`inputtime` int(10) unsigned NOT NULL COMMENT '录入时间',
`status` tinyint(1) DEFAULT NULL COMMENT '状态值',
`displayorder` int(10) NOT NULL DEFAULT '0' COMMENT '排序值',
`tableid` smallint(5) unsigned NOT NULL COMMENT '附表id',
PRIMARY KEY `id` (`id`),
KEY `uid` (`uid`),
KEY `status` (`status`),
KEY `inputtime` (`inputtime`),
KEY `displayorder` (`displayorder`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='".$data['name']."表单表';"
,
"CREATE TABLE IF NOT EXISTS `".$pre.'_'.$data['table']."_data_0` (
`id` int(10) unsigned NOT NULL,
`uid` int(10) unsigned DEFAULT 0 COMMENT '录入者uid',
UNIQUE KEY `id` (`id`),
KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='".$data['name']."表单附表';"
];
foreach ($sql as $s) {
$this->db->simpleQuery(dr_format_create_sql($s));
}
$this->db->table('field')->insert(array(
'name' => '主题',
'fieldname' => 'title',
'fieldtype' => 'Text',
'relatedid' => $data['id'],
'relatedname' => 'form-'.SITE_ID,
'isedit' => 1,
'ismain' => 1,
'ismember' => 1,
'issystem' => 1,
'issearch' => 1,
'disabled' => 0,
'setting' => dr_array2string(array(
'option' => array(
'width' => 300, 'fieldtype' => 'VARCHAR', 'fieldlength' => '255' ),
'validate' => array(
'xss' => 1, 'required' => 1, )
)),
'displayorder' => 0,
));
}
public function sys_field_form() {
return [
1 => ['id', 'title', 'uid', 'inputip', 'inputtime', 'displayorder', 'tableid'],
0 => ['id', 'uid'],
];
}
public function my_field_form($form) {
if (!$form) {
return [];
}
}
public function delete_form($data) {
$id = intval($data['id']);
$pre = $this->dbprefix(SITE_ID.'_form');
$this->db->table('field')->where('relatedid', $id)->where('relatedname', 'form-'.SITE_ID)->delete();
$table = $pre.'_'.$data['table'];
$this->db->simpleQuery('DROP TABLE IF EXISTS `'.$table.'`');
for ($i = 0; $i < 200; $i ++) {
if (!$this->db->query("SHOW TABLES LIKE '".$table.'_data_'.$i."'")->getRowArray()) {
break;
}
$this->db->simpleQuery('DROP TABLE IF EXISTS '.$table.'_data_'.$i);
}
$this->db->table('admin_menu')->where('mark', 'form-'.$data['table'])->delete();
$this->db->table('member_menu')->where('mark', 'form-'.$data['table'])->delete();
$this->db->table(SITE_ID.'_form')->delete($id);
}
public function create_module_form($data) {
$data['name'] = dr_safe_filename($data['name']);
$sql = [
"
CREATE TABLE IF NOT EXISTS `{tablename}` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`cid` int(10) unsigned NOT NULL COMMENT '内容id',
`catid` mediumint(8) unsigned NOT NULL COMMENT '栏目id',
`uid` mediumint(8) unsigned NOT NULL COMMENT '作者id',
`author` varchar(50) NOT NULL COMMENT '作者名称',
`inputip` varchar(30) DEFAULT NULL COMMENT '录入者ip',
`inputtime` int(10) unsigned NOT NULL COMMENT '录入时间',
`title` varchar(255) DEFAULT NULL COMMENT '表单主题',
`status` tinyint(1) DEFAULT NULL COMMENT '状态值',
`tableid` smallint(5) unsigned NOT NULL COMMENT '附表id',
`displayorder` int(10) DEFAULT NULL COMMENT '排序值',
PRIMARY KEY `id` (`id`),
KEY `cid` (`cid`),
KEY `uid` (`uid`),
KEY `catid` (`catid`),
KEY `author` (`author`),
KEY `status` (`status`),
KEY `displayorder` (`displayorder`),
KEY `inputtime` (`inputtime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模块表单".$data['name']."表';"
,
"CREATE TABLE IF NOT EXISTS `{tablename}_data_0` (
`id` int(10) unsigned NOT NULL,
`cid` int(10) unsigned NOT NULL COMMENT '内容id',
`catid` mediumint(8) unsigned NOT NULL COMMENT '栏目id',
`uid` mediumint(8) unsigned NOT NULL COMMENT '作者id',
UNIQUE KEY `id` (`id`),
KEY `cid` (`cid`),
KEY `catid` (`catid`),
KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模块表单".$data['name']."附表';"
];
foreach (\Phpcmf\Service::C()->site_info as $sid => $v) {
$par = $this->dbprefix(dr_module_table_prefix($data['module'], $sid)); $pre = $par.'_form_'.$data['table']; if (!$this->is_table_exists($par)) {
continue;
}
foreach ($sql as $s) {
$this->db->simpleQuery(str_replace('{tablename}', $pre, dr_format_create_sql($s)));
}
if ($this->is_field_exists($par, $data['table']."_total")) {
continue;
}
$this->db->simpleQuery("ALTER TABLE `{$par}` ADD `".$data['table']."_total` INT(10) UNSIGNED NULL DEFAULT '0' COMMENT '表单".$data['name']."统计' , ADD INDEX (`".$data['table']."_total`) ;");
}
$this->db->table('field')->where('relatedid', $data['id'])->where('relatedname', 'mform-'.$data['module'])->delete();
$this->db->table('field')->insert(array(
'name' => '主题',
'fieldname' => 'title',
'fieldtype' => 'Text',
'relatedid' => $data['id'],
'relatedname' => 'mform-'.$data['module'],
'isedit' => 1,
'ismain' => 1,
'ismember' => 1,
'issystem' => 1,
'issearch' => 1,
'disabled' => 0,
'setting' => dr_array2string(array(
'option' => array(
'width' => 300, 'fieldtype' => 'VARCHAR', 'fieldlength' => '255' ),
'validate' => array(
'xss' => 1, 'required' => 1, )
)),
'displayorder' => 0,
));
}
public function delete_module_form($data) {
$id = intval($data['id']);
$pre = $this->dbprefix(SITE_ID.'_'.$data['module'].'_form');
if (!$this->is_table_exists($pre)) {
return;
}
$this->db->table('field')->where('relatedid', $id)->where('relatedname', 'mform-'.$data['module'])->delete();
$table = $pre.'_'.$data['table'];
$this->db->simpleQuery('DROP TABLE IF EXISTS `'.$table.'`');
for ($i = 0; $i < 200; $i ++) {
if (!$this->db->query("SHOW TABLES LIKE '".$table.'_data_'.$i."'")->getRowArray()) {
break;
}
$this->db->simpleQuery('DROP TABLE IF EXISTS '.$table.'_data_'.$i);
}
$par = $this->dbprefix(SITE_ID.'_'.$data['module']);
if ($this->is_field_exists($par, $data['table']."_total")) {
$this->db->simpleQuery("ALTER TABLE `{$par}` DROP `".$data['table']."_total` , DROP INDEX (`".$data['table']."_total`) ;");
}
$this->db->table('module_form')->delete($id);
}
public function create_site($siteid) {
$this->db->simpleQuery("DROP TABLE IF EXISTS `".$this->dbprefix($siteid.'_form')."`");
$this->db->simpleQuery(dr_format_create_sql("
CREATE TABLE IF NOT EXISTS `".$this->dbprefix($siteid.'_form')."` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL COMMENT '名称',
`table` varchar(50) NOT NULL COMMENT '表名',
`setting` text DEFAULT NULL COMMENT '配置信息',
PRIMARY KEY (`id`),
UNIQUE KEY `table` (`table`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='表单模型表';
"));
$this->db->simpleQuery("DROP TABLE IF EXISTS `".$this->dbprefix($siteid.'_share_category')."`");
$this->db->simpleQuery(dr_format_create_sql("
CREATE TABLE IF NOT EXISTS `".$this->dbprefix($siteid.'_share_category')."` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`tid` tinyint(1) NOT NULL COMMENT '栏目类型,0单页,1模块,2外链',
`pid` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '上级id',
`mid` varchar(20) NOT NULL COMMENT '模块目录',
`pids` varchar(255) NOT NULL COMMENT '所有上级id',
`name` varchar(30) NOT NULL COMMENT '栏目名称',
`dirname` varchar(30) NOT NULL COMMENT '栏目目录',
`pdirname` varchar(100) NOT NULL COMMENT '上级目录',
`child` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否有下级',
`childids` text NOT NULL COMMENT '下级所有id',
`domain` varchar(50) DEFAULT NULL COMMENT '绑定电脑域名',
`mobile_domain` varchar(50) DEFAULT NULL COMMENT '绑定手机域名',
`thumb` varchar(255) NOT NULL COMMENT '栏目图片',
`show` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '是否显示',
`content` mediumtext NOT NULL COMMENT '单页内容',
`setting` text NOT NULL COMMENT '属性配置',
`displayorder` smallint(5) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `mid` (`mid`),
KEY `tid` (`tid`),
KEY `show` (`show`),
KEY `dirname` (`dirname`),
KEY `module` (`pid`,`displayorder`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='共享模块栏目表';
"));
$this->db->simpleQuery("DROP TABLE IF EXISTS `".$this->dbprefix($siteid.'_share_index')."`");
$this->db->simpleQuery(dr_format_create_sql("
CREATE TABLE IF NOT EXISTS `".$this->dbprefix($siteid.'_share_index')."` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`mid` varchar(20) NOT NULL COMMENT '模块目录',
PRIMARY KEY (`id`),
KEY `mid` (`mid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='共享模块内容索引表';
"));
$local = dr_dir_map(dr_get_app_list(), 1);
foreach ($local as $dir) {
$path = dr_get_app_dir($dir);
if (is_file($path.'install.lock') && is_file($path.'Config/Install_site.php')) {
$sql = file_get_contents($path.'Config/Install_site.sql');
$this->_query(str_replace('{dbprefix}', $this->preifx.$siteid.'_', $sql));
}
}
}
public function get_export_field_name($table, $update = 0) {
$row = $this->db->table('export')->where('name', $table)->get()->getRowArray();
!$row && $this->table('export')->replace([
'name' => $table,
'value' => ''
]);
$value = $row['value'] ? dr_string2array($row['value']) : [];
if ($update) {
$tables = explode(',', $table);
foreach ($tables as $tt) {
$field = $this->db->query('SHOW FULL COLUMNS FROM `'.$tt.'`')->getResultArray();
if (!$field) {
return $value;
}
foreach ($field as $t) {
if (!isset($value[$t['Field']])) {
$value[$t['Field']] = [
'use' => 1,
'name' => $t['Comment'] ? $t['Comment'] : $t['Field'],
'func' => '',
];
}
}
}
$this->table('export')->replace([
'name' => $table,
'value' => dr_array2string($value)
]);
}
return $value;
}
public function save_export_field_name($table, $data) {
$this->table('export')->replace([
'name' => $table,
'value' => dr_array2string($data)
]);
}
}