From 61e788ab46403df7fbe89f194082c869b6fc8613 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Wed, 29 Aug 2012 14:44:30 -0400 Subject: [PATCH] Created date/time view helper to eliminate redundant logic in export templates. --- .../src/VuFind/Theme/Root/Helper/DateTime.php | 69 +++++++++++++++++++ .../AbstractBase/export-bibtex.phtml | 12 ++-- .../AbstractBase/export-endnote.phtml | 12 ++-- .../AbstractBase/export-refworks.phtml | 12 ++-- themes/root/theme.ini | 1 + 5 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 module/VuFind/src/VuFind/Theme/Root/Helper/DateTime.php 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 00000000000..9212c55c9f7 --- /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 3c1aa977f7b..12214f6987d 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 55990b79d98..cbe348f33df 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 e55b5e77b5f..6d55187694c 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 971c6c474eb..4eecd264563 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" -- GitLab