rsanchez / dynamo

Makes Dynamic Parameters behave more like the Search module.
http://devot-ee.com/add-ons/dynamo/
10 stars 2 forks source link

POST variables are not sent to results page #30

Closed iankhoughton closed 10 years ago

iankhoughton commented 10 years ago

Hi - I'm trying to use Dynamo to set up search functionality for accommodation packages: each package is an entry in a channel. I'm currently using it to filter categories such as the hotel the entry is linked to which is working well, but I also need to filter by date. The user submits their start and end dates for their stay, and the page needs to display active entries within that date.

I'm setting entry availability start and end dates using the entry date and expiry date (show_future_entries is on). I can't set 'start_on' and 'stop_before' as my user-submitted dates because if the entry date is before the 'start_on' date submitted, it won't display that particular entry.

I figured I could use dynamo POST values to filter the packages using conditionals to hide packages not available within the selected date range, but I can't seem to access the POST values. A regular form submission will pass POST, but after watching in the Firefox dev console, it appears Dynamo submits POST, but then redirects to the results page using GET, meaning the variables aren't persistent and I can't access them. I've tried to access the values using mo' variables, raw PHP, and EE syntax, but nothing works - which is curious as the output profiler shows POST data as being present - but the Firefox dev console insists it is not.

Any assistance would be greatly appreciated! Ideally, all I need is a way to access the POST values from the Dynamo form.

iankhoughton commented 10 years ago

I should mention that mo' variables has no issues accessing POST values submitted from a regular form, but cannot access Dynamo POST values, despite the output profiler insisting they are present (although Firefox disagrees).

rsanchez commented 10 years ago

When you submit the form, $_POST is serialized and saved to the database, and then you are redirected to your search results page with a unique hash of that saved search as your final url segment. When you hit the search results page, the serialized $_POST is looked up in the database by its hash, and then $_POST is repopulated with that data. So it's not a true POST, but simulated. This is why Mo' doesn't see them: it is bootstrapped prior to Dynamo repopulating $_POST (this is also why the profiler shows the POST values, since it occurs after Dynamo repopulates).

When Dynamo repopulates $_POST, it also adds those values as variables to the Dynamo form and results tags. So you should just be able to do {your_key_here}.

iankhoughton commented 10 years ago

Interesting! I'd already tried that in the results tag with no luck, but I just tried placing the values at the end of the form tag and the variables showed up, so they appear to be persistent within the form tag, but not the results tag. Any idea why that might be?

rsanchez commented 10 years ago

Shit, I was wrong about the variables being available in the entries tag. I think you'll either have to use a plugin (something with it's own {exp: tag, NOT Mo' Variables, for instance) that retrieves POST vars for you (there are a number of them listed here: http://devot-ee.com/search/results?keywords=get+post&addon_version_support=ee2). Or you can enable PHP in your template, parsed on output.

The trick is to get something that will parse after Dynamo has injected your vars back into $_POST.

iankhoughton commented 10 years ago

Thanks Rob. I thought I'd tried pure PHP in the entry tag, but I might have only done so on input as output is working OK. Obviously not ideal as then I can't interact directly with the entries tag, but I'll try and figure out a workaround. Just out of curiosity, would it be a big job to modify Dynamo so that POST variables are passed on to the entries tag as well as the form tag?

rsanchez commented 10 years ago

Unfortunately, yes it's not an easy thing. The reason is that internally Dynamo uses the Channel module to parse the entries tag, so we have no direct control over its parsing.

Try adding this to your mod.dynamo.php file:

    public function param()
    {
        $vars = $this->search($this->EE->TMPL->fetch_param('search_id'));

        $name = $this->EE->TMPL->fetch_param('name');

        return isset($vars[$name]) ? $vars[$name] : '';
    }

With this you should be able to do something like:

{exp:dynamo:entries ...}
{if '{exp:dynamo:param name="your_post_key"}' > entry_date}
rsanchez commented 10 years ago

I added that param method to the core. Hope that does what you need. Please re-open this issue if not.

iankhoughton commented 10 years ago

Hey Rob, thank you for your help. I've managed to get the search functionality up and running - I used a third party plugin to access the POST variables, but I'll give the core method a try too; it should streamline things somewhat. Thanks for the great plugin!

rsanchez commented 10 years ago

:thumbsup: