3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Add a function for loading a list of record IDs associated with a plan.

This commit is contained in:
Michael Stenta 2019-05-01 17:10:01 -04:00
parent 6d4b22309e
commit f4413324c7

View file

@ -624,6 +624,54 @@ function farm_plan_type_get_names() {
* Functions for linking/unlinking plans with other records.
* *************************************************************/
/**
* Load a list of records associated with a plan.
*
* @param string $record_type
* The record type (see farm_plan_record_relationships()).
* @param int $plan_id
* The plan ID.
*
* @return array
* Returns an array of record IDs associated with the plan ID.
*/
function farm_plan_linked_records($record_type, $plan_id) {
// Start an empty records array.
$records = array();
// If the plan ID is empty, bail.
if (empty($plan_id)) {
return;
}
// Get available relationships between plans and other record types.
$relationships = farm_plan_record_relationships();
// If a database table and field are not available, bail.
if (empty($relationships[$record_type]['table']) || empty($relationships[$record_type]['field'])) {
return;
}
// Get the table and field.
$table = $relationships[$record_type]['table'];
$field = $relationships[$record_type]['field'];
// Query for record IDs.
$query = db_select($table, 'r');
$query->addField('r', $field);
$query->condition('plan_id', $plan_id);
$result = $query->execute();
foreach ($result as $row) {
if (!empty($row->{$field})) {
$records[] = $row->{$field};
}
}
// Return the record IDs.
return $records;
}
/**
* Link a plan to a record.
*