You are here

function merci_node_type_save_validate in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.2

Same name and namespace in other branches
  1. 6.2 merci.module \merci_node_type_save_validate()
  2. 6 merci.module \merci_node_type_save_validate()

Validates saving of MERCI node types.

1 string reference to 'merci_node_type_save_validate'
merci_form_alter in ./merci.module
Implementation of hook_form_alter().

File

./merci.module, line 1077
MERCI - Managed Equipment Reservation Checkout and Inventory

Code

function merci_node_type_save_validate($form, &$form_state) {
  $values = $form_state['values'];

  // Only validate node types set to an inactive status.
  if ($values['merci_type_setting'] != 'disabled' && (int) $values['merci_active_status'] == MERCI_STATUS_INACTIVE) {

    // Determine CCK table and columns the date data is stored in.
    $field = field_info_field('field_merci_date');
    $table = key($field['storage']['details']['sql']['FIELD_LOAD_CURRENT']);
    $column_end_date = $field['storage']['details']['sql']['FIELD_LOAD_CURRENT'][$table]['value2'];
    $time = gmdate('Y-m-d H:i:s');
    $type_setting = $values['merci_type_setting'];

    // Pull all active reservations that use the node type.
    // TODO Please convert this statement to the D7 database API syntax.
    $reservations = db_query("SELECT ctn.nid, ctn.title FROM {" . $table . "} ct INNER JOIN {merci_reservation_detail} md ON ct.revision_id = md.vid INNER JOIN {node} ctn ON ct.revision_id = ctn.vid INNER JOIN {merci_{$type_setting}_node} m ON md.merci_placeholder_nid = m.nid INNER JOIN {node} mn ON m.vid = mn.vid  WHERE mn.type = :type AND m.merci_sub_type = :merci_sub_type AND {$column_end_date} >= :end AND NOT (md.merci_item_status <= :merci_item_status)", array(
      ':type' => $values['old_type'],
      ':merci_sub_type' => MERCI_SUB_TYPE_RESERVATION,
      ':end' => $time,
      ':merci_item_status' => MERCI_ITEM_STATUS_CHECKED_IN,
    ));
    $bad_reservations = array();
    foreach ($reservations as $reservation) {
      $bad_reservations[] = l($reservation->title, "node/{$reservation->nid}/edit", array(
        'query' => drupal_get_destination(),
      ));
    }
    if (!empty($bad_reservations)) {
      form_set_error('merci_active_status', t('@type_setting can not be set to an inactive status until all @type_setting items are removed from the following reservations:', array(
        '@type_setting' => $type_setting,
      )) . theme('item_list', array(
        'items' => $bad_reservations,
      )));
    }
  }
}