Hello there,
I haven´t found examples how to use joomla cookie method with seblod list & search type.
I created a code to do that and it works. I will let it here to others as an example and ask more experienced seblod users
if does exist a better way to have a persistent search form.
// Verify if does exist cookie in the search form
$cookieFac = '?';
$newSearch = '';
//Loops through each field to make a search string and test if it is a new search
foreach ($fields as $field) {
$cookieFac .= $field->name."=".$field->value."&";
if ($field->value !== ''){
$newSearch = 'true';
}
}
$cookieFac = substr($cookieFac, 0, -1); //remove last character of string
if ($newSearch == 'true') {
// Get input cookie object
$app = JFactory::getApplication();
$inputCookie = $app->input->cookie;
// Set cookie data
$inputCookie->set($name = 'cookieFac', $cookieFac, $expire = 0); //Put some value in seconds to $expire to determinate how long this cookie will last
}
$app = JFactory::getApplication();
$redirectedFac = $app->getUserState('redirectedFac', NULL);
$inputCookie = $app->input->cookie;
// Get cookie data
$cookieFac = $inputCookie->getString($name = 'cookieFac', $defaultValue = null);
// Check that cookie exists
if($cookieFac and $redirectedFac == NULL){
$app->setUserState('redirectedFac',1); // This userState variable is used to avoid continuous loop
$app->redirect('/index.php'.$cookieFac);
}
$app->setUserState('redirectedFac',NULL); // Returns userState variable to original state after redirection
The code above goes in a before render field (seblod code pack). To remove the cookie I created a free button in the search form and put javascript code in the stuff javascript field:
$('#fac_remove_filter').click(function () { // use your button id here
document.cookie = 'cookieFac' + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; // javascript to remove cookie
window.location.href = "/index.php"; // url of you list and search type
});
<br>
If someone ccan suggest a more efficient way to to it please let me know.
Thanks!