Skip to content

Commit

Permalink
Dev: Use date lib in LEMval()
Browse files Browse the repository at this point in the history
Dev: Fixed issue #8669 (proper fix)
Dev: More supported date formats
Dev: in dropdown boxes (Anything goes ;) )
  • Loading branch information
mfaber committed Feb 16, 2014
1 parent e141305 commit 5d0060f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 68 deletions.
2 changes: 1 addition & 1 deletion application/helpers/expressions/em_manager_helper.php
Expand Up @@ -4733,7 +4733,7 @@ static function StartSurvey($surveyid,$surveyMode='group',$aSurveyOptions=NULL,$
{
$dateformatdatat=getDateFormatData($LEM->surveyOptions['surveyls_dateformat']);
$datetimeobj = new Date_Time_Converter($value, $dateformatdatat['phpdate']);
$value=$datetimeobj->convert("Y-m-d");
$value=$datetimeobj->convert("Y-m-d H:i");
}
break;
case 'N': //NUMERICAL QUESTION TYPE
Expand Down
20 changes: 10 additions & 10 deletions application/helpers/qanda_helper.php
Expand Up @@ -996,16 +996,7 @@ function do_date($ia)
};";
App()->getClientScript()->registerScript("sDateLangvarJS",$sDateLangvarJS,CClientScript::POS_HEAD);
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig("generalscripts").'date.js');

//register timepicker extension, also needed for dropbox style dates for format conversions
App()->getClientScript()->registerPackage('jqueryui-timepicker');

// Locale for datepicker and timpicker extension
if ($clang->langcode !== 'en')
{
Yii::app()->getClientScript()->registerScriptFile(App()->getConfig('third_party')."/jqueryui/development-bundle/ui/i18n/jquery.ui.datepicker-{$clang->langcode}.js");
Yii::app()->getClientScript()->registerScriptFile(App()->getConfig('third_party')."/jquery-ui-timepicker-addon/i18n/jquery-ui-timepicker-{$clang->langcode}.js");
}
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig("third_party").'/jstoolbox/date.js');
$checkconditionFunction = "checkconditions";

