You are here

webform_default_fields.module in Webform Default Fields 6

File

webform_default_fields.module
View source
<?php

// $Id$

/**
 * @file
 * Main Webform Default Fields routines
 */
require_once 'includes/webform_default_fields.data.inc';
define('WEBFORM_DEFAULT_FIELDS_DEFAUT_TYPE', 'webform');

/**
 * Implements hook_perm().
 */
function webform_default_fields_perm() {
  return array(
    'administer webform default fields',
  );
}

/**
 * Implements hook_menu().
 */
function webform_default_fields_menu() {
  $items = array();
  $path = drupal_get_path('module', 'webform_default_fields');
  foreach (_webform_default_fields_webform_node_types() as $type) {
    $type_url_str = $type['url'];
    $type_name = $type['name'];

    // Main fields form
    $items['admin/content/node-type/' . $type_url_str . '/webform-fields'] = array(
      'title' => 'Default Webform Fields',
      'page callback' => 'webform_default_fields_list',
      'page arguments' => array(
        3,
      ),
      'access arguments' => array(
        'administer webform default fields',
      ),
      'type' => MENU_LOCAL_TASK,
      'weight' => 1,
      'file' => 'webform_default_fields.admin.inc',
      'file path' => $path . '/includes/pages',
    );
    $nid = _webform_default_fields_container_node($type_name);

    // Redirect master node webforms to content type fields page
    $items['node/' . $nid . '/webform/components'] = array(
      'page callback' => 'webform_default_fields_redirect_master',
      'page arguments' => array(
        1,
      ),
      'type' => MENU_CALLBACK,
      'access arguments' => array(
        'administer webform default fields',
      ),
      'file' => 'webform_default_fields.admin.inc',
      'file path' => $path . '/includes/pages',
    );
    $items['node/' . $nid] = $items['node/' . $nid . '/webform/components'];
  }
  return $items;
}

/**
 * Implements hook_node_type().
 */
function webform_default_fields_node_type($op, $info) {
  switch ($op) {
    case 'update':
      if (!empty($info->old_type) && $info->old_type != $info->type) {
        _webform_default_fields_update_container_node($info->type, $info->old_type);
      }
      break;
  }
}

/**
 * Implements hook_nodeapi().
 */
function webform_default_fields_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($nid = variable_get('webform_default_fields_nid_' . $node->type, 0)) {
    switch ($op) {
      case 'insert':
        module_load_include('inc', 'webform', 'includes/webform.components');
        webform_ensure_record($node);
        $master_node = node_load($nid, NULL, TRUE);
        foreach ($master_node->webform['components'] as $component) {
          $component['nid'] = $node->nid;
          $node->webform['components'][] = $component;
          webform_component_insert($component);
        }
        break;
      case 'delete':
        if ($nid == $node->nid) {
          variable_del('webform_default_fields_nid_' . $node->type);
        }
        break;
      default:
        return;
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter (FORM_ID = "webform_admin_settings").
 */
function webform_default_fields_form_webform_admin_settings_alter(&$form, $form_state) {
  module_load_include('inc', 'webform_default_fields', 'includes/webform_default_fields.form');
  $form['#submit'][] = 'webform_default_fields_form_webform_admin_settings_submit';
}

/**
 * Implements hook_db_rewrite_sql().
 */
function webform_default_fields_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
  if (substr($_GET['q'], 0, 18) != 'admin/content/node' && substr($_GET['q'], 0, 21) != 'admin/content/webform') {
    return;
  }
  switch ($primary_field) {
    case 'nid':
      $nids = array(
        0,
      );
      foreach (_webform_default_fields_webform_node_types() as $type) {
        $nids[] = _webform_default_fields_container_node($type['name']);
      }
      $return = array();
      if ($primary_table != 'n') {
        $return['join'] = "LEFT JOIN {node} n ON {$primary_table}.nid = n.nid";
      }
      $return['where'] = 'n.nid != ' . implode(' AND n.nid != ', $nids);
      return $return;
  }
}