Skip to content

Instantly share code, notes, and snippets.

@julienbourdeau
Created November 6, 2014 15:44
Show Gist options
  • Select an option

  • Save julienbourdeau/cd7d322468bb2bd1263a to your computer and use it in GitHub Desktop.

Select an option

Save julienbourdeau/cd7d322468bb2bd1263a to your computer and use it in GitHub Desktop.
processFilter for HelperList in Module classes
public function processFilter()
{
if (!isset($this->list_id))
$this->list_id = $this->table;
$prefix = str_replace(array('admin', 'controller'), '', Tools::strtolower(get_class($this)));
if (isset($this->list_id))
{
foreach ($_POST as $key => $value)
{
if ($value === '')
unset($this->context->cookie->{$prefix.$key});
elseif (stripos($key, $this->list_id.'Filter_') === 0)
$this->context->cookie->{$prefix.$key} = !is_array($value) ? $value : serialize($value);
elseif (stripos($key, 'submitFilter') === 0)
$this->context->cookie->$key = !is_array($value) ? $value : serialize($value);
}
foreach ($_GET as $key => $value)
if (stripos($key, $this->list_id.'Orderby') === 0 && Validate::isOrderBy($value))
{
if ($value === '' || $value == $this->_defaultOrderBy)
unset($this->context->cookie->{$prefix.$key});
else
$this->context->cookie->{$prefix.$key} = $value;
}
elseif (stripos($key, $this->list_id.'Orderway') === 0 && Validate::isOrderWay($value))
{
if ($value === '' || $value == $this->_defaultOrderWay)
unset($this->context->cookie->{$prefix.$key});
else
$this->context->cookie->{$prefix.$key} = $value;
}
elseif ($value === '')
unset($this->context->cookie->{$prefix.$key});
elseif (stripos($key, $this->list_id.'Filter_') === 0)
$this->context->cookie->{$prefix.$key} = !is_array($value) ? $value : serialize($value);
elseif (stripos($key, 'submitFilter') === 0)
$this->context->cookie->$key = !is_array($value) ? $value : serialize($value);
}
$filters = $this->context->cookie->getFamily($prefix.$this->list_id.'Filter_');
foreach ($filters as $key => $value)
{
/* Extracting filters from $_POST on key filter_ */
if ($value != null && !strncmp($key, $prefix.$this->list_id.'Filter_', 7 + Tools::strlen($prefix.$this->list_id)))
{
$key = Tools::substr($key, 7 + Tools::strlen($prefix.$this->list_id));
/* Table alias could be specified using a ! eg. alias!field */
$tmp_tab = explode('!', $key);
$filter = count($tmp_tab) > 1 ? $tmp_tab[1] : $tmp_tab[0];
if ($field = $this->filterToField($key, $filter))
{
$type = (array_key_exists('filter_type', $field) ? $field['filter_type'] : (array_key_exists('type', $field) ? $field['type'] : false));
if (($type == 'date' || $type == 'datetime') && is_string($value))
$value = Tools::unSerialize($value);
$key = isset($tmp_tab[1]) ? $tmp_tab[0].'.`'.$tmp_tab[1].'`' : '`'.$tmp_tab[0].'`';
// Assignement by reference
if (array_key_exists('tmpTableFilter', $field))
$sql_filter = & $this->_tmpTableFilter;
elseif (array_key_exists('havingFilter', $field))
$sql_filter = & $this->_filterHaving;
else
$sql_filter = & $this->_filter;
/* Only for date filtering (from, to) */
if (is_array($value))
{
if (isset($value[0]) && !empty($value[0]))
{
if (!Validate::isDate($value[0]))
$this->errors[] = Tools::displayError('The \'From\' date format is invalid (YYYY-MM-DD)');
else
$sql_filter .= ' AND '.pSQL($key).' >= \''.pSQL(Tools::dateFrom($value[0])).'\'';
}
if (isset($value[1]) && !empty($value[1]))
{
if (!Validate::isDate($value[1]))
$this->errors[] = Tools::displayError('The \'To\' date format is invalid (YYYY-MM-DD)');
else
$sql_filter .= ' AND '.pSQL($key).' <= \''.pSQL(Tools::dateTo($value[1])).'\'';
}
}
else
{
$sql_filter .= ' AND ';
$check_key = ($key == $this->identifier || $key == '`'.$this->identifier.'`');
if ($type == 'int' || $type == 'bool')
$sql_filter .= (($check_key || $key == '`active`') ? 'a.' : '').pSQL($key).' = '.(int)$value.' ';
elseif ($type == 'decimal')
$sql_filter .= ($check_key ? 'a.' : '').pSQL($key).' = '.(float)$value.' ';
elseif ($type == 'select')
$sql_filter .= ($check_key ? 'a.' : '').pSQL($key).' = \''.pSQL($value).'\' ';
else
{
if ($type == 'price')
$value = (float)str_replace(',', '.', $value);
$sql_filter .= ($check_key ? 'a.' : '').pSQL($key).' LIKE \'%'.pSQL($value).'%\' ';
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment