diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/DateTime.php b/module/VuFind/src/VuFind/Theme/Root/Helper/DateTime.php new file mode 100644 index 0000000000000000000000000000000000000000..9212c55c9f70dc935efe6460aecea473a37cf410 --- /dev/null +++ b/module/VuFind/src/VuFind/Theme/Root/Helper/DateTime.php @@ -0,0 +1,69 @@ +<?php +/** + * View helper for formatting dates and times. + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package View_Helpers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/building_a_recommendations_module Wiki + */ +namespace VuFind\Theme\Root\Helper; +use VuFind\Date\Converter as DateConverter, Zend\View\Helper\AbstractHelper; + +/** + * View helper for formatting dates and times + * + * @category VuFind2 + * @package View_Helpers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/building_a_recommendations_module Wiki + */ +class DateTime extends AbstractHelper +{ + protected $converter; + + /** + * Constructor + */ + public function __construct() + { + $this->converter = new DateConverter(); + } + + /** + * Extract a year from a human-readable date. Return false if no year can + * be found. + * + * @param string $date Date to reformat + * + * @return string|bool + */ + public function extractYear($date) + { + try { + return $this->converter->convertFromDisplayDate('Y', $date); + } catch (\VuFind\Exception\Date $e) { + // bad date? just ignore it! + return false; + } + } +} \ No newline at end of file diff --git a/themes/root/templates/RecordDriver/AbstractBase/export-bibtex.phtml b/themes/root/templates/RecordDriver/AbstractBase/export-bibtex.phtml index 3c1aa977f7b674b5eda5c561d0dfc6b04c4f151c..12214f6987d0e37e5c353cd615e2aab9f5094527 100644 --- a/themes/root/templates/RecordDriver/AbstractBase/export-bibtex.phtml +++ b/themes/root/templates/RecordDriver/AbstractBase/export-bibtex.phtml @@ -79,15 +79,11 @@ if (is_array($pubPlaces) && is_array($pubDates) && is_array($pubNames)) { } $date = trim($pubDates[$i], '[]. '); if (strlen($date) > 4) { - try { - $converter = new \VuFind\Date\Converter(); - $date = $converter->convertFromDisplayDate('Y', $date); - } catch (\VuFind\Exception\Date $e) { - // bad date? just ignore it! - continue; - } + $date = $this->dateTime()->extractYear($date); + } + if ($date) { + echo "year = $date,\n"; } - echo "year = $date,\n"; } } diff --git a/themes/root/templates/RecordDriver/AbstractBase/export-endnote.phtml b/themes/root/templates/RecordDriver/AbstractBase/export-endnote.phtml index 55990b79d982423b41649501ed12575650476368..cbe348f33dfcd7decc271e04886c16a8178bfd31 100644 --- a/themes/root/templates/RecordDriver/AbstractBase/export-endnote.phtml +++ b/themes/root/templates/RecordDriver/AbstractBase/export-endnote.phtml @@ -34,15 +34,11 @@ if (is_array($pubPlaces) && is_array($pubDates) && is_array($pubNames)) { } $date = trim($pubDates[$i], '[]. '); if (strlen($date) > 4) { - try { - $converter = new \VuFind\Date\Converter(); - $date = $converter->convertFromDisplayDate('Y', $date); - } catch (\VuFind\Exception\Date $e) { - // bad date? just ignore it! - continue; - } + $date = $this->dateTime()->extractYear($date); + } + if ($date) { + echo "%D $date\n"; } - echo "%D $date\n"; if (isset($pubPlaces[$i])) { echo "%C " . rtrim(str_replace(array('[', ']'), '', $pubPlaces[$i]), ': '). "\n"; } diff --git a/themes/root/templates/RecordDriver/AbstractBase/export-refworks.phtml b/themes/root/templates/RecordDriver/AbstractBase/export-refworks.phtml index e55b5e77b5f0ef68686a55467d966738424dfd04..6d55187694c11f112d7453a2da545066d77e6331 100644 --- a/themes/root/templates/RecordDriver/AbstractBase/export-refworks.phtml +++ b/themes/root/templates/RecordDriver/AbstractBase/export-refworks.phtml @@ -75,15 +75,11 @@ if (is_array($pubPlaces) && is_array($pubDates) && is_array($pubNames)) { } $date = trim($pubDates[$i], '[]. '); if (strlen($date) > 4) { - try { - $converter = new \VuFind\Date\Converter(); - $date = $converter->convertFromDisplayDate('Y', $date); - } catch (\VuFind\Exception\Date $e) { - // bad date? just ignore it! - continue; - } + $date = $this->dateTime()->extractYear($date); + } + if ($date) { + echo "YR $date\n"; } - echo "YR $date\n"; } } diff --git a/themes/root/theme.ini b/themes/root/theme.ini index 971c6c474eb988c431f3dceddc0a17025286baef..4eecd26456366c4596224a47c3378590dfcd67eb 100644 --- a/themes/root/theme.ini +++ b/themes/root/theme.ini @@ -10,6 +10,7 @@ helpers_to_register[] = "Cart" helpers_to_register[] = "Citation" helpers_to_register[] = "Context" helpers_to_register[] = "CurrentPath" +helpers_to_register[] = "DateTime" helpers_to_register[] = "DisplayDateFormat" helpers_to_register[] = "DisplayLanguageOption" helpers_to_register[] = "Excerpt"