5 years ago
3
Topic

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!

Get a VIP membership
4229 Posts
Kadministrator
5 years ago
2
Level 1

Actually in the recent Seblod versions this is already built in, you just need to activate Persistant search option in  Search tab  -> Configuration

5 years ago
1
Level 2

Hello Klas,

Thank you for your answer.

I didn´t know the persistent search setting.

I tried It and it persists only during the user session. (please coorrect me if I am wrong).

Does exist a way to set it to persist during a pre-determined period of time?

Thanks

4229 Posts
Kadministrator
5 years ago
0
Level 3

Hi, no,  it is bound to the user session. If you need other timings than go for your option.

Get a Book for SEBLOD