Framework  3.9
FilterFieldRenderer\DateRangeFilterRenderer Class Reference

Adds a Date Range Filter with start and end dates and Next and Previous buttons to a FilterForm. More...

+ Inheritance diagram for FilterFieldRenderer\DateRangeFilterRenderer:
+ Collaboration diagram for FilterFieldRenderer\DateRangeFilterRenderer:

Public Member Functions

 __construct ($parent, $field, $endDateField="", $stepMode="month", $fiscal_year_start=1, $startParam=null, $endParam=null)
 
 calculateDateRange ()
 
 setStepModeMonth ()
 
 setStepModeYear ($startMonth=1)
 
 getConstraint ()
 
 setHandler ($handler)
 
 renderSearchScript ($field)
 
 renderSearchField ($field)
 If the user hides the startParm field, then don't display the calendar picker and input fields for dates. More...
 
 writeDateHiddenFields ()
 
 writeFilter ()
 
 writeButtons ()
 
- Public Member Functions inherited from FieldRenderer
 FieldRenderer ($parent)
 Constructor. More...
 
 _printLabel ($field, $colspan=1, $styles="", $annotation="")
 Internal method to generate the HTML for the field label. More...
 
 _getLabel ($field, $addSuffix=true)
 
 _startField ($field, $styles="")
 Internal method to generate the starting HTML for the field (including the label) More...
 
 _endField ($field)
 Internal method to generate the closing HTML for the field. More...
 
 addValidatorsToForm ($field, $required=false)
 This method is called by the AutoForm to add any default input validators that are required by the FieldRenderer. More...
 
 addSearchValidatorsToForm ($field, $mode, $required=false)
 For SearchForm, the validator field needs to match the name tag in the form which is in the format field:mode. More...
 
 formatName ($item, $name)
 Formats the given DataItem based on the supplied format string. More...
 
 renderScript ($field)
 FieldRenderers can override this method to provide any Javascript that their control requires for an edit form. More...
 
 renderField ($field)
 FieldRenderers must override this method to provide the HTML implementation of the control used to edit the field. More...
 
 renderSearchScript ($field, $mode)
 FieldRenderers can override this method to provide any Javascript that the control requires when being used in a search form. More...
 
 renderSearchField ($field, $mode)
 FieldRenderers must override this method to provide the HTML implementation of the control displayed for the field in a search form. More...
 
 renderOnSubmitHandler ($field)
 FieldRenderers can override this method to provide any Javascript that must be executed when the form is submitted on the client. More...
 
 preProcess ($field="")
 FieldRenderers can override this method to provide behavior that occurs prior to the saving of the parent form's target object to the database. More...
 
 postProcess ($field="")
 FieldRenderers can override this method to provide behavior that occurs after the parent form's target object has been saved to the database. More...
 

Static Public Member Functions

static convertMonthStringToNumber ($month)
 

Public Attributes

 $startParam = "startDate"
 
 $endParam = "endDate"
 
 $stepMode
 
 $fiscal_year_start = 1
 
 $startDate
 
 $endDate
 
 $field
 
 $endDateField
 
 $stepLabel
 
 $dateRangeLabel = "Reporting Period"
 
 $subTitle
 
 $handler
 
 $cssClass = "date_range"
 
- Public Attributes inherited from FieldRenderer
 $parent = null
 
 $labelSuffix = ""
 
 $colspan = 1
 
 $annotateBefore = false
 
 $annotateNextLine = true
 
 $hideLabel = false
 
 $onPreProcess = null
 callback hook for processing prior to saving the form's data object - individual renderers may override with custom processing More...
 
 $onPostProcess = null
 callback hook for processing after saving the form's data object - individual renderers may override with custom processing More...
 

Detailed Description

Adds a Date Range Filter with start and end dates and Next and Previous buttons to a FilterForm.

Usage: first create the instance of FilterForm, including in the DataItem object filter any other fields that you want to filter on (e.g., status drop down)

 $filterForm = new FilterForm($obj);

Then add the date range renderer to the form:

$dateRangeRenderer = new DateRangeFilterRenderer($filterForm, "start_date_field", "end_date_field", "month");

