Last active
July 29, 2021 17:45
-
-
Save xnau/001f3435b4b193260d8b19228e031af9 to your computer and use it in GitHub Desktop.
Demonstrates how to set up a field that shows a person's age in Participants Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Participants Database Show Participant's Age | |
* Description: Sets up a field that shows a person's age | |
* Version: 2.0 | |
*/ | |
class pdb_set_age_value { | |
// put the name of your birth date field here | |
var $birth_date_field = 'birthday'; | |
// put the name of your age field here | |
// the age field MUST be a 'numeric' type field | |
var $age_field = 'age'; | |
public function __construct() | |
{ | |
/* | |
* here we set up the filter that calls our age calculation function. This is | |
* called when we are only displaying the value of the field, not in a form | |
*/ | |
add_action( 'pdb-before_field_added_to_iterator', array($this, 'set_age_value_display') ); | |
/* | |
* here we set the action handler for altering the value when the age field | |
* is shown in a form | |
*/ | |
add_action( 'pdb-form_element_build_numeric', array( $this, 'set_age_value' ) ); | |
} | |
/** | |
* sets the value of the age field in a display context | |
* | |
* @param PDb_Field_Item $field | |
* @return string HTML | |
* | |
*/ | |
public function set_age_value_display( $field ) | |
{ | |
if ( $field->name() === $this->age_field ) { | |
// get the participnat's record data | |
$record = $this->record_data( $field->record_id ); | |
// if they have the birthday value set, calc the age | |
if ( isset( $record[ $this->birth_date_field ] ) && ! empty( $record[ $this->birth_date_field ] ) ) { | |
// calculate the age | |
$age = $this->get_age( $record[ $this->birth_date_field ] ); | |
// set the field's value to the participant's age | |
$field->set_value( $age ); | |
} | |
} | |
} | |
/** | |
* sets the age field value in a form | |
* | |
* @param PDb_Form_Element $element | |
*/ | |
public function set_age_value( $element ) | |
{ | |
if ( $element->name === $this->age_field ) { | |
// set the record ID property | |
$element->record_id = $this->get_id(''); | |
// get the participnat's record data | |
$record = $this->record_data( $element->record_id ); | |
// now set the age value | |
if ( is_numeric( $record[ $this->birth_date_field ] ) ) { | |
$element->value = $this->get_age( $record[ $this->birth_date_field ] ); | |
} | |
} | |
} | |
/** | |
* calculates an age in years given the birth date | |
* | |
* @see http://php.net/manual/en/class.datetime.php | |
* | |
* @param int $birthdate unix timestamp | |
* @return string | |
*/ | |
private function get_age( $birthdate ) | |
{ | |
// our birth date | |
$dob = new DateTime("@$birthdate"); | |
$today = new DateTime(); | |
// this finds the difference between the two dates | |
$diff = $today->diff($dob); | |
return $diff->y; // return the year value for the difference | |
} | |
/** | |
* provides the record data | |
* | |
* @param int $id the record ID | |
* @return array of record values | |
*/ | |
private function record_data( $id ) | |
{ | |
return Participants_Db::get_participant( $this->get_id( $id ) ); | |
} | |
/** | |
* supplies the current participant ID | |
* | |
* there are several possibilities (depending on the context) for the location | |
* of this information, we need to check each one | |
* | |
* @param int $id the id (if known) | |
* @return int the ID | |
*/ | |
private function get_id ( $id ) | |
{ | |
if ( empty( $id ) ) { | |
// this is for backward compatibility | |
$id = filter_input( INPUT_GET, Participants_Db::$single_query, FILTER_SANITIZE_NUMBER_INT ); | |
} | |
if ( empty( $id ) ) { | |
$id = Participants_Db::get_participant_id( filter_input( INPUT_GET, Participants_Db::$record_query, FILTER_SANITIZE_STRING ) ); | |
} | |
if ( empty( $id ) && is_admin() ) { | |
$id = filter_input( INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT ); | |
} | |
if ( empty( $id ) ) { | |
$id = Participants_Db::$session->get( 'pdbid' ); | |
} | |
return $id; | |
} | |
} | |
new pdb_set_age_value; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment