You are here

function farm_plan_record_unlink_form_submit in farmOS 7

Unlink record form submit handler.

Parameters

array $form: The form array.

array $form_state: The form state array.

1 string reference to 'farm_plan_record_unlink_form_submit'
farm_plan_record_unlink_form in modules/farm/farm_plan/farm_plan.pages.inc
Generic form for removing a record from a plan, and optionally deleting it.

File

modules/farm/farm_plan/farm_plan.pages.inc, line 536
Farm plan pages.

Code

function farm_plan_record_unlink_form_submit(array $form, array &$form_state) {

  // Get the plan from the submitted values.
  $plan = $form_state['values']['plan'];

  // Get the record type and ID from the submitted values.
  $record_type = $form_state['values']['record_type'];
  $record_id = $form_state['values']['record_id'];

  // Unlink the record from the plan.
  farm_plan_unlink_record($record_type, $plan->id, $record_id);

  // Delete the record, if that was explicitly requested.
  if (!empty($form_state['values']['delete'])) {
    $entity_type = $form_state['values']['entity_type'];
    entity_delete($entity_type, $record_id);
  }

  // If log IDs were also marked for removal, unlink and optionally delete them
  // as well.
  if (!empty($form_state['values']['log_ids'])) {

    // Get information about available plan record relationships.
    $relationships = farm_plan_record_relationships();

    // Figure out which record types link to logs.
    $log_record_types = array();
    foreach ($relationships as $record_type => $info) {
      if ($info['entity_type'] == 'log') {
        $log_record_types[] = $record_type;
      }
    }

    // Iterate through the log IDs.
    foreach ($form_state['values']['log_ids'] as $log_id) {

      // Unlink the log from the plan.
      foreach ($log_record_types as $record_type) {
        farm_plan_unlink_record($record_type, $plan->id, $log_id);
      }

      // Delete the record, if that was explicitly requested.
      if (!empty($form_state['values']['delete'])) {
        entity_delete('log', $log_id);
      }
    }
  }

  // Get the path to the plan.
  $plan_uri = entity_uri('farm_plan', $plan);
  $plan_path = $plan_uri['path'];

  // Redirect to the plan.
  $form_state['redirect'] = $plan_path;
}