Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

render_date_range_field()

Parameters

ColumnTypeDefaultDescription
$name
$value
$forsearch true
$autoupdate false
$field array
$reset ""

Location

include/render_functions.php lines 2247 to 2620

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_year""start_month","start_day","end_year","end_month","end_day") as $subpart)
            {
            if(
getval($name "_" $subpart,"") != "")
                {
                ${
"found_" $subpart} = getval($name "_" $subpart,"");
                }
            }
        }
    
    if(
$daterange_edtf_support)
        {
        
// Use EDTF format for date input
        
?>      
        <input class=" echo $forsearch?"SearchWidth":"stdwidth"?>"  name=" echo $name?>_edtf" id=" echo $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 $name?>_range" id=" echo $name?>_from">
    <label class="InnerLabel"> echo escape($lang["fromdate"])?></label>
    
               
        
if($date_d_m_y)
            {  
            
?>
            <label class="accessibility-hidden" for=" echo escape($name?>_start_day"> echo escape($lang["day"]); ?></label>
            <select name=" echo $name?>_start_day"
             
            
if ($forsearch && $autoupdate
                    { 
?>onChange="UpdateResultCount();" }
            elseif (!
$forsearch  && $edit_autosave)
            {
?>onChange="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_month"> echo escape($lang["month"]); ?></label>
            <select name=" echo $name?>_start_month"
                 
                
if ($forsearch && $autoupdate
                    { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="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_month"> echo escape($lang["month"]); ?></label>
            <select name=" echo $name?>_start_month"
                 
                
if ($forsearch && $autoupdate
                    { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="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_day"> echo escape($lang["day"]); ?></label>
            <select name=" echo $name?>_start_day"
               
                
if ($forsearch && $autoupdate
                    { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="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?>_end_year"> echo escape($lang["year"]); ?></label>
            <select name=" echo escape($name?>_start_year"
                 
                
if ($forsearch && $autoupdate
                        { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                {
?>onChange="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_year"> echo escape($lang["year"]); ?></label>
            <input size="5" name=" echo escape($name?>_start_year" id=" echo escape($name?>_start_year" 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="AutoSave(' echo $field["ref"]; ?>');" ?>>
            
            
}?>
    </div>
    
    <div class="clearerleft"> </div>
    
    <!--- to date -->
    <label  class='daterangelabel'></label>
    
    
    
    <div class="stdwidth indent  echo $name?>_range" id=" echo $name?>_to" >
    <label class="InnerLabel"> echo escape($lang["todate"])?></label>
           
        
if($date_d_m_y)
            {
            
?>
            <label class="accessibility-hidden" for=" echo escape($name?>_end_day"> echo escape($lang["day"]); ?></label>
            <select name=" echo $name?>_end_day"
               
                
if ($forsearch && $autoupdate
                    { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="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_month"> echo escape($lang["month"]); ?></label>
            <select name=" echo $name?>_end_month"
                 
                
if ($forsearch && $autoupdate
                    { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="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_month"> echo escape($lang["month"]); ?></label>
            <select name=" echo $name?>_end_month"  
                
if (!$forsearch  && $edit_autosave)
                    {
?>onChange="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_day"> echo escape($lang["day"]); ?></label>
            <select name=" echo $name?>_end_day"
               
                
if ($forsearch && $autoupdate
                    { 
?>onChange="UpdateResultCount();" }
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="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_year"> echo escape($lang["year"]); ?></label>
            <select name=" echo $name?>_end_year" 
             
            
if ($forsearch && $autoupdate) { ?>onChange="UpdateResultCount();" 
                elseif (!
$forsearch  && $edit_autosave)
                    {
?>onChange="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_year"> echo escape($lang["year"]); ?></label>
                <input size="5" name=" echo escape($name?>_end_year" id=" echo escape($name?>_end_year" 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="AutoSave(' echo $field["ref"]; ?>');" ?>>
                
                
}
            
            if(
$forsearch !== true)
                {
                
?>
        <script>
            //Get value of the date element before the change
            jQuery('[name^= echo $name;?>]').on('focus', function(){
                jQuery.data(this, 'current', jQuery(this).val());
            });
            //Check the value of the date after the change
            jQuery('[name^= echo $name;?>_start]').on('change', function(){
                let day   = jQuery('[name= echo escape($name); ?>_start_day]').val().trim();
                let month = jQuery('[name= echo escape($name); ?>_start_month]').val().trim();
                let year  = jQuery('[name= echo escape($name); ?>_start_year]').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 $name;?>_end]').on('change', function(){
                let day   = jQuery('[name= echo escape($name); ?>_end_day]').val().trim();
                let month = jQuery('[name= echo escape($name); ?>_end_month]').val().trim();
                let year  = jQuery('[name= echo escape($name); ?>_end_year]').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 $name ?>_edtf').prop('disabled')){jQuery('# echo $name ?>_edtf').prop('disabled',false);jQuery('# echo $name ?>_edtf').show();jQuery('. echo $name ?>_range').hide();}else{jQuery('# echo $name ?>_edtf').prop('disabled',true);jQuery('# echo $name ?>_edtf').hide();jQuery('. echo $name ?>_range').show();}return false;">
            <i aria-hidden="true" class="fa fa-caret-right"></i>
             echo "EDTF"?>
        </a>
        
        
}
    }

This article was last updated 22nd May 2024 15:05 Europe/London time based on the source file dated 17th May 2024 16:45 Europe/London time.