You are here

pay_node.module in Pay 7

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

File

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

/**
 * Implements hook_menu().
 */
function pay_node_menu() {
  return array(
    'admin/config/pay/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;
}

/**
 * Implements 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;
    }
  }
}

/**
 * Implements hook_node_load().
 */
function pay_node_node_load($nodes, $types) {

  // Add the payment acceptance form to the node.
  foreach ($nodes as $node) {
    if ($pay_form = pay_node_form_load($node)) {
      $node->pay_form = $pay_form;
    }
  }
}

/**
 * Implements hook_node_view().
 */
function pay_node_node_view($node, $view_mode = 'full') {

  // Add the payment acceptance form to the node display.
  // Determine the selected theme function for this node type.
  if ($view_mode == '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;
    }

    // TODO Please change this theme call to use an associative array for the $variables parameter.
    $node->content['pay_node'] = array(
      '#type' => 'markup',
      '#value' => theme($theme, $node->pay_form),
      '#weight' => $weight,
    );
  }
}

/**
 * Implements hook_node_insert().
 */
function pay_node_node_insert($node) {

  // Create a record that links this payment form to this node.
  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_delete('pay_form_node')
        ->condition('nid', $node->nid)
        ->condition('method', $method)
        ->execute();
      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);
        }
      }
    }
  }
}

/**
 * Implements hook_node_update().
 */
function pay_node_node_update($node) {

  // Create a record that links this payment form to this node.
  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_delete('pay_form_node')
        ->condition('nid', $node->nid)
        ->condition('method', $method)
        ->execute();
      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);
        }
      }
    }
  }
}

/**
 * Implements hook_node_delete().
 */
function pay_node_node_delete($node) {

  // Remove the pay_form <-> node relationship from the database.
  // Delete the relationship in the database.
  db_delete('pay_form_node')
    ->condition('nid', $node->nid)
    ->execute();
}

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

/**
 * Implements 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,
      ),
    );
  }
}

/**
 * Implements 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);
  }
}

/**
 * Implements hook_pay_transaction_delete().
 */
function pay_node_pay_transaction_delete(&$transaction) {
  db_delete('pay_transaction_node')
    ->condition('pxid', $transaction->pxid)
    ->execute();
}

/**
 * A callback for node/x/pay which presents a payment form.
 */
function pay_node_pay_form($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_query("SELECT pfid FROM {pay_form_node} n\n      INNER JOIN {pay_form} f USING ( pfid )\n      WHERE f.handler = :f.handler\n      AND n.nid = :n.nid\n      AND method = :method", array(
      ':f.handler' => $handler,
      ':n.nid' => $node->nid,
      ':method' => $method,
    ))
      ->fetchField()) {
      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 Implements hook_content_extra_fields().
pay_node_form_alter Implements 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 Implements hook_menu().
pay_node_node_delete Implements hook_node_delete().
pay_node_node_insert Implements hook_node_insert().
pay_node_node_load Implements hook_node_load().
pay_node_node_update Implements hook_node_update().
pay_node_node_view Implements hook_node_view().
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 Implements hook_pay_transaction_create().
pay_node_pay_transaction_delete Implements hook_pay_transaction_delete().
pay_node_views_api Implements hook_views_api().