Skip to content

Instantly share code, notes, and snippets.

@walterdavis
Created January 21, 2016 20:45
Show Gist options
  • Save walterdavis/39231d8bd543a8a60cdc to your computer and use it in GitHub Desktop.
Save walterdavis/39231d8bd543a8a60cdc to your computer and use it in GitHub Desktop.
<?php
/**
* A field type for recording the date a form was submitted
*
* @package default
* @author Walter Davis
*/
class PerchFieldType_updated_at extends PerchAPI_FieldType
{
/**
* Output the form fields for the edit page
*
* @param array $details
* @return void
* @author Rachel Andrew
*/
public function render_inputs($details=array())
{
// The input_id is the ID to use for the field. It accounts for multiple items appearing in one edit form.
$id = $this->Tag->input_id();
// Set the current value of the field to nothing
$val = strftime('%Y-%m-%d %h:%i:%s', time());
// $details contains the previous data stored for this field. When editing, the field needs to be repopulated with the previous values.
if (isset($details[$id]) && $details[$id]!='') {
$json = $details[$id];
$val = $json['updated_at'];
}
// return a HTML string containing the form fields to display
return $this->Form->hidden($id, $val);;
}
/**
* Read in the form input, prepare data for storage in the database.
*
* @param string $post
* @param object $Item
* @return void
* @author Rachel Andrew
*/
public function get_raw($post=false, $Item=false)
{
// Pepare an array of the values we're going to store.
$store = array();
// Use $this->Tag->id() to get the ID at this point
$id = $this->Tag->id();
// $post should normally be set, but if it's not, try $_POST
if ($post===false) {
$post = $_POST;
}
// Find the data we need from the post
if (isset($post[$id])) {
$this->raw_item = trim($post[$id]);
// Store the data as we want it
$store['updated_at'] = $this->raw_item;
// Also, set the special value '_default' which is used as the value when we don't know the fieldtype (e.g. in a <perch:if /> comparison.)
$store['_default'] = $this->raw_item;
}
// Return the data to store. Can be an array or just a string in the simplest case.
return $store;
}
/**
* Take the raw data input and return process values for templating
*
* @param string $raw
* @return void
* @author Rachel Andrew
*/
public function get_processed($raw=false)
{
// Check we've got the array we're expecting
if (is_array($raw) && isset($raw['updated_at'])) {
// Look at the 'output' attribute and see what we should return
switch($this->Tag->output()) {
// return the value by default
default:
return $raw['updated_at'];
break;
}
}
// else just return what we were given
return $raw;
}
/**
* Get the value to be used for searching
*
* @param string $raw
* @return void
* @author Rachel Andrew
*/
public function get_search_text($raw=false)
{
if ($raw===false) $raw = $this->get_raw();
if (!PerchUtil::count($raw)) return false;
if (isset($raw['updated_at'])) return $raw['updated_at'];
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment