You are here

webform_default_fields.module in Webform Default Fields 7.4

File

webform_default_fields.module
View source
<?php

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

/**
 * Implements hook_permission().
 */
function webform_default_fields_permission() {
  return array(
    'webform default fields reset' => array(
      'title' => t('Reset Webform components to Default'),
      'description' => t('Grants access to the "Reset" page on all webform content.'),
    ),
    '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.'),
    ),
    'webform default fields edit' => array(
      'title' => t('Webform default fields edit'),
      'description' => t('Grants access to edit to the "Webform Default Fields".'),
    ),
    'webform default fields delete' => array(
      'title' => t('Webform default fields delete'),
      'description' => t('Grants access to delete to the "Webform Default Fields".'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function webform_default_fields_menu() {
  $items = array();
  $path = drupal_get_path('module', 'webform_default_fields');
  $webform_includes_path = drupal_get_path('module', 'webform') . '/includes/';
  $items['admin/structure/types/manage/%node_type/webform-fields'] = array(
    'title callback' => 'webform_default_fields_ui_title',
    'title arguments' => array(
      4,
    ),
    'page callback' => 'webform_default_fields_list',
    'page arguments' => array(
      4,
    ),
    'access callback' => 'webform_default_fields_type_access',
    'access arguments' => array(
      4,
      'administer webform default fields',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
    'file' => 'webform_default_fields.admin.inc',
    'file path' => $path . '/includes/pages',
  );
  $items['admin/structure/types/manage/%node_type/webform-fields/components'] = array(
    'title' => 'Components',
    'page callback' => 'webform_default_fields_list',
    'page arguments' => array(
      4,
    ),
    'access callback' => 'webform_default_fields_type_access',
    'access arguments' => array(
      4,
      'administer webform default fields',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 1,
    'file' => 'webform_default_fields.admin.inc',
    'file path' => $path . '/includes/pages',
  );

  // Conditionals form.
  $items['admin/structure/types/manage/%node_type/webform-fields/conditions'] = array(
    'title' => 'Conditionals',
    'page callback' => 'webform_default_fields_get_form',
    'page arguments' => array(
      'webform_conditionals_form',
      4,
    ),
    'access callback' => 'webform_default_fields_type_access',
    'access arguments' => array(
      4,
      'administer webform default fields',
    ),
    'file' => 'webform.conditionals.inc',
    'file path' => $webform_includes_path,
    'weight' => 5,
    'type' => MENU_LOCAL_TASK,
  );

  // Emails form.
  $items['admin/structure/types/manage/%node_type/webform-fields/emails'] = array(
    'title' => 'E-mails',
    'page callback' => 'webform_default_fields_get_form',
    'page arguments' => array(
      'webform_emails_form',
      4,
    ),
    'access callback' => 'webform_default_fields_type_access',
    'access arguments' => array(
      4,
      'administer webform default fields',
    ),
    'file' => 'webform.emails.inc',
    'file path' => $webform_includes_path,
    'weight' => 9,
    'type' => MENU_LOCAL_TASK,
  );

  // Configuration form.
  $items['admin/structure/types/manage/%node_type/webform-fields/configure'] = array(
    'title' => 'Configure',
    'page callback' => 'webform_default_fields_get_form',
    'page arguments' => array(
      'webform_configure_form',
      4,
    ),
    'access callback' => 'webform_default_fields_type_access',
    'access arguments' => array(
      4,
      'administer webform default fields',
    ),
    'file' => 'webform.pages.inc',
    'file path' => $webform_includes_path,
    'weight' => 13,
    'type' => MENU_LOCAL_TASK,
  );

  // Reset form.
  $items['admin/structure/types/manage/%node_type/webform-fields/reset'] = array(
    'title' => 'Reset',
    'page callback' => 'webform_default_fields_reset_type',
    'page arguments' => array(
      4,
    ),
    'access callback' => 'webform_default_fields_type_access',
    'access arguments' => array(
      4,
      'webform default fields reset',
    ),
    'file' => 'webform_default_fields.reset.inc',
    'file path' => $path . '/includes',
    'weight' => 17,
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  );
  $items['node/%webform_menu/webform/webform-reset'] = array(
    'title' => 'Reset',
    'page callback' => 'webform_default_fields_reset',
    'page arguments' => array(
      1,
    ),
    'access arguments' => array(
      'webform default fields reset',
    ),
    'file' => 'webform_default_fields.reset.inc',
    'file path' => $path . '/includes',
    'weight' => 8,
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  );
  return $items;
}

/**
 * Menu item title callback.
 *
 * @param object $type
 *   Drupal node type object.
 *
 * @return string
 *   The title name.
 */
function webform_default_fields_ui_title($type) {
  $return = isset($type->name) ? $type->name . ': Default Webform Fields' : 'Default Webform Fields';
  return $return;
}

/**
 * Access callback for node types menu.
 *
 * @param object $node_type
 *   Drupal node type object.
 * @param string $permission
 *   A valid Drupal permission to pass to user_access().
 *
 * @return bool
 *   TRUE if type is a webform with default fields and user has access.
 */
function webform_default_fields_type_access($node_type, $permission) {
  $access = FALSE;
  if (user_access($permission)) {
    if ($node_type && isset($node_type->type)) {
      $access = webform_default_fields_is_type($node_type->type);
    }
  }
  return $access;
}

/**
 * Helper to determine if a given node type uses webform default fields.
 *
 * @param string $node_type
 *   A valid node_type machine name.
 *
 * @return bool
 *   Whether the type uses webform_default_fields or not.
 */
function webform_default_fields_is_type($node_type) {
  $return = FALSE;
  $types = _webform_default_fields_webform_node_types();
  foreach ($types as $type) {
    if (isset($type['name']) && $type['name'] == $node_type) {
      $return = TRUE;
    }
  }
  return $return;
}

/**
 * Implements hook_node_view().
 */
function webform_default_fields_node_view($node, $view_mode, $langcode) {
  if ($view_mode == 'full' || $view_mode == 'teaser') {
    if (webform_default_fields_is_type($node->type)) {
      $nid = _webform_default_fields_container_node($node->type);
      if ($node->nid == $nid) {
        drupal_goto('/admin/structure/types/manage/' . $node->type . '/webform-fields');
      }
    }
  }
}

/**
 * Helper to get a form with the correct default node.
 *
 * @param string $form_id
 *   A form id to load.
 * @param object $node_type
 *   Drupal node type object.
 *
 * @return array|mixed
 *   The result of drupal_get_form.
 */
function webform_default_fields_get_form($form_id, $node_type) {
  $nid = _webform_default_fields_container_node($node_type->type);
  $node = node_load($nid);
  return drupal_get_form($form_id, $node);
}

/**
 * Implements hook_features_api().
 */
function webform_default_fields_features_api() {
  return array(
    'webform_default_fields' => array(
      'name' => t('Webform Default Fields'),
      'default_hook' => 'webform_default_fields_defaults',
      'default_file' => FEATURES_DEFAULTS_INCLUDED,
      'feature_source' => TRUE,
      'file' => drupal_get_path('module', 'webform_default_fields') . '/webform_default_fields.features.inc',
    ),
  );
}

/**
 * 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_node_type_form_alter(&$form, &$form_state) {
  if (isset($form['type'])) {
    form_load_include($form_state, 'inc', 'webform_default_fields', 'includes/webform_default_fields.form');
    $default_value = FALSE;
    if (isset($form['#node_type']->type)) {
      $default_value = variable_get('webform_default_fields_allow_node_' . $form['#node_type']->type, FALSE);
    }
    $form['#submit'][] = 'webform_default_fields_form_webform_admin_settings_submit';
    $form['webform']['webform_default_fields'] = array(
      '#type' => 'checkbox',
      '#title' => t('Webform default fields'),
      '#description' => t('Allows Webform default fields create default fields for this content type.'),
      '#default_value' => $default_value,
      '#attributes' => array(
        'data-enabled-description' => t('Enabled'),
        'data-disabled-description' => t('Disabled'),
      ),
      '#states' => array(
        'invisible' => array(
          ':input[name="webform_node"]' => array(
            'checked' => FALSE,
          ),
        ),
      ),
    );
  }
}

/**
 * Implements hook_node_insert().
 *
 * If you use hook_webform_node_defaults_alter() and don't want Webform
 * Default Fields to overwrite the your defaults in $node->webform, you should
 * add $defaults['webform_default_fields_ignore'] = TRUE in your implementing.
 */
function webform_default_fields_node_insert($node) {
  $ignore = isset($node->webform['webform_default_fields_ignore']);
  $nid = variable_get('webform_default_fields_nid_' . $node->type, 0);
  $content_type_allow = variable_get('webform_default_fields_allow_node_' . $node->type, TRUE);
  if ($nid && $content_type_allow && !$ignore) {
    if (empty($node->webform['components'])) {
      module_load_include('inc', 'webform', 'includes/webform.components');
      webform_ensure_record($node);
      $master_node = node_load($nid, NULL, TRUE);
      if (isset($master_node->webform)) {
        $node->original = clone $node;
        $node->webform = $master_node->webform;
        $node->webform = _webform_default_fields_set_nid($node->webform, $node->nid);
        webform_node_update($node);
        if (module_exists('webform_workflow')) {
          $node->webform_workflow = $master_node->webform_workflow;
          db_merge('webform_workflow')
            ->key(array(
            'nid' => $node->nid,
          ))
            ->fields(array(
            'workflow' => $node->webform_workflow->workflow,
            'data' => serialize($node->webform_workflow->data),
          ))
            ->execute();
          $node->webform_workflow_states = $master_node->webform_workflow_states;
          field_attach_update('node', $node);
        }
      }
    }
  }
}

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

/**
 * Implements hook_node_access().
 */
function webform_default_fields_node_access($node, $op, $account) {
  $type = is_string($node) ? $node : $node->type;
  $nid = isset($node->nid) ? $node->nid : NULL;
  if (variable_get('webform_default_fields_nid_' . $type, 0) == $nid) {
    if ($op == 'update') {
      if (user_access('webform default fields edit', $account)) {
        return NODE_ACCESS_ALLOW;
      }
      else {
        return NODE_ACCESS_DENY;
      }
    }
    if ($op == 'delete') {
      if (user_access('webform default fields delete', $account)) {
        return NODE_ACCESS_ALLOW;
      }
      else {
        return NODE_ACCESS_DENY;
      }
    }
  }

  // Returning nothing from this function would have the same effect.
  return NODE_ACCESS_IGNORE;
}

/**
 * Helper function to set nid.
 *
 * Setting nid in webform components.
 */
function _webform_default_fields_set_nid($webform, $nid = 0) {
  $webform['nid'] = $nid;
  foreach ($webform['emails'] as &$component) {
    $component['nid'] = $nid;
  }
  foreach ($webform['components'] as &$component) {
    $component['nid'] = $nid;
  }
  if (isset($webform['conditionals']) && !empty($webform['conditionals'])) {
    foreach ($webform['conditionals'] as &$component) {
      $component['nid'] = $nid;
    }
  }
  return $webform;
}