add format types for formatInterval
authorJoshua Rüsweg <josh@bastelstu.be>
Thu, 8 Jun 2017 17:20:56 +0000 (19:20 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Thu, 8 Jun 2017 17:20:56 +0000 (19:20 +0200)
wcfsetup/install/files/lib/system/template/plugin/DateDiffModifierTemplatePlugin.class.php
wcfsetup/install/files/lib/util/DateUtil.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index 6032896b1c62fb080129ea08346a56e8635343cd..b3ecd4bc4cfd4770c28cb0211e3ce68d2e97b3e5 100644 (file)
@@ -11,7 +11,7 @@ use wcf\util\DateUtil;
  * Usage:
  *     {$endTimestamp|dateDiff}
  *     {$endTimestamp|dateDiff:$startTimestamp:$fullInterval}
- *     {$endTimestamp|dateDiff:$startTimestamp:$fullInterval:$inSentence}
+ *     {$endTimestamp|dateDiff:$startTimestamp:$fullInterval:$formatType}
  * 
  * @author     Matthias Schmidt, Marcel Werk
  * @copyright  2001-2017 WoltLab GmbH
@@ -32,14 +32,18 @@ class DateDiffModifierTemplatePlugin implements IModifierTemplatePlugin {
                        $fullInterval = $tagArgs[2];
                }
                
-               $inSentence = false;
+               $formatType = DateUtil::FORMAT_DEFAULT;
                if (isset($tagArgs[3])) {
-                       $inSentence = $tagArgs[3];
+                       if (!defined(DateUtil::class .'::'. strtoupper($tagArgs[3]))) {
+                               throw new \InvalidArgumentException('Invalid $formatType value');
+                       }
+                       
+                       $formatType = constant(DateUtil::class .'::'. strtoupper($tagArgs[3]));
                }
                
                $startTime = DateUtil::getDateTimeByTimestamp($tagArgs[1]);
                $endTime = DateUtil::getDateTimeByTimestamp($tagArgs[0]);
                
-               return DateUtil::formatInterval($endTime->diff($startTime), $fullInterval, $inSentence);
+               return DateUtil::formatInterval($endTime->diff($startTime), $fullInterval, $formatType);
        }
 }
