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

249 lines
5.8 KiB
PHP

<?php
class Region
{
private $db; //传入PDO对象.
private $gdb; //geonetwork database
protected $events = NULL; //事件
public $tbl_regions = "regions";
public $tbl_regionsdes = "regionsdes";
function __construct($db)
{
$this->db = $db;
$this->config = Zend_Registry::get('config');
$this->geonetwork();
}
private function geonetwork(){
$db = Zend_Db::factory($this->config->geonetwork);
Zend_Db_Table::setDefaultAdapter($db);
$this->gdb = $db;
}
public function events(Zend_EventManager_EventCollection $events = NULL)
{
if ($events !== NULL) {
$this->events = $events;
} elseif ($this->events === NULL) {
$this->events = new Zend_EventManager_EventManager(__CLASS__);
}
return $this->events;
}
public function fetchRegion($type = NULL,$filter = NULL)
{
if(empty($type))
{
$sql = "SELECT * FROM ".$this->tbl_regions." ORDER BY id ASC";
}else{
$wheresql = array();
if(!empty($filter))
{
if(isset($filter['q']))
{
$wheresql[] = " rs.label LIKE '%".$filter['q']."%'";
}
}
if(count($wheresql)<1)
$wheresql = "";
else
$wheresql = " WHERE ".join(" AND ",$wheresql);
$sql = "SELECT rs.*,r.* FROM ".$this->tbl_regions." r
LEFT JOIN ".$this->tbl_regionsdes." rs ON r.id=rs.iddes
$wheresql
ORDER BY r.id ASC";
}
$rs = $this->gdb->query($sql);
return $rs->fetchAll();
}//列表
public function get($id)
{
$sql = "SELECT * FROM ".$this->tbl_regions." WHERE id=$id";
$rs = $this->gdb->query($sql);
$data = $rs->fetch();
$sql = "SELECT * FROM ".$this->tbl_regionsdes. " WHERE iddes=$id";
$rs = $this->gdb->query($sql);
$data['info'] = $rs->fetchAll();
return $data;
}//获取单个
public function del($id,$langid = NULL)
{
if(empty($langid))
{
$this->gdb->exec("DELETE FROM ".$this->tbl_regionsdes." WHERE iddes=$id");
$this->gdb->exec("DELETE FROM ".$this->tbl_regions." WHERE id=$id");
return true;
}else{
return $this->gdb->exec("DELETE FROM ".$this->tbl_regionsdes." WHERE iddes=$id AND langid='$langid'");
}
}//shanchui
public function addRegion($data,$id = '')
{
$results = $this->events()->trigger('addRegion.pre', $this, compact('data'));
$data = $results->bottom();
if(is_string($data))
return $data;
$results = $this->events()->trigger('addRegion.check', $this, compact('data','id'));
$data = $results->bottom();
if(is_string($data))
return $data;
include_once("helper/dbh.php");
$dbh = new dbh($this->gdb);
$cache_data = $data;
unset($data['info']);
if(empty($id))
{
$id = $dbh->insert($this->tbl_regions,$data,true);
if($id > 0)
{
$s = true;
}
}else{
$s = $dbh->update($this->tbl_regions,$data,"id=$id");
}
$results = $this->events()->trigger('addRegion.post', $this, compact('cache_data','id'));
if($s)
{
return true;
}else{
return "添加失败,请重试";
}
}//增加
}
class RegionListener implements Zend_EventManager_ListenerAggregate
{
private $db; //传入PDO对象.
private $gdb; //geonetwork database
private $id_start = 5000;
private $id_end = 9999;
public $tbl_regions = "regions";
public $tbl_regionsdes = "regionsdes";
function __construct($db = NULL)
{
if(empty($db))
{
$this->db = Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = Zend_Registry::get('config');
$this->geonetwork();
}
private function geonetwork(){
$db = Zend_Db::factory($this->config->geonetwork);
Zend_Db_Table::setDefaultAdapter($db);
$this->gdb = $db;
}
public function attach(Zend_EventManager_EventCollection $events)
{
$events->attach('addRegion.pre', array($this, 'scanField'), 100);
$events->attach('addRegion.check', array($this, 'checkParam'), 80);
$events->attach('addRegion.post', array($this, 'process'), 50);
}
public function detach(Zend_EventManager_EventCollection $events)
{
}
//扫描字段
function scanField($e){
$data = $e->getParam('data');
if(!is_array($data))
{
return false;
}
$data['east'] = (float)$data['east'];
$data['west'] = (float)$data['west'];
$data['south'] = (float)$data['south'];
$data['north'] = (float)$data['north'];
if(empty($data['east']) || empty($data['west']) || empty($data['south']) || empty($data['north']))
{
return "请填写四至范围";
}
foreach($data['info'] as $k=>$v)
{
if(empty($v['langid']) && empty($v['label']))
{
unset($data['info'][$k]);
}
}
return $data;
}//scanField
//检查参数
function checkParam($e){
$data = $e->getParam('data');
$id = $e->getParam('id');
if(empty($id))
{
$sql = "SELECT * FROM ".$this->tbl_regions."
WHERE north=".$data['north']." AND south=".$data['south']." AND west=".$data['west']." AND east=".$data['east'];
$rs = $this->gdb->query($sql);
$row = $rs->fetch();
if(!empty($row['id']))
{
return "此范围已存在";
}
$sql = "select max(id) as c FROM ".$this->tbl_regions." WHERE id>=".$this->id_start." AND id<".$this->id_end;
$rs = $this->gdb->query($sql);
$row = $rs->fetch();
if(empty($row['c']))
{
$data['id'] = $this->id_start;
}else{
$data['id'] = $row['c'] + 1;
}
}
return $data;
}//checkParam
function process($e)
{
$data = $e->getParam('cache_data');
$id = $e->getParam('id');
$this->gdb->exec("DELETE FROM ".$this->tbl_regionsdes." WHERE iddes=$id");
foreach($data['info'] as $k=>$v)
{
$this->gdb->insert($this->tbl_regionsdes,array('iddes'=>$id,'langid'=>$v['langid'],'label'=>$v['label']));
}
}
}