Beispiel #1
0
 /**
  * Download a citation for a publication
  *
  * @return  void
  */
 public function citationTask()
 {
     // Incoming
     $format = Request::getVar('type', 'bibtex');
     // Get our model and load publication data
     $this->model = new Models\Publication($this->_identifier, $this->_version);
     // Make sure we got a result from the database
     if (!$this->model->exists() || $this->model->isDeleted()) {
         App::redirect(Route::url('index.php?option=' . $this->_option), Lang::txt('COM_PUBLICATIONS_RESOURCE_NOT_FOUND'), 'error');
         return;
     }
     // Get version authors
     $authors = $this->model->table('Author')->getAuthors($this->model->version->get('id'));
     // Build publication path
     $path = $this->model->path('base', true);
     if (!is_dir($path)) {
         if (!Filesystem::makeDirectory(PATH_APP . $path, 0755, true, true)) {
             $this->setError('Error. Unable to create path.');
         }
     }
     // Build the URL for this resource
     $sef = Route::url($this->model->link('version'));
     $url = Request::base() . ltrim($sef, '/');
     // Choose the format
     switch ($format) {
         case 'endnote':
             $doc = "%0 " . Lang::txt('COM_PUBLICATIONS_GENERIC') . "\r\n";
             $doc .= "%D " . Date::of($this->model->published())->toLocal('Y') . "\r\n";
             $doc .= "%T " . trim(stripslashes($this->model->version->get('title'))) . "\r\n";
             if ($authors) {
                 foreach ($authors as $author) {
                     $name = $author->name ? $author->name : $author->p_name;
                     $auth = preg_replace('/{{(.*?)}}/s', '', $name);
                     if (!strstr($auth, ',')) {
                         $bits = explode(' ', $auth);
                         $n = array_pop($bits) . ', ';
                         $bits = array_map('trim', $bits);
                         $auth = $n . trim(implode(' ', $bits));
                     }
                     $doc .= "%A " . trim($auth) . "\r\n";
                 }
             }
             $doc .= "%U " . $url . "\r\n";
             if ($this->model->published()) {
                 $doc .= "%8 " . Date::of($this->model->published())->toLocal('M') . "\r\n";
             }
             if ($this->model->version->get('doi')) {
                 $doc .= "%1 " . 'doi:' . $this->model->version->get('doi');
                 $doc .= "\r\n";
             }
             $file = 'publication' . $this->model->get('id') . '.enw';
             $mime = 'application/x-endnote-refer';
             break;
         case 'bibtex':
         default:
             include_once PATH_CORE . DS . 'components' . DS . 'com_citations' . DS . 'helpers' . DS . 'BibTex.php';
             $bibtex = new \Structures_BibTex();
             $addarray = array();
             $addarray['type'] = 'misc';
             $addarray['cite'] = Config::get('sitename') . $this->model->get('id');
             $addarray['title'] = stripslashes($this->model->version->get('title'));
             if ($authors) {
                 $i = 0;
                 foreach ($authors as $author) {
                     $name = $author->name ? $author->name : $author->p_name;
                     $author_arr = explode(',', $name);
                     $author_arr = array_map('trim', $author_arr);
                     $addarray['author'][$i]['first'] = isset($author_arr[1]) ? $author_arr[1] : '';
                     $addarray['author'][$i]['last'] = isset($author_arr[0]) ? $author_arr[0] : '';
                     $i++;
                 }
             }
             $addarray['month'] = Date::of($this->model->published())->toLocal('M');
             $addarray['url'] = $url;
             $addarray['year'] = Date::of($this->model->published())->toLocal('Y');
             if ($this->model->version->get('doi')) {
                 $addarray['doi'] = 'doi:' . DS . $this->model->version->get('doi');
             }
             $bibtex->addEntry($addarray);
             $file = 'publication_' . $this->model->get('id') . '.bib';
             $mime = 'application/x-bibtex';
             $doc = $bibtex->bibTex();
             break;
     }
     // Write the contents to a file
     $fp = fopen($path . DS . $file, "w") or die("can't open file");
     fwrite($fp, $doc);
     fclose($fp);
     $this->_serveup(false, $path, $file, $mime);
     die;
     // REQUIRED
 }
