Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

render_date_range_field()

Description

Renders a date range input field for resource editing.

This function creates a set of input fields for selecting a start and end date. It supports EDTF format
and includes validation for the entered dates. It also provides functionality for handling multiple edit modes
and integrates with the autosave feature.

Parameters

ColumnTypeDefaultDescription
$name string The base name for the input fields.
$value string The value of the date range (in a specific format).
$forsearch bool true Indicates if the field is being used for a search query.
$autoupdate bool false Indicates if the field should trigger an update on change.
$field array array The field configuration array containing settings and metadata for the field.
$reset string "" An optional parameter indicating if the form should be reset.

Location

include/render_functions.php lines 2337 to 2708

Definition

 
function render_date_range_field($name,$value,$forsearch=true,$autoupdate=false,$field=array(),$reset="")
    {
    
$found_year='';$found_month='';$found_day='';$found_start_year='';$found_start_month='';$found_start_day='';$found_end_year='';$found_end_month='';$found_end_day=''
    global 
$daterange_edtf_support,$lang$minyear,$date_d_m_y$edit_autosave,$forsearchbar$maxyear_extends_current;
    if(
$forsearch)
        {
        
// Get the start/end date from the string
        
$startpos   strpos($value,"start");
        
$endpos     strpos($value,"end");
        
$startvalue $startpos !== false substr($value,$startpos+5,($endpos ? ($endpos - ($startpos 5)) : null)) : "";
        
$endvalue   $endpos !== false substr($value,strpos($value,"end")+3,10) : "";
        }
    else
        {
        if(
$value!="" && strpos($value,",")!==false)
            {
            
// Extract the start date from the value obtained from get_resource_field_data
            
$rangevalues explode(",",$value);
            
$startvalue $rangevalues[0];
            
$endvalue $rangevalues[1];
            }
        elseif(
strlen($value)==10 && strpos($value,"-") !==  false)
            {
            
$startvalue $value;
            
$endvalue "";
            }
        else
            {
            
$startvalue "";
            
$endvalue "";
            }
        }

    
$startvalue trim($startvalue);
    
$endvalue trim($endvalue);

    
$ss=explode("-",$startvalue);
    if (
count($ss)>=1)
        {
        
$found_start_year   $ss[0] ?? "";
        
$found_start_month  $ss[1] ?? "";
        
$found_start_day    $ss[2] ?? "";
        }
    
$se=explode("-",$endvalue);
    if (
count($se)>=1)
        {
        
$found_end_year     $se[0] ?? "";
        
$found_end_month    $se[1] ?? "";
        
$found_end_day      $se[2] ?? "";
        }
        
    
// If the form has been submitted (but not reset) but data was not saved get the submitted values   
    
if($reset == ""
        {
        foreach(array(
"start_y""start-m","start-d","end-y","end-m","end-d") as $subpart)
            {
            if(
getval($name "_" $subpart,"") != "")
                {
                ${
"found_" $subpart} = escape(getval($name "_" $subpart,""));
                }
            }
        }
    
    if(
$daterange_edtf_support)
        {
        
// Use EDTF format for date input
        
?>      
        <input class=" echo $forsearch?"SearchWidth":"stdwidth"?>"  name=" echo escape($name); ?>_edtf" id=" echo escape($name); ?>_edtf" type="text" value=" echo ($startvalue!=""|$endvalue!="")?$startvalue "/" $endvalue:""?>" style="display:none;" disabled  if ($forsearch && $autoupdate) { ?>onChange="UpdateResultCount();" } if($forsearch && !$forsearchbar){ ?> onKeyPress="if (!(updating)) {setTimeout('UpdateResultCount()',2000);updating=true;}" } elseif (!$forsearch  && $edit_autosave){?>onChange="AutoSave(' echo $field["ref"]; ?>');" ?>>

        
}?>
    <!--  date range search start -->
    <!--- start date -->
    <div class="stdwidth indent  echo escape($name); ?>_range" id=" echo escape($name); ?>_start">
    <label class="InnerLabel"> echo escape($lang["fromdate"])?></label>

        
        
if($date_d_m_y)
            {
            
?>
            <label class="accessibility-hidden" for=" echo escape($name?>_start-d"> echo escape($lang["day"]); ?></label>
            <select name=" echo escape($name); ?>_start-d" id=" echo escape($name); ?>_start-d"
             
            
if ($forsearch && $autoupdate)
                    { 
?>onChange="UpdateResultCount();" }
            elseif (!
$forsearch  && $edit_autosave)
            {
?>onChange="if(sufficientDateParts(' echo escape($name); ?>_start')){AutoSave(' echo $field["ref"]; ?>');}" ?>
              >
              <option value=""> echo escape($forsearch?$lang["anyday"]:$lang["day"]); ?></option>
              
              
for ($d=1;$d<=31;$d++)
                {
                
$m=str_pad($d,2,"0",STR_PAD_LEFT);
                
?><option  if ($d==$found_start_day) { ?>selected ?> value=" echo $m?>"> echo $m?></option>
                
}
              
?>
            </select>
            <label class="accessibility-hidden" for=" echo escape($name?>_start-m"> echo escape($lang["month"]); ?></label>
            <select name=" echo escape($name); ?>_start-m" id=" echo escape($name); ?>_start-m"
                
                
if ($forsearch && $autoupdate)
                    { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="if(sufficientDateParts(' echo escape($name); ?>_start')){AutoSave(' echo $field["ref"]; ?>');}" ?>
                    >
                <option value=""> echo escape($forsearch?$lang["anymonth"]:$lang["month"]); ?></option>
                
                
for ($d=1;$d<=12;$d++)
                    {
                    
$m=str_pad($d,2,"0",STR_PAD_LEFT);
                    
?><option  if ($d==$found_start_month) { ?>selected ?> value=" echo $m?>"> echo escape($lang["months"][$d-1])?></option>
                    
}?>
            </select>
            
            
}
        else
            {
            
?>
            <label class="accessibility-hidden" for=" echo escape($name?>_start-m"> echo escape($lang["month"]); ?></label>
            <select name=" echo escape($name); ?>_start-m" id=" echo escape($name); ?>_start-m"
                 
                
if ($forsearch && $autoupdate
                    { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="if(sufficientDateParts(' echo escape($name); ?>_start')){AutoSave(' echo $field["ref"]; ?>');}" ?>
                    >
                <option value=""> echo escape($forsearch?$lang["anymonth"]:$lang["month"]); ?></option>
                
                
for ($d=1;$d<=12;$d++)
                    {
                    
$m=str_pad($d,2,"0",STR_PAD_LEFT);
                    
?><option  if ($d==$found_start_month) { ?>selected ?> value=" echo $m?>">  echo escape($lang["months"][$d-1])  ?></option>
                    
}?>
            </select>
            <label class="accessibility-hidden" for=" echo escape($name?>_start-d"> echo escape($lang["day"]); ?></label>
            <select name=" echo escape($name); ?>_start-d"  id=" echo escape($name); ?>_start-d"
              
                
if ($forsearch && $autoupdate)
                    { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="if(sufficientDateParts(' echo escape($name); ?>_start')){AutoSave(' echo $field["ref"]; ?>');}" ?>
                    >
              <option value=""> echo escape($forsearch?$lang["anyday"]:$lang["day"]); ?></option>
              
              
for ($d=1;$d<=31;$d++)
                {
                
$m=str_pad($d,2,"0",STR_PAD_LEFT);
                
?><option  if ($d==$found_start_day) { ?>selected ?> value=" echo $m?>"> echo $m?></option>
                
}
              
?>
            </select>
            
            
}
        if(
$forsearch)
            {
?>
            <label class="accessibility-hidden" for=" echo escape($name?>_start-y"> echo escape($lang["year"]); ?></label>
            <select name=" echo escape($name?>_start-y" id=" echo escape($name); ?>_start-y"
                
                
if ($forsearch && $autoupdate)
                        { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                {
?>onChange="if(sufficientDateParts(' echo escape($name); ?>_start')){AutoSave(' echo $field["ref"]; ?>');}" ?>
                >
                <option value=""> echo escape($forsearch?$lang["anyyear"]:$lang["year"]); ?></option>
                
                $y
=date("Y");
                
$y += $maxyear_extends_current;
                for (
$d=$y;$d>=$minyear;$d--)
                    {
                    
?><option  if ($d==$found_start_year) { ?>selected ?>> echo $d?></option>
                    
}?>
            </select>
            
            
}
        else
            {
?>
            <label class="accessibility-hidden" for=" echo escape($name?>_end-y"> echo escape($lang["year"]); ?></label>
            <input size="5" name=" echo escape($name?>_start-y" id=" echo escape($name?>_start-y" type="text" value=" echo $found_start_year ?>"
                 
                
if ($forsearch && $autoupdate)
                    { 
?>onChange="UpdateResultCount();" }
                if(
$forsearch && !$forsearchbar)
                    { 
?> onKeyPress="if (!(updating)) {setTimeout('UpdateResultCount()',2000);updating=true;}" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="if(sufficientDateParts(' echo escape($name); ?>_start')){AutoSave(' echo $field["ref"]; ?>');}" ?>>
            
            
}?>
    </div>

    <div class="clearerleft"> </div>

    <!--- to date -->
    <label  class='daterangelabel'></label>

    <div class="stdwidth indent  echo escape($name); ?>_range" id=" echo escape($name); ?>_to" >
    <label class="InnerLabel"> echo escape($lang["todate"])?></label>
    
        
if($date_d_m_y)
            {
            
?>
            <label class="accessibility-hidden" for=" echo escape($name?>_end-d"> echo escape($lang["day"]); ?></label>
            <select name=" echo escape($name); ?>_end-d" id=" echo escape($name); ?>_end-d"
              
                
if ($forsearch && $autoupdate)
                    { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="if(sufficientDateParts(' echo escape($name); ?>_end')){AutoSave(' echo $field["ref"]; ?>');}" ?>
                    >
                <option value=""> echo escape($forsearch?$lang["anyday"]:$lang["day"]); ?></option>
                
                
for ($d=1;$d<=31;$d++)
                    {
                    
$m=str_pad($d,2,"0",STR_PAD_LEFT);
                    
?><option  if ($d==$found_end_day) { ?>selected ?> value=" echo $m?>"> echo $m?></option>
                    
}?>
            </select>
            <label class="accessibility-hidden" for=" echo escape($name?>_end-m"> echo escape($lang["month"]); ?></label>
            <select name=" echo escape($name); ?>_end-m"  id=" echo escape($name); ?>_end-m"
                
                
if ($forsearch && $autoupdate)
                    { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="if(sufficientDateParts(' echo escape($name); ?>_end')){AutoSave(' echo $field["ref"]; ?>');}" ?>
                    >
                <option value=""> echo escape($forsearch?$lang["anymonth"]:$lang["month"]); ?></option>
                
                
for ($d=1;$d<=12;$d++)
                    {
                    
$m=str_pad($d,2,"0",STR_PAD_LEFT);
                    
?><option  if ($d==$found_end_month) { ?>selected ?> value=" echo $m?>"> echo escape($lang["months"][$d-1])?></option>
                    
}?>
            </select>
            
            
}
        else
            {
            
?>
            <label class="accessibility-hidden" for=" echo escape($name?>_end-m"> echo escape($lang["month"]); ?></label>
            <select name=" echo escape($name); ?>_end-m" id=" echo escape($name); ?>_end-m" 
                
if (!$forsearch  && $edit_autosave)
                    {
?>onChange="if(sufficientDateParts(' echo escape($name); ?>_end')){AutoSave(' echo $field["ref"]; ?>');}" 
                else
                    {
?>onChange="UpdateResultCount();" ?>
                    >
                <option value=""> echo escape($forsearch?$lang["anymonth"]:$lang["month"]); ?></option>
                
                
for ($d=1;$d<=12;$d++)
                    {
                    
$m=str_pad($d,2,"0",STR_PAD_LEFT);
                    
?><option  if ($d==$found_end_month) { ?>selected ?> value=" echo $m?>"> echo escape($lang["months"][$d-1]) ?></option>
                    
}?>
            </select>
            <label class="accessibility-hidden" for=" echo escape($name?>_end-d"> echo escape($lang["day"]); ?></label>
            <select name=" echo escape($name); ?>_end-d" id=" echo escape($name); ?>_end-d"
              
                
if ($forsearch && $autoupdate)
                    { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="if(sufficientDateParts(' echo escape($name); ?>_end')){AutoSave(' echo $field["ref"]; ?>');}" ?>
                    >
              <option value=""> echo escape($forsearch?$lang["anyday"]:$lang["day"]); ?></option>
              
              
for ($d=1;$d<=31;$d++)
                {
                
$m=str_pad($d,2,"0",STR_PAD_LEFT);
                
?><option  if ($d==$found_end_day) { ?>selected ?> value=" echo $m?>"> echo $m?></option>
                
}
              
?>
            </select>
            
            
}
        if(
$forsearch)
            {
?>
            <label class="accessibility-hidden" for=" echo escape($name?>_end-y"> echo escape($lang["year"]); ?></label>
            <select name=" echo escape($name); ?>_end-y" id=" echo escape($name); ?>_end-y"
            
            
if ($forsearch && $autoupdate) { ?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="if(sufficientDateParts(' echo escape($name); ?>_end')){AutoSave(' echo $field["ref"]; ?>');}" ?>
                    >
              <option value=""> echo escape($forsearch?$lang["anyyear"]:$lang["year"]); ?></option>
              
              $y
=date("Y");
              
$y += $maxyear_extends_current;
              for (
$d=$y;$d>=$minyear;$d--)
                {
                
?><option  if ($d==$found_end_year ) { ?>selected ?>> echo $d?></option>
                
}
              
?>
            </select>
             
                
}
            else
                {
?>
                <label class="accessibility-hidden" for=" echo escape($name?>_end-y"> echo escape($lang["year"]); ?></label>
                <input size="5" name=" echo escape($name?>_end-y" id=" echo escape($name?>_end-y" type="text" value=" echo $found_end_year ?>"
                    

                    
if ($forsearch && $autoupdate)
                        { 
?>onChange="UpdateResultCount();" }
                    if(
$forsearch && !$forsearchbar)
                        { 
?> onKeyPress="if (!(updating)) {setTimeout('UpdateResultCount()',2000);updating=true;}" }
                    elseif (!
$forsearch  && $edit_autosave)
                        {
?>onChange="if(sufficientDateParts(' echo escape($name); ?>_end')){AutoSave(' echo $field["ref"]; ?>');}" ?>>
                
                
}

            if(
$forsearch !== true)
                {
                
?>
        <script>
            // Get value of the date element before the change
            jQuery('[name^= echo escape($name); ?>]').on('focus', function(){
                jQuery.data(this, 'current', jQuery(this).val());
            });
            //Check the value of the date after the change
            jQuery('[name^= echo escape($name); ?>_start]').on('change', function(){
                let day   = jQuery('[name= echo escape($name); ?>_start-d]').val().trim();
                let month = jQuery('[name= echo escape($name); ?>_start-m]').val().trim();
                let year  = jQuery('[name= echo escape($name); ?>_start-y]').val().trim(); 
                if (year != "" && !jQuery.isNumeric(year))
                    {
                    styledalert( echo "'" escape($lang["error"] . "','" $lang["invalid_date_generic"]) . "'" ?>);
                    jQuery(this).val(jQuery.data(this, 'current'));
                    }
                if(jQuery.isNumeric(year) && jQuery.isNumeric(day) && jQuery.isNumeric(month)){
                    //format date string into yyyy-mm-dd
                    let date_string = year + '-' + month + '-' + day;
                    //get a timestamp from the date string and then convert that back to yyyy-mm-dd
                    let date        = new Date(date_string).toISOString().split('T')[0];
                    //check if the before and after are the same, if a date like 2021-02-30 is selected date would be 2021-03-02
                    if(date_string !== date){
                        styledalert( echo "'" escape($lang["error"] . "','" $lang["invalid_date_generic"]) . "'" ?>);
                        jQuery(this).val(jQuery.data(this, 'current'))
                    }
                }
            })
            //Same again but for the end of the date range
            jQuery('[name^= echo escape($name); ?>_end]').on('change', function(){
                let day   = jQuery('[name= echo escape($name); ?>_end-d]').val().trim();
                let month = jQuery('[name= echo escape($name); ?>_end-m]').val().trim();
                let year  = jQuery('[name= echo escape($name); ?>_end-y]').val().trim();
                if (year != "" && !jQuery.isNumeric(year))
                    {
                    styledalert( echo "'" escape($lang["error"] . "','" $lang["invalid_date_generic"]) . "'" ?>);
                    jQuery(this).val(jQuery.data(this, 'current'));
                    }
                if(jQuery.isNumeric(year) && jQuery.isNumeric(day) && jQuery.isNumeric(month)){
                    //format date string into yyyy-mm-dd
                    let date_string = year + '-' + month + '-' + day;
                    //get a timestamp from the date string and then convert that back to yyyy-mm-dd
                    let date        = new Date(date_string).toISOString().split('T')[0];
                    //check if the before and after are the same, if a date like 2021-02-30 is selected date would be 2021-03-02
                    if(date_string !== date){
                        styledalert( echo "'" escape($lang["error"] . "','" $lang["invalid_date_generic"]) . "'" ?>);
                        jQuery(this).val(jQuery.data(this, 'current'))
                    }
                }
            })
        </script>
         ?>
    <!--  date range search end date-->
    </div>
    <div class="clearerleft"></div>
     if($daterange_edtf_support)
        {
?>
        <a href="#" onclick="if(jQuery('# echo escape($name); ?>_edtf').prop('disabled')){jQuery('# echo escape($name); ?>_edtf').prop('disabled',false);jQuery('# echo escape($name); ?>_edtf').show();jQuery('. echo escape($name); ?>_range').hide();}else{jQuery('# echo escape($name); ?>_edtf').prop('disabled',true);jQuery('# echo escape($name); ?>_edtf').hide();jQuery('. echo escape($name); ?>_range').show();}return false;">
            <i aria-hidden="true" class="fa fa-caret-right"></i>
             echo "EDTF"?>
        </a>
        
        
}
    }

This article was last updated 14th December 2024 16:35 Europe/London time based on the source file dated 13th December 2024 14:05 Europe/London time.