View source
<?php
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,
),
);
}
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;
}
function pay_node_form_alter(&$form, &$form_state, $form_id) {
if ($form['#id'] == 'node-form') {
require_once dirname(__FILE__) . '/includes/pay_node.admin.inc';
pay_node_admin_node_form_alter($form, $form_state);
}
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);
}
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;
}
}
}
function pay_node_nodeapi(&$node, $op, $teaser = NULL, $a4 = NULL) {
if ($op == 'load') {
if ($pay_form = pay_node_form_load($node)) {
$node->pay_form = $pay_form;
}
}
if ($op == 'view') {
if ($teaser) {
$theme = variable_get('pay_node_display_teaser_' . $node->type, '');
}
else {
$theme = variable_get('pay_node_display_' . $node->type, 'pay_form_default');
}
if (in_array($theme, array(
'',
'tab',
))) {
return;
}
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,
);
}
}
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,
);
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);
}
}
}
}
}
if ($op == 'delete') {
db_query("DELETE FROM {pay_form_node} WHERE nid = %d", $node->nid);
}
}
function pay_node_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'pay_node') . '/includes/views',
);
}
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,
),
);
}
}
function pay_node_pay_transaction_create(&$transaction, $values = array()) {
if (isset($values['pay_node_nid']) && ($nid = $values['pay_node_nid'])) {
if (!($pay_form = pay_node_form_load($nid))) {
return;
}
if ($pay_form->pfid != $transaction->pfid) {
return;
}
$record = array(
'nid' => $nid,
'pxid' => $transaction->pxid,
);
drupal_write_record('pay_transaction_node', $record);
}
}
function pay_node_pay_transaction_delete(&$transaction) {
db_query("DELETE FROM {pay_transaction_node}\n WHERE pxid = %d", $transaction->pxid);
}
function pay_node_pay_form(&$form_state, $node) {
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);
}
}
function pay_node_form_load($node) {
if (is_scalar($node)) {
$node = node_load($node);
}
if (!variable_get('pay_node_enabled_' . $node->type, FALSE)) {
return;
}
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;
}
$method = variable_get('pay_node_method_' . $node->type, '');
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)) {
$pay_form
->set_menu_path('node/' . $node->nid . '/pay');
}
}
elseif ($method == 'select') {
return;
}
}
if (!isset($pay_form)) {
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;
}
function pay_node_admin_node_form_after_build(&$form, &$form_state) {
$form['#submit'][] = 'pay_node_form_submit';
return $form;
}
function pay_node_form_submit(&$form, &$form_state) {
if (isset($form_state['pay'])) {
$form_state['values']['pay_node'] = $form_state['pay'];
}
}