if (PEAR::isError($ret)) {
    print $ret->getMessage();
    die;
}
$bibtex->parse();
//Creating an entry
$addarray = array();
$addarray['entryType'] = 'Article';
$addarray['cite'] = 'art2';
$addarray['title'] = 'Titel2';
$addarray['author'][0]['first'] = 'John';
$addarray['author'][0]['last'] = 'Doe';
$addarray['author'][1]['first'] = 'Jane';
$addarray['author'][1]['last'] = 'Doe';
//Adding the entry
$bibtex->addEntry($addarray);
//Printing the result
echo "Converting This Array:\n\n";
echo "<pre>";
print_r($bibtex->data);
echo "\nInto this:\n\n";
echo $bibtex->bibTex();
echo "<hr />";
echo "\nAnd here is the RTF String:\n\n";
echo $bibtex->rtf();
echo "<hr />";
echo "\nAnd here are the data in  HTML:\n\n";
echo $bibtex->html();
echo "<hr />";
echo "\nAnd here is the statistic:\n\n";
print_r($bibtex->getStatistic());
Beispiel #3
0
 /**
  * Format the file
  *
  * @param      object $row Record to format
  * @return     string
  */
 public function format($row)
 {
     // get fields to not include for all citations
     $config = \Component::params('com_citations');
     $exclude = $config->get('citation_download_exclude', '');
     if (strpos($exclude, ',') !== false) {
         $exclude = str_replace(',', "\n", $exclude);
     }
     $exclude = array_values(array_filter(array_map('trim', explode("\n", $exclude))));
     //get fields to not include for specific citation
     $cparams = new \Hubzero\Config\Registry($row->params);
     $citation_exclude = $cparams->get('exclude', '');
     if (strpos($citation_exclude, ',') !== false) {
         $citation_exclude = str_replace(',', "\n", $citation_exclude);
     }
     $citation_exclude = array_values(array_filter(array_map('trim', explode("\n", $citation_exclude))));
     //merge overall exclude and specific exclude
     $exclude = array_values(array_unique(array_merge($exclude, $citation_exclude)));
     include_once dirname(__DIR__) . DS . 'helpers' . DS . 'BibTex.php';
     $bibtex = new \Structures_BibTex();
     $addarray = array();
     //get all the citation types
     $db = \App::get('db');
     $ct = new Type($db);
     $types = $ct->getType();
     //find the right title
     $type = '';
     foreach ($types as $t) {
         if ($t['id'] == $row->type) {
             $type = $t['type'];
         }
     }
     $type = $type != '' ? $type : 'Generic';
     if (!$row->cite) {
         $au = new \Components\Citations\Tables\Author($db);
         $authors = $au->getRecords(array('cid' => $row->id, 'start' => 0, 'limit' => 1));
         foreach ($authors as $author) {
             $row->cite .= strtolower($author->surname);
         }
         $row->cite .= $row->year;
         $t = preg_replace('/[^a-zA-Z0-9]/', '', strtolower($row->title));
         $row->cite .= strlen($t) > 10 ? substr($t, 0, 10) : $t;
     }
     $addarray['type'] = $type;
     $addarray['cite'] = $row->cite;
     $addarray['title'] = $row->title;
     $addarray['address'] = $row->address;
     $auths = explode(';', $row->author);
     for ($i = 0, $n = count($auths); $i < $n; $i++) {
         $author = trim($auths[$i]);
         $author_arr = explode(',', $author);
         $author_arr = array_map('trim', $author_arr);
         $addarray['author'][$i]['first'] = isset($author_arr[1]) ? $author_arr[1] : '';
         $addarray['author'][$i]['last'] = isset($author_arr[0]) ? $author_arr[0] : '';
         $addarray['author'][$i]['first'] = preg_replace('/\\{\\{\\d+\\}\\}/', '', $addarray['author'][$i]['first']);
         $addarray['author'][$i]['last'] = preg_replace('/\\{\\{\\d+\\}\\}/', '', $addarray['author'][$i]['last']);
     }
     $addarray['booktitle'] = $row->booktitle;
     $addarray['chapter'] = $row->chapter;
     $addarray['edition'] = $row->edition;
     $addarray['editor'] = $row->editor;
     $addarray['eprint'] = $row->eprint;
     $addarray['howpublished'] = $row->howpublished;
     $addarray['institution'] = $row->institution;
     $addarray['journal'] = $row->journal;
     $addarray['key'] = $row->key;
     $addarray['location'] = $row->location;
     $addarray['month'] = $row->month != 0 || $row->month != '0' ? $row->month : '';
     $addarray['note'] = $row->note;
     $addarray['number'] = $row->number;
     $addarray['organization'] = $row->organization;
     $addarray['pages'] = $row->pages != 0 || $row->pages != '0' ? $row->pages : '';
     $addarray['publisher'] = $row->publisher;
     $addarray['series'] = $row->series;
     $addarray['school'] = $row->school;
     $addarray['url'] = $row->url;
     $addarray['volume'] = $row->volume;
     $addarray['year'] = $row->year;
     if ($row->journal != '') {
         $addarray['issn'] = $row->isbn;
     } else {
         $addarray['isbn'] = $row->isbn;
     }
     $addarray['doi'] = $row->doi;
     $addarray['language'] = $row->language;
     $addarray['accession_number'] = $row->accession_number;
     $addarray['short_title'] = html_entity_decode($row->short_title);
     $addarray['author_address'] = $row->author_address;
     $addarray['keywords'] = str_replace("\r\n", ', ', $row->keywords);
     $addarray['abstract'] = $row->abstract;
     $addarray['call_number'] = $row->call_number;
     $addarray['label'] = $row->label;
     $addarray['research_notes'] = $row->research_notes;
     foreach ($addarray as $k => $v) {
         if (in_array($k, $exclude)) {
             unset($addarray[$k]);
         }
     }
     $bibtex->addEntry($addarray);
     //$file = 'download_'.$id.'.bib';
     //$mime = 'application/x-bibtex';
     $doc = $bibtex->bibTex();
     return $doc;
 }
