Skip to content

Commit

Permalink
Fixed issue #18337: Placeholders VALIDFROM and VALIDUNTIL at email te…
Browse files Browse the repository at this point in the history
…mplates do not use localized dates (#2591)

- Added a 'dateFunctions' core plugin with new EM function: localize_date(date, language)
- Make language parameter optional on localize_date()
Co-authored-by: encuestabizdevgit <devgit@encuesta.biz>
  • Loading branch information
gabrieljenik committed Dec 1, 2022
1 parent a03a958 commit e54d2f5
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
57 changes: 57 additions & 0 deletions application/core/plugins/dateFunctions/EMFunctions.php
@@ -0,0 +1,57 @@
<?php

/**
* This file is part of dateFunctions plugin
* @version 1.0.0
*/

namespace dateFunctions;

use LimeExpressionManager;
use Survey;

class EMFunctions
{
/**
* Formats a date according to the Survey's date format for the specified language
* @param string $date : a date in "Y-m-d H:i:s" format. Example: VALIDFROM.
* @param string|null $language : the language used for localization. Defaults to current session language. If the current language is not configured in the survey, the survey base language will be used. When using in email templates, please use the token language as parameter as to set the expected language. Example: TOKEN:LANGUAGE
* @return string
*/
public static function localize_date($date, $language = null)
{
if (empty($date)) {
return '';
}

// Try to get Survey ID from the EM
$surveyId = LimeExpressionManager::getLEMsurveyId();

// If it's not set, try to get it from the session
if (empty($surveyId)) {
$surveyId = \Yii::app()->session['LEMsid'];
}
if (empty($surveyId)) {
return '';
}

$survey = Survey::model()->findByPk($surveyId);
if (empty($survey)) {
return '';
}

if (empty($language)) {
$language = \Yii::app()->getLanguage();
}

// If the specified language is not one of the survey's languages, fallback to the survey's base language.
if (!in_array($language, $survey->getAllLanguages()) || empty($survey->languagesettings[$language])) {
$language = $survey->language;
}

$dateFormat = $survey->languagesettings[$language]->surveyls_dateformat;
$dateFormatDetails = getDateFormatData($dateFormat);
$datetimeobj = new \Date_Time_Converter($date, "Y-m-d H:i:s");
return $datetimeobj->convert($dateFormatDetails['phpdate']);
}
}
21 changes: 21 additions & 0 deletions application/core/plugins/dateFunctions/config.xml
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<config>
<metadata>
<name>dateFunctions</name>
<type>plugin</type>
<creationDate>2022-09-12</creationDate>
<last_update>2022-09-12</last_update>
<author>LimeSurvey GmbH</author>
<authorUrl>https://www.limesurvey.org</authorUrl>
<version>1.0.0</version>
<license>GNU General Public License version 2 or later</license>
<description><![CDATA[Core: Date related Expression Manager functions]]></description>
</metadata>

<compatibility>
<version>5.0</version>
</compatibility>

<updaters disabled="disabled">
</updaters>
</config>
32 changes: 32 additions & 0 deletions application/core/plugins/dateFunctions/dateFunctions.php
@@ -0,0 +1,32 @@
<?php

class dateFunctions extends PluginBase
{
protected static $description = 'Core: Date related Expression Manager functions';
protected static $name = 'dateFunctions';

public function init()
{
$this->subscribe('ExpressionManagerStart');
}

public function ExpressionManagerStart()
{
Yii::setPathOfAlias(get_class($this), dirname(__FILE__));
$newFunctions = array(
'localize_date' => array(
'\dateFunctions\EMFunctions::localize_date',
null, // No javascript function : set as static function
$this->gT("Formats a date according to the Survey's date format for the specified language. Example: localize_date(VALIDUNTIL, TOKEN:LANGUAGE)"), // Description for admin
'string localize_date(date [, language])', // Extra description
'https://manual.limesurvey.org/', // Help url
1, 2 // Number of arguments : 1 or 2 (language is optional)
)
);
$this->getEvent()->append('functions', $newFunctions);

/**
* TODO: Update the manual URL
*/
}
}

0 comments on commit e54d2f5

Please sign in to comment.