You are here

function acquia_agent_update_6000 in Acquia Connector 6

Same name and namespace in other branches
  1. 6.2 acquia_agent/acquia_agent.install \acquia_agent_update_6000()

Warn users who are upgrading from a pre-release version of CCK about any broken Views on their site.

File

acquia_agent/acquia_agent.install, line 160

Code

function acquia_agent_update_6000() {
  $ret = array();
  $broken_filters = array();

  // To be safe, we require that CCK and Views both be enabled, and also
  // perform many checks of their APIs during the course of this update.
  if (module_exists('content') && module_exists('views') && function_exists('views_api_version') && views_api_version() == 2.0) {

    // We also make sure only to print warnings in the case where the user
    // has a pre-release CCK schema version, because otherwise they may have
    // already made manual changes to their Views, and the warning messages
    // here therefore would not be accurate.
    $cck_schema_version = variable_get('content_schema_version', 0);
    if ($cck_schema_version >= 6000 && $cck_schema_version <= 6008) {

      // Search through all filters for all Views.
      if (function_exists('views_get_all_views') && ($views = views_get_all_views()) && is_array($views)) {
        foreach ($views as $view) {
          if (method_exists($view, 'init_display')) {
            $view
              ->init_display();
            if (is_array($view->display)) {
              foreach ($view->display as $display_id => $display) {
                if (method_exists($display->handler, 'is_defaulted') && !$display->handler
                  ->is_defaulted('filter') && method_exists($display->handler, 'get_handlers')) {
                  $handlers = $display->handler
                    ->get_handlers('filter');
                  if (is_array($handlers)) {
                    foreach ($handlers as $handler_id => $handler) {

                      // If the filter is on a CCK text or number field and
                      // defines a list of allowed values, then it is broken.
                      if (isset($handler->content_field)) {
                        $field = $handler->content_field;
                        if (isset($field['type']) && in_array($field['type'], array(
                          'text',
                          'number_integer',
                          'number_decimal',
                          'number_float',
                        ), TRUE)) {
                          if (function_exists('content_allowed_values')) {
                            $allowed_values = content_allowed_values($field);
                            if (count($allowed_values) && isset($view->type)) {
                              switch ($view->type) {
                                case t('Normal'):

                                  // The human-readable name is friendlier
                                  // for Views accessible via the UI.
                                  $broken_filters['database'][$view->name][] = t('Content: @label', array(
                                    '@label' => $field['widget']['label'],
                                  ));
                                  break;
                                case t('Overriden'):
                                case t('Default'):

                                  // The machine-readable name is friendlier
                                  // for Views stored in code.
                                  $broken_filters['code'][$view->name][] = $field['field_name'];
                                  break;
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  if (!empty($broken_filters)) {

    // Convert the list of broken filters to a user-friendly format.
    foreach ($broken_filters as $type => $views) {
      foreach ($views as $view => $fields) {
        $view_replacement = $type == 'database' ? l($view, 'admin/build/views/edit/' . $view) : check_plain($view);

        // Make sure we don't print the same field name twice.
        $broken_filters[$type][$view] = t('!view (broken filters: %filters)', array(
          '!view' => $view_replacement,
          '%filters' => implode(', ', array_unique($fields)),
        ));
      }
    }
    $broken_database_filters = isset($broken_filters['database']) ? $broken_filters['database'] : array();
    $broken_code_filters = isset($broken_filters['code']) ? $broken_filters['code'] : array();
    $t_strings = array(
      '!database_views' => '<li>' . implode('</li><li>', $broken_database_filters) . '</li>',
      '!code_views' => '<li>' . implode('</li><li>', $broken_code_filters) . '</li>',
      '@drupal_url' => 'http://drupal.org/node/330658',
    );
    if (!empty($broken_database_filters)) {
      $text = 'The following Views on your site may not work correctly as a result of this upgrade:<ul>!database_views</ul>In particular, filters on CCK text or numeric fields that refer to a list of allowed values will need to be recreated. You may do this through the administrative interface by editing the "Filters" section of each View, removing the broken filters listed above, and replacing each with a new one of type "Content: ... Allowed values". Alternatively, you can edit an exported version of the View and then reimport it; <a href="@drupal_url">more information</a> on this option is available from the CCK issue queue on drupal.org.';
      $message = t($text, $t_strings);
      watchdog('acquia', $text, $t_strings, WATCHDOG_WARNING);
      drupal_set_message($message, 'warning');
      $ret[] = array(
        'success' => TRUE,
        'query' => $message,
      );
    }
    if (!empty($broken_code_filters)) {
      $text = 'The following Views on your site may not work correctly as a result of this upgrade:<ul>!code_views</ul>In particular, filters on CCK text or numeric fields that refer to a list of allowed values will need to be recreated. The Views listed above are defined in code and will need to be edited there. <a href="@drupal_url">More information</a> on how to do this is available from the CCK issue queue on drupal.org.';
      $message = t($text, $t_strings);
      watchdog('acquia', $text, $t_strings, WATCHDOG_WARNING);
      drupal_set_message($message, 'warning');
      $ret[] = array(
        'success' => TRUE,
        'query' => $message,
      );
    }
  }
  return $ret;
}