Parameters
-filterForm - instand of class FilterForm
-start_date_field - the field in the DataItem obj sent to the FilterForm that is the date to be queried on (e.g., order_date)
-end_date_field - the field in the DataItem obj sent to the FilterForm that is the end date to be queried on. If the query involves just one field, send the same field name for both. Example where field names are different: program_start_date and program_end_date
-stepMode - the increment for the Next and Previous buttons. Default is "month", can be "year" or "fiscal_year"
-fiscal_year_start - the numeric starting month of the fiscal_year, if the step mode is fiscal_year. May be stored in Settings - varies among web sites so is not queried directly here.

Retrieve the date range constraint through FilterForm instance $constraint = $filterForm->getConstraint();

To show only the Prev/Next buttons and not the start/end date input fields, hide the startDate field

$filterForm->hide("startDate");

Author
: Andy Green

Definition at line 898 of file filter_form.inc.

Constructor & Destructor Documentation

◆ __construct()

FilterFieldRenderer\DateRangeFilterRenderer::__construct (   $parent,
  $field,
  $endDateField = "",
  $stepMode = "month",
  $fiscal_year_start = 1,
  $startParam = null,
  $endParam = null 
)

Definition at line 920 of file filter_form.inc.

921  {
922  $this->stepMode = $stepMode;
923  $this->field = $field;
924 
925  $this->endDateField = (!$endDateField) ? $field : $endDateField;
926  $this->fiscal_year_start = $fiscal_year_start;
927 
928  if ($startParam) $this->startParam = $startParam;
929  if ($endParam) $this->endParam = $endParam;
930 
931  $this->calculateDateRange();
932 
933  $this->label = "";
934  if ($parent->data->hasField($field))
935  {
936  $parent->override($field, $label, $this);
937  }
938  else
939  {
940  $parent->add($this, $field);
941  }
942  $this->FieldRenderer($parent);
943 
944  // Needs to have a value so that it is included in the constraint
945  $parent->setParam($this->startParam, "to", $this->to);
946  // override default search constraint generation
947  $parent->setHandler($this->startParam, array($this, getConstraint));
948  }
FieldRenderer($parent)
Constructor.

Member Function Documentation

◆ calculateDateRange()

FilterFieldRenderer\DateRangeFilterRenderer::calculateDateRange ( )

Definition at line 950 of file filter_form.inc.

951  {
952  $this->startDate = $_REQUEST[$this->startParam];
953  $this->endDate = $_REQUEST[$this->endParam];
954 
955  if ($this->startDate || $this->endDate)
956  {
957  $validator = new DateValidator($this->startParam, "From");
958  $validator->required = false;
959  $this->msg = $validator->validate();
960 
961  if ($this->msg)
962  {
963  $startDate = null;
964  }
965 
966  $validator = new DateValidator($this->endParam, "To");
967  $validator->required = false;
968 
969  $this->msg = $validator->validate();
970 
971  if ($this->msg)
972  {
973  $endDate = null;
974  }
975 
976  if ($this->startDate) $this->startDate = new DateTime($this->startDate);
977  if ($this->endDate) $this->endDate = new DateTime($this->endDate);
978 
979  if ($this->startDate && $this->endDate)
980  {
981  $this->subTitle = $this->startDate->format("jS F Y")." to ".$this->endDate->format("jS F Y");
982  }
983 
984  // JDG 2/12 - remove "inclusive of last day"
985 
986  // JDG 11/2011 - Need to set this for stepping with Prev Next
987  $this->month = $this->startDate->format("m");
988  $this->year = $this->startDate->format("Y");
989  }
990 
991  $this->month = checkNumeric($_REQUEST['month']);
992  $this->year = checkNumeric($_REQUEST['year']);
993 
994  if (!$this->year) $this->year = date("Y");
995 
996  // JDG 11/2011 - need to set step mode whether we have a
997  // custom start/end date or not
998  if ($this->stepMode == "year")
999  {
1000  // calendar year stepping mode
1001  $this->setStepModeYear();
1002  }
1003  /*
1004  * If today's month for this year is less than
1005  * the fiscal year month, then show last year's
1006  * data
1007  */
1008  elseif($this->stepMode == "fiscal_year")
1009  {
1010  if(!checkNumeric($_REQUEST['year']) && $this->fiscal_year_start > date("n"))
1011  $this->year--;
1012  $this->setStepModeYear($this->fiscal_year_start);
1013  }
1014  else
1015  {
1016  // Monthly stepping mode
1017  $this->setStepModeMonth();
1018  }
1019 
1020  $this->from = $this->startDate->format("Y-m-d");
1021  $this->to = $this->endDate->format("Y-m-d");
1022  $this->calculated = true;
1023  }
Date Validator.
Definition: validation.inc:338
checkNumeric($p)
Security helper function.
Definition: functions.inc:630

