function farm_plan_record_unlink_form in farmOS 7
Generic form for removing a record from a plan, and optionally deleting it.
Parameters
array $form: The form array.
array $form_state: The form state array.
$plan: The plan entity.
string $record_type: The record type (see farm_plan_record_relationships()).
int $record_id: The record ID.
bool $delete: If this is set to TRUE, a "Delete" checkbox will be displayed, with a default value of TRUE, which will cause the record entity to be deleted. Defaults to FALSE.
Return value
array Returns a Drupal confirmation form.
2 calls to farm_plan_record_unlink_form()
- farm_plan_record_delete_form in modules/
farm/ farm_plan/ farm_plan.pages.inc - Form for deleting a record from a plan. See farm_plan_record_unlink_form() for details.
- farm_plan_record_remove_form in modules/
farm/ farm_plan/ farm_plan.pages.inc - Form for removing a record from a plan. See farm_plan_record_unlink_form() for details.
File
- modules/
farm/ farm_plan/ farm_plan.pages.inc, line 326 - Farm plan pages.
Code
function farm_plan_record_unlink_form($form, &$form_state, $plan, $record_type, $record_id, $delete = FALSE) {
// If any of the arguments are not set, bail.
if (empty($plan) || empty($record_type) || empty($record_id)) {
return array();
}
// Save the plan, record type, and record ID in the form values.
$form['plan'] = array(
'#type' => 'value',
'#value' => $plan,
);
$form['record_type'] = array(
'#type' => 'value',
'#value' => $record_type,
);
$form['record_id'] = array(
'#type' => 'value',
'#value' => $record_id,
);
// Get information about available plan record relationships.
$relationships = farm_plan_record_relationships();
// If the record type is not defined, bail.
if (empty($relationships[$record_type])) {
return array();
}
// Get the entity type. Bail if it isn't available.
if (empty($relationships[$record_type]['entity_type'])) {
return array();
}
$entity_type = $relationships[$record_type]['entity_type'];
// Save the entity type to the form values.
$form['entity_type'] = array(
'#type' => 'value',
'#value' => $entity_type,
);
// Load the record entity.
$records = entity_load($entity_type, array(
$record_id,
));
$record = reset($records);
// If the record didn't load, bail.
if (empty($record)) {
return array();
}
// Add the record to the form values.
$form['record'] = array(
'#type' => 'value',
'#value' => $record,
);
// If the record is an asset, we'll do some special checks and give the user
// more options for cleaning things up.
if ($entity_type == 'farm_asset') {
// Load a list of logs that reference the asset.
$query = farm_log_asset_query($record_id, 0, NULL, NULL, FALSE);
$query
->addField('ss_log', 'id');
$result = $query
->execute();
$log_ids = array();
foreach ($result as $row) {
if (!empty($row->id)) {
$log_ids[] = $row->id;
}
}
// Add the log IDs to the form values.
$form['log_ids'] = array(
'#type' => 'value',
'#value' => $log_ids,
);
// Show a list of links to the logs.
$log_links = array();
foreach ($log_ids as $log_id) {
$log = log_load($log_id);
$log_label = entity_label('log', $log);
$log_uri = entity_uri('log', $log);
$log_links[] = l($log_label, $log_uri['path']);
}
if (!empty($log_links)) {
$form['log_links'] = array(
'#type' => 'markup',
'#markup' => '<p>' . t('The following logs will also be removed.') . '</p>' . theme('item_list', array(
'items' => $log_links,
)),
);
}
// We also need to check constraints, for both the asset, and all the logs
// we found above - because it's possible that other things are referencing
// them. It is also possible that the logs reference multiple assets. It can
// get complicated quickly, so we take a simple approach with this form. If
// constraints exist beyond the log-asset references we know about, we will
// prevent deletion and summarize them, so the user can make a decision and
// clean them up manually if they want.
$constraints = array();
// Check to see if any constraints exist on the asset. Filter out the logs
// we found above because we will handle those automatically in the submit
// function. Also filter out the link between the plan and the asset.
$asset_constraints = farm_constraint_list('farm_asset', $record->type, $record_id);
foreach ($asset_constraints as $constraint) {
// If the constraint type is "field_reference" and it is a log that we
// already found above, skip it.
if (!empty($constraint['constraint']) && !empty($constraint['field']) && !empty($constraint['entity_type']) && !empty($constraint['entity_id'])) {
if ($constraint['constraint'] == 'field_reference' && $constraint['field'] == 'field_farm_asset' && $constraint['entity_type'] == 'log' && in_array($constraint['entity_id'], $log_ids)) {
continue;
}
}
// If the constraint type is "table_reference" and it's the table that
// defines the relationship between the plan and asset, skip it.
if (!empty($constraint['constraint']) && !empty($constraint['table'])) {
if ($constraint['constraint'] == 'table_reference' && $constraint['table'] == $relationships[$record_type]['table']) {
continue;
}
}
// Otherwise, add the constraint to the filtered list.
$constraints[] = $constraint;
}
// Check to see if any constraints exist on the logs. Filter out any links
// between the plan and the log.
foreach ($log_ids as $log_id) {
$log = log_load($log_id);
$log_constraints = farm_constraint_list('log', $log->type, $log_id);
foreach ($log_constraints as $constraint) {
// If the constraint type is "table_reference" and the table is
// "farm_plan_log" OR the same table as the asset relationship table
// (indicating that it is a single table that references multiple
// different types of records), skip it.
if (!empty($constraint['constraint']) && !empty($constraint['table'])) {
if ($constraint['constraint'] == 'table_reference' && in_array($constraint['table'], array(
'farm_plan_log',
$relationships[$record_type]['table'],
))) {
continue;
}
}
// Otherwise, add the constraint to the filtered list.
$constraints[] = $constraint;
}
// Also check to see if the log references other assets. If so, add a
// simple boolean constraint.
if (!empty($log->field_farm_asset[LANGUAGE_NONE]) && count($log->field_farm_asset[LANGUAGE_NONE]) > 1) {
$constraints[] = TRUE;
}
}
// If constraints do exist, prevent deletion and add explain why.
if ($constraints) {
$delete = FALSE;
$form['no_delete'] = array(
'#type' => 'markup',
'#markup' => '<p>' . t('The record(s) will be unlinked from this plan, but they will not be deleted because they are referenced by other records.') . '</p>',
);
}
}
// Get the title of the plan and record.
$plan_name = entity_label('farm_plan', $plan);
$record_name = entity_label($entity_type, $record);
// Get the path to the plan.
$plan_uri = entity_uri('farm_plan', $plan);
$plan_path = $plan_uri['path'];
// Add a checkbox to delete the entity.
if (!empty($delete)) {
$form['delete'] = array(
'#type' => 'checkbox',
'#title' => t('Delete the record(s)'),
'#description' => t('If this is checked, the record(s) will be permanently deleted. Otherwise, they will just be unlinked from the plan.'),
'#default_value' => TRUE,
);
}
else {
$form['delete'] = array(
'#type' => 'value',
'#value' => FALSE,
);
}
// Explicitly add the submit handler, because this form function may be called
// by farm_plan_record_remove_form() or farm_plan_record_delete_form() above.
$form['#submit'][] = 'farm_plan_record_unlink_form_submit';
// Return a Drupal confirmation form.
return confirm_form($form, t('Are you sure you want to remove %record from the plan %plan?', array(
'%record' => $record_name,
'%plan' => $plan_name,
)), $plan_path, '', t('Remove'), t('Cancel'));
}