index a355048528e5fc3df46e8193f1697aacab0682de..d7b0b1e1a490c43f54705d50deaba87ad21f590a 100644 (file)
@@ -26,6 +26,24 @@ final class DateUtil {
         */
        const TIME_FORMAT = 'wcf.date.timeFormat';
        
+       /**
+        * format the interval to be used as a standalone phrase
+        * @var integer
+        */
+       const FORMAT_DEFAULT = 1;
+       
+       /**
+        * format the interval to be used as a phrase in a sentence
+        * @var integer
+        */
+       const FORMAT_SENTENCE = 2;
+       
+       /**
+        * format the interval without direction
+        * @var integer
+        */
+       const FORMAT_PLAIN = 3; 
+       
        /**
         * list of available time zones
         * @var string[]
@@ -181,10 +199,10 @@ final class DateUtil {
         * 
         * @param       \DateInterval   $interval       interval to be formatted
         * @param       boolean         $fullInterval   if `true`, the complete interval is returned, otherwise a rounded interval is used
-        * @param       boolean         $inSentence     if `true`, the returned phrase is considered to be used in a sentence, otherwise as a standalone phrase
+        * @param       integer         $formatType     format type for the interval, use the class constant FORMAT_DEFAULT, FORMAT_SENTENCE or FORMAT_PLAIN
         * @return      string
         */
-       public static function formatInterval(\DateInterval $interval, $fullInterval = false, $inSentence = false) {
+       public static function formatInterval(\DateInterval $interval, $fullInterval = false, $formatType = self::FORMAT_DEFAULT) {
                $years = $interval->format('%y');
                $months = $interval->format('%m');
                $days = $interval->format('%d');
@@ -202,9 +220,21 @@ final class DateUtil {
                        break;
                }
                
-               $languageItemSuffix = $direction;
-               if ($inSentence) {
-                       $languageItemSuffix .= '.inSentence';
+               switch ($formatType) {
+                       case self::FORMAT_DEFAULT:
+                               $languageItemSuffix = $direction;
+                       break; 
+                       
+                       case self::FORMAT_SENTENCE:
+                               $languageItemSuffix = $direction . '.inSentence';
+                       break;
+                               
+                       case self::FORMAT_PLAIN:
+                               $languageItemSuffix = 'plain';
+                       break; 
+                       
+                       default: 
+                               throw new \InvalidArgumentException('Invalid $formatType value');
                }
                
                if ($fullInterval) {
index 41616639dc05ee742a7f45516fb3ba39645ead88..bf1af4eac854372e3cc284ae27f169b01cc307d5 100644 (file)
@@ -2295,6 +2295,14 @@ Fehler sind beispielsweise:
                <item name="wcf.date.interval.weeks.future.inSentence"><![CDATA[in {if $weeks > 1}{#$weeks} Wochen{else}einer Woche{/if}]]></item>
                <item name="wcf.date.interval.years.future.inSentence"><![CDATA[in {if $years > 1}{#$years} Jahren{else}einem Jahr{/if}]]></item>
                
+               <item name="wcf.date.interval.days.plain"><![CDATA[{if $days > 1}{#$days} Tage{else}ein Tag{/if}]]></item>
+               <item name="wcf.date.interval.full.plain"><![CDATA[{if $years}{if $years > 1}{#$years} Jahre{else}ein Jahr{/if}{/if}{if $months}{if $firstElement != 'months'}{if $lastElement == 'months'} und {else}, {/if}{/if}{if $months > 1}{#$months} Monate{else}ein Monat{/if}{/if}{if $weeks}{if $firstElement != 'weeks'}{if $lastElement == 'weeks'} und {else}, {/if}{/if}{if $weeks > 1}{#$weeks} Wochen{else}eine Woche{/if}{/if}{if $days}{if $firstElement != 'days'}{if $lastElement == 'days'} und {else}, {/if}{/if}{if $days > 1}{#$days} Tage{else}ein Tag{/if}{/if}{if $hours}{if $firstElement != 'hours'}{if $lastElement == 'hours'} und {else}, {/if}{/if}{if $hours > 1}{#$hours} Stunden{else}eine Stunde{/if}{/if}{if $minutes}{if $firstElement != 'minutes' && $lastElement == 'minutes'} und {/if}{if $minutes > 1}{#$minutes} Minuten{else}eine Minute{/if}{/if}]]></item>
+               <item name="wcf.date.interval.hours.plain"><![CDATA[{if $hours > 1}{#$hours} Stunden{else}eine Stunde{/if}]]></item>
+               <item name="wcf.date.interval.minutes.plain"><![CDATA[{if $minutes > 1}{#$minutes} Minuten{else}eine Minute{/if}]]></item>
+               <item name="wcf.date.interval.months.plain"><![CDATA[{if $months > 1}{#$months} Monate{else}ein Monat{/if}]]></item>
+               <item name="wcf.date.interval.weeks.plain"><![CDATA[{if $weeks > 1}{#$weeks} Wochen{else}eine Woche{/if}]]></item>
+               <item name="wcf.date.interval.years.plain"><![CDATA[{if $years > 1}{#$years} Jahre{else}ein Jahr{/if}]]></item>
+               
                <!-- variables for time periods -->
                <item name="wcf.date.period.older"><![CDATA[Älter]]></item>
                <item name="wcf.date.period.today"><![CDATA[Heute]]></item>
@@ -2998,7 +3006,7 @@ Fehler sind beispielsweise:
                <item name="wcf.paidSubscription.confirmTOS"><![CDATA[Hiermit bestätige ich mein Einverständnis mit den <a href="{PAID_SUBSCRIPTION_TOS_URL}">Nutzungsbedingungen</a>]]></item>
                <item name="wcf.paidSubscription.button.moreInformation"><![CDATA[Mehr Informationen]]></item>
                <item name="wcf.paidSubscription.expiringSubscription.notification.title"><![CDATA[Ablaufende Mitgliedschaft]]></item>
-               <item name="wcf.paidSubscription.expiringSubscription.notification.message"><![CDATA[{if LANGUAGE_USE_INFORMAL_VARIANT}Deine{else}Ihre{/if} Mitgliedschaft „{$userNotificationObject->getTitle()}“ läuft {$userNotificationObject->endDate|dateDiff:$notification->time:false:true} (am {$userNotificationObject->endDate|date:'d. F'}) ab.]]></item>
+               <item name="wcf.paidSubscription.expiringSubscription.notification.message"><![CDATA[{if LANGUAGE_USE_INFORMAL_VARIANT}Deine{else}Ihre{/if} Mitgliedschaft „{$userNotificationObject->getTitle()}“ läuft {$userNotificationObject->endDate|dateDiff:$notification->time:false:"format_sentence"} (am {$userNotificationObject->endDate|date:'d. F'}) ab.]]></item>
        </category>
        
        <category name="wcf.payment">
index 8990dbbc144a4da98ec7fc0102a6070b9822d197..7ebcf86592110ed9b5e6121e6e4652ce4fe0b85f 100644 (file)
@@ -2239,6 +2239,14 @@ Errors are:
                <item name="wcf.date.interval.weeks.future.inSentence"><![CDATA[in {if $weeks > 1}{#$weeks} weeks{else}a week{/if}]]></item>
                <item name="wcf.date.interval.years.future.inSentence"><![CDATA[in {if $years > 1}{#$years} years{else}a year{/if}]]></item>
                
+               <item name="wcf.date.interval.days.plain"><![CDATA[{if $days > 1}{#$days} days{else}one day{/if}]]></item>
+               <item name="wcf.date.interval.full.plain"><![CDATA[{if $years}{if $years > 1}{#$years} years{else}one year{/if}{/if}{if $months}{if $firstElement != 'months'}{if $lastElement == 'months'} and {else}, {/if}{/if}{if $months > 1}{#$months} months{else}one month{/if}{/if}{if $weeks}{if $firstElement != 'weeks'}{if $lastElement == 'weeks'} and {else}, {/if}{/if}{if $weeks > 1}{#$weeks} weeks{else}one week{/if}{/if}{if $days}{if $firstElement != 'days'}{if $lastElement == 'days'} and {else}, {/if}{/if}{if $days > 1}{#$days} days{else}one day{/if}{/if}{if $hours}{if $firstElement != 'hours'}{if $lastElement == 'hours'} and {else}, {/if}{/if}{if $hours > 1}{#$hours} hours{else}one hour{/if}{/if}{if $minutes}{if $firstElement != 'minutes' && $lastElement == 'minutes'} and {/if}{if $minutes > 1}{#$minutes} minutes{else}one minute{/if}{/if}]]></item>
+               <item name="wcf.date.interval.hours.plain"><![CDATA[{if $hours > 1}{#$hours} hours{else}one hour{/if}]]></item>
+               <item name="wcf.date.interval.minutes.plain"><![CDATA[{if $minutes > 1}{#$minutes} minutes{else}one minute{/if}]]></item>
+               <item name="wcf.date.interval.months.plain"><![CDATA[{if $months > 1}{#$months} months{else}one Month{/if}]]></item>
+               <item name="wcf.date.interval.weeks.plain"><![CDATA[{if $weeks > 1}{#$weeks} weeks{else}one week{/if}]]></item>
+               <item name="wcf.date.interval.years.plain"><![CDATA[{if $years > 1}{#$years} years{else}one year{/if}]]></item>
+               
                <!-- variables for time periods -->
                <item name="wcf.date.period.older"><![CDATA[Older]]></item>
                <item name="wcf.date.period.today"><![CDATA[Today]]></item>
@@ -2991,7 +2999,7 @@ Errors are:
                <item name="wcf.paidSubscription.confirmTOS"><![CDATA[I agree to the <a href="{PAID_SUBSCRIPTION_TOS_URL}">Terms of Service</a>]]></item>
                <item name="wcf.paidSubscription.button.moreInformation"><![CDATA[More Details]]></item>
                <item name="wcf.paidSubscription.expiringSubscription.notification.title"><![CDATA[Expiring Subscription]]></item>
-               <item name="wcf.paidSubscription.expiringSubscription.notification.message"><![CDATA[Your subscription “{$userNotificationObject->getTitle()}” will expire {$userNotificationObject->endDate|dateDiff:$notification->time:false:true} (on {$userNotificationObject->endDate|date:'F jS'}).]]></item>
+               <item name="wcf.paidSubscription.expiringSubscription.notification.message"><![CDATA[Your subscription “{$userNotificationObject->getTitle()}” will expire {$userNotificationObject->endDate|dateDiff:$notification->time:false:"format_sentence"} (on {$userNotificationObject->endDate|date:'F jS'}).]]></item>
        </category>
        
        <category name="wcf.payment">