Forum

Thread tagged as: Question, Addons

Need timestamp field for records in event app

Is there any way to get a field to record the time the record is saved in the event app?

Like a timestamp on the record that would update every time the record is submitted.

Ideally it would be a hidden field in the admin.

Monty Lewis

Monty Lewis 2 points

  • 3 years ago

UPDATE: oops, I think that's the time/date of the event, not when it was updated.... Sorry

It's already there... as eventDateTime... Have you done a <perch:showall /> in the template? It's also available on $Event->eventDateTime() on the Event object.

So, now I ask you "When, where, how??" do you want to use this and I will try to help you further.

RK

You could add this as an additional field... and if push comes to shove create a new fieldtype to handle this... :)

I'm thinking it would be a timestamp field type. Is there one out there?

Datetime

Hi Robert, not sure I follow...

https://docs.grabaperch.com/templates/form/input/datetime/

This is what I was thinking, but now I see it’s just for perch forms

Drew McLellan

Drew McLellan 2638 points
Perch Support

You could make a timestamp fieldtype pretty easily.

I think I would need help with that unfortunately...

How about adding a column of the type TIMESTAMP to the perch_events2 table called timestamp that has 'on update CURRENT_TIMESTAMP' set?

I've done that on my local server and I'm able to retrieve it in perch. It's working well there.

Would there be any adverse repercussions in doing it that way?

Here's my attempt at a timestamp field type. Is there a way to make the field un-editable in the admin? Or perhaps hidden altogether? I would appreciate any comments as to how to improve this.

<?php
/**
 * Timestamp Field Type.
 *
 * File:  PERCH_PATH/addons/fieldtypes/timestamp/timestamp.class.php
 * Usage: <perch:content id="timestamp" type="timestamp" suppress="true" />
 * @author Monty Lewis
 **/
class PerchFieldType_timestamp extends PerchFieldType
{ 
public function get_raw($post=false, $Item=false) { 
$id = $this->Tag->id();

if ($post===false) {
$post = $_POST;
$timestamp = new DateTime();
$timestamp = $timestamp->format('Y-m-d H:i:s');

}

$timestamp = '';

$timestamp = new DateTime();
$timestamp = $timestamp->format('Y-m-d H:i:s');


return $timestamp;
}
}
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you'd write your own render_inputs method - that's the part that gets called to display the form field.

So I can hide the field value by adding:

    public function render_inputs($details=array())
    {
        return '';
    }

But what would I do if I wanted to just add the tag disabled="disabled" to the input?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I think the slug field type is a good example of this.

Thank you for that pointer! Here's my final timestamp field type. it does not allow for editing but updates the timestamp each time the record is saved. Any comments on the code would be appreciated.

<?php
/**
 * Timestamp Field Type.
 *
 * File:  PERCH_PATH/addons/fieldtypes/timestamp/timestamp.class.php
 * Usage: <perch:content id="timestamp" type="timestamp" suppress="true" />
 * @author Monty Lewis
 **/
class PerchFieldType_timestamp extends PerchFieldType
{ 
     public function render_inputs($details = array())
     {
        $attrs = '';
        $attrs .= 'disabled="disabled" '; 
        $t = $this->Form->text($this->Tag->input_id(), $this->Form->get($details, $this->Tag->id(), $this->Tag->default(), $this->Tag->post_prefix()), $this->Tag->size(), $this->Tag->maxlength(), 'timestamp', $attrs);

        return $t;
    }

    public function get_raw($post=false, $Item=false)
    {   
        $id = $this->Tag->id();

        if ($post===false)
        {
            $post = $_POST;
            $timestamp = new DateTime();
            $timestamp = $timestamp->format('Y-m-d H:i:s');
        }

        $timestamp = '';

        $timestamp = new DateTime();
        $timestamp = $timestamp->format('Y-m-d H:i:s');

        return $timestamp;
    }
}