This topic is locked

Block file downloads conditionally

6/3/2023 4:47:47 AM
PHPRunner General questions
T
technoserv author

Good morning everyone.
On my site I have a view page where you can download pdf files.
In fact, when I click on the field, the download and visualization of the file starts immediately.
Every time you download a counter (I used field click event) is incremented on a specific table
where the day and the user are also recorded.
I would like that when the counter exceeds a set daily number (if limit_count>5 and date_count=now() ) it would no longer be possible to make other downloads, the field would no longer be active, blocked.
I thought about making the field readonly but it doesn't work.
Another solution I thought of was to change the field parameters from type "file" to "text" with a custom setting but I'm having trouble with the code.
Could you tell me the best approach to use, in detail if possible because I'm newbie. Thank you

D
david22585 6/3/2023

Is this a list page or view/edit page?
I assume that the tables are joined, and the query pulls the limit_count total on the page as well. If it does, create an alias of the file field as say 'file AS filelimit', Set the filelimit as custom, with text saying File download limit has been reached for the day. Put that in the same column as the file.
Then on the After Record Processed Event:
if ($data["limit_count"] > '5'){ $pageObject->hideItem("file", $recordId); } else ($data["limit_count"] <= '5'){ $pageObject->hideItem("filelimit", $recordId); }This way for each row, it will only show one field vs the other depening upon the limit.

T
technoserv author 6/4/2023

Hello david22585 and thanks for your reply.
Your approach completely hides the field and its contents.
It is a solution that I have used on other occasions but that I had not considered because I would like to see the file that you can download it if you still have the opportunity to do so.

D
david22585 6/4/2023

I'm a little confused as to what you want, as you stated "Another solution I thought of was to change the field parameters from type "file" to "text" with a custom setting but I'm having trouble with the code."
This code effectively does that. It shows the file download if there is less than 5 downloads already, but if there is more than 5, then ithides the download, and the alias field with the custom text is shown. You're duplicating the field, but showing 1 vs. the other.
Do you want the file to still be there, but you can't click the link to download it? Or just have text that the file download limit has been reached?