====== How to filter records on a module based on menu parameter ======
Sometimes we put in one module two or more similar concepts; products we sell and products we buy, accounts we work with and accounts that are partners, quotes for clients and template quotes,... and also different categories, for example, tickets of different company departments.
We can easily filter these on the module. Using the custom filter functionality we can create filters to show the different concepts being saved in the same module, but sometimes, we want to take this a step further and simulate the existence of a real separation without having to create two or more custom modules (which is also a very good alternative).
To accomplish this logical separation of physical records living on the same table we have to add some custom programming on our module to detect the category the user is looking for and force the condition onto the SQL that retrieves the records.
First we have to create a menu entry that indicates what the user is going to see and pass in, on the URL, some indicator of what we want to filter. Then we have to save that information somewhere so it gets passed around for an event handler to access it and modify the query accordingly. It actually is that easy, just three steps, but there is one caveat that we have to avoid. Let's see how to do this with a real example.
I am going to separate the Account module records. In one category we will show all accounts that live in Australia, on the other we will show all the other accounts.
The condition we are going to add to the query is:
bill_country='Australia'
or
bill_country!='Australia'
We create two menu URL entries:
^Accounts from Australia|index.php?action=index&module=Accounts&filterAustralia=Australia|
^Accounts not from Australia|index.php?action=index&module=Accounts&filterAustralia=|
Now we register the event handler and create a script to modify the query. The event is:
[[en:devel:corebos_hooks#corebosfilterlistviewquerygeneratorbefore|corebos.filter.listview.querygenerator.before]]
and our handler script will be **modules/Accounts/FilterAustralia.php**
The code to register the event is
registerHandler($event, 'modules/Accounts/FilterAustralia.php', 'AccountFilterAustraliaHandler');
echo "Event $event registered.
";
}
registerEvent('corebos.filter.listview.querygenerator.before');
echo '