vendor/codefog/contao-haste/library/Haste/Util/Format.php line 146

Open in your IDE?
  1. <?php
  2. /**
  3.  * Haste utilities for Contao Open Source CMS
  4.  *
  5.  * Copyright (C) 2012-2013 Codefog & terminal42 gmbh
  6.  *
  7.  * @package    Haste
  8.  * @link       http://github.com/codefog/contao-haste/
  9.  * @license    http://opensource.org/licenses/lgpl-3.0.html LGPL
  10.  */
  11. namespace Haste\Util;
  12. use Contao\Controller;
  13. use Contao\Database;
  14. use Contao\DataContainer;
  15. use Contao\System;
  16. class Format
  17. {
  18.     /**
  19.      * Format date according to the system config
  20.      * @param   int
  21.      * @return  string
  22.      */
  23.     public static function date($intTstamp)
  24.     {
  25.         $strFormat = isset($GLOBALS['objPage']) ? $GLOBALS['objPage']->dateFormat $GLOBALS['TL_CONFIG']['dateFormat'];
  26.         return System::parseDate($strFormat$intTstamp);
  27.     }
  28.     /**
  29.      * Format time according to the system config
  30.      * @param   int
  31.      * @return  string
  32.      */
  33.     public static function time($intTstamp)
  34.     {
  35.         $strFormat = isset($GLOBALS['objPage']) ? $GLOBALS['objPage']->timeFormat $GLOBALS['TL_CONFIG']['timeFormat'];
  36.         return System::parseDate($strFormat$intTstamp);
  37.     }
  38.     /**
  39.      * Format date & time according to the system config
  40.      * @param   int
  41.      * @return  string
  42.      */
  43.     public static function datim($intTstamp)
  44.     {
  45.         $strFormat = isset($GLOBALS['objPage']) ? $GLOBALS['objPage']->datimFormat $GLOBALS['TL_CONFIG']['datimFormat'];
  46.         return System::parseDate($strFormat$intTstamp);
  47.     }
  48.     /**
  49.      * Get field label based on DCA config
  50.      *
  51.      * @param string $strTable
  52.      * @param string $strField
  53.      *
  54.      * @return string
  55.      */
  56.     public static function dcaLabel($strTable$strField)
  57.     {
  58.         System::loadLanguageFile($strTable);
  59.         Controller::loadDataContainer($strTable);
  60.         $arrField $GLOBALS['TL_DCA'][$strTable]['fields'][$strField] ?? [];
  61.         // Add the "name" key (backwards compatibility)
  62.         if (!isset($arrField['name'])) {
  63.             $arrField['name'] = $strField;
  64.         }
  65.         return static::dcaLabelFromArray($arrField);
  66.     }
  67.     /**
  68.      * Get field label based on field config
  69.      *
  70.      * @param array $arrField
  71.      *
  72.      * @return string
  73.      */
  74.     public static function dcaLabelFromArray(array $arrField)
  75.     {
  76.         if (!empty($arrField['label'])) {
  77.             $strLabel is_array($arrField['label']) ? $arrField['label'][0] : $arrField['label'];
  78.         } else {
  79.             $strLabel is_array($GLOBALS['TL_LANG']['MSC'][$arrField['name']] ?? null) ? $GLOBALS['TL_LANG']['MSC'][$arrField['name']][0] : $GLOBALS['TL_LANG']['MSC'][$arrField['name']] ?? '';
  80.         }
  81.         if ($strLabel == '') {
  82.             $strLabel $arrField['name'];
  83.         }
  84.         return $strLabel;
  85.     }
  86.     /**
  87.      * Format DCA field value according to Contao Core standard
  88.      *
  89.      * @param string              $strTable
  90.      * @param string              $strField
  91.      * @param mixed               $varValue
  92.      * @param DataContainer|null $objDc
  93.      *
  94.      * @return mixed
  95.      */
  96.     public static function dcaValue($strTable$strField$varValueDataContainer $objDc null)
  97.     {
  98.         System::loadLanguageFile('default');
  99.         System::loadLanguageFile($strTable);
  100.         Controller::loadDataContainer($strTable);
  101.         if (!isset($GLOBALS['TL_DCA'][$strTable]['fields'][$strField])) {
  102.             return '';
  103.         }
  104.         $arrField $GLOBALS['TL_DCA'][$strTable]['fields'][$strField];
  105.         // Add the "name" key (backwards compatibility)
  106.         if (!isset($arrField['name'])) {
  107.             $arrField['name'] = $strField;
  108.         }
  109.         return static::dcaValueFromArray($arrField$varValue$objDc);
  110.     }
  111.     /**
  112.      * Format field value according to Contao Core standard
  113.      *
  114.      * @param array               $arrField
  115.      * @param                     $varValue
  116.      * @param DataContainer|null $objDc
  117.      *
  118.      * @return mixed
  119.      */
  120.     public static function dcaValueFromArray(array $arrField$varValueDataContainer $objDc null)
  121.     {
  122.         $varValue deserialize($varValue);
  123.         if (is_array($arrField['options_callback'] ?? null) && $objDc !== null) { // Options callback (array)
  124.             $arrCallback $arrField['options_callback'];
  125.             $arrField['options'] = System::importStatic($arrCallback[0])->{$arrCallback[1]}($objDc);
  126.         } elseif (is_callable($arrField['options_callback'] ?? null) && $objDc !== null) { // Options callback (callable)
  127.             $arrField['options'] = $arrField['options_callback']($objDc);
  128.         } elseif (isset($arrField['foreignKey']) && $varValue) { // foreignKey
  129.             $chunks explode('.'$arrField['foreignKey'], 2);
  130.             $objOptions Database::getInstance()->query("SELECT id, " $chunks[1] . " AS value FROM " $chunks[0] . " WHERE id IN (" implode(','array_map('intval', (array) $varValue)) . ")");
  131.             $arrField['options'] = array();
  132.             while ($objOptions->next()) {
  133.                 $arrField['options'][$objOptions->id] = $objOptions->value;
  134.             }
  135.         }
  136.         if (is_array($varValue)) {
  137.             foreach ($varValue as $kk => $vv) {
  138.                 $varValue[$kk] = static::dcaValueFromArray($arrField$vv);
  139.             }
  140.             return implode(', '$varValue);
  141.         }
  142.         if ('date' === ($arrField['eval']['rgxp'] ?? null)) {
  143.             return static::date($varValue);
  144.         }
  145.         if ('time' === ($arrField['eval']['rgxp'] ?? null)) {
  146.             return static::time($varValue);
  147.         }
  148.         if ('datim' === ($arrField['eval']['rgxp'] ?? null) || in_array(($arrField['flag'] ?? null), array(5678910)) || 'tstamp' === ($arrField['name'] ?? null)) {
  149.             return static::datim($varValue);
  150.         }
  151.         if (($arrField['eval']['isBoolean'] ?? false) || ('checkbox' === ($arrField['inputType'] ?? null) && !($arrField['eval']['multiple'] ?? false))) {
  152.             return !empty($varValue) ? $GLOBALS['TL_LANG']['MSC']['yes'] : $GLOBALS['TL_LANG']['MSC']['no'];
  153.         }
  154.         if ('textarea' === ($arrField['inputType'] ?? null) && (($arrField['eval']['allowHtml'] ?? false) || ($arrField['eval']['preserveTags'] ?? false))) {
  155.             return \Contao\StringUtil::specialchars($varValue);
  156.         }
  157.         if (is_array($arrField['reference'] ?? null) && isset($arrField['reference'][$varValue])) {
  158.             return is_array($arrField['reference'][$varValue]) ? $arrField['reference'][$varValue][0] : $arrField['reference'][$varValue];
  159.         }
  160.         if ((($arrField['eval']['isAssociative'] ?? null) || array_is_assoc($arrField['options'] ?? null)) && isset($arrField['options'][$varValue])) {
  161.             return is_array($arrField['options'][$varValue]) ? $arrField['options'][$varValue][0] : $arrField['options'][$varValue];
  162.         }
  163.         return $varValue;
  164.     }
  165.     /**
  166.      * @deprecated Deprecated since Haste 4.8, to be removed in Haste 5.
  167.      *             Use RepositoryVersion::format() instead.
  168.      */
  169.     public static function repositoryVersion($aVersion)
  170.     {
  171.         return RepositoryVersion::format($aVersion);
  172.     }
  173.     /**
  174.      * @deprecated Deprecated since Haste 4.8, to be removed in Haste 5.
  175.      *             Use RepositoryVersion::formatShort() instead.
  176.      */
  177.     public static function repositoryShortVersion($aVersion)
  178.     {
  179.         return RepositoryVersion::formatShort($aVersion);
  180.     }
  181. }