You are here

function farm_plan_unlink_record in farmOS 7

Unlink a plan from an area.

Parameters

string $record_type: The record type (see farm_plan_record_relationships()).

int $plan_id: The plan ID.

int $record_id: The record ID.

2 calls to farm_plan_unlink_record()
farm_plan_link_record in modules/farm/farm_plan/farm_plan.module
Link a plan to a record.
farm_plan_record_unlink_form_submit in modules/farm/farm_plan/farm_plan.pages.inc
Unlink record form submit handler.

File

modules/farm/farm_plan/farm_plan.module, line 814
Farm plan - A farm plan entity type.

Code

function farm_plan_unlink_record($record_type, $plan_id, $record_id) {

  // 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'];

  // If this record type is not required, simply update the relationship row in
  // the database to remove reference to the record.
  if ($relationships[$record_type]['required'] === FALSE) {
    $query = db_update($table);
    $query
      ->fields(array(
      $field => NULL,
    ));
    $query
      ->condition('plan_id', $plan_id);
    $query
      ->condition($field, $record_id);
    $query
      ->execute();
  }
  else {
    $query = db_delete($table);
    $query
      ->condition('plan_id', $plan_id);
    $query
      ->condition($field, $record_id);
    $query
      ->execute();
  }
}