Gossamer Forum
Home : Products : DBMan : Customization :

Conditional Display of Add Record

Quote Reply
Conditional Display of Add Record
Having spent most of my Sunday trying to figure out just HOW NOT to display the Link to Add a Record, I can now thank Carol (again) for leading the way. The database I am building allows only one record per user; having the ADD a RECORD option on a User's screen is confusing for users who have already added to the database. What I wanted was this:
- Get the userid (my key field) for the current user
- determine if the Userid exists in the database
If it does, a record exists for that user, therefore, do not display the ADD A RECORD option.
If it does not, there is no record for that user, therefore, display the ADD A RECORD option.

Below is my clunky code. That part of it that isn't clunky belongs to Carol.
If anyone happens to have nothing better to do and notices something odd, please let me know. Yes I have tested this and it appears to work...but stuff like this always seems to bite me in the behind.

Thank you!


sub html_permissions_menu {
# --------------------------------------------------------
# Controls Display of Add, View, Modify, Delete
# Checks if Record already exists for user and conditionally prints Add
# If *Record does not exist OR User is Admin* AND user has permission to ADD
# print ADD.
# If Record does exist, ADD is not displayed.


%rec = &get_record($db_userid); # Get the record
# Use of unless
# If the database key - in this case the Userid - already exists, OR user is an Admin
# AND User has Add permissions, display Link to ADD Record

unless (($rec{$db_key} or ($per_admin)) and ($per_add)) {
print qq! | <A HREF="$db_script_link_url&add_form=1">Add My Resume</A> | !;
return;
}

# If not Admin AND user has permission to modify own records
# Print Update if User has Modify Permission and Print Delete if User has Delete Permission

if (!$per_admin && $auth_modify_own) {
print qq!| <A HREF="$db_script_link_url&modify_form=1&$db_key=*">Update My Resume</A>|<BR>! if ($per_mod);
print qq!| <A HREF="$db_script_link_url&delete_form=1&$db_key=*">Delete My Resume</A>|<BR>! if ($per_del);
}

# Must be an Admin so allow Delete if User has Delete Permissions
# allow Modify if User has Modify Permission
else {
print qq!| <A HREF="$db_script_link_url&delete_search=1">Delete</A> ! if ($per_del);
print qq!| <A HREF="$db_script_link_url&modify_search=1">Modify</A> ! if ($per_mod);
}
# If not Admin AND user has View Permission
# Print View
if (!$per_admin && $auth_view_own) {
print qq!| <A HREF="$db_script_link_url&view_records=1&$db_key=*">View My Resume</A> ! if ($per_view);
}

else {
print qq!| <A HREF="$db_script_link_url&view_search=1">Search Resumes</A> ! if ($per_view);
print qq!| <A HREF="$db_script_link_url&view_records=1&$db_key=*">List All Resumes</A>|<BR> ! if ($per_view);
}
}





Valerie
http://www.ad-ink.com
Quote Reply
Re: Conditional Display of Add Record In reply to
Looks good to me. Smile

The only way that you could make it simpler would be if you changed the add permission once a user added a record. The code for that is just a clunky, though, even though it would be in a different place.



JPD
Quote Reply
Re: Conditional Display of Add Record In reply to
After I posted the previous response, I got to thinking about removing add permissions after a user adds a record. If you'd be willing to give it a try, here's the code you would need. You would add it to sub add_record, just after

&auth_logging("added record: $in{$db_key}") if ($auth_logging);

Code:

