You are here

function farm_plan_consideration_save in farmOS 7

Save a consideration to the database.

Parameters

object $consideration: The consideration object.

1 call to farm_plan_consideration_save()
farm_plan_consideration_form_submit in modules/farm/farm_plan/farm_plan_consideration/farm_plan_consideration.module
Consideration form submit function.

File

modules/farm/farm_plan/farm_plan_consideration/farm_plan_consideration.module, line 129
Farm plan consideration module.

Code

function farm_plan_consideration_save(&$consideration) {

  // Build the primary keys parameter for drupal_write_record() depending on
  // whether or not the consideration has an ID (insert vs update).
  $keys = array();
  if (!empty($consideration->id)) {
    $keys[] = 'id';
  }

  // Save the consideration to the database.
  drupal_write_record('farm_plan_consideration', $consideration, $keys);

  // Save all entity links to the consideration.
  if (!empty($consideration->id)) {

    // First delete all existing links.
    db_query('DELETE FROM {farm_plan_consideration_entity} WHERE consideration_id = :id', array(
      ':id' => $consideration->id,
    ));

    // If there are links to save, iterate through them.
    if (!empty($consideration->entities)) {
      foreach ($consideration->entities as $entity_type => $entity_ids) {
        if (!empty($entity_ids)) {
          foreach ($entity_ids as $entity_id) {

            // Save the entity+consideration link.
            $record = array(
              'consideration_id' => $consideration->id,
              'entity_type' => $entity_type,
              'entity_id' => $entity_id,
            );
            drupal_write_record('farm_plan_consideration_entity', $record);
          }
        }
      }
    }
  }
}