Prevent editing based upon a future date

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Prevent editing based upon a future date

Post by shasta59 » 2013-01-29 02:50

Situation: You wish to prevent records from being changed 10 days before an event. (In other words last chance to change a registration form etc)

This is one way. It is not the best way but works well and is understandable. The code can be a lot tighter but then is harder to understand.

function tablename_before_update(&$data, $memberInfo, &$args){

$eventdate=(strtotime("7 Feb 2013"));
$currentdatenow=(strtotime("now"));

if(($eventdate - $currentdatenow) < 864000) return FALSE;

return TRUE;
}


$eventdate - this is where you put in the date of the event
$currentdatenow - this calculates the current date

if(($eventdate - $currentdatenow) < 864000) return FALSE; - this subtracts the event date from the current date and if it is less than a certain # of seconds it returns false and a record cannot be edited.

864000 - # of seconds. There are 86400 seconds in a day so this is a ten day time period.

Summary:

This statement checks to see if there are less than 10 days before the event. If so it will not allow editing to happen for any record in this table.

Next will add what to add to this to allow a specific group to edit the records even if editing is not allowed anymore. Reason for this is you may have designed a conference registration form but the registrar still needs to update records.

You could change the one line to: (more compact and a bit faster - depends on your comfort level coding)

if(($lastchangedate - strtotime("now")) < 864000) return FALSE;

Enjoy

Alan
Calgary, Alberta, Canada - Using Appgini 5.50 -

User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Re: Prevent editing based upon a future date

Post by shasta59 » 2013-01-29 04:03

Okay next addition to this. And this is just the adding in the example as found on the AppGini website.

This example will let you prevent editing based upon how old a record is and how close to a future date the record is.

(I do hope these examples are of some use to someone - based upon other examples and other comments I am not posting examples which are too complex.)

Here is the full function to do the above.