◆ convertMonthStringToNumber()

static FilterFieldRenderer\DateRangeFilterRenderer::convertMonthStringToNumber (   $month)
static

Definition at line 1029 of file filter_form.inc.

1030  {
1031  for($i=1;$i<=12;$i++)
1032  {
1033  if(date("F", mktime(0, 0, 0, $i, 1, 0)) == $month)
1034  {
1035  $month_number = $i;
1036  break;
1037  }
1038  }
1039  return $month_number;
1040  }

◆ getConstraint()

FilterFieldRenderer\DateRangeFilterRenderer::getConstraint ( )

Definition at line 1107 of file filter_form.inc.

1108  {
1109  if(is_array($this->handler) && method_exists($this->handler[0], $this->handler[1]))
1110  {
1111  return call_user_func($this->handler, $this);
1112  }
1113 
1114  //AJG: Cope with date rounding
1115  $to = $this->to . " 23:59:59.999";
1116 
1117  if($this->field == $this->endDateField)
1118  {
1119  $name = $this->field;
1120  $constraint = "$name BETWEEN '{$this->from}' AND '{$to}'";
1121  }
1122  else
1123  {
1124  $constraint = "{$this->endDateField} <= '{$to}' AND {$this->field} >= '{$this->from}' ";
1125  }
1126 
1127  return $constraint;
1128  }

◆ renderSearchField()

FilterFieldRenderer\DateRangeFilterRenderer::renderSearchField (   $field)

If the user hides the startParm field, then don't display the calendar picker and input fields for dates.

If we don't display the input fields for start and end dates, then write them as hidden fields so the values are in $_GET.

Definition at line 1161 of file filter_form.inc.

1162  {
1163 
1164  $this->_startField($field);
1165 
1166  if (!array_key_exists($this->startParam, $this->parent->hidden))
1167  {
1168  $this->writeFilter();
1169  }
1170  else
1171  {
1172  $this->writeDateHiddenFields();
1173  }
1174 
1175  $this->writeButtons();
1176 
1177  $this->_endField($field);
1178  }
_startField($field, $styles="")
Internal method to generate the starting HTML for the field (including the label)
_endField($field)
Internal method to generate the closing HTML for the field.

◆ renderSearchScript()

FilterFieldRenderer\DateRangeFilterRenderer::renderSearchScript (   $field)

Definition at line 1135 of file filter_form.inc.

1136  {
1137  if (array_key_exists($field, $this->parent->hidden)) return "";
1138 
1139  $script .= <<<ENDSCRIPT
1140 
1141 <script src='/fakoli/calendar/sonjara_calendar.js' type='text/javascript'></script>
1142 <link href='/fakoli/calendar/sonjara_calendar.css' type='text/css' rel='stylesheet'></link>
1143 <script type="text/javascript">
1144 var startDateCalendar = new Calendar('startDateCalendar', '{$this->parent->id}', '{$this->startParam}');
1145 var endDateCalendar = new Calendar('endDateCalendar', '{$this->parent->id}', '{$this->endParam}');
1146 </script>
1147 
1148 ENDSCRIPT;
1149 
1150  echo $script;
1151  }

◆ setHandler()

FilterFieldRenderer\DateRangeFilterRenderer::setHandler (   $handler)

Definition at line 1130 of file filter_form.inc.

1131  {
1132  $this->handler = $handler;
1133  }

◆ setStepModeMonth()

