Пример #1
0
 /**
  * return date in expected format
  *
  * available parameters :
  *  date => DateTime object (mandatory)
  *  format => expected format
  *  output => list of default system format. Values available :
  *      date => date format
  *      time => time format
  *      datetime => datetime format (default)
  *
  * ex :
  *  {format_date date=$dateTimeObject format="Y-m-d H:i:s"} will output the format with specific format
  *  {format_date date=$dateTimeObject format="%e %B %Y" locale="fr_FR"} will output the format with specific format (see strftime() function)
  *  {format_date date=$dateTimeObject output="date"} will output the date using the default date system format
  *  {format_date date=$dateTimeObject} will output with the default datetime system format
  *
  * @param  array                                                        $params
  * @param  null                                                         $template
  * @throws \TheliaSmarty\Template\Exception\SmartyPluginException
  * @return string
  */
 public function formatDate($params, $template = null)
 {
     $date = $this->getParam($params, "date", false);
     if ($date === false) {
         // Check if we have a timestamp
         $timestamp = $this->getParam($params, "timestamp", false);
         if ($timestamp === false) {
             // No timestamp => error
             throw new SmartyPluginException("Either date or timestamp is a mandatory parameter in format_date function");
         } else {
             $date = new \DateTime();
             $date->setTimestamp($timestamp);
         }
     }
     if (!$date instanceof \DateTime) {
         try {
             $date = new \DateTime($date);
         } catch (\Exception $e) {
             return "";
         }
     }
     $format = $this->getParam($params, "format", false);
     if ($format === false) {
         $format = DateTimeFormat::getInstance($this->request)->getFormat($this->getParam($params, "output", null));
     }
     $locale = $this->getParam($params, 'locale', false);
     if (false === $locale) {
         $value = $date->format($format);
     } else {
         $value = $this->formatDateWithLocale($date, $locale, $format);
     }
     return $value;
 }
 /**
  * @param       $objectLabel
  * @param       $params
  * @param       $data
  * @param array $noGetterData
  *
  * @return string
  * @throws \InvalidArgumentException
  */
 protected function dataAccess($objectLabel, $params, $data, $noGetterData = array())
 {
     $attribute = $this->getNormalizedParam($params, array('attribute', 'attrib', 'attr'));
     if (!empty($attribute)) {
         if (null != $data) {
             $keyAttribute = strtoupper($attribute);
             if (array_key_exists($keyAttribute, $noGetterData)) {
                 return $noGetterData[$keyAttribute];
             }
             $getter = sprintf("get%s", $this->underscoreToCamelcase($attribute));
             if (method_exists($data, $getter)) {
                 $return = $data->{$getter}();
                 if ($return instanceof \DateTime) {
                     if (array_key_exists("format", $params)) {
                         $format = $params["format"];
                     } else {
                         $format = DateTimeFormat::getInstance($this->getRequest())->getFormat(array_key_exists("output", $params) ? $params["output"] : null);
                     }
                     $return = $return->format($format);
                 }
                 return $return;
             }
             throw new \InvalidArgumentException(sprintf("%s has no '%s' attribute", $objectLabel, $attribute));
         }
     }
     return '';
 }
Пример #3
0
 private function getDateFormat()
 {
     return DateTimeFormat::getInstance($this->facade->getRequest())->getFormat("date");
 }
Пример #4
0
 /**
  * return date in expected format
  *
  * available parameters :
  *  date => DateTime object (mandatory)
  *  format => expected format
  *  output => list of default system format. Values available :
  *      date => date format
  *      time => time format
  *      datetime => datetime format (default)
  *
  * ex :
  *  {format_date date=$dateTimeObject format="Y-m-d H:i:s"} will output the format with specific format
  *  {format_date date=$dateTimeObject format="l F j" locale="fr_FR"} will output the format with specific format (see date() function)
  *  {format_date date=$dateTimeObject output="date"} will output the date using the default date system format
  *  {format_date date=$dateTimeObject} will output with the default datetime system format
  *
  * @param  array                                                  $params
  * @param  null                                                   $template
  * @throws \TheliaSmarty\Template\Exception\SmartyPluginException
  * @return string
  */
 public function formatDate($params, $template = null)
 {
     $date = $this->getParam($params, "date", false);
     if ($date === false) {
         // Check if we have a timestamp
         $timestamp = $this->getParam($params, "timestamp", false);
         if ($timestamp === false) {
             // No timestamp => error
             throw new SmartyPluginException("Either date or timestamp is a mandatory parameter in format_date function");
         } else {
             $date = new \DateTime();
             $date->setTimestamp($timestamp);
         }
     } elseif (is_array($date)) {
         $keys = array_keys($date);
         $isDate = $this->arrayContains(static::$dateKeys, $keys);
         $isTime = $this->arrayContains(static::$timeKeys, $keys);
         // If this is not a date, fallback on today
         // If this is not a time, fallback on midnight
         $dateFormat = $isDate ? sprintf("%d-%d-%d", $date["year"], $date["month"], $date["day"]) : (new \DateTime())->format("Y-m-d");
         $timeFormat = $isTime ? sprintf("%d:%d:%d", $date["hour"], $date["minute"], $date["second"]) : "0:0:0";
         $date = new \DateTime(sprintf("%s %s", $dateFormat, $timeFormat));
     }
     if (!$date instanceof \DateTime) {
         try {
             $date = new \DateTime($date);
         } catch (\Exception $e) {
             return "";
         }
     }
     $format = $this->getParam($params, "format", false);
     if ($format === false) {
         $format = DateTimeFormat::getInstance($this->request)->getFormat($this->getParam($params, "output", null));
     }
     $locale = $this->getParam($params, 'locale', false);
     if (false === $locale) {
         $value = $date->format($format);
     } else {
         $value = $this->formatDateWithLocale($date, $locale, $format);
     }
     return $value;
 }