westdc-zf1/application/models/MetaData.php

242 lines
4.5 KiB
PHP
Raw Normal View History

<?php
/**
* Metadata 元数据
*/
class Metadata
{
public $db; //PDO
public $tbl_Metadata = "metadata"; //元数据数据表
public $tbl_Metadata_temp = "metadata_temp"; //新元数据缓存表
public $SavePath = "../data/metadata_temp/"; //临时元数据保存地址
public $SaveMethod = "JSON"; //临时元数据保存格式 XML or JSON
public $XMLNamespace = "metadata_temp"; //临时元数据XML的根域名称
public $MetadataFields; //元数据涉及的字段
function __construct($db)
{
$this->db = $db;
$this->_init_Fields();
}
//初始化简化版元数据的字段
function _init_Fields(){
$this->MetadataFields = array(
//中文标题
array(
'Title' => '中文标题',
'FieldName' => 'title',
'FieldName_temp' => 'title',
'Type' => 'varchar',
'MaxLength' => 200,
'Required' => true
),
//英文标题
array(
'Title' => '英文标题',
'FieldName' => 'title_en',
'FieldName_temp' => 'title_en',
'Type' => 'varchar',
'MaxLength' => 200,
'Required' => true
),
//摘要
array(
'Title' => '摘要',
'FieldName' => 'description',
'FieldName_temp' => 'description',
'Type' => 'text',
'MaxLength' => 200,
'Required' => true
),
//开始时间
array(
'Title' => '开始时间',
'FieldName' => 'timebegin',
'FieldName_temp' => 'timebegin',
'Type' => 'varchar',
'MaxLength' => 24,
'Required' => true
),
//结束时间
array(
'Title' => '结束时间',
'FieldName' => 'timeend',
'FieldName_temp' => 'timeend',
'Type' => 'varchar',
'MaxLength' => 24,
'Required' => true
),
//空间范围-东
array(
'Title' => '东',
'FieldName' => 'east',
'FieldName_temp' => 'east',
'Type' => 'varchar',
'MaxLength' => 20,
'Required' => true
),
//空间范围-南
array(
'Title' => '南',
'FieldName' => 'south',
'FieldName_temp' => 'south',
'Type' => 'varchar',
'MaxLength' => 20,
'Required' => true
),
//空间范围-西
array(
'Title' => '西',
'FieldName' => 'west',
'FieldName_temp' => 'west',
'Type' => 'varchar',
'MaxLength' => 20,
'Required' => true
),
//空间范围-北
array(
'Title' => '北',
'FieldName' => 'north',
'FieldName_temp' => 'north',
'Type' => 'varchar',
'MaxLength' => 20,
'Required' => true
),
);
}//_init_Fields()
//记录
function Record($uid,$data,$uuid=''){
if(!is_array($data) || count($data)<1)
{
return false;
}
$content = "";
if($this->SaveMethod == "XML")
{
$content = $this->RecordXML($data);
}
else if($this->SaveMethod == "JSON")
{
$content = $this->RecordJSON($data);
}
else{
return false;
}
$update = 0;
if(empty($uuid))
{
include_once("uuid.php");
@$unqid = new uuid();
$uuid = @$unqid->toString();
}else{
$update = 1;
}
if(!preg_match("/^[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{12}$/",$uuid))
{
return false;
}
//添加一条新记录
if($update == 0)
{
$temp = array(
"uuid" => $uuid,
"userid" => $uid,
"content" => $content,
);
if($this->db->insert($this->tbl_Metadata_temp,$temp))
{
return $uuid;
}else{
return false;
}
}//update == 0
//编辑或者更新已存在的记录
else{
$temp = array(
"content" => $content,
"ts_update" => 'now()'
);
$whereSql = "userid=$uid AND uuid='$uuid'";
if($this->db->update($this->tbl_Metadata_temp,$temp,$whereSql))
{
return $uuid;
}else{
return false;
}
}//update == 1
}//Record()
//返回XML内容
function RecordXML($data){
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$rootelement =$dom->createElement($this->XMLNamespace);
foreach($data as $dk=>$dv)
{
foreach($this->MetadataFields as $k=>$v)
{
if($dk == $v['FieldName'])
{
$nod = $dom->createElement($v['FieldName'], $dv);
$rootelement->appendChild($nod);
}
}
}
$dom->appendChild($rootelement);
return $dom->saveHTML();
}//RecordXML
//返回JSON内容
function RecordJSON($data)
{
$content = json_encode($data);
return $content;
}//RecordJSON
}