Add DateIntervalFunctionTemplatePlugin
authorMatthias Schmidt <gravatronics@live.com>
Sat, 10 Jun 2017 13:35:01 +0000 (15:35 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 10 Jun 2017 13:35:01 +0000 (15:35 +0200)
Closes #2303

wcfsetup/install/files/acp/templates/sitemapList.tpl
wcfsetup/install/files/lib/system/template/plugin/DateDiffModifierTemplatePlugin.class.php
wcfsetup/install/files/lib/system/template/plugin/DateIntervalFunctionTemplatePlugin.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/util/DateUtil.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index 98d64712dcef7a42ef6e5687f94faac48e9a9d53..e58164cdb039cba56f2980621a9d4ebc66cd4fa2 100755 (executable)
@@ -38,8 +38,8 @@
                                                <td class="columnTitle columnSitemap"><a href="{link controller="SitemapEdit"}objectType={$object->objectType}{/link}">{lang}wcf.acp.sitemap.objectType.{$object->objectType}{/lang}</a></td>
                                                <td class="columnInteger columnPriority">{$object->priority}</td>
                                                <td class="columnText columnChangeFreq">{lang}wcf.acp.sitemap.changeFreq.{$object->changeFreq}{/lang}</td>
-                                               <td class="columnInteger columnRebuildTime">{@TIME_NOW + $object->rebuildTime|dateDiff:TIME_NOW:true:"format_plain"}</td>
-       
+                                               <td class="columnInteger columnRebuildTime">{dateInterval end=TIME_NOW+$object->rebuildTime full=true format='plain'}</td>
+                                               
                                                {event name='columns'}
                                        </tr>
                                {/foreach}
index b3ecd4bc4cfd4770c28cb0211e3ce68d2e97b3e5..4e472669d48518858e7cfb7a7e60522728312b72 100644 (file)
@@ -11,12 +11,12 @@ use wcf\util\DateUtil;
  * Usage:
  *     {$endTimestamp|dateDiff}
  *     {$endTimestamp|dateDiff:$startTimestamp:$fullInterval}
- *     {$endTimestamp|dateDiff:$startTimestamp:$fullInterval:$formatType}
  * 
  * @author     Matthias Schmidt, Marcel Werk
  * @copyright  2001-2017 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    WoltLabSuite\Core\System\Template\Plugin
+ * @deprecated since 3.1, use `DateIntervalFunctionTemplatePlugin`
  */
 class DateDiffModifierTemplatePlugin implements IModifierTemplatePlugin {
        /**
@@ -32,18 +32,9 @@ class DateDiffModifierTemplatePlugin implements IModifierTemplatePlugin {
                        $fullInterval = $tagArgs[2];
                }
                
-               $formatType = DateUtil::FORMAT_DEFAULT;
-               if (isset($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, $formatType);
+               return DateUtil::formatInterval($endTime->diff($startTime), $fullInterval);
        }
 }
diff --git a/wcfsetup/install/files/lib/system/template/plugin/DateIntervalFunctionTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/DateIntervalFunctionTemplatePlugin.class.php
new file mode 100644 (file)
index 0000000..ca8c6f3
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+namespace wcf\system\template\plugin;
+use wcf\system\template\TemplateEngine;
+use wcf\util\DateUtil;
+
+/**
+ * Template modifier plugin which calculates the difference between two unix timestamps
+ * and returns it as a textual date interval.
+ * Compared to `DateDiffModifierTemplatePlugin`, this plugin allows a cleaner syntax
+ * and offers more features.
+ *
+ * Usage:
+ *     {dateInterval start=$startTimestamp end=$endTimestamp full=true format='sentence'}
+ * 
+ * Parameters:
+ *     - `start` refers to the start of the time interval, defaults to the current time
+ *     - `end` refers to the end of the time interval, default to the current time
+ *       (though either `start` or `end` has to be set)
+ *     - `full` determines if the full difference down to minutes (`true`) will be
+ *        shown or just the longest time interval type, defaults to `false`
+ *     - `format` determines how the output is formatted, see `DateUtil::FORMAT_*`
+ *        constants, defaults to `default` 
+ *
+ * @author     Matthias Schmidt
+ * @copyright  2001-2017 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Template\Plugin
+ * @since      3.1
+ */
+class DateIntervalFunctionTemplatePlugin implements IFunctionTemplatePlugin {
+       /**
+        * @inheritDoc
+        */
+       public function execute($tagArgs, TemplateEngine $tplObj) {
+               // read start and end time, each defaulting to current time
+               $start = $end = TIME_NOW;
+               
+               if (!isset($tagArgs['start']) && !isset($tagArgs['end'])) {
+                       throw new \InvalidArgumentException("Neither a 'start' nor an 'end' argument has been provided.");
+               }
+               
+               if (isset($tagArgs['start'])) {
+                       $start = intval($tagArgs['start']);
+               }
+               if (isset($tagArgs['end'])) {
+                       $end = intval($tagArgs['end']);
+               }
+               
+               $startTime = DateUtil::getDateTimeByTimestamp($start);
+               $endTime = DateUtil::getDateTimeByTimestamp($end);
+               
+               // read `full` flag for output precision
+               $fullInterval = false;
+               if (!empty($tagArgs['full'])) {
+                       $fullInterval = true;
+               }
+               
+               // read output format
+               $formatType = DateUtil::FORMAT_DEFAULT;
+               if (isset($tagArgs['format'])) {
+                       $constant = DateUtil::class .'::FORMAT_'. strtoupper($tagArgs['format']);
+                       if (!defined($constant)) {
+                               throw new \InvalidArgumentException("Invalid format '{$tagArgs['format']}' provided.");
+                       }
+                       
+                       $formatType = constant($constant);
+               }
+               
+               return DateUtil::formatInterval($endTime->diff($startTime), $fullInterval, $formatType);
+       }
+}
index d7b0b1e1a490c43f54705d50deaba87ad21f590a..af4b2cd79785c67ab6fd9814a9632b3b6fccf2e8 100644 (file)
@@ -228,7 +228,7 @@ final class DateUtil {
                        case self::FORMAT_SENTENCE:
                                $languageItemSuffix = $direction . '.inSentence';
                        break;
-                               
+                       
                        case self::FORMAT_PLAIN:
                                $languageItemSuffix = 'plain';
                        break; 
index 7657abe5e07ed9b7ef7a451db52d0025629bf3d2..136b02321a1c62c39a4a01ab2c785e04384efe60 100644 (file)
@@ -3032,7 +3032,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:"format_sentence"} (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 {dateInterval start=$notification->time end=$userNotificationObject->endDate format='sentence'} (am {$userNotificationObject->endDate|date:'d. F'}) ab.]]></item>
        </category>
        
        <category name="wcf.payment">
index 70a8683d88bed1743d2e650a7cc4be61bd622c3f..64e4efce1482e6701d42e05193a6aa0c1908963c 100644 (file)
@@ -3025,7 +3025,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:"format_sentence"} (on {$userNotificationObject->endDate|date:'F jS'}).]]></item>
+               <item name="wcf.paidSubscription.expiringSubscription.notification.message"><![CDATA[Your subscription “{$userNotificationObject->getTitle()}” will expire {dateInterval start=$notification->time end=$userNotificationObject->endDate format='sentence'} (on {$userNotificationObject->endDate|date:'F jS'}).]]></item>
        </category>
        
        <category name="wcf.payment">