Skip to content
Snippets Groups Projects
Commit 61e788ab authored by Demian Katz's avatar Demian Katz
Browse files

Created date/time view helper to eliminate redundant logic in export templates.

parent 60be1baf
No related merge requests found
<?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
......@@ -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";
}
}
......
......@@ -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";
}
......
......@@ -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";
}
}
......
......@@ -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"
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment