Skip to content

Commit

Permalink
Fixed issue #18381: PluginSetting type date must be saved as a common…
Browse files Browse the repository at this point in the history
… datetime string (#2753)

Dev: use saveformat option
Dev: Can be set by default to "Y-m-d H:i" ?
Dev: send array of settings, check if datetime and datetimesaveformat is set in array
Co-authored-by: Lapiu Dev <devgit@lapiu.biz>
  • Loading branch information
Shnoulle and lapiudevgit committed Mar 31, 2023
1 parent 6875b22 commit aef1090
Show file tree
Hide file tree
Showing 9 changed files with 449 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -70,6 +70,7 @@
# if you want to add a new distributed plugin : add the directory here
/plugins/*
!/plugins/index.html
!/plugins/Demo/

#ignore old styles
/styles/
Expand Down
43 changes: 29 additions & 14 deletions application/extensions/SettingsWidget/SettingsWidget.php
Expand Up @@ -612,33 +612,48 @@ public function renderDate($name, array $metaData, $form = null)
{
$dateformatdetails = getDateFormatData(Yii::app()->session['dateformat']);
$value = $metaData['current'] ?? '';
$html = Yii::app()->getController()->widget('yiiwheels.widgets.datetimepicker.WhDateTimePicker', array(
/**
* Fix the value according to saveformat only if isset and not empty
* By defalt : save as sent by input (admin lanuage dependent
**/
if (!empty($metaData['saveformat'])) {
if (is_string($value) && $value !== "") {
$datetimeobj = new Date_Time_Converter($value, $metaData['saveformat']);
$value = $datetimeobj->convert($dateformatdetails['phpdate'] . "H:i");
} else {
$value = "";
}
}
$metaData['class'][] = 'form-control';
$htmlOptions = $this->htmlOptions($metaData, $form);

return Yii::app()->getController()->widget('yiiwheels.widgets.datetimepicker.WhDateTimePicker', array(
'name' => $name,
'id' => \CHtml::getIdByName($name),
'value' => $value,
'htmlOptions' => $htmlOptions,
'pluginOptions' => array(
'format' => $dateformatdetails['jsdate'] . " HH:mm",
'allowInputToggle' =>true,
'showClear' => true,
'tooltips' => array(
'clear'=> gT('Clear selection'),
'prevMonth'=> gT('Previous month'),
'nextMonth'=> gT('Next month'),
'selectYear'=> gT('Select year'),
'prevYear'=> gT('Previous year'),
'nextYear'=> gT('Next year'),
'selectDecade'=> gT('Select decade'),
'prevDecade'=> gT('Previous decade'),
'nextDecade'=> gT('Next decade'),
'prevCentury'=> gT('Previous century'),
'nextCentury'=> gT('Next century'),
'selectTime'=> gT('Select time')
'clear' => gT('Clear selection'),
'prevMonth' => gT('Previous month'),
'nextMonth' => gT('Next month'),
'selectYear' => gT('Select year'),
'prevYear' => gT('Previous year'),
'nextYear' => gT('Next year'),
'selectDecade' => gT('Select decade'),
'prevDecade' => gT('Previous decade'),
'nextDecade' => gT('Next decade'),
'prevCentury' => gT('Previous century'),
'nextCentury' => gT('Next century'),
'selectTime' => gT('Select time')
),
'locale' => convertLStoDateTimePickerLocale(Yii::app()->session['adminlang'])
)
), true
);
return $html;
}

/* Return htmlOptions for an input od seting
Expand Down
21 changes: 21 additions & 0 deletions application/libraries/PluginManager/LimesurveyApi.php
Expand Up @@ -628,4 +628,25 @@ public function getQuestionAttributes($questionId, $language = null)

return $questionAttributes;
}

/**
* Get a formatted date time by a string
* Used to return date from date input in admin
* @param string $dateValue the string as date value
* @param string $returnFormat the final date format
* @param integer|null $currentFormat the current format of dateValue, defaut from App()->session['dateformat'] @see getDateFormatData function (in surveytranslator_helper)
* @return string
*/
public static function getFormattedDateTime($dateValue, $returnFormat, $currentFormat = null)
{
if (empty($dateValue)) {
return "";
}
if (empty($currentFormat)) {
$currentFormat = intval(App()->session['dateformat']);
}
$dateformatdetails = getDateFormatData($currentFormat);
$datetimeobj = new \Date_Time_Converter($dateValue, $dateformatdetails['phpdate'] . " H:i");
return $datetimeobj->convert($returnFormat);
}
}
4 changes: 4 additions & 0 deletions application/libraries/PluginManager/PluginBase.php
Expand Up @@ -295,6 +295,10 @@ public function saveSettings($settings)
*/
protected function set($key, $data, $model = null, $id = null)
{
/* Date time settings format */
if (isset($this->settings[$key]['type']) && $this->settings[$key]['type'] == 'date' && !empty($this->settings[$key]['saveformat'])) {
$data = LimesurveyApi::getFormattedDateTime($data, $this->settings[$key]['saveformat']);
}
// Encrypt the attribute if needed
// TODO: Handle encryption in storage class, as that would allow each storage to handle
// it on it's own way. Currently there is no good way of telling the storage which
Expand Down
160 changes: 160 additions & 0 deletions plugins/Demo/DemoDateSetting/DemoDateSetting.php
@@ -0,0 +1,160 @@
<?php

/**
* DemoDateSetting Plugin for LimeSurvey
* @author : Denis Chenu
* @copyright: LimeSurvey <https://community.limesurvey.org/>
* @version:; 0.2.0
* @licence MIT Licence
*/
class DemoDateSetting extends PluginBase
{
/** @inheritdoc **/
protected static $name = 'DemoDateSetting';
/** @inheritdoc **/
protected static $description = 'Plugin to show saveformat function in date setting.';
/** @inheritdoc **/
protected $storage = 'DbStorage';
/** @inheritdoc **/
protected $settings = [
'checkDateDate' => array(
'type' => 'date',
'saveformat' => "Y-m-d",
),
'checkDateYear' => array(
'type' => 'date',
'saveformat' => "Y",
),
'checkDateFalse' => array(
'type' => 'date',
'saveformat' => false,
),
'checkDateDefault' => array(
'type' => 'date',
),
];

/** @inheritdoc **/
public function init()
{
$this->subscribe('beforeSurveySettings');
$this->subscribe('newSurveySettings');
}

/** Event to register the survey settings */
public function beforeSurveySettings()
{
$oEvent = $this->event;
$surveyId = $oEvent->get('survey');
$oEvent->set(
"surveysettings.{$this->id}",
array(
'name' => get_class($this),
'settings' => array(
'checkSurveyDateDate' => array(
'type' => 'date',
'label' => $this->gT('Save date only'),
'help' => sprintf(
$this->gT('Save format is set as Y-m-d, you have only the date when get the settings (“%s“ currently)'),
"<code>" . $this->get('checkSurveyDateDate', 'Survey', $surveyId, '') . "</code>"
),
'saveformat' => "Y-m-d",
'current' => $this->get('checkSurveyDateDate', 'Survey', $surveyId, ''),
),
'checkSurveyDateYear' => array(
'type' => 'date',
'label' => $this->gT('Save year only'),
'help' => sprintf(
$this->gT('Save format is set as Y, you have only the year when get the settings (“%s“ currently)'),
"<code>" . $this->get('checkSurveyDateYear', 'Survey', $surveyId, '') . "</code>"
),
'saveformat' => "Y",
'current' => $this->get('checkSurveyDateYear', 'Survey', $surveyId, ''),
),
'checkSurveyDateFalse' => array(
'type' => 'date',
'label' => $this->gT('Save as shown'),
'help' => sprintf(
$this->gT('Save format is set to false, you have the settings like it shown to the user when get it (“%s“ currently).'),
"<code>" . $this->get('checkSurveyDateFalse', 'Survey', $surveyId, '') . "</code>"
),
'saveformat' => false,
'current' => $this->get('checkSurveyDateFalse', 'Survey', $surveyId, ''),
),
'checkSurveyDateDefault' => array(
'type' => 'date',
'label' => $this->gT('Default save'),
'help' => sprintf(
$this->gT('Save format is not set, you have the settings like it shown to the user when get it (“%s“ currently).'),
"<code>" . $this->get('checkSurveyDateDefault', 'Survey', $surveyId, '') . "</code>"
),
'current' => $this->get('checkSurveyDateDefault', 'Survey', $surveyId, ''),
),
)
)
);
}

/** Event to save the survey settings **/
public function newSurveySettings()
{
$event = $this->event;
/**
* If you don't use same settings in Survey and global : you have to set it before saving
* then when save current saveformat can be used (we don't set it for Default)
**/
$this->settings = [
'checkSurveyDateDate' => array(
'type' => 'date',
'saveformat' => "Y-m-d",
),
'checkSurveyDateYear' => array(
'type' => 'date',
'saveformat' => "Y",
),
'checkSurveyDateFalse' => array(
'type' => 'date',
),
'checkSurveyDateFalse' => array(
'type' => 'date',
'saveformat' => false,
),
];
foreach ($event->get('settings') as $name => $value) {
$this->set($name, $value, 'Survey', $event->get('survey'));
}
}

/**
* @inheritdoc
* Update language strings and help
**/
public function getPluginSettings($getValues = true)
{
$pluginSettings = parent::getPluginSettings($getValues);
if (!$getValues) {
return $pluginSettings;
}
$pluginSettings['checkDateDate']['label'] = $this->gT("Save date only");
$pluginSettings['checkDateDate']['help'] = sprintf(
$this->gT("Save format is set as Y-m-d, you have only the date when get the settings (“%s” currently)."),
'<code>' . strval($pluginSettings['checkDateDate']['current']) . "</code>"
);
$pluginSettings['checkDateYear']['label'] = $this->gT("Save year only");
$pluginSettings['checkDateYear']['help'] = sprintf(
$this->gT("Save format is set as Y, you have only the year when get the settings (“%s” currently)."),
'<code>' . strval($pluginSettings['checkDateYear']['current']) . "</code>"
);
$pluginSettings['checkDateDefault']['label'] = $this->gT("Save as shown");
$pluginSettings['checkDateDefault']['help'] = sprintf(
$this->gT("Save format is not set, you save the settings like it shown to the user (“%s” currently)."),
'<code>' . strval($pluginSettings['checkDateDefault']['current']) . "</code>"
);
$pluginSettings['checkDateFalse']['label'] = $this->gT("Default save");
$pluginSettings['checkDateFalse']['help'] = sprintf(
$this->gT("Save format is set to false, you save the settings like it shown to the user (“%s” currently)."),
'<code>' . strval($pluginSettings['checkDateFalse']['current']) . "</code>"
);
return $pluginSettings;
}
}
21 changes: 21 additions & 0 deletions plugins/Demo/DemoDateSetting/LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022-2023 LimeSurvey / Denis Chenu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions plugins/Demo/DemoDateSetting/config.xml
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<config>
<metadata>
<name>DemoDateSetting</name>
<type>plugin</type>
<creationDate>2022-12-01</creationDate>
<lastUpdate>2023-03-06</lastUpdate>
<author>Denis Chenu</author>
<authorUrl>https://community.limesurvey.org/</authorUrl>
<version>0.1.0</version>
<license>MIT</license>
<description><![CDATA[Way to save date settings.]]></description>
</metadata>
<compatibility>
<version>5</version>
</compatibility>
</config>
12 changes: 12 additions & 0 deletions tests/data/plugins/SettingsPlugin.php
Expand Up @@ -6,6 +6,18 @@ class SettingsPlugin extends PluginBase
protected static $name = 'SettingsPlugin';
protected $storage = 'DbStorage';
protected $encryptedSettings = [];
/* @inheritdoc */
protected $settings = [];

/**
* Set the settings, used to test some settings
* @param array[]
* @return void
*/
public function setSettings($settings)
{
$this->settings = $settings;
}

public function init()
{
Expand Down

0 comments on commit aef1090

Please sign in to comment.