You are here

webform_default_fields.module in Webform Default Fields 7

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_permission().
 */
function webform_default_fields_permission() {
  return array(
    'administer webform default fields' => array(
      'title' => t('Administer Webform Default Fields'),
      'description' => t('Grants access to the "Webform Default Fields" tab on content types management page. Generally an administrative permission.'),
    ),
  );
}

/**
 * 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/structure/types/manage/' . $type_url_str . '/webform-fields'] = array(
      'title' => $type_name . ': Default Webform Fields',
      'page callback' => 'webform_default_fields_list',
      'page arguments' => array(
        4,
      ),
      '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_update().
 */
function webform_default_fields_node_type_update($info) {
  if (!empty($info->old_type) && $info->old_type != $info->type) {
    _webform_default_fields_update_container_node($info->type, $info->old_type);
  }
}

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

/**
 * Implements hook_insert().
 */
function webform_default_fields_node_insert($node) {
  if ($nid = variable_get('webform_default_fields_nid_' . $node->type, 0)) {
    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);
    }
  }
}

/**
 * Implements hook_node_update().
 */
function webform_default_fields_node_delete($node) {
  if ($nid = variable_get('webform_default_fields_nid_' . $node->type, 0)) {
    if ($nid == $node->nid) {
      variable_del('webform_default_fields_nid_' . $node->type);
    }
  }
}