Jump to content

limit date time to edit field value ?


bwakad
 Share

Recommended Posts

Assuming a member has the right to fill in some information on a certain page (this should be a template because we need to check).

Knowing PW, I think it should be possible to limit the date and time to update / change this information. I have no code yet. But would like to know what the required PW addressing is?

The two fields coming in mind are modified / created for $user. Do I need to use a php date function as well? Because the date/time need to be converted? Or are there more possibilities in PW?

Link to comment
Share on other sites

I guess the approach would depend on the actual time constraint requirements. If you needed an allowed range of time (ie: between 1pm and 2pm) you could write a simple php function that pulls the current time and tests to see if you're within the specified range.

// 130000 == 1:00:00 pm, 140000 == 2:00:00 pm

function testTheTime($startTime=130000, $endTime=140000){
    $currentTime = (int) date('Gis');

    if ($currentTime > $startTime && $currentTime < $endTime ){
        return true;
    }
    else{
        return false;
    }
}

$inRange = testTheTime();
// do something with it

The $page->created property comes in the form of a Unix timestamp, so you could format it with PHP's date() function however you need. Obviously, you could throw that into the mix of the above function and do what you needed with just a function. If you want a hook you could do something like: 

// add a method called "test"
wire()->addHook('Page::test',null,'lockPage');

// function evaluates the created time of the page, and compares it to specified start and end times
function testTheTime($createdTime, $startTime, $endTime){
    $createdTime = (int) date('Gis');

    if ( ($createdTime > $startTime) && ($createdTime < $endTime) ){
        return true;
    }
    else{
        return false;
    }
}

function lockPage(HookEvent $event){
    $page = $event->object; //grab the page instance
    $createdTime = $page->created; //grab the created timestamp
    $lock = testTheTime($createdTime,130000,140000); //pass it along 
    $event->return = $lock; //returns the value
}

if($page->test()){
    throw new Wire404Exception; //if the created time is within 1pm or 2pm, throw a 404
}

I know, I know, it's a crappy example of a use case but you get the idea of where you could take it (sorry, I'm that that imaginative when at work). BTW, the above is for using within a template file, you could easily make a module and not make your template file so ugly.

  • Like 1
Link to comment
Share on other sites

Hey, thanks for explaining. I understand the code but I never used hooks because I really don't understand them.

I guess I will go for the function. And google for something similar using 24 hr format.

ps. I really don't understand the date/time field in PW... time is always zero...

Link to comment
Share on other sites

ps. I really don't understand the date/time field in PW... time is always zero...

Weird. The only way I see this happening is if you are formatting the output of the date/time field to H:i:s or H:i but not setting the input formatting to something other than 'none', which is the default for input.  Then it makes sense that PW will always format it to zero. I've found the auto-formatting of output for the date/time fieldtype to be a timesaver - less interaction with the date() function. If you need help clarifying its usage, let me know and I'll post an example or two.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...