I would like to improve my filter in my custom joomla-component. I would like to create a filter that shows all items which have been created between 2 dates (MySQL-Table: creationdate
).
At the moment I'm using
JLayoutHelper::render('joomla.searchtools.default', array('view' => $this));
and a xml-defined filter-set to create a search-form and a category-based filter.
Does anybody know how to solve my problem? Or maybe somebody knows a component which filters for dates?
As I read in the component creator's source files I could so something like this:
<field name = "creationdate_from"
type = "calendar"
label = "test"
format = "%d.%m.%Y"
filter = "user_utc"
onchange = "this.form.submit();"/>
<field name = "creationdate_to"
type = "calendar"
label = "Test"
format = "%d.%m.%Y"
filter = "user_utc"
onchange = "this.form.submit();"/>
Additionally I would have to change the populateState-method
/**
* populateState function.
*
* @access protected
* @param mixed $ordering (default: null)
* @param mixed $direction (default: null)
* @return void
*/
protected function populateState($ordering = null, $direction = null)
{
...
$creationdatefrom = $this->getUserStateFromRequest($this->context . '.filter.creationdate_from', 'filter_creationdate_from', '');
$this->setState('filter.creationdate_from', $creationdatefrom);
$creationdateto = $this->getUserStateFromRequest($this->context . '.filter.creationdate_to', 'filter_creationdate_to', '');
$this->setState('filter.creationdate_to', $creationdateto);
...
}
and extend the query itself:
// Filter by date
$fromdate = $this->getState('filter.creationdate_from');
$todate = $this->getState('filter.creationdate_to');
if(!empty($fromdate))
{
$fromdate = $db->Quote($db->escape($fromdate, true));
$query->where('(DATE_FORMAT(a.creationdate, "%d.%m.%Y") >= ' . $fromdate . ')');
}
if(!empty($todate))
{
$todate = $db->Quote($db->escape($todate, true));
$query->where('(DATE_FORMAT(a.creationdate, "%d.%m.%Y") <= ' . $todate . ')');
}
This works fine - but there are 2 problems:
First problem: If I choose a date, the page reloads (of course) and after that the searchbar is not extended as it would be if I filter by state. The "reset"-button does not clear the values from calendar-fields aswell.
Second problem: If I enter some strange stuff, e.g. ";" or "/" into the date field, this input is not filtered and results in a crash: