2014-12-23 14:32:23 +00:00
|
|
|
<?php
|
2014-12-30 16:31:57 +00:00
|
|
|
namespace Westdc\Reference;
|
2014-12-23 14:32:23 +00:00
|
|
|
|
2014-12-30 16:31:57 +00:00
|
|
|
use Zend\ServiceManager\ServiceManager;
|
|
|
|
use Zend\ServiceManager\ServiceManagerAwareInterface;
|
|
|
|
use LibRIS\RISReader;
|
|
|
|
use LibRIS\RISTags;
|
|
|
|
use LibRIS\RISWriter;
|
2014-12-23 14:32:23 +00:00
|
|
|
|
2014-12-30 16:31:57 +00:00
|
|
|
class RisExport implements ServiceManagerAwareInterface
|
2014-12-23 14:32:23 +00:00
|
|
|
{
|
2014-12-30 16:31:57 +00:00
|
|
|
protected $serviceManager;
|
|
|
|
private $table;
|
|
|
|
private $db;
|
|
|
|
|
2014-12-23 14:32:23 +00:00
|
|
|
public $ris_records = NULL;
|
|
|
|
protected $ris;
|
|
|
|
public $attr;
|
|
|
|
public $attr_flip;
|
2014-12-30 16:31:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
public function setServiceManager(ServiceManager $serviceManager)
|
2014-12-23 14:32:23 +00:00
|
|
|
{
|
2014-12-30 16:31:57 +00:00
|
|
|
$this->serviceManager = $serviceManager;
|
|
|
|
|
|
|
|
$this->init();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->table = new \stdClass;
|
|
|
|
$this->table->reference = "reference";
|
|
|
|
|
|
|
|
$dbService = $this->serviceManager->get('Db');
|
|
|
|
$this->db = $dbService->getPdo();
|
2014-12-23 14:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//读取数据
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-12-30 16:31:57 +00:00
|
|
|
$this->ris = $this->serviceManager->get('Reference/Ris');
|
2014-12-23 14:32:23 +00:00
|
|
|
$this->attr = $this->ris->attr;
|
|
|
|
$this->attr_flip = array_flip($this->ris->attr);
|
|
|
|
unset($this->ris);
|
|
|
|
|
2014-12-30 16:31:57 +00:00
|
|
|
$this->reference = $this->serviceManager->get('Reference');
|
2014-12-23 14:32:23 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|