function yourtablename_before_update(&$data, $memberInfo, &$args){


// get the creation date of the record
$creationDate=sqlValue("select dateAdded from membership_userrecords
where tableName=yourtablename' and pkValue='{$data['selectedID']}'");
$lastchangedate=(strtotime("25 Feb 2013"));


// if the record is older than 180 days, deny changes
if($creationDate < strtotime('180 days ago')) return FALSE;
if(($lastchangedate - strtotime("now")) < 864000) return FALSE;


return TRUE;
}


Any questions just ask but all I did was add in Ahmad example on the AppGini site. This is going to be my last post for a while to let others join in and add their ideas etc. I will give someone else a chance to contribute tips and techniques before I post again. I still have over 20 more simple changes I have made to the base code and hooks to increase functionality. I am sure there are a lot of other great ideas and code which worked to give greater flexibility to the appgini code.

Alan
Calgary, Alberta, Canada - Using Appgini 5.50 -

KSan
AppGini Super Hero
AppGini Super Hero
Posts: 252
Joined: 2013-01-08 20:17

Re: Prevent editing based upon a future date

Post by KSan » 2013-01-29 07:45

shasta59 wrote:This is going to be my last post for a while to let others join in and add their ideas etc. I will give someone else a chance to contribute tips and techniques before I post again.
Thanks a lot for all your contribution!!! There is a lot to absorb here. Most appreciated.

Johnk
AppGini Super Hero
AppGini Super Hero
Posts: 68
Joined: 2013-01-09 03:47
Location: Cairns, Australia

Re: Prevent editing based upon a future date

Post by Johnk » 2013-01-29 13:37

You can always post raw beginners tips and no one in their right mind will complain if you make 100 posts a day.If you see my request for Hook samples, it's the little things that have most of us bluffed. I play with PHP, but hooks make no sense to me at all. If you want to hold our hands, you can only make the forum more useful and that's what makes forum's grow. People are looking for solutions not found or burried in the manual. Please forgive me for repeating myself, but I have asked these things before.

1. getting a Hook to extract the contents of a field and store it as a variable. Everything I do leaves the variable blank.
2. Getting another Hook to use that variable and add it to another field.
3. Getting the Hook to display the variable somewhere else on the page.

Here's an example. A table called Settings has a Yearend field (typically 30th June).
Another table has records which will expire at the end of the year.
When a form in the second table is accessed, it compares today's date with the yearend date.
A status field close to the top of that table will show Active or Expired accordingly. It's a visual warning.

I can show how to compare dates, but I can't get any of the Hook variables to hold data. Simple as that!

Your contributions are appreciated.

User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Re: Prevent editing based upon a future date

Post by shasta59 » 2013-01-29 14:33

John

Hooks, in my interpretation are ways Ahmad has put in the program to perform certain functions. They are actually just functions called by another function. He has condensed it all into calling it hooks. I have added my own hooks also so to speak above what he has provided. The use of hooks allows one place to put code without changing the base code. (I do this anyway but....). I think of them as subroutines called by another subroutine.

Most of those are not all that difficult to do. (Well that is relative - depends upon your skill level - I can write code and umpire baseball but am not even close to a decent ball player)

Here are my suggestions to your points.

1. The way I do things to store a variable forever is to use sessions. Have you tried that approach? You can also look at my posting re auto log outs. I use sessions. Here is the link:

http://forums.appgini.com/phpbb/viewtopic.php?f=4&t=40

2. Again you will need to set a session variable. Go to this link for the low down - but it is the technical reference

http://php.net/manual/en/reserved.variables.session.php

It states, down the page a bit the following:
Note:

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.


3. This could be done using a hook but I would do it by changing some of the other code lines instead to place/show the variable after a button is clicked etc. You could have a hook change the visibility of an item and do a couple of other things. I see hooks as more behind the scenes stuff rather than in your face stuff. I do all my 'in your face stuff' elsewhere rather than in hooks. Ahmad may have a way to do this in hooks as he knows his code better than anyone.

Do not forget Ahmad offers a paid service to write code to perform a set function. No idea what he charges for that service.

AppGini in my opinion, is a very good program for what it does. It allows you to have, and no offense to Ahmad, a cheap website program/database up and running in no time. That said, if you wish more features it then moves it out of that realm. I love it for what it does. Rapid prototyping but that is what I use it for then modify it. I then use other tools to create other links, features etc. It has also allowed me to do things for local charities for free and quickly.
Calgary, Alberta, Canada - Using Appgini 5.50 -

pgkounelas
Posts: 10
Joined: 2013-01-30 14:55
Location: Larissa, Hellas

Re: Prevent editing based upon a future date

Post by pgkounelas » 2013-01-30 15:22

Hello to all forum users.
Τhe shasta59 gave us a practical and useful piece of code.
Does someone could combine this piece code to the fields in the dv form, so the records who have a creation date earlier from other date make the fields of the record for reading only?

User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Re: Prevent editing based upon a future date

Post by shasta59 » 2013-02-01 15:52

The earlier records are read only if you implement all aspects on my code on this topic.

Here are links to my posts:
Future date editing - http://forums.appgini.com/phpbb/viewtopic.php?f=7&t=260
In the same above post I cover two areas - future editing and past editing.

http://forums.appgini.com/phpbb/viewtopic.php?f=7&t=318
and this one covers allow certain groups to edit.


Here is what my (and Ahmad's) code does.

1. Allows for reading all records but those older than a certain date cannot be changed only saved as a new record (this is from the AppGini site)
2. My addition was to set a date up so you could prevent changes a certain # of days before an event occurs - useful in an event registration system.
3. Allows for groups members of certain groups to be allowed to edit all records - again useful in a event registration system.

If you wish the records read only then yes it could be coded to not allow showing the Save As Copy button. However I cannot see how this may help since with the first code changes you prevent editing of an existing record which is older than a certain # of days. I recommend that you always have a date record created, date record modified and modified by whom field(s) to see who is making changes. Couple this with a backup system (I program in a once an hour backup of the database on the server) and it should be good to go. If someone makes a change you can go to the backups of the database and get the older data back if needed.

But I could be missing the point as to what you are looking for. Let me know and, if I have time, I will post code and where to put it to turn on/off the save as copy button as needed.

Alan
Calgary, Alberta, Canada - Using Appgini 5.50 -

pgkounelas
Posts: 10
Joined: 2013-01-30 14:55
Location: Larissa, Hellas

Re: Prevent editing based upon a future date

Post by pgkounelas » 2013-02-01 21:47

Thanks shasta59 for the quick reply.
I found the solution in the code of file name_dml.php(Ahmad's code)

Code: Select all

// process form title
	$templateCode=str_replace('<%%DETAIL_VIEW_TITLE%%>', 'DETAIL VIEW TITLE', $templateCode);
	// unique random identifier
	$rnd1=($dvprint ? rand(1000000, 9999999) : '');
//my code
	$templateCode=str_replace('<%%RND1%%>', $rnd1, $templateCode);
//If the date specified by the administrator is less than the date of the records, then the dv form not allow changes and make form fields read-only
	$ownerDate=sqlValue("select `dateTimologioy` from `exoda` where `idExodon`='$selected_id'", $eo);
	$ownerEgkrisi=sqlValue("select `egkrisi` from `exoda` where `idExodon`='$selected_id'", $eo);
	$eReadOnlyDate=sqlValue("select `eDate` from `filterDate` where `idDate`=1", $eo);
	
	if(strtotime($ownerDate) < strtotime($eReadOnlyDate) && $ownerEgkrisi == 'yes'){
				$jsReadOnly.="\n\n\tif(document.getElementsByName('idExodon').length){ document.getElementsByName('idExodon')[0].readOnly=true; }\n";
				$jsReadOnly.="\n\n\tif(document.getElementsByName('egkrisi').length){ var e_egkrisi=document.getElementsByName('egkrisi')[0]; e_egkrisi.disabled=true; e_egkrisi.style.backgroundColor='white'; e_egkrisi.style.color='black'; }\n";
				$jsReadOnly.="\n\n\tif(document.getElementsByName('onamasiaKoinotitasEx').length){ var e_onamasiaKoinotitasEx=document.getElementsByName('onamasiaKoinotitasEx')[0]; e_onamasiaKoinotitasEx.disabled=true; e_onamasiaKoinotitasEx.style.backgroundColor='white'; e_onamasiaKoinotitasEx.style.color='black'; }\n";
$noUploads=true;
			}
//end of my code
// process buttons
some code(Ahmad's code)
// set records to read only if user can't insert new records
if(!$arrPerm[1]){
				$jsReadOnly.="\n\n\tif(document.getElementsByName('idExodon').length){ document.getElementsByName('idExodon')[0].readOnly=true; }\n";
				$jsReadOnly.="\n\n\tif(document.getElementsByName('egkrisi').length){ var e_egkrisi=document.getElementsByName('egkrisi')[0]; e_egkrisi.disabled=true; e_egkrisi.style.backgroundColor='white'; e_egkrisi.style.color='black'; }\n";
				$jsReadOnly.="\n\n\tif(document.getElementsByName('onamasiaKoinotitasEx').length){ var e_onamasiaKoinotitasEx=document.getElementsByName('onamasiaKoinotitasEx')[0]; e_onamasiaKoinotitasEx.disabled=true; e_onamasiaKoinotitasEx.style.backgroundColor='white'; e_onamasiaKoinotitasEx.style.color='black'; }\n";
				$noUploads=true;
			}
//The rest of the code
I hope this will be useful for someone.
Ahmad has done a great job.

User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Re: Prevent editing based upon a future date

Post by shasta59 » 2013-02-01 22:30

Looks good. Will take a more intense look at later. As a suggestion for those not as experienced in coding it may be an idea to do a quick explanation of what each line of code does. (This comes from a couple of requests I got to do similar things on another forum with which I am involved) It is time consuming to do this however and straight code works for me. May be idea for other members of this board to state if they would like posters to do this sort of explaining.

Since my last post on this board here is what I have been working on.

A table which will contain fields where the date can be set. So the programmer is not needed to code a date in the hook file but anyone with the right privileges can put in a date for any table and limit changes that way. It will also incorporate my future date concept.

I am working on additions to allow me to walk away from the code but the admin or some one else for the site can easily do the date setting.

The table could be called, change_dates or something similar with the following fields:

A drop down list with all the table names
A field for future date - a date where editing will not be allowed after.
A field for how many days prior to the above date editing is not allowed
A field for past date (say 180 days for example - no editing records older than this date)
A field to put in which group(s) can still edit records

Then I can have a simple hook file look up in this table for the permissions. This way I only write the hook and the person in charge of the site takes care of such details.

This way this is one less task I have to worry about.

All of this leans then to a turnkey system for the most part. I already use tables in this fashion. Basically it is like a preferences section of the application. I have built one that allows changing theme colours or rather setting the theme colours for the app by setting a value in the table which then loads some css calls. This is in addition to the one I made which changes themes based upon a date as put into a table. Ah, Feb 1, Valentine's day theme will come up. Heavy in red etc.

Alan
Calgary, Alberta, Canada - Using Appgini 5.50 -

User avatar
ronwill
Veteran Member
Posts: 228
Joined: 2015-08-08 10:12
Location: Cheltenham UK +Weatherford USA

Re: Prevent editing based upon a future date

Post by ronwill » 2016-06-24 13:03

Hi Alan,

As new user trying to write first App your posts are proving very helpful - thanks for that. I need to prevent updating of a submission/record form from future changes/amendments by the user (unless done via Admin) so will try it as suggested in old post above.
Before trying it just thought it wise to check if you progressed with above on hook for site admins to handle the tasks?

Cheers, Thanks again for giving users like myself very helpful and appreciated guidance & insights
Ron
- AppGini Pro V5.50 Rev 835
Ron - Gloucestershire, UK: AppGini Pro V 23.15 Rev 1484 - LOVING IT!
Plugins: Mass Update + Search Page Maker + Summary Reports + Calendar + Messages
Bizzworxx: AppGiniHelper + Inline Detail View
Alejandro Landini: To-Do List + MPI + TV Field Editor
Other: Udemy Course

Post Reply