Beispiel #4
0
 /**
  * Download a citation for a resource
  *
  * @return     void
  */
 public function citationTask()
 {
     $yearFormat = 'Y';
     $monthFormat = 'M';
     // Get contribtool params
     $tconfig = Component::params('com_tools');
     // Incoming
     $id = Request::getInt('id', 0);
     $format = Request::getVar('format', 'bibtex');
     // Append DOI handle
     $revision = Request::getVar('rev', 0);
     $handle = '';
     if ($revision) {
         $rdoi = new Doi($this->database);
         $rdoi->loadDoi($id, $revision);
         if (isset($rdoi->doi) && $rdoi->doi && $tconfig->get('doi_shoulder')) {
             $handle = 'doi:' . $tconfig->get('doi_shoulder') . DS . strtoupper($rdoi->doi);
         } else {
             if ($rdoi->doi_label) {
                 $handle = 'doi:10254/' . $tconfig->get('doi_prefix') . $id . '.' . $rdoi->doi_label;
             }
         }
     }
     // Load the resource
     $row = new Resource($this->database);
     $row->load($id);
     $thedate = $row->publish_up != '0000-00-00 00:00:00' ? $row->publish_up : $row->created;
     $helper = new Helper($row->id, $this->database);
     $helper->getUnlinkedContributors();
     $row->author = $helper->ul_contributors;
     // Build the download path
     $path = PATH_APP . $this->config->get('cachepath', '/cache/resources');
     $date = $row->created;
     $dir_resid = \Hubzero\Utility\String::pad($row->id);
     if ($date && preg_match("#([0-9]{4})-([0-9]{2})-([0-9]{2})[ ]([0-9]{2}):([0-9]{2}):([0-9]{2})#", $date, $regs)) {
         $date = mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
     }
     $dir_year = date('Y', $date);
     $dir_month = date('m', $date);
     $path .= DS . $dir_year . DS . $dir_month . DS . $dir_resid . DS;
     if (!is_dir($path)) {
         if (!\Filesystem::makeDirectory($path)) {
             $this->setError('Error. Unable to create path.');
         }
     }
     // Build the URL for this resource
     $sef = Route::url('index.php?option=' . $this->_option . '&id=' . $row->id);
     $url = Request::base() . ltrim($sef, '/');
     // Choose the format
     switch ($format) {
         case 'endnote':
             $doc = '';
             switch ($row->type) {
                 case 'misc':
                 default:
                     $doc .= "%0 " . Lang::txt('COM_RESOURCES_GENERIC') . "\r\n";
                     break;
                     // generic
             }
             $doc .= "%D " . Date::of($thedate)->toLocal($yearFormat) . "\r\n";
             $doc .= "%T " . trim(stripslashes($row->title)) . "\r\n";
             $author_array = explode(';', $row->author);
             foreach ($author_array as $auth) {
                 $auth = preg_replace('/{{(.*?)}}/s', '', $auth);
                 if (!strstr($auth, ',')) {
                     $bits = explode(' ', $auth);
                     $n = array_pop($bits) . ', ';
                     $bits = array_map('trim', $bits);
                     $auth = $n . trim(implode(' ', $bits));
                 }
                 $doc .= "%A " . trim($auth) . "\r\n";
             }
             $doc .= "%U " . $url . "\r\n";
             if ($thedate) {
                 $doc .= "%8 " . Date::of($thedate)->toLocal($monthFormat) . "\r\n";
             }
             //$doc .= "\r\n";
             if ($handle) {
                 $doc .= "%1 " . 'doi:' . $handle;
                 $doc .= "\r\n";
             }
             $file = 'resource_' . $id . '.enw';
             $mime = 'application/x-endnote-refer';
             break;
         case 'bibtex':
         default:
             include_once PATH_CORE . DS . 'components' . DS . 'com_citations' . DS . 'helpers' . DS . 'BibTex.php';
             $bibtex = new \Structures_BibTex();
             $addarray = array();
             $addarray['type'] = 'misc';
             $addarray['cite'] = $this->_config['sitename'] . $row->id;
             $addarray['title'] = stripslashes($row->title);
             $auths = explode(';', $row->author);
             for ($i = 0, $n = count($auths); $i < $n; $i++) {
                 $author = trim($auths[$i]);
                 $author = preg_replace('/\\{\\{(.+)\\}\\}/i', '', $author);
                 if (strstr($author, ',')) {
                     $author_arr = explode(',', $author);
                     $author_arr = array_map('trim', $author_arr);
                     $addarray['author'][$i]['first'] = isset($author_arr[1]) ? trim($author_arr[1]) : '';
                     $addarray['author'][$i]['last'] = isset($author_arr[0]) ? trim($author_arr[0]) : '';
                 } else {
                     $author_arr = explode(' ', $author);
                     $author_arr = array_map('trim', $author_arr);
                     $last = array_pop($author_arr);
                     $addarray['author'][$i]['first'] = count($author_arr) > 0 ? implode(' ', $author_arr) : '';
                     $addarray['author'][$i]['last'] = $last ? trim($last) : '';
                 }
             }
             $addarray['month'] = Date::of($thedate)->toLocal($monthFormat);
             $addarray['url'] = $url;
             $addarray['year'] = Date::of($thedatez)->toLocal($yearFormat);
             if ($handle) {
                 $addarray['doi'] = $handle;
             }
             $bibtex->addEntry($addarray);
             $file = 'resource_' . $id . '.bib';
             $mime = 'application/x-bibtex';
             $doc = $bibtex->bibTex();
             break;
     }
     // Write the contents to a file
     $fp = fopen($path . $file, "w") or die("can't open file");
     fwrite($fp, $doc);
     fclose($fp);
     $this->_serveup(false, $path, $file, $mime);
     die;
     // REQUIRED
 }