You are here

function finder_update_7203 in Finder 7.2

Convert Finder 1.x finders to Finder 2.x.

File

./finder.install, line 274
Finder module install file.

Code

function finder_update_7203() {

  // Rebuild finder schema cache.
  drupal_get_schema('finder', TRUE);
  $finders = variable_get('finder_old_finders', array());
  $custom_matching = variable_get('finder_custom_matching', array());
  $max_finder_id = max(array_keys($finders));
  $dummies = array();
  for ($i = 1; $i <= $max_finder_id; $i++) {
    if (!empty($finders[$i])) {

      // Reformat the finder here and finder_save() it.
      $new_finder = finder_new();
      $finder =& $finders[$i];
      $new_finder->name = 'finder_' . $finder->finder_id;
      $new_finder->title = $finder->title;
      $new_finder->description = $finder->description;
      $new_finder->path = $finder->path;
      $new_finder->block = $finder->block;
      $new_finder->settings = array();
      if ($finder->base == 'node') {
        $new_finder->views_view = 'finder_node';
        $new_finder->views_display = 'page_1';
      }
      elseif ($finder->base == 'user') {
        $new_finder->views_view = 'finder_user';
        $new_finder->views_display = 'page_1';
      }
      else {
        if (isset($finder->settings['views']['view'])) {
          $new_finder->views_view = $finder->settings['views']['view'];
          $new_finder->views_display = 'default';
          $view = views_get_view($finder->settings['views']['view']);
          foreach ($view->display as $key => $display) {
            if ($key != 'default') {
              $new_finder->views_display = $key;
              break;
            }
          }
        }
        if (isset($finder->settings['views']['displays']['query']) && $finder->settings['views']['displays']['query'] != 'default') {
          $new_finder->views_display = $finder->settings['views']['displays']['query'];
        }
        if (isset($finder->settings['views']['results'])) {
          $new_finder->settings['results_style'] = $finder->settings['views']['results'] ? 'custom' : 'views';
        }
        if (isset($finder->settings['views']['relationships'])) {
          foreach ($finder->settings['views']['relationships'] as $rel_key => $fields) {
            foreach ($fields as $field) {
              $relationships[$field] = $rel_key;
            }
          }
        }
        if (isset($finder->settings['views']['views_args']['args'])) {
          if (isset($finder->settings['views']['views_args']['delimiter'])) {
            $finder->settings['views']['views_args']['args'] = str_replace($finder->settings['views']['views_args']['delimiter'], '/', $finder->settings['views']['views_args']['args']);
          }
          $new_finder->settings['contextual_filter'] = $finder->settings['views']['views_args']['args'];
        }
      }
      if (isset($finder->settings['form_on_page'])) {
        $new_finder->settings['form_on_page'] = $finder->settings['form_on_page'];
      }
      if (isset($finder->settings['form']['prefix']['value'])) {
        $new_finder->settings['header']['value'] = $finder->settings['form']['prefix']['value'];
      }
      if (isset($finder->settings['form']['prefix']['format'])) {
        $new_finder->settings['header']['format'] = $finder->settings['form']['prefix']['format'];
      }
      if (isset($finder->settings['form']['suffix']['value'])) {
        $new_finder->settings['footer']['value'] = $finder->settings['form']['suffix']['value'];
      }
      if (isset($finder->settings['form']['prefix']['format'])) {
        $new_finder->settings['footer']['format'] = $finder->settings['form']['suffix']['format'];
      }
      if (isset($finder->settings['form']['button_text'])) {
        $new_finder->settings['find_text'] = $finder->settings['form']['button_text'];
      }
      if (isset($finder->settings['form']['go_text'])) {
        $new_finder->settings['go_text'] = $finder->settings['form']['go_text'];
      }
      if (isset($finder->settings['advanced']['no_results']['no_results'])) {
        $new_finder->settings['no_results'] = 'configured';
        $new_finder->settings['no_results_configured'] = $finder->settings['advanced']['no_results']['no_results'];
      }
      if (!empty($finder->settings['advanced']['filter'])) {
        $new_finder->settings['show_results'] = 'filter';
      }
      if (!empty($finder->settings['advanced']['validate_empty'])) {
        $new_finder->settings['validate_empty'] = TRUE;
      }
      if (isset($finder->settings['advanced']['submission'])) {
        $new_finder->settings['find_button'] = $finder->settings['advanced']['submission'];
      }
      if (isset($finder->settings['advanced']['ajax'])) {
        $new_finder->settings['ajax'] = $finder->settings['advanced']['ajax'];
      }
      if (isset($finder->settings['advanced']['ajax_effect'])) {
        $new_finder->settings['ajax_effect'] = $finder->settings['advanced']['ajax_effect'];
      }
      if (isset($finder->settings['advanced']['ajax_remote'])) {
        $new_finder->settings['ajax_remote'] = $finder->settings['advanced']['ajax_remote'];
      }
      if (isset($finder->settings['advanced']['element_combination'])) {
        $new_finder->settings['element_logic'] = $finder->settings['advanced']['element_combination'] ? 'OR' : 'AND';
      }
      if (isset($finder->settings['advanced']['pager'])) {
        $new_finder->settings['pager'] = $finder->settings['advanced']['pager'];
      }
      if (isset($finder->settings['advanced']['goto'])) {
        if ($finder->settings['advanced']['goto'] == '') {
          $new_finder->settings['goto'] = 'never';
        }
        elseif ($finder->settings['advanced']['goto'] == 'go') {
          $new_finder->settings['goto'] = 'never';
          $new_finder->settings['go_button'] = TRUE;
        }
        else {
          $new_finder->settings['goto'] = $finder->settings['advanced']['goto'];
        }
      }
      if (isset($finder->settings['advanced']['hide_args'])) {
        $new_finder->settings['url'] = $finder->settings['advanced']['hide_args'] ? 'disabled' : 'enabled';
      }
      if (isset($finder->settings['advanced']['arg_sep'])) {
        $new_finder->settings['url_delimiter'] = $finder->settings['advanced']['arg_sep'];
      }
      if (isset($finder->settings['advanced']['empty_symbol'])) {
        $new_finder->settings['url_empty'] = $finder->settings['advanced']['empty_symbol'];
      }
      if (isset($finder->settings['advanced']['cache_finder_find'])) {
        $new_finder->settings['cache'] = $finder->settings['advanced']['cache_finder_find'];
      }
      if (isset($finder->settings['advanced']['search'])) {
        $new_finder->settings['search'] = $finder->settings['advanced']['search'];
      }
      if (isset($finder->settings['advanced']['search_tab'])) {
        $new_finder->settings['search_tab'] = $finder->settings['advanced']['search_tab'];
      }
      $new_finder->elements = array();
      foreach ($finder->elements as $element) {
        $new_element = (object) array();
        $new_element->id = 'element_' . $element->finder_element_id;
        $new_element->finder = $new_finder->name;
        $new_element->element = $element->element;
        $new_element->title = $element->title;
        $new_element->weight = $element->weight;
        if (isset($element->settings['form']['description'])) {
          $new_element->settings['description'] = $element->settings['form']['description'];
        }
        if (isset($element->settings['form']['prefix']['value'])) {
          $new_element->settings['header']['value'] = $element->settings['form']['prefix']['value'];
        }
        if (isset($element->settings['form']['prefix']['format'])) {
          $new_element->settings['header']['format'] = $element->settings['form']['prefix']['format'];
        }
        if (isset($element->settings['form']['suffix']['value'])) {
          $new_element->settings['footer']['value'] = $element->settings['form']['suffix']['value'];
        }
        if (isset($element->settings['form']['suffix']['format'])) {
          $new_element->settings['footer']['format'] = $element->settings['form']['suffix']['format'];
        }
        if (isset($element->settings['form']['default_value'])) {
          $new_element->settings['default_value'] = $element->settings['form']['default_value'];
        }
        if (isset($element->settings['form']['required'])) {
          $new_element->settings['required'] = $element->settings['form']['required'];
        }
        if (isset($element->settings['form']['field_prefix'])) {
          $new_element->settings['field_prefix'] = $element->settings['form']['field_prefix'];
        }
        if (isset($element->settings['form']['field_suffix'])) {
          $new_element->settings['field_suffix'] = $element->settings['form']['field_suffix'];
        }
        if (isset($element->settings['form']['maxlength'])) {
          $new_element->settings['maxlength'] = $element->settings['form']['maxlength'];
        }
        if (isset($element->settings['form']['size'])) {
          $new_element->settings['size'] = $element->settings['form']['size'];
        }
        if (isset($element->settings['form']['max_suggestions'])) {
          $new_element->settings['max_suggestions'] = $element->settings['form']['max_suggestions'];
        }
        if (isset($element->settings['form']['match'])) {
          $match = $element->settings['form']['match'];
          if (isset($custom_matching[$match])) {
            $custom_match = $custom_matching[$match];
            $new_element->settings['autocomplete_match'] = 'x';
            $new_element->settings['autocomplete_match_custom_prefix'] = $custom_match['value_prefix'];
            $new_element->settings['autocomplete_match_custom_suffix'] = $custom_match['value_suffix'];
            $new_element->settings['autocomplete_match_custom_operator'] = $custom_match['operator'];
          }
          else {
            $new_element->settings['autocomplete_match'] = $match;
          }
        }
        if (isset($element->settings['form']['autosubmit'])) {
          $new_element->settings['autosubmit'] = $element->settings['form']['autosubmit'];
        }
        if (isset($element->settings['form']['delimit_autocomplete'])) {
          $new_element->settings['autocomplete_delimit'] = $element->settings['form']['delimit_autocomplete'];
        }
        if (isset($element->settings['form']['multiple'])) {
          $new_element->settings['multiple'] = $element->settings['form']['multiple'];
        }
        if (isset($element->settings['form']['empty'])) {
          $new_element->settings['empty'] = $element->settings['form']['empty'];
        }
        if (isset($element->settings['form']['empty_text'])) {
          $new_element->settings['empty_text'] = $element->settings['form']['empty_text'];
        }
        if (isset($element->settings['form']['minlength'])) {
          $new_element->settings['minlength'] = $element->settings['form']['minlength'];
        }
        if (isset($element->settings['form']['rows'])) {
          $new_element->settings['rows'] = $element->settings['form']['rows'];
        }
        if (isset($element->settings['choices']['field'])) {
          foreach ($element->settings['choices']['field'] as $field) {
            list($table, $field_name) = explode('.', $field);
            $new_field = (object) array(
              'table' => $table,
              'field' => $field_name,
              'relationship' => NULL,
              'format' => isset($element->settings['choices']['sanitization']['format']) ? $element->settings['choices']['sanitization']['format'] : 'filter_xss',
            );
            if (isset($relationships[$field])) {
              $new_field->relationship = $relationships[$field];
            }
            $new_element->settings['fields'][$field] = $new_field;
          }
        }
        if (isset($element->settings['choices']['sort'])) {
          $new_element->settings['sort'] = $element->settings['choices']['sort'];
        }
        if (isset($element->settings['choices']['views']['args'])) {
          if (isset($element->settings['choices']['views']['delimiter'])) {
            $element->settings['choices']['views']['args'] = str_replace($element->settings['choices']['views']['delimiter'], '/', $element->settings['choices']['views']['args']);
          }
          $new_element->settings['contextual_filter'] = $element->settings['choices']['views']['args'];
        }
        if (isset($element->settings['choices']['displays']['query']) && $element->settings['choices']['displays']['query'] != 'default') {
          $new_element->settings['display'] = $element->settings['choices']['displays']['query'];
        }
        if (isset($element->settings['advanced']['delimit'])) {
          $new_element->settings['delimit'] = $element->settings['advanced']['delimit'];
        }
        if (isset($element->settings['advanced']['field_combination'])) {
          $new_element->settings['field_logic'] = $element->settings['advanced']['field_combination'] ? 'AND' : 'OR';
        }
        if (isset($element->settings['advanced']['value_combination'])) {
          $new_element->settings['value_logic'] = $element->settings['advanced']['value_combination'] ? 'AND' : 'OR';
        }
        if (isset($element->settings['advanced']['nesting_order'])) {
          $new_element->settings['nesting_order'] = $element->settings['advanced']['nesting_order'];
        }
        if (isset($element->settings['advanced']['match'])) {
          $match = $element->settings['advanced']['match'];
          if (isset($custom_matching[$match])) {
            $custom_match = $custom_matching[$match];
            $new_element->settings['match'] = 'x';
            $new_element->settings['match_custom_prefix'] = $custom_match['value_prefix'];
            $new_element->settings['match_custom_suffix'] = $custom_match['value_suffix'];
            $new_element->settings['match_custom_operator'] = $custom_match['operator'];
          }
          else {
            $new_element->settings['match'] = $match;
          }
        }
        $args = array();
        if ($finder->base == 'node') {
          if (!empty($element->settings['choices']['node_types'])) {
            foreach ($element->settings['choices']['node_types'] as $node_type) {
              if ($node_type) {
                $args[] = $node_type;
              }
            }
          }
        }
        elseif ($finder->base == 'user') {
          if (!empty($element->settings['choices']['user_roles'])) {
            foreach ($element->settings['choices']['user_roles'] as $user_role) {
              if ($user_role) {
                $args[] = $user_role;
              }
            }
          }
        }
        if (!empty($args)) {
          $new_element->settings['contextual_filter'] = implode('/', $args);
        }
        $new_finder->elements[$new_element->id] = $new_element;
      }
      finder_save($new_finder);
    }
    else {

      // Create a dummy entry, and mark it so we delete it later.
      $new_finder = finder_new();
      $dummies[] = $new_finder->name;
      finder_save($new_finder);
    }
  }
  if (!empty($dummies)) {

    // Delete the dummies.
    db_delete('finder')
      ->condition('name', $dummies, 'IN')
      ->execute();
  }
  return 'Convert Finder 1.x finders to Finder 2.x.';
}