$dateformatdetails = getDateFormatDataForQID($aQuestionAttributes,$thissurvey);
Expand Down Expand Up @@ -1299,7 +1290,16 @@ function do_date($ia)
}
else
{
//register timepicker extension
App()->getClientScript()->registerPackage('jqueryui-timepicker');

// Locale for datepicker and timpicker extension
if ($clang->langcode !== 'en')
{
Yii::app()->getClientScript()->registerScriptFile(App()->getConfig('third_party')."/jqueryui/development-bundle/ui/i18n/jquery.ui.datepicker-{$clang->langcode}.js");
Yii::app()->getClientScript()->registerScriptFile(App()->getConfig('third_party')."/jquery-ui-timepicker-addon/i18n/jquery-ui-timepicker-{$clang->langcode}.js");
}

// Format the date for output
if (trim($_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$ia[1]])!='')
{
Expand Down
2 changes: 1 addition & 1 deletion application/helpers/surveytranslator_helper.php
Expand Up @@ -747,7 +747,7 @@ function getPHPDateFromDateFormat($sDateformat)
function getJSDateFromDateFormat($sDateformat)
{
// The only difference from dateformat is that Jsdate does not support truncated years
return str_replace(array('yy', 'MM','M'), array('y','NN','NN'), $sDateformat);
return str_replace(array('yy'), array('y'), $sDateformat);
}


Expand Down
24 changes: 9 additions & 15 deletions scripts/date.js
Expand Up @@ -44,14 +44,13 @@ function setPickerOptions(input)

//split format into a date part and a time part
var datepattern=new RegExp(/[mydYD][mydYD.:\/-]*[mydYD]/);
var timepattern=new RegExp(/[HMN][HMN.:\/-]*[HMN]/);
var timepattern=new RegExp(/[HM][HM.:\/-]*[HM]/);
var sdateFormat=datepattern.exec(format);
if (sdateFormat!=null)
sdateFormat=sdateFormat.toString();
var stimeFormat=timepattern.exec(format);
if (stimeFormat!=null)
stimeFormat=stimeFormat.toString().replace(/N/gi,"M");
// alert(format+' '+sdateFormat+' '+stimeFormat);

var btimeOnly=false;
var bshowButtonPanel=true;
Expand Down Expand Up @@ -138,7 +137,7 @@ function validateInput(basename)
format=$('#dateformat'+basename).val();
answer=$('#answer'+basename).val();
//only validate if the format mask says it's a complete date and only a date
var str_regexp = /^[mydMYD]{1,4}[-.\s\/][mydMYD]{1,4}[-.\/\s][mydMYD]{1,4}$/;
var str_regexp = /^[mydYD]{1,4}[-.\s\/][mydYD]{1,4}[-.\/\s][mydYD]{1,4}$/;
var pattern = new RegExp(str_regexp);
if (format.match(pattern)!=null)
{
Expand Down Expand Up @@ -198,43 +197,42 @@ function dateUpdater() {
($('#hour'+thisid).length && $('#hour'+thisid).val()==''))
{
$('#qattribute_answer'+thisid).val(translt.infoCompleteAll);
$('#answer'+thisid).val('');
}
else
{
if ($('#year'+thisid).size()==0)
if (!$('#year'+thisid).val())
{
iYear='1900';
}
else
{
iYear=$('#year'+thisid).val();
}
if ($('#month'+thisid).size()==0)
if (!$('#month'+thisid).val())
{
iMonth='01';
}
else
{
iMonth=$('#month'+thisid).val();
}
if ($('#day'+thisid).size()==0)
if (!$('#day'+thisid).val())
{
iDay='01';
}
else
{
iDay=$('#day'+thisid).val();
}
if ($('#hour'+thisid).size()==0)
if (!$('#hour'+thisid).val())
{
iHour='00';
}
else
{
iHour=$('#hour'+thisid).val();
}
if ($('#minute'+thisid).size()==0)
if (!$('#minute'+thisid).val())
{
iMinute='00';
}
Expand All @@ -243,12 +241,8 @@ function dateUpdater() {
iMinute=$('#minute'+thisid).val();
}
ValidDate(this,iYear+'-'+iMonth+'-'+iDay);
parseddate=$.datepicker.parseDate( 'dd-mm-yy', iDay+'-'+iMonth+'-'+iYear);
parseddate=$.datepicker.formatDate( $('#dateformat'+thisid).val(), parseddate);
parseddate=parseddate.replace('HH',pad(iHour,2));
parseddate=parseddate.replace('H',iHour);
parseddate=parseddate.replace('NN',pad(iMinute,2));
parseddate=parseddate.replace('N',iMinute);
parseddate=Date.parseString(trim(iDay+'-'+iMonth+'-'+iYear+' '+iHour+':'+iMinute), 'dd-mm-yy H:M');
parseddate=parseddate.format($('#dateformat'+thisid).val());
$('#answer'+thisid).val(parseddate);
$('#answer'+thisid).change();
$('#qattribute_answer'+thisid).val('');
Expand Down
56 changes: 15 additions & 41 deletions scripts/expressions/em_javascript.js
Expand Up @@ -624,48 +624,22 @@ function LEMval(alias)
// }
return +newval;
}
// convert content in date questions to standard format yy-mm-dd to facilitate use in EM (comparisons, min/max etc.)
else if (attr.type=='D') {
// get date format pattern of referenced question
var sdatetimePattern=$(jsName.replace(/java/g, '#dateformat')).attr('value');

// if undefined (eg., variable on a previous page), set default format yy-mm-dd HH:MM
sdatetimePattern=typeof sdatetimePattern=='undefined'? 'yy-mm-dd HH:MM': sdatetimePattern;

//split format into a date part and a time part
var datepattern=new RegExp(/[mydYD][mydYD.:\/-]*[mydYD]/);
var timepattern=new RegExp(/[HMN][HMN.:\/-]*[HMN]/);
var sdateFormat=datepattern.exec(sdatetimePattern);
var stimeFormat=timepattern.exec(sdatetimePattern);
if (sdateFormat!=null) {
sdateFormat=sdateFormat.toString();

// quick fix for #08669...really need a proper date function lib or a date parsing function in js
datepattern=new RegExp(/m.*/);
match=datepattern.exec(sdatetimePattern);
if (match==null) {
sdateFormat=sdateFormat+'-mm';
value=trim(value)+'-01';
}
datepattern=new RegExp(/d.*/);
match=datepattern.exec(sdatetimePattern);
if (match==null) {
sdateFormat=sdateFormat+'-dd';
value=trim(value)+'-01';
}
} else {
sdateFormat="";
}

// datetimepicker needs minutes lower case
stimeFormat=stimeFormat!=null? stimeFormat.toString().replace(/[MN]/gi,"m"): "";

// For parsing patterns with time first (eg., HH:MM dd/mm/yyyy), we might need a specialised js lib
value=date('Y-m-d H:i', $.datepicker.parseDateTime(sdateFormat, stimeFormat, value));
return value;
}
// convert content in date questions to standard format yy-mm-dd to facilitate use in EM (comparisons, min/max etc.)
else if (attr.type=='D') {
// get date format pattern of referenced question
var sdatetimePattern=$(jsName.replace(/java/g, '#dateformat')).attr('value');

// if undefined (eg., variable on a previous page), set default format yy-mm-dd HH:MM
sdatetimePattern=typeof sdatetimePattern=='undefined'? 'yy-mm-dd HH:MM': sdatetimePattern;

if (sdatetimePattern==null) {
sdatetimePattern="";
}
value=date('Y-m-d H:i', Date.parseString(trim(value), sdatetimePattern));
return value;
}
else if (isNaN(value)) {
if (value==='false') {
if (value==='false') {
return ''; // so Boolean operations will treat it as false. In JavaScript, Boolean("false") is true since "false" is not a zero-length string
}
return value;
Expand Down

0 comments on commit 5d0060f

Please sign in to comment.