Skip to content

Commit

Permalink
UI: Filter Revision - Implementation (#3390)
Browse files Browse the repository at this point in the history
* Filter Revision: Implementation

* Fix Filter tests and add missing render tests for Select Input

* Filter Revision: Implementation, part II
  • Loading branch information
tfamula committed Aug 17, 2021
1 parent a3aa757 commit f3a8b1a
Show file tree
Hide file tree
Showing 27 changed files with 1,670 additions and 897 deletions.
14 changes: 4 additions & 10 deletions Services/UI/classes/class.ilUIFilterRequestAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/
class ilUIFilterRequestAdapter
{
const CMD_PARAMETER = "cmdFilter";
const RENDER_INPUT_BASE = "__filter_status_";
public const CMD_PARAMETER = "cmdFilter";
public const RENDER_INPUT_BASE = "__filter_status_";

/**
* @var ServerRequestInterface
Expand All @@ -27,20 +27,13 @@ class ilUIFilterRequestAdapter
*/
protected $params;

/**
* post data
* @var array|null
*/
protected $post;

/**
* Constructor
*/
public function __construct(ServerRequestInterface $request)
{
$this->request = $request;
$this->params = $this->request->getQueryParams();
$this->post = $this->request->getParsedBody();
}

/**
Expand All @@ -56,7 +49,7 @@ public function getFilterCmd() : string
}

/**
* Has an input field been rendered in current post request?
* Has an input field been rendered in current request?
*
* @param $input_id
* @return bool
Expand Down Expand Up @@ -86,6 +79,7 @@ public function getFilterWithRequest(Filter\Standard $filter) : Filter\Standard
*
* @param string $base_action
* @param string $filter_cmd
* @param bool $non_asynch
* @return string
*/
public function getAction(string $base_action, string $filter_cmd, $non_asynch = false) : string
Expand Down
68 changes: 48 additions & 20 deletions Services/UI/classes/class.ilUIFilterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
class ilUIFilterService
{
// command constants
const CMD_TOGGLE_ON = "toggleOn";
const CMD_TOGGLE_OFF = "toggleOff";
const CMD_EXPAND = "expand";
const CMD_COLLAPSE = "collapse";
const CMD_APPLY = "apply";
const CMD_RESET = "reset";
public const CMD_TOGGLE_ON = "toggleOn";
public const CMD_TOGGLE_OFF = "toggleOff";
public const CMD_EXPAND = "expand";
public const CMD_COLLAPSE = "collapse";
public const CMD_APPLY = "apply";
public const CMD_RESET = "reset";


/**
Expand Down Expand Up @@ -69,12 +69,12 @@ public function __construct(ilUIService $service, ilUIServiceDependencies $deps)
* @return Filter\Standard
*/
public function standard(
$filter_id,
$base_action,
string $filter_id,
string $base_action,
array $inputs,
array $is_input_initially_rendered,
$is_activated = false,
$is_expanded = false
bool $is_activated = false,
bool $is_expanded = false
) : Filter\Standard {
$ui = $this->ui->factory();

Expand All @@ -91,6 +91,13 @@ public function standard(
// put data from session into filter
$inputs_with_session_data = [];
$is_input_initially_rendered_with_session = [];

if (count($inputs) != count($is_input_initially_rendered)) {
throw new \ArgumentCountError(
"Inputs and boolean values for initial rendering must be arrays of same size."
);
}

foreach ($inputs as $input_id => $i) {
// rendering information
$rendered =
Expand All @@ -100,7 +107,7 @@ public function standard(

// values
$val = $this->session->getValue($filter_id, $input_id);
if ($rendered && !is_null($val)) {
if (!is_null($val)) {
$i = $i->withValue($val);
}
$inputs_with_session_data[$input_id] = $i;
Expand All @@ -120,8 +127,8 @@ public function standard(
$is_expanded
);

// handle apply command
$filter = $this->handleApply($filter_id, $filter);
// handle apply and toggle commands
$filter = $this->handleApplyAndToggle($filter_id, $filter);

return $filter;
}
Expand All @@ -135,11 +142,13 @@ public function standard(
public function getData(Filter\Standard $filter)
{
$result = null;
$filter_data = null;
if ($filter->isActivated()) {
$filter = $this->request->getFilterWithRequest($filter);
$result = $filter->getData();
foreach ($filter->getInputs() as $k => $i) {
$filter_data[$k] = $i->getValue();
}
}
return $result;
return $filter_data;
}

/**
Expand All @@ -150,10 +159,12 @@ public function getData(Filter\Standard $filter)
protected function writeFilterStatusToSession($filter_id, $inputs)
{
if ($this->request->getFilterCmd() == self::CMD_TOGGLE_ON) {
$this->handleRendering($filter_id, $inputs);
$this->session->writeActivated($filter_id, true);
}

if ($this->request->getFilterCmd() == self::CMD_TOGGLE_OFF) {
$this->handleRendering($filter_id, $inputs);
$this->session->writeActivated($filter_id, false);
}

Expand All @@ -162,12 +173,13 @@ protected function writeFilterStatusToSession($filter_id, $inputs)
}

if ($this->request->getFilterCmd() == self::CMD_COLLAPSE) {
$this->handleRendering($filter_id, $inputs);
$this->session->writeExpanded($filter_id, false);
}

if ($this->request->getFilterCmd() == self::CMD_APPLY) {
$this->handleRendering($filter_id, $inputs);
// always activate the filter when it is applied
$this->session->writeActivated($filter_id, true);
}
}

Expand Down Expand Up @@ -202,19 +214,35 @@ protected function handleReset(string $filter_id)


/**
* Handle apply command
* Handle apply and toggle commands
*
* @param string $filter_id
* @param Filter\Standard $filter
* @return Filter\Standard
*/
protected function handleApply(string $filter_id, Filter\Standard $filter) : Filter\Standard
protected function handleApplyAndToggle(string $filter_id, Filter\Standard $filter) : Filter\Standard
{
if ((in_array(
$this->request->getFilterCmd(),
[self::CMD_APPLY]
[self::CMD_APPLY, self::CMD_TOGGLE_ON, self::CMD_TOGGLE_OFF]
))) {
$filter = $this->request->getFilterWithRequest($filter);

// always expand the filter, when it is activated with empty input values
if ($this->request->getFilterCmd() == self::CMD_TOGGLE_ON) {
$result = $filter->getData();
$expand = true;
foreach ($result as $k => $v) {
if (!empty($v) || $v === 0 || $v === "0") {
$expand = false;
}
}
if ($expand) {
$this->session->writeExpanded($filter_id, true);
$filter = $filter->withExpanded();
}
}

foreach ($filter->getInputs() as $input_id => $i) {
$this->session->writeValue($filter_id, $input_id, $i->getValue());
}
Expand Down
39 changes: 18 additions & 21 deletions Services/UI/classes/class.ilUIFilterServiceSessionGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
*/
class ilUIFilterServiceSessionGateway
{
const TYPE_VALUE = "value"; // value of an input
const TYPE_RENDERED = "rendered"; // is input rendered or not?
const TYPE_ACTIVATED = "activated"; // is filter activated?
const TYPE_EXPANDED = "expanded"; // is filter expanded?
public const TYPE_VALUE = "value"; // value of an input
public const TYPE_RENDERED = "rendered"; // is input rendered or not?
public const TYPE_ACTIVATED = "activated"; // is filter activated?
public const TYPE_EXPANDED = "expanded"; // is filter expanded?
/**
* Constructor
Expand All @@ -32,7 +32,7 @@ public function __construct()
public function writeValue(string $filter_id, string $input_id, $value)
{
$value = serialize($value);
$_SESSION["ui"]["filter"][self::TYPE_VALUE][$filter_id][$input_id] = $value;
$_SESSION["ui"]["filter"][$filter_id][self::TYPE_VALUE][$input_id] = $value;
}

/**
Expand All @@ -44,8 +44,8 @@ public function writeValue(string $filter_id, string $input_id, $value)
*/
public function getValue(string $filter_id, string $input_id)
{
if (isset($_SESSION["ui"]["filter"][self::TYPE_VALUE][$filter_id][$input_id])) {
return unserialize($_SESSION["ui"]["filter"][self::TYPE_VALUE][$filter_id][$input_id]);
if (isset($_SESSION["ui"]["filter"][$filter_id][self::TYPE_VALUE][$input_id])) {
return unserialize($_SESSION["ui"]["filter"][$filter_id][self::TYPE_VALUE][$input_id]);
}
return null;
}
Expand All @@ -60,7 +60,7 @@ public function getValue(string $filter_id, string $input_id)
*/
public function writeRendered(string $filter_id, string $input_id, bool $value)
{
$_SESSION["ui"]["filter"][self::TYPE_RENDERED][$filter_id][$input_id] = $value;
$_SESSION["ui"]["filter"][$filter_id][self::TYPE_RENDERED][$input_id] = $value;
}

/**
Expand All @@ -73,23 +73,20 @@ public function writeRendered(string $filter_id, string $input_id, bool $value)
*/
public function isRendered(string $filter_id, string $input_id, bool $default) : bool
{
if (isset($_SESSION["ui"]["filter"][self::TYPE_RENDERED][$filter_id][$input_id])) {
return $_SESSION["ui"]["filter"][self::TYPE_RENDERED][$filter_id][$input_id];
if (isset($_SESSION["ui"]["filter"][$filter_id][self::TYPE_RENDERED][$input_id])) {
return $_SESSION["ui"]["filter"][$filter_id][self::TYPE_RENDERED][$input_id];
}
return $default;
}


/**
* Resets values and rendered status
* Resets filter to its default state
* @param string $filter_id
*/
public function reset(string $filter_id)
{
if (is_array($_SESSION["ui"]["filter"][self::TYPE_VALUE][$filter_id])) {
unset($_SESSION["ui"]["filter"][self::TYPE_VALUE][$filter_id]);
}
unset($_SESSION["ui"]["filter"][self::TYPE_RENDERED][$filter_id]);
unset($_SESSION["ui"]["filter"][$filter_id]);
}


Expand All @@ -101,7 +98,7 @@ public function reset(string $filter_id)
*/
public function writeActivated(string $filter_id, bool $value)
{
$_SESSION["ui"]["filter"][self::TYPE_ACTIVATED][$filter_id] = $value;
$_SESSION["ui"]["filter"][$filter_id][self::TYPE_ACTIVATED] = $value;
}

/**
Expand All @@ -112,7 +109,7 @@ public function writeActivated(string $filter_id, bool $value)
*/
public function writeExpanded(string $filter_id, bool $value)
{
$_SESSION["ui"]["filter"][self::TYPE_EXPANDED][$filter_id] = $value;
$_SESSION["ui"]["filter"][$filter_id][self::TYPE_EXPANDED] = $value;
}

/**
Expand All @@ -124,8 +121,8 @@ public function writeExpanded(string $filter_id, bool $value)
*/
public function isActivated(string $filter_id, bool $default) : bool
{
if (isset($_SESSION["ui"]["filter"][self::TYPE_ACTIVATED][$filter_id])) {
return (bool) $_SESSION["ui"]["filter"][self::TYPE_ACTIVATED][$filter_id];
if (isset($_SESSION["ui"]["filter"][$filter_id][self::TYPE_ACTIVATED])) {
return (bool) $_SESSION["ui"]["filter"][$filter_id][self::TYPE_ACTIVATED];
}
return $default;
}
Expand All @@ -139,8 +136,8 @@ public function isActivated(string $filter_id, bool $default) : bool
*/
public function isExpanded(string $filter_id, bool $default) : bool
{
if (isset($_SESSION["ui"]["filter"][self::TYPE_EXPANDED][$filter_id])) {
return (bool) $_SESSION["ui"]["filter"][self::TYPE_EXPANDED][$filter_id];
if (isset($_SESSION["ui"]["filter"][$filter_id][self::TYPE_EXPANDED])) {
return (bool) $_SESSION["ui"]["filter"][$filter_id][self::TYPE_EXPANDED];
}
return $default;
}
Expand Down
1 change: 1 addition & 0 deletions Services/UI/classes/class.ilUIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ilUIService
* Constructor
*
* @param ServerRequestInterface $request
* @param UIServices $ui
*/
public function __construct(ServerRequestInterface $request, UIServices $ui)
{
Expand Down
7 changes: 5 additions & 2 deletions Services/UI/classes/class.ilUIServiceDependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ class ilUIServiceDependencies
* @param ilUIFilterRequestAdapter $request
* @param ilUIFilterServiceSessionGateway|null $session
*/
public function __construct(UIServices $ui, ilUIFilterRequestAdapter $request, ilUIFilterServiceSessionGateway $session = null)
{
public function __construct(
UIServices $ui,
ilUIFilterRequestAdapter $request,
ilUIFilterServiceSessionGateway $session = null
) {
$this->ui = $ui;
$this->request_adapter = $request;
$this->session = (is_null($session))
Expand Down
9 changes: 8 additions & 1 deletion src/UI/Component/Input/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,11 @@ methods in the implementation of each Input.
Due to a different appearance of Inputs in the Filter component, Filter Inputs can not
use the standard Renderer like for Forms. Therefore, the [FilterContextRenderer](src/UI/Implementation/Component/Input/Field/FilterContextRenderer.php)
was introduced. In this renderer, you must provide an appropriate presentation of each
new Filter Input within the Filter.
new Filter Input within the Filter.

#### Step 3, Check the filter.js and adapt it if needed
Much magic happens via Javascript, so that the Filter can work properly. When you add
a new Filter Input, it is highly probable that you have to add some Javascript code
especially for your Filter Input. To have an idea about the necessary changes, please
search for "Multi Select" in the `filter.js` and check the extra code which was added
for that Input.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ abstract class Filter implements C\Input\Container\Filter\Filter, CI\Input\NameS
* @param string|Signal $collapse_action
* @param string|Signal $apply_action
* @param string|Signal $reset_action
* @param C\Input\Field\Input $inputs
* @param C\Input\Field\Input[] $inputs
* @param bool[] $is_input_rendered
* @param bool $is_activated
* @param bool $is_expanded
Expand All @@ -119,8 +119,8 @@ public function __construct(
$reset_action,
array $inputs,
array $is_input_rendered,
$is_activated,
$is_expanded
bool $is_activated,
bool $is_expanded
) {
$this->signal_generator = $signal_generator;
$this->field_factory = $field_factory;
Expand All @@ -132,9 +132,6 @@ public function __construct(
$this->reset_action = $reset_action;
//No further handling for actions needed here, will be done in constructors of the respective component

if (count($inputs) != count($is_input_rendered)) {
throw new \ArgumentCountError("Inputs and boolean values must be arrays of same size.");
}
$classes = ['\ILIAS\UI\Component\Input\Field\FilterInput'];
$this->checkArgListElements("input", $inputs, $classes);

Expand All @@ -145,10 +142,7 @@ public function __construct(
$this->checkBoolArg("is_input_rendered", $r);
}
$this->is_input_rendered = $is_input_rendered;

$this->checkBoolArg("is_activated", $is_activated);
$this->is_activated = $is_activated;
$this->checkBoolArg("is_expanded", $is_expanded);
$this->is_expanded = $is_expanded;
}

Expand Down
Loading

0 comments on commit f3a8b1a

Please sign in to comment.