FilterFieldRenderer\DateRangeFilterRenderer::setStepModeMonth ( )

Definition at line 1042 of file filter_form.inc.

1043  {
1044  if (!$this->month)
1045  {
1046  $this->year = date("Y");
1047  $this->month = date("m");
1048  }
1049 
1050  $this->prevMonth = $this->month - 1;
1051  $this->prevYear = $this->year;
1052  if ($this->prevMonth == 0)
1053  {
1054  $this->prevYear--;
1055  $this->prevMonth = 12;
1056  }
1057 
1058  $this->nextMonth = $this->month + 1;
1059  $this->nextYear = $this->year;
1060  if ($this->nextMonth > 12)
1061  {
1062  $this->nextMonth = 1;
1063  $this->nextYear++;
1064  }
1065 
1066  if(!$this->startDate)
1067  $this->startDate = new DateTime("{$this->year}-{$this->month}-01T00:00:00");
1068 
1069  if(!$this->endDate)
1070  {
1071  $this->endDate = clone $this->startDate;
1072  $this->endDate->modify("+1 months");
1073  // JDG 2/12 - show last day of month
1074  $this->endDate->modify("-1 days");
1075  }
1076 
1077  if(!$this->subTitle)
1078  {
1079  $this->subTitle = $this->startDate->format("F Y");
1080  }
1081  }

◆ setStepModeYear()

FilterFieldRenderer\DateRangeFilterRenderer::setStepModeYear (   $startMonth = 1)

Definition at line 1083 of file filter_form.inc.

1084  {
1085  $this->month = $startMonth;
1086 
1087  $this->prevMonth = $startMonth;
1088  $this->prevYear = $this->year - 1;
1089 
1090  $this->nextMonth = $startMonth;
1091  $this->nextYear = $this->year + 1;
1092  $this->subTitle= $this->year;
1093 
1094  if(!$this->startDate)
1095  {
1096  $this->startDate = new DateTime("{$this->year}-{$this->month}-01T00:00:00");
1097  }
1098 
1099  if(!$this->endDate)
1100  {
1101  $this->endDate = clone $this->startDate;
1102  $this->endDate->modify("+1 years");
1103  $this->endDate->modify("-1 days");
1104  }
1105  }

◆ writeButtons()

FilterFieldRenderer\DateRangeFilterRenderer::writeButtons ( )

Definition at line 1215 of file filter_form.inc.

