]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Temporal.php
typo
[friendica.git] / src / Util / Temporal.php
index 46bd8bba50871deafc49449891feb330de8ebe70..ec71ab5e963e4604f7c8c072ff1c11295f7bc3b7 100644 (file)
@@ -8,12 +8,10 @@ namespace Friendica\Util;
 
 use DateTime;
 use DateTimeZone;
-use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Core\PConfig;
-
-require_once 'boot.php';
-require_once 'include/text.php';
+use Friendica\Core\Renderer;
+use Friendica\Database\DBA;
 
 /**
  * @brief Temporal class
@@ -100,12 +98,13 @@ class Temporal
         * arguments follow convention as other field_* template array:
         * 'name', 'label', $value, 'help'
         *
-        * @param string $name Name of the selector
-        * @param string $label Label for the selector
+        * @param string $name    Name of the selector
+        * @param string $label   Label for the selector
         * @param string $current Timezone
-        * @param string $help Help text
+        * @param string $help    Help text
         *
         * @return string Parsed HTML
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function getTimezoneField($name = 'timezone', $label = '', $current = 'America/Los_Angeles', $help = '')
        {
@@ -113,8 +112,8 @@ class Temporal
                $options = str_replace('<select id="timezone_select" name="timezone">', '', $options);
                $options = str_replace('</select>', '', $options);
 
-               $tpl = get_markup_template('field_select_raw.tpl');
-               return replace_macros($tpl, [
+               $tpl = Renderer::getMarkupTemplate('field_select_raw.tpl');
+               return Renderer::replaceMacros($tpl, [
                        '$field' => [$name, $label, $current, $help, $options],
                ]);
        }
@@ -124,10 +123,11 @@ class Temporal
         *
         * @param string $dob Date of Birth
         * @return string Formatted HTML
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function getDateofBirthField($dob)
        {
-               $a = get_app();
+               $a = \get_app();
 
                list($year, $month, $day) = sscanf($dob, '%4d-%2d-%2d');
 
@@ -139,8 +139,8 @@ class Temporal
 
                $age = (intval($value) ? self::getAgeByTimezone($value, $a->user["timezone"], $a->user["timezone"]) : "");
 
-               $tpl = get_markup_template("field_input.tpl");
-               $o = replace_macros($tpl,
+               $tpl = Renderer::getMarkupTemplate("field_input.tpl");
+               $o = Renderer::replaceMacros($tpl,
                        [
                        '$field' => [
                                'dob',
@@ -158,12 +158,13 @@ class Temporal
        /**
         * @brief Returns a date selector
         *
-        * @param string $min     Unix timestamp of minimum date
-        * @param string $max     Unix timestap of maximum date
-        * @param string $default Unix timestamp of default date
-        * @param string $id      ID and name of datetimepicker (defaults to "datetimepicker")
+        * @param DateTime $min     Minimum date
+        * @param DateTime $max     Maximum date
+        * @param DateTime $default Default date
+        * @param string   $id      ID and name of datetimepicker (defaults to "datetimepicker")
         *
         * @return string Parsed HTML output.
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function getDateField($min, $max, $default, $id = 'datepicker')
        {
@@ -178,6 +179,7 @@ class Temporal
         * @param string $id ID and name of datetimepicker (defaults to "timepicker")
         *
         * @return string Parsed HTML output.
+        * @throws \Exception
         */
        public static function getTimeField($h, $m, $id = 'timepicker')
        {
@@ -190,6 +192,7 @@ class Temporal
         * @param DateTime $minDate     Minimum date
         * @param DateTime $maxDate     Maximum date
         * @param DateTime $defaultDate Default date
+        * @param          $label
         * @param string   $id          Id and name of datetimepicker (defaults to "datetimepicker")
         * @param bool     $pickdate    true to show date picker (default)
         * @param bool     $picktime    true to show time picker (default)
@@ -199,7 +202,8 @@ class Temporal
         *
         * @return string Parsed HTML output.
         *
-        * @todo Once browser support is better this could probably be replaced with
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @todo  Once browser support is better this could probably be replaced with
         * native HTML5 date picker.
         */
        public static function getDateTimeField(
@@ -217,13 +221,13 @@ class Temporal
                // First day of the week (0 = Sunday)
                $firstDay = PConfig::get(local_user(), 'system', 'first_day_of_week', 0);
 
-               $lang = substr(L10n::getBrowserLanguage(), 0, 2);
+               $lang = substr(L10n::getCurrentLang(), 0, 2);
 
                // Check if the detected language is supported by the picker
                if (!in_array($lang,
                                ["ar", "ro", "id", "bg", "fa", "ru", "uk", "en", "el", "de", "nl", "tr", "fr", "es", "th", "pl", "pt", "ch", "se", "kr",
                                "it", "da", "no", "ja", "vi", "sl", "cs", "hu"])) {
-                       $lang = Config::get('system', 'language', 'en');
+                       $lang = 'en';
                }
 
                $o = '';
@@ -245,8 +249,8 @@ class Temporal
 
                $readable_format = str_replace(['Y', 'm', 'd', 'H', 'i'], ['yyyy', 'mm', 'dd', 'HH', 'MM'], $dateformat);
 
-               $tpl = get_markup_template('field_datetime.tpl');
-               $o .= replace_macros($tpl, [
+               $tpl = Renderer::getMarkupTemplate('field_datetime.tpl');
+               $o .= Renderer::replaceMacros($tpl, [
                        '$field' => [
                                $id,
                                $label,
@@ -290,16 +294,22 @@ class Temporal
 
                $abs = strtotime($localtime);
 
-               if (is_null($posted_date) || $posted_date <= NULL_DATE || $abs === false) {
+               if (is_null($posted_date) || $posted_date <= DBA::NULL_DATETIME || $abs === false) {
                        return L10n::t('never');
                }
 
+               $isfuture = false;
                $etime = time() - $abs;
 
-               if ($etime < 1) {
+               if ($etime < 1 && $etime >= 0) {
                        return L10n::t('less than a second ago');
                }
 
+               if ($etime < 0){
+                       $etime = -$etime;
+                       $isfuture = true;
+               }
+
                $a = [12 * 30 * 24 * 60 * 60 => [L10n::t('year'), L10n::t('years')],
                        30 * 24 * 60 * 60 => [L10n::t('month'), L10n::t('months')],
                        7 * 24 * 60 * 60 => [L10n::t('week'), L10n::t('weeks')],
@@ -315,7 +325,12 @@ class Temporal
                                $r = round($d);
                                // translators - e.g. 22 hours ago, 1 minute ago
                                if (!$format) {
-                                       $format = L10n::t('%1$d %2$s ago');
+                                       if($isfuture){
+                                               $format = L10n::t('in %1$d %2$s');
+                                       }
+                                       else {
+                                               $format = L10n::t('%1$d %2$s ago');
+                                       }
                                }
 
                                return sprintf($format, $r, (($r == 1) ? $str[0] : $str[1]));
@@ -338,11 +353,12 @@ class Temporal
         * and become a year older. If you wish me happy birthday on January 1
         * (San Bruno time), you'll be a day late.
         *
-        * @param string $dob Date of Birth
-        * @param string $owner_tz (optional) Timezone of the person of interest
+        * @param string $dob       Date of Birth
+        * @param string $owner_tz  (optional) Timezone of the person of interest
         * @param string $viewer_tz (optional) Timezone of the person viewing
         *
         * @return int Age in years
+        * @throws \Exception
         */
        public static function getAgeByTimezone($dob, $owner_tz = '', $viewer_tz = '')
        {
@@ -394,6 +410,7 @@ class Temporal
         * @param int $m Month (1=January, 12=December)
         *
         * @return string day 0 = Sunday through 6 = Saturday
+        * @throws \Exception
         */
        private static function getFirstDayInMonth($y, $m)
        {
@@ -410,14 +427,15 @@ class Temporal
         * altering td class.
         * Months count from 1.
         *
-        * @param int    $y Year
-        * @param int    $m Month
+        * @param int    $y     Year
+        * @param int    $m     Month
         * @param array  $links (default null)
         * @param string $class
         *
         * @return string
         *
-        * @todo Provide (prev, next) links, define class variations for different size calendars
+        * @throws \Exception
+        * @todo  Provide (prev, next) links, define class variations for different size calendars
         */
        public static function getCalendarTable($y = 0, $m = 0, $links = null, $class = '')
        {
@@ -450,11 +468,11 @@ class Temporal
                        $tddate = intval(DateTimeFormat::localNow('j'));
                }
 
-               $str_month = day_translate($mtab[$m]);
+               $str_month = L10n::getDay($mtab[$m]);
                $o = '<table class="calendar' . $class . '">';
                $o .= "<caption>$str_month $y</caption><tr>";
                for ($a = 0; $a < 7; $a ++) {
-                       $o .= '<th>' . mb_substr(day_translate($dn[$a]), 0, 3, 'UTF-8') . '</th>';
+                       $o .= '<th>' . mb_substr(L10n::getDay($dn[$a]), 0, 3, 'UTF-8') . '</th>';
                }
 
                $o .= '</tr><tr>';
@@ -468,7 +486,7 @@ class Temporal
                        $o .= "<td $today>";
                        $day = str_replace(' ', '&nbsp;', sprintf('%2.2d', $d));
                        if ($started) {
-                               if (x($links, $d) !== false) {
+                               if (isset($links[$d])) {
                                        $o .= "<a href=\"{$links[$d]}\">$day</a>";
                                } else {
                                        $o .= $day;