Benutzer: Gast
Eintrag: Extbase: DateStartEndViewHelper

Extbase: DateStartEndViewHelper

von: marcel
Tags: Typo3, Extbase
Kreise: HTML / CSS / JS
Code: PHP
<?php
namespace VENDOR\Exttitle\ViewHelpers\Format;

/*                                                                        *
 * This script belongs to the FLOW3 package "Fluid".                      *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License as published by the *
 * Free Software Foundation, either version 3 of the License, or (at your *
 * option) any later version.                                             *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
 * General Public License for more details.                               *
 *                                                                        *
 * You should have received a copy of the GNU Lesser General Public       *
 * License along with the script.                                         *
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

/**
 * Formats a Start- and End-Timestamp or Start- and End-DateTime-Object in strftime() by trying to prevent showing double values like
 * year, month or day, if the values are the same in start- and end-date
 */
class DateStartEndViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {

  /**
   * Render the supplied DateTime object as a formatted date.
   *
   * @param mixed   $start               either a DateTime object or a string (UNIX-Timestamp) for the start-date
   * @param mixed   $end                 either a DateTime object or a string (UNIX-Timestamp) for the end-date
   * @param boolean $showTime
   * @param string  $weekday
   * @param string  $day
   * @param string  $month
   * @param string  $year
   * @param string  $time
   * @param string  $limiter
   * @return string Formatted date
   */
  public function render(
    $start = NULL,
    $end = NULL,
    $showTime = true,
    $weekday = '%A, den ',
    $day = '%e.',
    $month = '%B',
    $year = ' %Y',
    $time = '%H:%M Uhr',
    $limiter = ' - '
    ) {

    $start = self::getTs($start);
    $end = self::getTs($end);

    $content = "";
    if ($start == 0) {
      return "";
    }
    
    // get the strftime'd values
    $startArr = array(
      'weekday' => ($weekday)  ? strftime($weekday, $start) : "",
      'day'     => ($day)  ? strftime($day, $start) : "",
      'month'   => ($month)  ? strftime($month, $start) : "",
    	'year'    => ($year)  ? strftime($year, $start) : "",
      'time'    => ($time)  ? strftime($time, $start) : "",
    );
    
    if (!$showTime) {
      unset($startArr['time']);
    }
    
    if ($end) {
      $endArr = array(
        'weekday' => ($weekday)  ? strftime($weekday, $end) : "",
        'day'     => ($day)  ? strftime($day, $end) : "",
        'month'   => ($month)  ? strftime($month, $end) : "",
      	'year'    => ($year)  ? strftime($year, $end) : "",
        'time'    => ($time)  ? strftime($time, $end) : "",
      );
      
      
      if (!$showTime) {
        unset($startArr['time']);
        unset($endArr['time']);
      }
      
      if ($startArr['year'] == $endArr['year'] && $startArr['month'] == $endArr['month'] && $startArr['day'] == $endArr['day']) {
        unset($startArr['day']);
        unset($startArr['month']);
        unset($startArr['year']);
        
        if ($showTime) {
          if ($startArr['time'] != $endArr['time']) {
            $timeStr = " " . $startArr['time'] . " - " . $endArr['time'];
            unset($startArr['time']);
            unset($endArr['time']);
          } else {
            unset($startArr['time']);
          }
        }
        $limiter = "";
      } else  if ($startArr['year'] == $endArr['year'] && $startArr['month'] == $endArr['month']) {
        unset($startArr['month']);
        unset($startArr['year']);
      } else if ($startArr['year'] == $endArr['year']) {
        unset($startArr['year']);
      }
      
      
      
      return implode("", $startArr) . $limiter . implode("", $endArr) . $timeStr;
    } else {
      return implode("", $startArr);
    }
  }

  /**
   * @param mixed $date
   * @return integer Timestamp
   */
  private function getTs($date) {
    if ($date === NULL) {
      return 0;
    }
    if (!$date) {
      return 0;
    }
    if (!$date instanceof \DateTime) {
      try {
        if (is_integer($date)) {
          $date = new \DateTime('@' . $date);
        } else {
          $date = new \DateTime($date);
        }
        $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
        return $date->getTimestamp();
      } catch (\Exception $exception) {
        throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('"' . $date . '" could not be parsed by DateTime constructor.', 1241722579);
      }
    }
    return (int) $date->getTimestamp();
  }
}
?>

Beispiele

Code: XML XHTML XSLT HTML
<!-- 
01. - 04.04.2013
01.04. - 04.05.2013
01.04.2013 - 01.01.2014 
-->
<tx:format.DateStartEnd
 start="{e.dtstart}"
 end="{e.dtend}"
 showTime="0"
 weekday=""
 day="%d."
 month="%m."
 year="%Y"
 time=" %H:%M Uhr"
 limiter="–" />

<!--
8. bis 11. Mai 2013
31. Dezember 2013
31. Dezember 2013 bis 1. Januar 2014
10. Mai 2013 13:30 Uhr
-->
<tx:format.DateStartEnd
 start="{e.dtstart}"
 end="{e.dtend}"
 showTime="1"
 weekday=""
 day="%e. "
 month="%B "
 year="%Y"
 time=" %H:%M Uhr"
 limiter=" bis " />

Es ist noch nicht alles getestet – es können also noch diverse Fehler vorkommen.

Es gibt noch keine Kommentare.