1216  {
1217  if ($this->stepMode == "none") return;
1218 
1219  if (!$this->calculated) $this->calculateDateRange();
1220 
1221  $queryString = "?".$_SERVER["QUERY_STRING"];
1222 
1223  $queryString = preg_replace("/identifier=\\w+\\&?/", "", $queryString);
1224  $queryString = preg_replace("/year=\\d+\\&?/", "", $queryString);
1225  $queryString = preg_replace("/month=\\d+\\&?/", "", $queryString);
1226  $queryString = preg_replace("/startDate=\\d+\\&?/", "", $queryString);
1227  $queryString = preg_replace("/endDate=\\d+\\&?/", "", $queryString);
1228 
1229  $prevLink = appendToQueryString($queryString, "year=$this->prevYear&month=$this->prevMonth");
1230  $nextLink = appendToQueryString($queryString, "year=$this->nextYear&month=$this->nextMonth");
1231 
1232  $stepLabel = ($this->stepLabel) ? $this->stepLabel : prettify($this->stepMode);
1233 ?>
1234 <div class="<?php echo $this->cssClass ?>">
1235 <div style="float: right; z-index:2; padding-bottom: 10px;">
1236 <input type="button" class="button" value=" Next <?echo $stepLabel ?> &raquo; " onclick="go('<?echo $nextLink?>');">
1237 </div>
1238 <input type="button" class="button" value=" &laquo; Previous <?echo $stepLabel ?> " onclick="go('<?echo $prevLink?>');">
1239 </div>
1240 <?
1241  }
appendToQueryString($qs, $params)
Appends the specified parameters to the supplied query string.
Definition: functions.inc:1594
prettify($name)
Takes a variable or field name and converts it into a human-readable version (assuming that the origi...
Definition: functions.inc:1413

◆ writeDateHiddenFields()

FilterFieldRenderer\DateRangeFilterRenderer::writeDateHiddenFields ( )

Definition at line 1180 of file filter_form.inc.

1181  {
1182  if(!$_REQUEST["month"] || !$_REQUEST["year"])
1183  {
1184  ?>
1185  <input type="hidden" name="<?php echo $this->startParam ?>" value="<?echo $this->startDate->format("m/d/Y")?>">
1186  <input type="hidden" name="<?php echo $this->endParam ?>" value="<?echo $this->endDate->format("m/d/Y")?>">
1187  <?php
1188  }
1189  else
1190  {
1191  ?>
1192  <input type="hidden" name="year" value="<? echo $this->year ?>">
1193  <input type="hidden" name="month" value="<? echo $this->month ?>">
1194  <?php
1195  }
1196  }

◆ writeFilter()

FilterFieldRenderer\DateRangeFilterRenderer::writeFilter ( )

Definition at line 1198 of file filter_form.inc.

1199  {
1200  if (!$this->calculated) $this->calculateDateRange();
1201  $endDate = clone $this->endDate;
1202 
1203 ?>
1204 <div style="clear: both; float: left; padding-bottom: 4px">
1205  <label><?php echo $this->dateRangeLabel ?> From: </label>
1206  <input type="text" size="10" name="<?php echo $this->startParam ?>" value="<?echo $this->startDate->format("m/d/Y")?>">&nbsp;
1207  <img src='/fakoli/calendar/calendar.gif' alt='Popup Calendar' style='vertical-align: middle; border:0' onclick='startDateCalendar.toggle(this)'/>
1208  <label> To: </label><input type="text" size="10" name="<?php echo $this->endParam ?>" value="<?echo $endDate->format("m/d/Y")?>">&nbsp;
1209  <img src='/fakoli/calendar/calendar.gif' alt='Popup Calendar' style='vertical-align: middle; border:0' onclick='endDateCalendar.toggle(this)'/>
1210  &nbsp;<input type="submit" class="button" value=" Update View "/>
1211  </div>
1212 <?
1213  }

Member Data Documentation

◆ $cssClass

FilterFieldRenderer\DateRangeFilterRenderer::$cssClass = "date_range"

Definition at line 918 of file filter_form.inc.

◆ $dateRangeLabel

FilterFieldRenderer\DateRangeFilterRenderer::$dateRangeLabel = "Reporting Period"

Definition at line 914 of file filter_form.inc.

◆ $endDate

FilterFieldRenderer\DateRangeFilterRenderer::$endDate

Definition at line 906 of file filter_form.inc.

◆ $endDateField

FilterFieldRenderer\DateRangeFilterRenderer::$endDateField

Definition at line 911 of file filter_form.inc.

◆ $endParam

FilterFieldRenderer\DateRangeFilterRenderer::$endParam = "endDate"

Definition at line 901 of file filter_form.inc.

◆ $field

FilterFieldRenderer\DateRangeFilterRenderer::$field

Definition at line 910 of file filter_form.inc.

◆ $fiscal_year_start

FilterFieldRenderer\DateRangeFilterRenderer::$fiscal_year_start = 1

Definition at line 903 of file filter_form.inc.

◆ $handler

FilterFieldRenderer\DateRangeFilterRenderer::$handler

Definition at line 917 of file filter_form.inc.

◆ $startDate

FilterFieldRenderer\DateRangeFilterRenderer::$startDate

Definition at line 905 of file filter_form.inc.

◆ $startParam

FilterFieldRenderer\DateRangeFilterRenderer::$startParam = "startDate"

Definition at line 900 of file filter_form.inc.

◆ $stepLabel

FilterFieldRenderer\DateRangeFilterRenderer::$stepLabel

Definition at line 913 of file filter_form.inc.

◆ $stepMode

FilterFieldRenderer\DateRangeFilterRenderer::$stepMode

Definition at line 902 of file filter_form.inc.

◆ $subTitle

FilterFieldRenderer\DateRangeFilterRenderer::$subTitle

Definition at line 915 of file filter_form.inc.


The documentation for this class was generated from the following file: