pre-filter depending on user

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
graham
Veteran Member
Posts: 86
Joined: 2020-09-29 12:30

pre-filter depending on user

Post by graham » 2020-10-21 13:40

Here is the challenge I need to address, if anybody can help.
Scenario: Users all belong to one permissions group or another, based on their country - Thailand, UK etc. Records in a table include field 'Country' - Thailand, UK etc.
Users from Thailand can access all the table records, as can User from England. When User from User group 'Thailand' arrives, she wants the pre-filtered configured so she only sees records where country field = Thailand. (She can click View all if she wants to see the rest.) When UK guy arrives, he wants the pre-filter to only show where country = UK. That's it - not to much to ask!
But I can't figure out how to reference the current User in order to say - 'ah, she's in the Thai group, we need to filter by country="Thailand".
I have seen the partial solution at: https://bigprof.com/appgini/tips-and-tu ... ult-filter.
But after the usual time consuming non-result, this is my best effort to later it to reflect my needs. My solution doesn't work - any thoughts much appreciated. Thanks

function meetings_init(&$options, $memberInfo, &$args){
/* Apply a default filter only if no filter is already applied by the user */

if(!$_POST['FilterField'][1] && !$_GET['FilterField'][1]){
/*
In the call below, we want to display records of the meetings
table where the value of the 1st field is equal to '(the Group name of thge current user'.
*/
$group = $_REQUEST[Group'];
addFilter(1, 'and', 1, 'equal-to', '$group');
}

return TRUE;
}

User avatar
D Oliveira
AppGini Super Hero
AppGini Super Hero
Posts: 347
Joined: 2018-03-04 09:30
Location: David

Re: pre-filter depending on user

Post by D Oliveira » 2020-10-21 21:24

you can try:

Code: Select all

function meetings_init(&$options, $memberInfo, &$args){
/* Apply a default filter only if no filter is already applied by the user */

if(!$_POST['FilterField'][1] && !$_GET['FilterField'][1]){
/*
In the call below, we want to display records of the meetings
table where the value of the 1st field is equal to '(the Group name of thge current user'.
*/

$sql = "SELECT `name` FROM `membership_groups` WHERE `groupID` = '{$memberInfo['groupID']}' ";

$group_name= sqlValue($sql);

$groupID = $memberInfo['groupID'];

addFilter(1, 'and', 1, 'equal-to', '$group_name'); // or use $groupID if thats what you need
}

return TRUE;
}

graham
Veteran Member
Posts: 86
Joined: 2020-09-29 12:30

Re: pre-filter depending on user

Post by graham » 2020-10-21 22:13

D Oliveira, thank you very much. To me your suggestion reads perfectly - but I can't get it to work. I've paid attention to the 1 in addFilter(1 - it is the only filter so 1 seems correct. The field I want to match with the group name is 2 (where I originally said was 1 as in 'and', 1, 'equal to..) which is the second field in the table (I've tried 1,2,3 etc!) So I can't see why it won't work? What I get is 'No results' (when I know there are). If I change it to 'not-equal-to' I get the full unfiltered list of records which is of course also wrong but perhaps that's a pointer? Thanks for any input - it would be really helpful to crack this and I think we're close.

User avatar
D Oliveira
AppGini Super Hero
AppGini Super Hero
Posts: 347
Joined: 2018-03-04 09:30
Location: David

Re: pre-filter depending on user

Post by D Oliveira » 2020-10-28 06:28

to diagnose this you would want to enable saving filters feature to that table, then manually apply the filters and look at the generated URL to see how that looks like, from that point on you can change the code

graham
Veteran Member
Posts: 86
Joined: 2020-09-29 12:30

Re: pre-filter depending on user

Post by graham » 2020-10-28 10:09

Great, thanks for that - I'll look into it and report back. I really need to get this working - and I think it might be a common requirement.

graham
Veteran Member
Posts: 86
Joined: 2020-09-29 12:30

Re: pre-filter depending on user

Post by graham » 2020-10-29 13:01

I found the problem .. addFilter(1, 'and', 1, 'equal-to', '$group_name'); - dropped the ' ' from $group_name so now reads:
addFilter(1, 'and', 1, 'equal-to', $group_name);
The above is the answer to the problem but in case it helps anyone I'll als mention that the field I was filtering on was 2 so my actual output is addFilter(1, 'and', 2, 'equal-to', $group_name);
Thanks D Oliveira for your help.

graham
Veteran Member
Posts: 86
Joined: 2020-09-29 12:30

Re: pre-filter depending on user

Post by graham » 2020-10-30 13:09

Oh dear! Problems. On the face of it, it works. BUT I have realised it's causing two errors:

1) You can't view a second page (or 3rd ..) of records.
When you first arrive, you see just your results - as per the above solution. In my example, 6 records out of 17. Then you click to see all records - which displays page one, 1-10 of 17 assorted records. Then click to go to page 2 of the results (11 -17) and I get my original 6 pre-filtered records. So the summary is I can't view all records after page 1. (It's fine when I remove the the pre-filter code)

2) The search errors when you use it (it's fine when I remove the the pre-filter code).
When I search for 'gum' I get the following error message:
Query:
SELECT `conferences`.`ID` AS 'ID', `conferences`.`country` AS 'country', if(`conferences`.`date`,date_format(`conferences`.`date`,'%d/%m/%Y'),'') AS 'date', concat('<i class="glyphicon glyphicon-', if(`conferences`.`date_provisional`, 'check', 'unchecked'), '"></i>') AS 'date_provisional', `conferences`.`title` AS 'title', `conferences`.`location` AS 'location', concat('<i class="glyphicon glyphicon-', if(`conferences`.`location_provisional`, 'check', 'unchecked'), '"></i>') AS 'location_provisional', `conferences`.`organiser` AS 'organiser', concat('<i class="glyphicon glyphicon-', if(`conferences`.`organiser_provisional`, 'check', 'unchecked'), '"></i>') AS 'organiser_provisional', `conferences`.`topic` AS 'topic', `conferences`.`purpose` AS 'purpose', `conferences`.`est_participants` AS 'est_participants', `conferences`.`results_evaluation` AS 'results_evaluation', `conferences`.`conf_materials` AS 'conf_materials', `conferences`.`programme` AS 'programme', `conferences`.`powerpoint` AS 'powerpoint', `conferences`.`speeches` AS 'speeches', `conferences`.`summary` AS 'summary', `conferences`.`media_reports` AS 'media_reports', `conferences`.`testimonies` AS 'testimonies', `conferences`.`images` AS 'images', `conferences`.`category` AS 'category', `conferences`.`origin` AS 'origin', COALESCE(`conferences`.`ID`) AS 'conferences.ID' FROM `conferences` where (`conferences`.`ID` LIKE '%gum%' or `conferences`.`country` LIKE '%gum%' or if(`conferences`.`date`,date_format(`conferences`.`date`,'%d/%m/%Y'),'') LIKE '%gum%' or concat('<i class="glyphicon glyphicon-', if(`conferences`.`date_provisional`, 'check', 'unchecked'), '"></i>') LIKE '%gum%' or `conferences`.`title` LIKE '%gum%' or `conferences`.`location` LIKE '%gum%' or concat('<i class="glyphicon glyphicon-', if(`conferences`.`location_provisional`, 'check', 'unchecked'), '"></i>') LIKE '%gum%' or `conferences`.`organiser` LIKE '%gum%' or concat('<i class="glyphicon glyphicon-', if(`conferences`.`organiser_provisional`, 'check', 'unchecked'), '"></i>') LIKE '%gum%' or `conferences`.`topic` LIKE '%gum%' or `conferences`.`purpose` LIKE '%gum%' or `conferences`.`est_participants` LIKE '%gum%' or `conferences`.`results_evaluation` LIKE '%gum%' or `conferences`.`conf_materials` LIKE '%gum%' or `conferences`.`programme` LIKE '%gum%' or `conferences`.`powerpoint` LIKE '%gum%' or `conferences`.`speeches` LIKE '%gum%' or `conferences`.`summary` LIKE '%gum%' or `conferences`.`media_reports` LIKE '%gum%' or `conferences`.`testimonies` LIKE '%gum%' or `conferences`.`images` LIKE '%gum%' or `conferences`.`category` LIKE '%gum%' or `conferences`.`origin` LIKE '%gum%') and ( `conferences`.`country` <=> 'Admins' ) ) LIMIT 0, 10
The above info is displayed because you are currently signed in as the super admin. Other users won't see this.
error.png
error.png (69.1 KiB) Viewed 2303 times
So, any thoughts on how to address the two problems whilst retaining the pre-filter feature? (Or an alternative that achieves the original goal but without the side effects! Thanks in advance, any thoughts much appreciated as always.

graham
Veteran Member
Posts: 86
Joined: 2020-09-29 12:30

Re: pre-filter depending on user

Post by graham » 2020-11-02 12:49

Ah! I think the issue might be that the recommendation at https://bigprof.com/appgini/tips-and-tu ... ult-filter may itself be not correct. I say this because when I implement the standard solution as outlined I get the same two exact problems - can't view the second page of results (because they immediately get pre-filtered) and the general search error. I have reported this to Ahmed so hopefully he will be able to reply. But, if anybody has any thoughts it would be most helpful as I'm trying to go live with this app and it's the only thing holding me up. Thanks.

Post Reply