📄 model.class.php
字号:
<?php
class model
{
var $db;
var $pages;
var $number;
var $table;
var $table_field;
function __construct()
{
global $db;
$this->db = &$db;
$this->table = DB_PRE.'model';
$this->table_field = DB_PRE.'model_field';
}
function model()
{
$this->__construct();
}
function get($modelid)
{
$modelid = intval($modelid);
if($modelid < 1) return false;
return $this->db->get_one("SELECT * FROM $this->table WHERE modelid=$modelid");
}
function add($model)
{
if(!is_array($model) || empty($model['name']) || empty($model['tablename'])) return false;
if($this->db->table_status(DB_PRE.'c_'.$model['tablename'])) return false;
$this->db->insert($this->table, $model);
$modelid = $this->db->insert_id();
$arr_search = array('$tablename', '$table_model_field', '$modelid');
$arr_replace = array(DB_PRE.'c_'.$model['tablename'], DB_PRE.'model_field', $modelid);
$sql = file_get_contents(PHPCMS_ROOT.'include/admin/model.sql');
$sql = str_replace($arr_search, $arr_replace, $sql);
sql_execute($sql);
$this->cache();
$this->cache_field($modelid);
return $modelid;
}
function edit($modelid, $model)
{
if(!$modelid || !is_array($model) || empty($model['name']) || empty($model['tablename'])) return false;
$this->db->update($this->table, $model, "modelid=$modelid");
$this->cache();
$this->cache_field($modelid);
return true;
}
function delete($modelid)
{
global $CATEGORY;
$modelid = intval($modelid);
if($modelid < 1) return false;
$m = $this->get($modelid);
if(!$m) return false;
$this->db->query("DROP TABLE IF EXISTS `".DB_PRE."c_".$m['tablename']."`");
$this->db->query("DELETE FROM $this->table_field WHERE modelid=$modelid");
$this->db->query("DELETE FROM $this->table WHERE modelid=$modelid");
$this->cache();
$C = load('category.class.php','phpcms','include/admin/');
foreach($CATEGORY as $catid=>$value)
{
if($value['modelid'] != $modelid) continue;
$C->delete($catid);
}
return true;
}
function listinfo($where = '', $order = '', $page = 1, $pagesize = 20)
{
if($where) $where = " WHERE $where";
if($order) $order = " ORDER BY $order";
$page = max(intval($page), 1);
$offset = $pagesize*($page-1);
$limit = " LIMIT $offset, $pagesize";
$r = $this->db->get_one("SELECT count(*) AS number FROM $this->table $where");
$number = $r['number'];
$this->pages = pages($number, $page, $pagesize);
$array = array();
$result = $this->db->query("SELECT * FROM $this->table $where $order $limit");
while($r = $this->db->fetch_array($result))
{
$array[] = $r;
}
$this->number = $this->db->num_rows($result);
$this->db->free_result($result);
return $array;
}
function disable($modelid, $disabled)
{
$modelid = intval($modelid);
if($modelid < 1) return false;
$this->db->query("UPDATE $this->table SET disabled=$disabled WHERE modelid=$modelid");
$this->cache();
return true;
}
function import($array)
{
if(!is_array($array) || empty($array)) return false;
if ($this->check_modelname($modelid, $array['name']))
{
$this->msg = '妯″瀷鍚嶇О宸茬粡瀛樺湪锛岃
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -