db = \Zend_Registry::get('db'); }else{ $this->db = $db; } $this->config = \Zend_Registry::get('config'); //$Listener = new RisListener(); //@$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; } //ris导入 public function loadout() { $file = $this->uploadRisFile(); $text = $this->loadRisText(); if(empty($text) && $file === false) { return "导入失败,请选择要导入的文件或直接使用ris文本"; } $records = array(); if($file !== false) { $records = array_merge($records,$this->processRis($file,NULL)); } if(!empty($text)) { $records = array_merge($records,$this->processRis(NULL,$text)); } $data = $this->reBuildRisArray($records); return $data; //view::Dump($records,false); } //上传RIS文件 public function uploadRisFile() { $file = $_FILES['Filedata']; if (@is_uploaded_file($file['tmp_name']) === false) { return false; } return $file; } //文本直接导入 public function loadRisText() { $text = $_REQUEST['ristest']; return $text; } //处理ris文件 public function processRis($file = NULL,$text = NULL) { $ris = new RISReader(); if(!empty($file) && empty($text)) { $ris->parseFile($file['tmp_name']); }else{ $ris->parseString($text); } $records = $ris->getRecords(); return $records; } //对解析过的数据进行编排 public function reBuildRisArray($records) { $data = array(); foreach($records as $k=>$ref) { $data[$k] = array(); foreach($ref as $index=>$value) { if(isset($this->attr[$index])) { $index_name = $this->attr[$index]; if(count($value) > 1) { $data[$k][$index_name] = array(); foreach($value as $item) { $data[$k][$index_name][] = $item; } }else{ $data[$k][$index_name] = $value[0]; } } } } unset($records); return $data; } //将解析好的ris数据写入数据库 public function pushToDataTable($data){ if(!is_array($data) || count($data) < 1) { return false; } $dbh = new dbh(); foreach($data as $k=>$ref) { $tags = $ref['tags']; $author = $ref['author']; unset($ref['tags']); unset($ref['author']); $ref['reference'] = $ref['title'].'---'.date("Y-m-d H:i:s").".".microtime().rand(); $id = $dbh->insert($this->table->reference,$ref,true); if(is_array($tags) && count($tags) > 0) { foreach($tags as $v) { $dbh->insert($this->table->reference_tag,array('id'=>$id,'tag'=>$v)); } } $index = 0; if(is_array($author) && count($author) > 0) { foreach($author as $v) { $index ++ ; $author_splited = $this->splitAuthor($v); $dbh->insert($this->table->reference_author,array('id'=>$id , 'lastname'=>$author_splited['lastname'] , 'firstname'=>$author_splited['firstname'] , 'place'=>$index )); } } } } //将作者名字分割为数组 public function splitAuthor($author){ if(preg_match("/\,/",$author)) { $arr = explode(",",$author); return array( 'firstname' => trim($arr[0]), 'lastname' => trim($arr[1]) ); }else{ return array( 'firstname' => '', 'lastname' => trim($author) ); } } public $attr = array( 'TP' => 'type', 'TI' => 'title', 'AU' => 'author', 'PY' => 'year', 'LA' => 'language', 'KW' => 'tags', 'AB' => 'abstract', 'DO' => 'doi', 'PB' => 'publisher' ); }