Skip to content

Instantly share code, notes, and snippets.

@xnau
Created January 26, 2025 20:12
Show Gist options
  • Save xnau/fb9cc360c58eba270c456dcdf3c6cb4c to your computer and use it in GitHub Desktop.
Save xnau/fb9cc360c58eba270c456dcdf3c6cb4c to your computer and use it in GitHub Desktop.
Provides a utility class for the Participants Database Multi-Relational plugin that can be used in a shortcode template
<?php
/**
* Plugin Name: PDb Multi-Relational Utility Class
* Description: Provides a utility class for showing a record's relations
* Version: 0.1
*/
class pdbmrdb_record {
/**
* @var int the record id
*/
private $record_id = 0;
/**
* @var array of all the record's relations
*
* this is indexed by the typename, each element will be an array as $record_id => $title
*/
private $relations = [];
/**
* builds the object
*
* @param int $record_id
*/
public function __construct( $record_id )
{
$this->assign_record_id( $record_id );
if ( $record_id )
{
$this->build_relations();
}
}
/**
* provides a list of the record's relations for the given type
*
* return empty array if no relations of the type are set
*
* @param string $typename
* @return array as $record_id => $title
*/
public function get_relations( $typename )
{
return isset( $this->relations[ $typename ] ) ? $this->relations[ $typename ] : [];
}
/**
* assigns the record id
*
* @param int $record_id
*/
private function assign_record_id( $record_id )
{
$record = Participants_Db::get_participant( $record_id );
if ( $record )
{
$this->record_id = $record_id;
}
}
/**
* build the relations array
*/
private function build_relations()
{
foreach( $this->all_types() as $typename => $title )
{
$record_relations = new \pdbmrdb\store\get_related( $typename, $this->record_id );
$relations_array = [];
foreach( $record_relations->related_record_list() as $related_record_id )
{
$relations_array[ $related_record_id ] = \pdbmrdb\store\title::get_title( $typename, $related_record_id );
}
$this->relations[ $typename ] = $relations_array;
}
}
/**
* provides a list of all defined types
*
* @return array $typename => $title
*/
private function all_types()
{
$all_type_defs = \pdbmrdb\type\all_types::def_list();
$all_types = [];
foreach( $all_type_defs as $def )
{
/** @var \pdbmrdb\type\definition $def */
if ( $def->is_relational() ) // include only relational types
{
$all_types[ $def->type_name() ] = $def->label_singular();
}
}
return $all_types;
}
}
@xnau
Copy link
Author

xnau commented Jan 26, 2025

This is meant to be installed as a plugin: How to Install a WordPress Plugin from a Gist

This class can be used in a PDB shortcode template or any custom code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment