You are here

vbo_search_and_replace.inc in Views Bulk Operations Search & Replace 7

Private helper functions for VBO Search and Replace.

File

includes/vbo_search_and_replace.inc
View source
<?php

/**
 * @file
 * Private helper functions for VBO Search and Replace.
 */

/**
 * Returns all properties that can be modified.
 *
 * Only Text based properties can be changed.
 *
 * This function is largely a copy of
 * _views_bulk_operations_modify_action_get_properties but with the supported
 * types limited to 'text'.
 *
 * @param string $entity_type
 *   The entity type whose properties will be fetched.
 * @param array $display_values
 *   An optional, admin-provided list of properties and fields that should be
 *   displayed for editing, used to filter the returned list of properties.
 */
function _vbo_search_and_replace_action_get_properties($entity_type, array $display_values = NULL) {
  $properties = array();
  $info = entity_get_info($entity_type);
  $disabled_properties = array(
    'created',
    'changed',
  );
  foreach (array(
    'id',
    'bundle',
    'revision',
  ) as $key) {
    if (!empty($info['entity keys'][$key])) {
      $disabled_properties[] = $info['entity keys'][$key];
    }
  }
  $supported_types = array(
    'text',
  );
  $property_info = entity_get_property_info($entity_type);
  foreach ($property_info['properties'] as $key => $property) {
    if (in_array($key, $disabled_properties)) {
      continue;
    }

    // Filter out properties that can't be set (they are usually generated by a
    // getter callback based on other properties, and not stored in the DB).
    if (empty($property['setter callback'])) {
      continue;
    }

    // Determine the property type. If it's empty (permitted), default to text.
    // If it's a list type such as list<boolean>, extract the "boolean" part.
    $property['type'] = empty($property['type']) ? 'text' : $property['type'];
    $type = $property['type'];
    if ($list_type = entity_property_list_extract_type($type)) {
      $type = $list_type;
      $property['type'] = 'list';
    }

    // Filter out non-supported types (such as the Field API fields that
    // Commerce adds to its entities so that they show up in tokens).
    if (!in_array($type, $supported_types)) {
      continue;
    }
    $properties[$key] = $property;
  }
  if (isset($display_values) && empty($display_values[VBO_MODIFY_ACTION_ALL])) {

    // Return only the properties that the admin specified.
    return array_intersect_key($properties, $display_values);
  }
  return $properties;
}

/**
 * Performs a search and replace on a value and returns the result.
 *
 * Search is case insensitive by default but can be changed in $settings.
 *
 * @param string $search
 *    The value to search for.
 * @param string $replace
 *    The value to use as a replacement.
 * @param string $subject
 *    The Value to perform search and replace on.
 * @param array $settings
 *    An array of settings for the search and replace:
 *      - regular_expression: Set to TRUE for a regular expression search.
 *      - search_prefix: Add a prefix to the search.
 *      - search_suffix: Add a suffix to the search.
 *      - exact_match: Set to TRUE to match entire $subject instead of just a
 *        part.
 *      - case_sensitive: Set to TRUE to make the search case sensitive.
 */
function _vbo_search_and_replace_search_and_replace($search, $replace, $subject, array $settings = array()) {
  if ($settings['regular_expression']) {
    try {
      $subject = preg_replace($search, $replace, $subject);
    } catch (Exception $e) {
      drupal_set_message('There was an error performing a search and replace with this regular expression.', 'error');
    }
  }
  else {
    $search = $settings['search_prefix'] . $search . $settings['search_suffix'];
    $replace = $settings['search_prefix'] . $replace . $settings['search_suffix'];
    if ($settings['exact_match'] && $subject === $search) {
      $subject = $replace;
    }
    elseif ($settings['case_sensitive']) {
      $subject = str_replace($search, $replace, $subject);
    }
    else {
      $subject = str_ireplace($search, $replace, $subject);
    }
  }
  return $subject;
}

/**
 * Helper function that recursively strips #required from field widgets.
 */
function _vbo_search_and_replace_action_unset_required(&$element) {
  unset($element['#required']);
  foreach (element_children($element) as $key) {
    _views_bulk_operations_modify_action_unset_required($element[$key]);
  }
}

/**
 * Check the user-supplied regular expression pattern for the e modifier.
 *
 * @param string $pattern
 *   The PCRE pattern provided by the user.
 */
function _vbo_search_and_replace_pattern_has_exploitable_e_modifier($pattern) {
  $pattern = trim($pattern);
  $delimiter = $pattern[0];
  $parts = explode($delimiter, $pattern);
  $modifiers = $parts[count($parts) - 1];
  return strpos($modifiers, 'e') !== FALSE;
}

/**
 * Get Form API element states for "select all" option.
 *
 * @param string $name
 *   The name of the "select all" element.
 */
function _vbo_search_and_replace_select_all_states($name) {
  return array(
    'checked' => array(
      ':input[name="' . $name . '"]' => array(
        'checked' => TRUE,
      ),
    ),
    'disabled' => array(
      ':input[name="' . $name . '"]' => array(
        'checked' => TRUE,
      ),
    ),
    'unchecked' => array(
      ':input[name="' . $name . '"]' => array(
        'checked' => FALSE,
      ),
    ),
    'enabled' => array(
      ':input[name="' . $name . '"]' => array(
        'checked' => FALSE,
      ),
    ),
  );
}

Functions

Namesort descending Description
_vbo_search_and_replace_action_get_properties Returns all properties that can be modified.
_vbo_search_and_replace_action_unset_required Helper function that recursively strips #required from field widgets.
_vbo_search_and_replace_pattern_has_exploitable_e_modifier Check the user-supplied regular expression pattern for the e modifier.
_vbo_search_and_replace_search_and_replace Performs a search and replace on a value and returns the result.
_vbo_search_and_replace_select_all_states Get Form API element states for "select all" option.