westdc-zf1/application/models/data/DataSpatial.php

181 lines
4.8 KiB
PHP
Raw Normal View History

<?php
class DataSpatial extends Zend_Controller_Plugin_Abstract
{
private $db; //传入PDO对象.
private $auth = NULL; //Zend_Auth 对象
//使用到的公共变量
public $tbl = "dataspatial";
function __construct($db)
{
$this->db = $db;
}
function fetch($uid=0,$keyword='')
{
$wheresql=' 1=1 ';
if(!empty($keyword))
{
if(preg_match("/\'/",$keyword))
{
$keyword = preg_replace("/\'/","''",$keyword);
}
$wheresql.=" and m.title like '%$keyword%'";
}
if(empty($uid))
{
$sql = "SELECT m.title,d.id,d.uuid,st_astext(d.the_geom) as spatial,d.temporal,d.ts_changed FROM ".$this->tbl." d left join metadata m on d.uuid=m.uuid where ".$wheresql."ORDER BY d.ts_changed desc";
}else{
$sql = "SELECT m.title,d.id,d.uuid,st_astext(d.the_geom) as spatial,d.temporal,d.ts_changed FROM ".$this->tbl." d
left join metadata m on d.uuid=m.uuid
LEFT JOIN mdauthor a ON d.uuid=a.uuid
WHERE a.userid=".$uid." AND a.status>0 and ".$wheresql."
ORDER BY d.ts_published desc,d.ts_submitted desc,d.ts_created DESC
";
}
$rs = $this->db->query($sql);
$rows = $rs->fetchAll();
return $rows;
}
function update($data,$uuid,$uid=0){
if(!empty($uid))
{
include_once("data/Author.php");
$author = new Author($this->db);
if($author->checkAuthor($uuid,$uid)==false)
{
return "您没有权限";
}
}
$this->db->beginTransaction();
try
{
foreach($data['info'] as $item)
{
if ($item['id']>0)
{
//update
if (isset($item['temporal']))
$sql="update $this->tbl set the_geom=ST_GeometryFromText('SRID=4326;".$item['spatial']."'),temporal='".$item['temporal']."',ts_changed=now() where id=".$item['id']." and uuid='".$data['uuid']."'";
else
$sql="update $this->tbl set the_geom=ST_GeometryFromText('SRID=4326;".$item['spatial']."'),ts_changed=now() where id=".$item['id']." and uuid='".$data['uuid']."'";
} else {
//insert
if (isset($item['temporal']))
$sql="insert into $this->tbl (uuid,the_geom,temporal) values('".$data['uuid']."',ST_GeometryFromText('SRID=4326;".$item['spatial']."'),'".$item['temporal']."')";
else
$sql="insert into $this->tbl (uuid,the_geom) values('".$data['uuid']."',ST_GeometryFromText('SRID=4326;".$item['spatial']."'))";
}
$this->db->exec($sql);
}
$this->db->commit();
return true;
} catch (Exception $e) {
$this->db->rollBack();
return $e->getMessage();
}
}
function view($id)
{
if(is_numeric($id))
{
$sql = "SELECT m.title,d.id,d.uuid,st_astext(d.the_geom) as spatial,d.temporal,d.ts_changed FROM ".$this->tbl." d left join metadata m on d.uuid=m.uuid WHERE d.id=$id";
}else{
$sql = "SELECT m.title,d.id,d.uuid,st_astext(d.the_geom) as spatial,d.temporal,d.ts_changed FROM ".$this->tbl." d left join metadata m on d.uuid=m.uuid WHERE d.uuid='$id'";
}
$rs = $this->db->query($sql);
$row = $rs->fetchAll();
return $row;
}
function delete($id,$uid=0){
if(!is_numeric($id))
{
return "参数错误";
}
if($uid != 0){
$sql = "SELECT uuid FROM ".$this->tbl." WHERE id=$id";
$rs = $this->db->query($sql);
$row = $rs->fetch();
if(isset($row['uuid']) && !empty($row['uuid'])){
include_once("data/Author.php");
$author = new Author($this->db);
if($author->checkAuthor($row['uuid'],$uid)==false)
{
return "您没有权限";
}
$condition = " id=$id ";
$sql = "DELETE FROM ".$this->tbl." WHERE $condition";
return $this->db->exec($sql);
}else{
return "该记录不存在";
}
}else{
$condition = " id=$id ";
$sql = "DELETE FROM ".$this->tbl." WHERE $condition";
return $this->db->exec($sql);
}
}
function _getParams(Zend_Controller_Request_Abstract $request)
{
$data = array(
'uuid' => trim($request->getParam('uuid')),
'info'=>$_POST['info']
);
return $data;
}
function checkinfo($info){
if(!is_array($info)){
return NULL;
}
foreach($info as $k=>$v)
{
if(empty($v['spatial']) && empty($v['temporal']))
{
unset($info[$k]);
}else{
if(empty($v['spatial']))
{
return "请输入 $k 中的空间范围";
}
}
}
return $info;
}
function mkspatial($e,$s,$w,$n)
{
$str='GeometryCollection(';
if (($e==$w) && ($s==$n))
{
$str.="Point($e $s)";
} else {
$str.="Polygon(($e $s,$e $n,$w $n,$w $s,$e $s))";
}
$str.=')';
return $str;
}
function mktemporal($begin,$end=null)
{
if (!$begin) return '';
$str='[';
$begin=date('Y-m-d',strtotime($begin));
if (isset($end)) $end=date('Y-m-d',strtotime($end));
if (isset($end) && ($end!=$begin))
{
$str.="$begin,$end";
} else {
$str.="$begin,$begin";
}
$str.=']';
return $str;
}
}