vendor/codefog/contao-haste/library/Haste/Util/InsertTag.php line 55

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\Config;
  13. use Contao\Controller;
  14. use Contao\Date;
  15. use Contao\FormFieldModel;
  16. class InsertTag
  17. {
  18.     /**
  19.      * Recursively replace insert tags
  20.      *
  21.      * @param array|string $varValue
  22.      *
  23.      * @return array|string
  24.      */
  25.     public static function replaceRecursively($varValue)
  26.     {
  27.         if (is_array($varValue)) {
  28.             foreach ($varValue as $k => $v) {
  29.                 $varValue[$k] = static::replaceRecursively($v);
  30.             }
  31.             return $varValue;
  32.         } elseif (is_object($varValue)) {
  33.             return $varValue;
  34.         }
  35.         return Controller::replaceInsertTags($varValuefalse);
  36.     }
  37.     /**
  38.      * Replace generally useful insert tags
  39.      *
  40.      * @param string $strTag
  41.      *
  42.      * @return string|false
  43.      */
  44.     public function replaceHasteInsertTags($strTag)
  45.     {
  46.         $arrTag trimsplit('::'$strTag);
  47.         if ($arrTag[0] == 'convert_dateformat') {
  48.             return $this->replaceConvertedDateFormat($arrTag);
  49.         }
  50.         if ($arrTag[0] == 'formatted_datetime') {
  51.             return $this->replaceFormattedDateTime($arrTag);
  52.         }
  53.         if ($arrTag[0] == 'dca_label') {
  54.             return $this->replaceDcaLabel($arrTag);
  55.         }
  56.         if ($arrTag[0] == 'dca_value') {
  57.             return $this->replaceDcaValue($arrTag);
  58.         }
  59.         if ($arrTag[0] == 'rand') {
  60.             return (count($arrTag) === 3) ? mt_rand((int) $arrTag[1], (int) $arrTag[2]) : mt_rand();
  61.         }
  62.         if ($arrTag[0] == 'flag') {
  63.             return (string) $arrTag[1];
  64.         }
  65.         if ($arrTag[0] == 'options_label') {
  66.             return $this->replaceOptionsLabel($arrTag);
  67.         }
  68.         return false;
  69.     }
  70.     /**
  71.      * Replace {{convert_dateformat::*}} insert tag
  72.      *
  73.      * Format:
  74.      *
  75.      * {{convert_dateformat::<value>::<source_format>::<target_format>}}
  76.      *
  77.      * Description:
  78.      *
  79.      * The source_format and target_format can be any format from php date()
  80.      * or "date", "datim" or "time" to take the the format from the root page settings
  81.      * (or system settings, in case not defined).
  82.      *
  83.      * Possible use cases:
  84.      *
  85.      * {{convert_dateformat::2018-11-21 10:00::datim::date}} –> outputs 2018-11-21
  86.      * {{convert_dateformat::21.03.2018::d.m.Y::j. F Y}}     –> outputs 21. März 2018
  87.      *
  88.      * @param array $chunks
  89.      *
  90.      * @return string|boolean
  91.      */
  92.     private function replaceConvertedDateFormat($chunks)
  93.     {
  94.         if (!== \count($chunks)) {
  95.             return false;
  96.         }
  97.         try {
  98.             $date = new Date($chunks[1], $this->determineFormat($chunks[2]));
  99.         } catch (\OutOfBoundsException $e) {
  100.             return false;
  101.         }
  102.         return Date::parse($this->determineFormat($chunks[3]), $date->tstamp);
  103.     }
  104.     /**
  105.      * Determine the date format
  106.      *
  107.      * @param string $format
  108.      *
  109.      * @return string
  110.      */
  111.     private function determineFormat($format)
  112.     {
  113.         if (\in_array($format, ['datim''date''time'], true)) {
  114.             $key $format.'Format';
  115.             return isset($GLOBALS['objPage']) ? $GLOBALS['objPage']->{$key} : Config::get($key);
  116.         }
  117.         return $format;
  118.     }
  119.     /**
  120.      * Replace {{formatted_datetime::*}} insert tag
  121.      *
  122.      * 5 possible use cases:
  123.      *
  124.      * {{formatted_datetime::timestamp}}
  125.      *      or
  126.      * {{formatted_datetime::timestamp::datim}}     - formats a given timestamp with the global date and time (datim) format
  127.      * {{formatted_datetime::timestamp::date}}      - formats a given timestamp with the global date format
  128.      * {{formatted_datetime::timestamp::time}}      - formats a given timestamp with the global time format
  129.      * {{formatted_datetime::timestamp::Y-m-d H:i}} - formats a given timestamp with the specified format
  130.      * {{formatted_datetime::+1 day::Y-m-d H:i}}    - formats a given php date/time format (see http://php.net/manual/en/function.strtotime.php) with the specified format
  131.      *
  132.      * @param array $arrTag
  133.      *
  134.      * @return string
  135.      */
  136.     private function replaceFormattedDateTime($arrTag)
  137.     {
  138.         $intTimestamp $arrTag[1];
  139.         // Support strtotime()
  140.         if (!is_numeric($intTimestamp)) {
  141.             $intTimestamp strtotime($intTimestamp);
  142.         }
  143.         $strFormat $arrTag[2];
  144.         // Fallback
  145.         if ($strFormat === null) {
  146.             $strFormat 'datim';
  147.         }
  148.         // Custom format
  149.         if (!in_array($strFormat, array('datim''date''time'))) {
  150.             return Date::parse($strFormat$intTimestamp);
  151.         }
  152.         return Format::$strFormat($intTimestamp);
  153.     }
  154.     /**
  155.      * Replace {{dca_label::*}} insert tag
  156.      *
  157.      * use case:
  158.      *
  159.      * {{dca_label::table::field}}
  160.      *
  161.      * @param array $arrTag
  162.      *
  163.      * @return string
  164.      */
  165.     private function replaceDcaLabel($arrTag)
  166.     {
  167.         $strTable $arrTag[1];
  168.         $strField $arrTag[2];
  169.         return Format::dcaLabel($strTable$strField);
  170.     }
  171.     /**
  172.      * Replace {{dca_value::*}} insert tag
  173.      *
  174.      * use case:
  175.      *
  176.      * {{dca_value::table::field::value}}
  177.      *
  178.      * @param array $arrTag
  179.      *
  180.      * @return string
  181.      */
  182.     private function replaceDcaValue($arrTag)
  183.     {
  184.         $strTable $arrTag[1];
  185.         $strField $arrTag[2];
  186.         $varValue $arrTag[3];
  187.         return Format::dcaValue($strTable$strField$varValue);
  188.     }
  189.     /**
  190.      * Replace {{option_label::*}} insert tag
  191.      *
  192.      * use case:
  193.      *
  194.      * {{option_label::ID::value}}
  195.      *
  196.      * @param array $arrTag
  197.      */
  198.     private function replaceOptionsLabel($arrTag)
  199.     {
  200.         $id    $arrTag[1];
  201.         $value $arrTag[2];
  202.         $field FormFieldModel::findByPk($id);
  203.         if (null === $field) {
  204.             return $value;
  205.         }
  206.         $options deserialize($field->options);
  207.         if (empty($options) || !is_array($options)) {
  208.             return $value;
  209.         }
  210.         foreach ($options as $option) {
  211.             if ($value == $option['value']) {
  212.                 return $option['label'];
  213.             }
  214.         }
  215.         return $value;
  216.     }
  217. }