unless ($per_admin) {
open (PASS, "<$auth_pw_file") or
&cgierr ("error in add_record. unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) { flock(PASS, 1); }
@lines = <PASS>;
close PASS;

open (PASS, ">$auth_pw_file") or &cgierr ("error in add_record. unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
foreach $line (@lines) {
if ($line =~ /^$db_userid:/) {
chomp $line;
@pass_data = split ":", $line;
$pass_data[3] = 0;
$output_line = join ":", @pass_data;
print PASS "$output_line\n";
}
else { print PASS $line; }
}
close PASS;
}
If you wanted to give them add permissions back again after they deleted their file, you would add the following to sub delete_records, just after

&auth_logging("deleted records: $succstr") if ($auth_logging);

Code:

unless ($per_admin) {
open (PASS, "<$auth_pw_file") or &cgierr ("error in delete_records. unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) { flock(PASS, 1); }
@lines = <PASS>;
close PASS;

open (PASS, ">$auth_pw_file") or &cgierr ("error in delete_records. unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
foreach $line (@lines) {
if ($line =~ /^$db_userid:/) {
chomp $line;
@pass_data = split ":", $line;
$pass_data[3] = 1;
$output_line = join ":", @pass_data;
print PASS "$output_line\n";
}
else { print PASS $line; }
}
close PASS;
}
Doing this would mean that you wouldn't have to do a search every time you wanted to print out the footer. You could pretty much go back to the default sub html_footer.

(BTW, if you don't want someone with admin permission to add, the easiest way is to remove the permission from the .pass file for that person. Smile)

If you use this, let me know what happens and I'll add it to my "One User One Record" mod. It seems like it should work fine, but one never knows. Wink

JPD
Quote Reply
Re: Conditional Display of Add Record In reply to
Carol, thanks - removing the Add permission is a more elegant way of tackling the display Add problem and, like you said, doesn't get the record every time. First to solve the (old) newest records problem (which I believe you offered a solution for a few months back) and then I'll get going on this mod.

Thank You!
Val

Valerie
http://www.ad-ink.com
Quote Reply
Re: Conditional Display of Add Record In reply to
The logic in the UNLESS section of this code was flawed...it appears to be fixed now!

CHANGE THIS CODE
unless (($rec{$db_key} or ($per_admin)) and ($per_add))

TO THIS CODE
unless ($rec{$db_key} or (!$per_add))

Hopefully I explained myself (to myself!) in the comments section of this workaround. And when the whole glorious project is functioning, I'll do it the right way (see Carol's MOD).




sub html_permissions_menu {
# --------------------------------------------------------
# Controls Display of Add, View, Modify, Delete
# Checks if Record already exists for User and conditionally prints Add
# If *Record does not exist AND User has Add permission
# print ADD.
# If Record does exist or User does not have Add permission, ADD is not displayed.


%rec = &get_record($db_userid); # Get the record
# Use of unless
# If the database key - in this case the Userid - already exists Don't print Add
# Unless the Record already exists or User does not have Add permisssion
# Print the Add Link (if record does not exist and user has Add permission, print Add)
unless ($rec{$db_key} or (!$per_add)) {
print qq! | <A HREF="$db_script_link_url&add_form=1">Add My Resume</A> | !;
return;
}

# If not Admin AND user has permission to modify own records
# Print Update if User has Modify Permission and
# Print Delete if User has Delete Permission

if (!$per_admin && $auth_modify_own) {
print qq!| <A HREF="$db_script_link_url&modify_form=1&$db_key=*">Update My Resume</A>|<BR>! if ($per_mod);
print qq!| <A HREF="$db_script_link_url&delete_form=1&$db_key=*">Delete My Resume</A>|<BR>! if ($per_del);
}

# Must be an Admin so allow Delete if User has Delete Permissions
# allow Modify if User has Modify Permission
else {
print qq!| <A HREF="$db_script_link_url&delete_search=1">Delete</A> ! if ($per_del);
print qq!| <A HREF="$db_script_link_url&modify_search=1">Modify</A> ! if ($per_mod);
}

# If not Admin AND user has View Permission
# Print View
if (!$per_admin && $auth_view_own) {
print qq!| <A HREF="$db_script_link_url&view_records=1&$db_key=*">View My Resume</A> ! if ($per_view);
}

else {
print qq!| <A HREF="$db_script_link_url&view_search=1">Search Resumes</A> ! if ($per_view);
print qq!| <A HREF="$db_script_link_url&view_records=1&$db_key=*">List All Resumes</A>|<BR> ! if ($per_view);
}

}




Valerie
http://www.ad-ink.com
Quote Reply
Re: Conditional Display of Add Record In reply to
Excellent! I always get confused when using "unless" with "and" or "or." I have to try it with every permutation before I get it right. Smile



JPD
Quote Reply
Re: [JPDeni] Conditional Display of Add Record In reply to
Hi,

It looks like that "One User One Record" finally worked out well.

What I am trying to accomplished is exactly the same thing hope this would be added soon as a new mod called "One User One Record".

Many thanks.
Quote Reply
Re: [Rashid12] Conditional Display of Add Record In reply to
Did you find this?

http://www.jpdeni.com/dbman/Mods/limitrecords.html

I did not look at it in detail but should be the "one"