Skip to content

Commit

Permalink
added option for list to either merge menu/module prefilters (preferr…
Browse files Browse the repository at this point in the history
…ed option) or to have the module/menu prefilters override its prefilters (previous default state). Closes GH-1226
  • Loading branch information
pollen8 committed Feb 19, 2016
1 parent 5874678 commit 0e09d0c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 45 deletions.
Expand Up @@ -1375,4 +1375,7 @@ COM_FABRIK_LABELS_CAPS="All Caps (e.g. user_name becomes 'USER NAME')"
COM_FABRIK_SHOW_IN_DASHBOARD_DESC="If turned on, then this will add a link to the list into the Fabrik Admin Quick Icon module"
COM_FABRIK_SHOW_IN_DASHBOARD_LABEL="Show in module"
COM_FABRIK_DASHBOARD_ICON_DESC="Link icon,glyphicon or fontawesome class e.g. 'icon-joomla'"
COM_FABRIK_DASHBOARD_ICON_LABEL="Link icon"
COM_FABRIK_DASHBOARD_ICON_LABEL="Link icon"

COM_FABRIK_FIELD_LIST_MENU_MODULE_PREFILTERS_OVERRIDE_LIST_DESC="If set to yes then the list prefilters are replaced by menu/module filters. If set to no then the menu/module prefilters are applied on top of the list prefilters. Although this option defaults to yes (for backwards compatibility) we recommend setting it to no"
COM_FABRIK_FIELD_LIST_MENU_MODULE_PREFILTERS_OVERRIDE_LIST_LABEL="Overriden by menu/modules"
10 changes: 10 additions & 0 deletions administrator/components/com_fabrik/models/forms/list.xml
Expand Up @@ -1301,6 +1301,16 @@

<fieldset name="prefilter">

<field name="menu_module_prefilters_override"
type="radio"
class="btn-group"
default="1"
description="COM_FABRIK_FIELD_LIST_MENU_MODULE_PREFILTERS_OVERRIDE_LIST_DESC"
label="COM_FABRIK_FIELD_LIST_MENU_MODULE_PREFILTERS_OVERRIDE_LIST_LABEL">
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>

<field name="prefilter_query"
type="text"
class="input-xxlarge"
Expand Down
147 changes: 103 additions & 44 deletions components/com_fabrik/models/list.php
Expand Up @@ -3396,8 +3396,18 @@ public function buildQueryWhere($incFilters = true, $query = false, $doJoins = t
private function _filtersToSQL(&$filters, $startWithWhere = true)
{
$prefilters = $this->groupFilterSQL($filters, 'prefilter');
$menuFilters = $this->groupFilterSQL($filters, 'menuPrefilter');
$postFilers = $this->groupFilterSQL($filters);

// Combine menu and prefilters
if (!empty($prefilters) && !empty($menuFilters))
{
array_unshift($menuFilters, 'AND');
}

$prefilters = array_merge($prefilters, $menuFilters);


if (!empty($prefilters) && !empty($postFilers))
{
array_unshift($postFilers, 'AND');
Expand Down Expand Up @@ -3462,18 +3472,18 @@ private function groupFilterSQL(&$filters, $type = '*')
{
// $$$rob - prefilter with element that is not published so ignore
$condition = JString::strtoupper(FArrayHelper::getValue($filters['condition'], $i, ''));

$searchType = $filters['search_type'][$i];
if (FArrayHelper::getValue($filters['sqlCond'], $i, '') == '' && !in_array($condition, $nullElementConditions))
{
continue;
}

if ($filters['search_type'][$i] == 'prefilter' && $type == '*')
if (in_array($searchType, array('prefilter', 'menuPrefilter')) && $type == '*')
{
continue;
}

if ($filters['search_type'][$i] != 'prefilter' && $type == 'prefilter')
if ($searchType !== $type && $type !== '*')
{
continue;
}
Expand Down Expand Up @@ -5075,7 +5085,7 @@ public function &getFilterArray()
*/
$this->getPrefilterArray($this->filters);

// These are filters created from a search form or normal search
// These are filters created from a search form or normal search, assign them to the filters array
$keys = array_keys($request);
$indexStep = count(FArrayHelper::getValue($this->filters, 'key', array()));
FabrikHelperHTML::debug($keys, 'filter:request keys');
Expand Down Expand Up @@ -5353,19 +5363,16 @@ private function showInList()
}

/**
* Get the prefilter settings from list/module/menu options
* Use in listModel::getPrefilterArray() and formModel::getElementIds()
* Get the module or then menu pre-filter settings
*
* @return multitype:array
* @return string
*/
public function prefilterSetting()
private function menuModulePrefilters()
{
$input = $this->app->input;
$package = $this->app->getUserState('com_fabrik.package', 'fabrik');
$params = $this->getParams();

// Are we coming from a post request via a module?
$moduleId = 0;
// Are we coming from a post request via a module?
$requestRef = $input->get('listref', '', 'string');

if ($requestRef !== '' && !strstr($requestRef, 'com_' . $package))
Expand Down Expand Up @@ -5393,18 +5400,6 @@ public function prefilterSetting()
}
}

// List pre-filter properties
$afilterFields = (array) $params->get('filter-fields');
$afilterConditions = (array) $params->get('filter-conditions');
$afilterValues = (array) $params->get('filter-value');
$afilterAccess = (array) $params->get('filter-access');
$afilterEval = (array) $params->get('filter-eval');
$afilterJoins = (array) $params->get('filter-join');
$afilterGrouped = (array) $params->get('filter-grouped');

/* If we are rendering as a module don't pick up the menu item options (params already set in list module)
* so first statement when rendering a module, 2nd when posting to the component from a module.
*/
if (!strstr($this->getRenderContext(), 'mod_fabrik_list') && $moduleId === 0)
{
$spoof_check = array(
Expand All @@ -5414,23 +5409,85 @@ public function prefilterSetting()
$properties = FabrikWorker::getMenuOrRequestVar('prefilters', '', $this->isMambot, 'menu', $spoof_check);
}

return $properties;
}

/**
* Get the prefilter settings from list/module/menu options
* Use in listModel::getPrefilterArray() and formModel::getElementIds()
*
* @return multitype:array
*/
public function prefilterSetting()
{
$params = $this->getParams();
$properties = $this->menuModulePrefilters();

// List pre-filter properties
$listFields = (array) $params->get('filter-fields');
$listConditions = (array) $params->get('filter-conditions');
$listValue = (array) $params->get('filter-value');
$listAccess = (array) $params->get('filter-access');
$listEval = (array) $params->get('filter-eval');
$listJoins = (array) $params->get('filter-join');
$listGrouped = (array) $params->get('filter-grouped');
$listSearchType = array_fill(0, count($listJoins), 'prefilter');

This comment has been minimized.

Copy link
@jaanusnurmoja

jaanusnurmoja Feb 20, 2016

If the site host has php version < 5.6.0 this row produces an error message if count($listJoins) = 0::
Warning: array_fill(): Number of elements must be positive in /site/components/com_fabrik/models/list.php on line 5434


/* If we are rendering as a module don't pick up the menu item options (params already set in list module)
* so first statement when rendering a module, 2nd when posting to the component from a module.
*/


if (isset($properties))
{
$prefilters = ArrayHelper::fromObject(json_decode($properties));
$conditions = (array) $prefilters['filter-conditions'];

if (!empty($conditions))
{
$afilterFields = FArrayHelper::getValue($prefilters, 'filter-fields', array());
$afilterConditions = FArrayHelper::getValue($prefilters, 'filter-conditions', array());
$afilterValues = FArrayHelper::getValue($prefilters, 'filter-value', array());
$afilterAccess = FArrayHelper::getValue($prefilters, 'filter-access', array());
$afilterEval = FArrayHelper::getValue($prefilters, 'filter-eval', array());
$afilterJoins = FArrayHelper::getValue($prefilters, 'filter-join', array());
$fields = FArrayHelper::getValue($prefilters, 'filter-fields', array());
$conditions = FArrayHelper::getValue($prefilters, 'filter-conditions', array());
$values = FArrayHelper::getValue($prefilters, 'filter-value', array());
$access = FArrayHelper::getValue($prefilters, 'filter-access', array());
$eval = FArrayHelper::getValue($prefilters, 'filter-eval', array());
$joins = FArrayHelper::getValue($prefilters, 'filter-join', array());
$searchType = array_fill(0, count($joins), 'menuPrefilter');

This comment has been minimized.

Copy link
@jaanusnurmoja

jaanusnurmoja Feb 20, 2016

If the site host has php version < 5.6.0 this row produces an error message if count($joins) = 0


$overrideListPrefilters = $params->get('menu_module_prefilters_override', true);

if ($overrideListPrefilters)
{
// Original behavior
$listFields = $fields;
$listConditions = $conditions;
$listValue = $values;
$listAccess = $access;
$listEval = $eval;
$listJoins = $joins;
$listSearchType = $searchType;
}
else
{
// Preferred behavior but for backwards compat we need to ask users to
// set this option in the menu/module settings
$joins[0] = 'AND';
$listFields = array_merge($listFields, $fields);
$listConditions = array_merge($listConditions, $conditions);
$listValue = array_merge($listValue, $values);
$listAccess = array_merge($listAccess, $access);
$listEval = array_merge($listEval, $eval);

$listSearchType = array_merge($listSearchType, $searchType);
//$listGrouped[count($listGrouped) -1] = '1';
$listJoins = array_merge($listJoins, $joins);

$listGrouped = array_merge($listGrouped, array_fill(0, count($joins), 0));
}

This comment has been minimized.

Copy link
@jaanusnurmoja

jaanusnurmoja Feb 20, 2016

may produce error when php<5.6.0
See changelog
http://php.net/manual/en/function.array-fill.php

}
}

return array($afilterFields, $afilterConditions, $afilterValues, $afilterAccess, $afilterEval, $afilterJoins, $afilterGrouped);
return array($listFields, $listConditions, $listValue, $listAccess,
$listEval, $listJoins, $listGrouped, $listSearchType);
}

/**
Expand All @@ -5439,36 +5496,38 @@ public function prefilterSetting()
*
* @param array &$filters filters
*
* @return array prefilters combinde with filters
* @return array prefilters combined with filters
*/
public function getPrefilterArray(&$filters)
{
if (!isset($this->prefilters))
{
$elements = $this->getElements('filtername', false, false);
list($afilterFields, $afilterConditions, $afilterValues, $afilterAccess, $afilterEval, $afilterJoins, $afilterGrouped) = $this->prefilterSetting();
$join = 'WHERE';
list($filterFields, $filterConditions, $filterValues, $filterAccess,
$filterEval, $filterJoins, $filterGrouped, $listSearchType) = $this->prefilterSetting();


for ($i = 0; $i < count($afilterFields); $i++)
for ($i = 0; $i < count($filterFields); $i++)
{
if (!array_key_exists(0, $afilterJoins) || $afilterJoins[0] == '')
if (!array_key_exists(0, $filterJoins) || $filterJoins[0] == '')
{
$afilterJoins[0] = 'AND';
$filterJoins[0] = 'AND';
}

$join = FArrayHelper::getValue($afilterJoins, $i, 'AND');
$join = FArrayHelper::getValue($filterJoins, $i, 'AND');

if (trim(JString::strtolower($join)) == 'where')
{
$join = 'AND';
}

$filter = $afilterFields[$i];
$condition = $afilterConditions[$i];
$selValue = FArrayHelper::getValue($afilterValues, $i, '');
$filterEval = FArrayHelper::getValue($afilterEval, $i, false);
$filterGrouped = FArrayHelper::getValue($afilterGrouped, $i, false);
$selAccess = $afilterAccess[$i];
$filter = $filterFields[$i];
$condition = $filterConditions[$i];
$searchType = ArrayHelper::getValue($listSearchType, $i, 'prefilter');
$selValue = FArrayHelper::getValue($filterValues, $i, '');
$filterEval = FArrayHelper::getValue($filterEval, $i, false);
$filterGrouped = FArrayHelper::getValue($filterGrouped, $i, false);
$selAccess = $filterAccess[$i];

if (!$this->mustApplyFilter($selAccess))
{
Expand Down Expand Up @@ -5508,7 +5567,7 @@ public function getPrefilterArray(&$filters)
}

$filters['join'][] = $join;
$filters['search_type'][] = 'prefilter';
$filters['search_type'][] = $searchType;
$filters['key'][] = $tmpFilter;
$filters['value'][] = $selValue;
$filters['origvalue'][] = $selValue;
Expand Down

0 comments on commit 0e09d0c

Please sign in to comment.