|
<?php |
|
|
|
/** |
|
* NavigationService (app/service/NavigationService.php) |
|
* |
|
* Stores and retrieves grid related parameters |
|
* (order, offset, limit, direction, page, first_page) |
|
* to fully retain pages' state in Adianti Framework. |
|
* |
|
* @author Renato Frota <[email protected]> |
|
*/ |
|
|
|
// Usage |
|
// |
|
// mandatory - 1. Within onReload to persist page's state: |
|
// |
|
// // before loading datagrid objects from repository |
|
// $param = NavigationService::update(); |
|
// $criteria->setProperties($param); |
|
// |
|
// mandatory - 2. When doing a search (onSearch): |
|
// |
|
// // before setting the filters |
|
// NavigationService::clear(); |
|
// |
|
// optional - 3. Create a 'clear search' form action (e.g.: onClear): |
|
// |
|
// $this->form->clear(); |
|
// NavigationService::clear(); |
|
// $this->onReload(); |
|
// |
|
// optional - 4. Create custom form actions for quick search (e.g.: onActive): |
|
// |
|
// $this->form->clear(); |
|
// $data = (object) ['active' => 'Y']; |
|
// $this->form->setData($data); |
|
// $filters = [new TFilter('active', '=', 'Y')]; |
|
// NavigationService::clear($data, $filters); |
|
// $this->onReload(); |
|
// |
|
// Tip Jars |
|
// |
|
// PIX: https://pix.ae/[email protected] |
|
// BRL: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=9JMBDY5QA8X5A |
|
// USD: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=R58RLRMM8YM6U |
|
|
|
class NavigationService { |
|
|
|
/** |
|
* Clear navigation filters saved in TSession for a class |
|
* @param $filter_data Object containing a new form data (optional) |
|
* @param $filters Array of TFilter elements equivalent to the form data (optional; required when filter data is present) |
|
* @param $class String name of class related to the filters (optional; fetched using debug_backtrace when null) |
|
*/ |
|
public static function clear(Object $filter_data = null, Array $filters = null, $class = null) |
|
{ |
|
$class = $class ?? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class']; |
|
$navigation = TSession::getValue('navigation'); |
|
unset($navigation[$class]); |
|
TSession::setValue('navigation', $navigation); |
|
TSession::setValue("{$class}_filter_data", $filter_data); |
|
TSession::setValue("{$class}_filters", $filters); |
|
} |
|
|
|
/** |
|
* Update navigation filters saved in TSession for a class |
|
* @param $input Array containing navigation related values (optional; $_REQUEST is used when null) |
|
* @param $class String name of class related to the filters (optional; fetched using debug_backtrace when null) |
|
*/ |
|
public static function update($input = null, $class = null) |
|
{ |
|
$input = $input ?? $_REQUEST; |
|
$class = $class ?? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class']; |
|
$params = ['order', 'offset', 'limit', 'direction', 'page', 'first_page']; |
|
$navigation = TSession::getValue('navigation'); |
|
foreach ($params as $param) { |
|
$value = $input[$param] ?? $navigation[$class][$param] ?? null; |
|
if (isset($value)) { |
|
$input[$param] = $navigation[$class][$param] = $value; |
|
} |
|
} |
|
TSession::setValue('navigation', $navigation); |
|
return $input; |
|
} |
|
} |