How Do I Detect the SPM Search Mode

Topics related to AppGini plugins/add-ons go here.
Post Reply
gmusgrave
Posts: 25
Joined: 2013-08-15 17:27
Location: Mexico

How Do I Detect the SPM Search Mode

Post by gmusgrave » 2019-06-25 17:55

I'm using the Search Page Maker plugin V1.08 with Appgini Pro 5.75.

Before I added the SPM plugin, I always included a custom header for the Advanced Search page. This header gave users instructions for using the filters. I did this using the tablename_header() function in tablename.php.

Now that I have a user-friendly search page generated by SPM (I love this plugin, BTW), I will re-write my custom header to match the simplified search page. No problem.

HOWEVER, there is a button to switch to "Advanced Search Mode" that returns us to the old, more complicated (but very versatile) filters. In this case, I'd like to change the header instructions back to my original. Then, if the user clicks on the "Simple Search Mode" button" to return to the SPM-generated filters, I'd like to change the instructions back again.

I know I can do this easily in tablename_filters.php, but any customisation there will be overwritten if I change and regenerate the filter page with SPM. I'd like to do this in a protected hooks file (ideally tablename.php).

So...

Assuming that tablename_header() gets called when switching modes (the page does reload), is there a way in tablename.php that I can detect whether the search page is in advanced mode or simple mode so that I can switch the custom header?

If tablename_header() DOES NOT get called when switching SPM modes, do you have any suggestions for how to do this?

Thank you.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: How Do I Detect the SPM Search Mode

Post by onoehring » 2019-06-30 11:39

Hi,

maybe you can use jquery to find the contents of html of
$("h1.page-header h1 button.btn.btn-lg.btn-default.pull-right.search_mode")
in your page. If the html is "Switch to advanced search mode" then you are on the easy filter page.
Use another jquery to show the information for this page - and the other way around.

Sorry, I am no jquery man and fiddled around with some code I received from pbötcher and jsetzer at some other places in my application.
Nevertheless, I believe, you could go this way.

If the button is not available at the time the page is rendered, you might use a timer function to do continuous checking (maybe check viewtopic.php?f=2&t=3102 and viewtopic.php?f=2&t=3104 ).

Olaf

gmusgrave
Posts: 25
Joined: 2013-08-15 17:27
Location: Mexico

Re: How Do I Detect the SPM Search Mode

Post by gmusgrave » 2019-06-30 16:38

Excellent idea... thank you. I just looked into this.

Yes, the filter page is rendered before the tablename_header() function is called.

BUT, $HTML is not accessible within the tablename_header() function. It is accessible from the tablename_init() function, but this is called before the filter page is rendered.

tablename_header() does have access to $contentType. In fact, this is how we have different headers for different views.

Unfortunately, $contentType is set to 'filters' regardless of the type of filter page that was just rendered. Probably because, up until SPM, there was only one possibility.

I think that what should happen is that we retain 'filters' as the $contentType for the Advanced View (i.e.: defaultFilters.php) so that old hook code isn't broken, but set it to something else ('SPM filters' perhaps?) when the SPM filter page is displayed. It would need to be set back again if the page is toggled to Advanced Filter mode, and change to follow any subsequent toggling between the two modes.

Unless someone can tell me how to access $HTML from within the tablename_header() function, this change to AppGini seems like the only solution (other than modifying code that will be overwritten after any update or regeneration of the site's code).

I'll wait and see if anyone has a bright idea as to how to access $HTML or another solution. If not, I'll suggest this change as a Feature Suggestion.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: How Do I Detect the SPM Search Mode

Post by onoehring » 2019-07-01 06:15

Hi

I meant you should be able to grab the contents of the button:
ec_54.png
ec_54.png (1.98 KiB) Viewed 13935 times
Even when the page is not rendered at start, at some point (milliseconds later) the content is set. At that time your (timer-)function will grab the contents and show the message you want.
Doesn't this work?

I have used this code to check something that does not exist when the page is rendered every 1 second. If the thing I am looking for finally exists, extra information is added once to the page.

Code: Select all

$BilderMaxAnzahlHinweis_kurz = "<script>
		\$j(function() {
        setInterval(function() {		
		var n = \$j('#panel_ecomo_ContainerBilder-ID_Container').find('td span.BilderMaxJeContainer').length;
		
if (n < 1 ) {
\$j('#panel_ecomo_ContainerBilder-ID_Container tfoot tr td').append(' " . $BilderMaxAnzahl . "')
} 
else {		
		}		
	}, 1000);
})
</script>";	
Is it does not work with the button, search for the first field label on your custom search page. If it's there, you are using the custom page, if not, you are using advanced. May be this works?

Olaf

gmusgrave
Posts: 25
Joined: 2013-08-15 17:27
Location: Mexico

Re: How Do I Detect the SPM Search Mode

Post by gmusgrave » 2019-07-01 19:28

I solved it... it wasn't actually that complicated.

When the filter page is loaded, tablename_filter.php sets $advanced_search_mode to the value of a hidden field at the time the page was loaded/reloaded. It uses the value of this variable to decide which search page to display.

That hidden field appears next. The value of this field is set to "0" for the SPM Page, and "1" for the Advanced Page.

A jQuery function is added to the page AFTER the hidden field.

When the "Change Search" button is submitted, this function gets the current value of the advanced_search_mode
hidden field, toggles it, then reloads the page. Since the value of the hidden field has changed, $advanced_search_mode will change, and so will the displayed search page. This just keeps toggling which search page is displayed as long as you keep clicking on the button.

So... in the tablename_header() function (in tablename.php), I find out what the current mode of the page is by getting the value passed to the page from the hidden field when it was loaded/reloaded. I use this to determine which set of instructions to display above the search page. Works beautifully!

For anyone else who wants to do this, here is the code I used in tablename_header():

Code: Select all

case 'filters': // Filter instructions
	// Get the current search mode
	// '0' = SPM Search Page, '1' = Advanced Search Page
	$search_mode = intval($_REQUEST['advanced_search_mode']);
				
	// Display the correct instructions based on the mode
	if( !$search_mode ) {
		$header='<%%HEADER%%>' . $SPM_filtHeadStr;
	} 
		else {
			$header='<%%HEADER%%>' . $filtHeadStr;
		}
	break;
Thanks all!

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: How Do I Detect the SPM Search Mode

Post by onoehring » 2019-07-02 05:31

Hi gmusgrave,

neat solution. Thanks for the explanation of the working behind the scenes and sharing.
Olaf

Post Reply