You are here

pay_node.module in Pay 6

Same filename and directory in other branches
  1. 7 modules/pay_node/pay_node.module

File

modules/pay_node/pay_node.module
View source
<?php

// $Id$

/**
 * Implementation of hook_menu().
 */
function pay_node_menu() {
  return array(
    'admin/settings/pay/node' => array(
      'title' => 'Node settings',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'pay_node_admin_settings',
      ),
      'access arguments' => array(
        'administer pay',
      ),
      'file' => 'pay_node.admin.inc',
      'file path' => drupal_get_path('module', 'pay_node') . '/includes',
      'type' => MENU_LOCAL_TASK,
    ),
    'node/%node/pay' => array(
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'pay_node_pay_form',
        1,
      ),
      'access callback' => 'pay_node_pay_form_access',
      'access arguments' => array(
        1,
      ),
      'type' => MENU_CALLBACK,
    ),
  );
}

/**
 * An access callback for payment forms.
 */
function pay_node_pay_form_access($node) {
  if (!($pay_form = pay_node_form_load($node))) {
    return FALSE;
  }
  if (!$pay_form
    ->pay_methods()) {
    return FALSE;
  }
  return TRUE;
}

/**
 * Implementation of hook_form_alter().
 */
function pay_node_form_alter(&$form, &$form_state, $form_id) {

  // Include the payment settings form on an individual node.
  if ($form['#id'] == 'node-form') {
    require_once dirname(__FILE__) . '/includes/pay_node.admin.inc';
    pay_node_admin_node_form_alter($form, $form_state);
  }

  // Provide general settings for any node type.
  if ($form['#id'] == 'node-type-form') {
    require_once dirname(__FILE__) . '/includes/pay_node.admin.inc';
    pay_node_admin_node_type_form_alter($form, $form_state);
  }

  // Include a nid If we're on a menu callback for node or pay_node_pay_form.
  if (isset($form_state['pay_node'])) {
    $node = $form_state['pay_node'];
    if (isset($form['#pay'])) {
      $form['pay_node_nid'] = array(
        '#type' => 'value',
        '#value' => $node->nid,
      );
      return;
    }
  }
}

/**
 * Implementation of hook_nodeapi().
 */
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);
  }
}

/**
 * Implementation of hook_views_api().
 */
function pay_node_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'pay_node') . '/includes/views',
  );
}

/**
 * Implementation of hook_content_extra_fields().
 */
function pay_node_content_extra_fields($type_name) {
  if (variable_get('pay_node_enabled_' . $type_name, FALSE)) {
    return array(
      'pay_node' => array(
        'label' => t('Payment information'),
        'description' => t('Payment form or summary information.'),
        'weight' => 25,
      ),
    );
  }
}

/**
 * Implementation of hook_pay_transaction_create().
 */
function pay_node_pay_transaction_create(&$transaction, $values = array()) {
  if (isset($values['pay_node_nid']) && ($nid = $values['pay_node_nid'])) {

    // Verify that this node actually has a pay form and is the correct one.
    if (!($pay_form = pay_node_form_load($nid))) {
      return;
    }
    if ($pay_form->pfid != $transaction->pfid) {
      return;
    }

    // Store a relationship between the transaction and the node it came from.
    $record = array(
      'nid' => $nid,
      'pxid' => $transaction->pxid,
    );
    drupal_write_record('pay_transaction_node', $record);
  }
}

/**
 * Implementation of hook_pay_transaction_delete().
 */
function pay_node_pay_transaction_delete(&$transaction) {
  db_query("DELETE FROM {pay_transaction_node}\n    WHERE pxid = %d", $transaction->pxid);
}

/**
 * A callback for node/x/pay which presents a payment form.
 */
function pay_node_pay_form(&$form_state, $node) {

  // Load the pay_form for this node, if it exists, and return a default form.
  if ($pay_form = pay_node_form_load($node)) {
    $form_state['pay_node'] = $node;
    drupal_set_title($pay_form
      ->title());
    return pay_form($form_state, $pay_form);
  }
}

/**
 * A load helper for pay_forms that are attached to a node.
 */
function pay_node_form_load($node) {

  // Allow this to work if we only get a nid.
  if (is_scalar($node)) {
    $node = node_load($node);
  }

  // Don't continue if pay_node settings aren't enabled for this node type.
  if (!variable_get('pay_node_enabled_' . $node->type, FALSE)) {
    return;
  }

  // Determine the pay_form handler for this type, and verify that it's enabled.
  if (!($handler = variable_get('pay_node_form_' . $node->type, NULL))) {
    return;
  }
  if (!array_key_exists($handler, array_filter(variable_get('pay_node_forms', array())))) {
    return;
  }

  // The linking method (create, select, etc.)
  $method = variable_get('pay_node_method_' . $node->type, '');

  // Load the pay_form that is associated with this node, if one exists.
  if ($node->nid) {
    if ($pfid = db_result(db_query("SELECT pfid FROM {pay_form_node} n\n      INNER JOIN {pay_form} f USING ( pfid )\n      WHERE f.handler = '%s'\n      AND n.nid = %d\n      AND method = '%s'", $handler, $node->nid, $method))) {
      if ($pay_form = pay_form_load($pfid)) {

        // Hard-code the payment form's URL.
        $pay_form
          ->set_menu_path('node/' . $node->nid . '/pay');
      }
    }
    elseif ($method == 'select') {
      return;
    }
  }

  // Rely on defaults.
  if (!isset($pay_form)) {

    // The node is linked to a form. Use the default value.
    if ($method = 'select') {
      if ($pfid = variable_get('pay_node_view_default_select_' . $node->type, '')) {
        $pay_form = pay_form_load($pfid);
      }
    }
    elseif ($method = 'create') {
      $pay_form = pay_form_load($handler);
    }
  }
  return $pay_form;
}

/**
 * An after_build callback for the node editing form.
 */
function pay_node_admin_node_form_after_build(&$form, &$form_state) {

  // Add our submit handler after all of the others. It's necessary to do this
  // here because the pay_form does it this way and we want to come afterwards.
  $form['#submit'][] = 'pay_node_form_submit';
  return $form;
}

/**
 * A submit callback for the node editing form.
 */
function pay_node_form_submit(&$form, &$form_state) {

  // Pay's save() function stored the new pay object in $form_state.
  // Copy it to $form_state['values'] where it can be accessed by hook_nodeapi.
  if (isset($form_state['pay'])) {
    $form_state['values']['pay_node'] = $form_state['pay'];
  }
}

Functions

Namesort descending Description
pay_node_admin_node_form_after_build An after_build callback for the node editing form.
pay_node_content_extra_fields Implementation of hook_content_extra_fields().
pay_node_form_alter Implementation of hook_form_alter().
pay_node_form_load A load helper for pay_forms that are attached to a node.
pay_node_form_submit A submit callback for the node editing form.
pay_node_menu Implementation of hook_menu().
pay_node_nodeapi Implementation of hook_nodeapi().
pay_node_pay_form A callback for node/x/pay which presents a payment form.
pay_node_pay_form_access An access callback for payment forms.
pay_node_pay_transaction_create Implementation of hook_pay_transaction_create().
pay_node_pay_transaction_delete Implementation of hook_pay_transaction_delete().
pay_node_views_api Implementation of hook_views_api().