westdc-zf1/application/module/Reference/RisOutput.php

154 lines
3.4 KiB
PHP

<?php
namespace Reference;
use \Helpers\View as view;
use \Helpers\dbh;
//use \Reference\Listener\RisOutputListener as Listener;
use \Files\Files;
use \Reference\Reference;
use \Reference\Ris;
use \LibRIS\RISReader;
use \LibRIS\RISTags;
use \LibRIS\RISWriter;
class RisOutput
{
private $db; //传入PDO对象.
private $config; //站点设置
private $dbh;
protected $events = NULL;
public $table;
public $ris_records = NULL;
protected $ris;
public $attr;
public $attr_flip;
function __construct($db = NULL,$mail = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
//$Listener = new Listener();
//@$this->events()->attachAggregate($Listener);
$this->table = new \Helpers\Table();
}
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 preRead($mode = "all")
{
if($mode == "all")
{
$sql = "SELECT * FROM {$this->table->reference} where length(ris)<10 or ris is null ORDER BY year DESC,title ASC,id ASC";
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
}//preRead
//将数据组成RIS数组格式
public function processArrayDataToRisData($arrayData,$risPrior = true,$mixAuthor = true,$mixTags = true)
{
if(!is_array($arrayData))
{
return false;
}
$risData = array();
if($risPrior === true)
{
$risReader = new RISReader();
}
$this->ris = new Ris();
$this->attr = $this->ris->attr;
$this->attr_flip = array_flip($this->ris->attr);
unset($this->ris);
$this->reference = new Reference();
foreach($arrayData as $k=>$v)
{
$risData[$k] = $this->transformToRis($v);
if($mixAuthor === true || $mixTags === true)
{
if($mixAuthor === true)
{
$author = $this->reference->getAuthorByReference($v['id'],true);
if(is_array($author) && count($author)>0)
{
$risData[$k] = array_merge($risData[$k],array("AU"=>$author));
}
unset($author);
}//mixAuthor
if($mixTags === true)
{
$tags = $this->reference->getTagsByReference($v['id'],true);
if(is_array($tags) && count($tags) > 0)
{
$risData[$k] = array_merge($risData[$k],array("KW"=>$tags));
}
unset($tags);
}
}
if(!is_array($risData[$k]) || count($risData[$k]) < 1)
{
unset($risData[$k]);
}
unset($arrayData[$k]);
}
return $risData;
}//processArrayDataToRisData
//单条记录的整编
public function transformToRis($record)
{
$arr = array();
foreach($record as $k=>$v)
{
if(!empty($v))
{
if(isset($this->attr_flip[$k]))
{
//echo $k ."-". $this->attr_flip[$k] . '-' .$v;
//echo "<br />";
$arr[$this->attr_flip[$k]] = array(0=>$v);
}
}
}
//echo "<br />";
return $arr;
}//transformToRis
//输出成文件
public function output($risData)
{
$risWirte = new RISWriter();
return @$risWirte->writeRecords($risData);
}//output
}