You are here

function pay_node_nodeapi in Pay 6

Implementation of hook_nodeapi().

File

modules/pay_node/pay_node.module, line 65

Code

function pay_node_nodeapi(&$node, $op, $teaser = NULL, $a4 = NULL) {

  // Add the payment acceptance form to the node.
  if ($op == 'load') {
    if ($pay_form = pay_node_form_load($node)) {
      $node->pay_form = $pay_form;
    }
  }

  // Add the payment acceptance form to the node display.
  if ($op == 'view') {

    // Determine the selected theme function for this node type.
    if ($teaser) {
      $theme = variable_get('pay_node_display_teaser_' . $node->type, '');
    }
    else {
      $theme = variable_get('pay_node_display_' . $node->type, 'pay_form_default');
    }

    // Do not include anything for 'tab' or empty functions (hidden).
    if (in_array($theme, array(
      '',
      'tab',
    ))) {
      return;
    }

    // Add the pay_form's content to the node display.
    if (isset($node->pay_form->pfid)) {
      if (function_exists('content_extra_field_weight')) {
        $weight = content_extra_field_weight($node->type, 'pay_node');
      }
      else {
        $weight = 25;
      }
      $node->content['pay_node'] = array(
        '#type' => 'markup',
        '#value' => theme($theme, $node->pay_form),
        '#weight' => $weight,
      );
    }
  }

  // Create a record that links this payment form to this node.
  if ($op == 'insert' || $op == 'update') {
    if ($node->nid && isset($node->pay_node)) {
      $method = variable_get('pay_node_method_' . $node->type, '');
      $record = array(
        'nid' => $node->nid,
        'method' => $method,
      );

      // Link to an existing pay_form using its id.
      if (is_scalar($node->pay_node) && $node->pay_node) {
        $record['pfid'] = $node->pay_node;
        db_query("DELETE FROM {pay_form_node}\n          WHERE nid = %d AND method = '%s'", $node->nid, $method);
        drupal_write_record('pay_form_node', $record);
      }
      else {
        foreach ($node->pay_node as $pay_form) {
          if ($pay_form->new) {
            $record['pfid'] = $pay_form->pfid;
            drupal_write_record('pay_form_node', $record);
          }
        }
      }
    }
  }

  // Remove the pay_form <-> node relationship from the database.
  if ($op == 'delete') {

    // Delete the relationship in the database.
    db_query("DELETE FROM {pay_form_node} WHERE nid = %d", $node->nid);
  }
}