You are here

function mediafront_update_6004 in MediaFront 6.2

Same name and namespace in other branches
  1. 6 mediafront.install \mediafront_update_6004()

I know this is a crazy update, but there is a reason.

We are now using the views styles to add the presets to each view instead of the UBER-HACK of adding "Media Player Page" and "Media Player Block". Now all you do is just add a new "Page" with a style of "Media Player". This is a MUCH more elegant solution and makes the code crazy simpler. So, instead of just killing every install out there for this upgrade when I blow away the old method, this script will now convert the old method to the new method.

File

./mediafront.install, line 173

Code

function mediafront_update_6004() {
  $update = array();
  $displays = array();

  // Get all of our views display's
  $result = db_query("SELECT * FROM {views_display}");

  // Create our displays array with all of our views displays.
  while ($display = db_fetch_object($result)) {
    $display->options = unserialize($display->display_options);
    $displays[$display->vid][$display->id] = $display;
  }

  // Now iterate through all of our displays.
  foreach ($displays as $vid => $group) {

    // Iterate through all of our display groups.
    foreach ($group as $id => $display) {

      // If this display is a mediaplayerpage or a mediaplayer ( block ).
      if ($display->display_plugin == 'mediaplayerpage' || $display->display_plugin == 'mediaplayer') {

        // Determine the preset of this display.
        $preset = '';
        if ($display->options['fields']) {

          // First search inside of the fields of this display...
          foreach ($display->options['fields'] as $name => $field) {
            if (isset($field['mediafront_preset'])) {
              $preset = $field['mediafront_preset'];
              break;
            }
          }
        }

        // If no preset is found, then search inside of the default display.
        if (!$preset) {
          foreach ($group['default']->options['fields'] as $name => $field) {
            if (isset($field['mediafront_preset'])) {
              $preset = $field['mediafront_preset'];
              break;
            }
          }
        }

        // Only continue if we found a preset.
        if ($preset) {

          // Set the new options...
          $new_options = $display->options;

          // Unset the fields since the new display's don't use them.
          unset($new_options['fields']);
          if ($new_options['defaults']) {
            unset($new_options['defaults']['fields']);
          }
          if ($display->options['defaults']) {
            $new_options['defaults'] = array_merge($display->options['defaults'], array(
              'style_plugin' => false,
              'style_options' => false,
              'row_plugin' => false,
              'row_options' => false,
            ));
          }
          else {
            $new_options['defaults'] = array(
              'style_plugin' => false,
              'style_options' => false,
              'row_plugin' => false,
              'row_options' => false,
            );
          }

          // Add the other display elements that are necessary.
          $new_options['row_options'] = array();
          $new_options['row_plugin'] = 'fields';
          $new_options['style_plugin'] = 'mediaplayer';
          $new_options['style_options'] = array(
            'mediafront_preset' => $preset,
          );
          if ($display->options['path']) {
            $new_options['path'] = $display->options['path'];
          }
          if ($display->options['menu']) {
            $new_options['menu'] = $display->options['menu'];
          }
          if ($display->options['block_description']) {
            $new_options['block_description'] = $display->options['block_description'];
          }
          if ($display->options['block_caching']) {
            $new_options['block_caching'] = $display->options['block_caching'];
          }

          // Now get the view for this display, and determine the id and title.
          $view = views_get_view(db_result(db_query("SELECT name FROM {views_view} WHERE vid=%d", $display->vid)));
          $type = $display->display_plugin == 'mediaplayerpage' ? 'page' : 'block';
          $plugin = views_fetch_plugin_data('display', $type);
          $id = $type . '_1';
          $viewTitle = $display->display_plugin == 'mediaplayerpage' ? 'Page' : 'Block';
          $title = $viewTitle;
          $count = 1;
          while (!empty($view->display[$id])) {
            $id = $type . '_' . ++$count;
            $title = $viewTitle . ' ' . $count;
          }

          // Now create the new block or page using the new system.
          $sql = "INSERT INTO {views_display} (vid, id, display_title, display_plugin, position, display_options) ";
          $sql .= "VALUES ({$display->vid}, '{$id}', '{$title}', '{$type}', 10, '%s')";
          db_query($sql, serialize($new_options));
          $update[] = array(
            'success' => TRUE,
            'query' => check_plain($sql),
          );
        }
      }
    }
  }

  // Now iterate through all of our displays... again.
  foreach ($displays as $vid => $group) {

    // Iterate through all of our display groups... again.
    foreach ($group as $id => $display) {

      // Store the new options.
      $new_options = $display->options;

      // Now check for the mediafront_preset field.
      if ($display->options['fields']) {

        // Iterate through all the fields.
        foreach ($display->options['fields'] as $name => $field) {

          // If this is a media front preset.
          if ($field['table'] == 'mediafront_preset') {

            // Unset this now bogus field.
            unset($new_options['fields'][$name]);

            // Now update this field with the new options.
            $sql = "UPDATE {views_display} SET display_options='%s' WHERE vid={$display->vid} AND id='{$display->id}'";
            db_query($sql, serialize($new_options));
            $update[] = array(
              'success' => TRUE,
              'query' => check_plain($sql),
            );
          }
        }
      }
    }
  }

  // Now delete the old displays.
  $update[] = update_sql("DELETE FROM {views_display} WHERE display_plugin='mediaplayerpage'");
  $update[] = update_sql("DELETE FROM {views_display} WHERE display_plugin='mediaplayer'");

  // Clear the OSM CSS folder..
  osmplayer_clear_css();

  // Now delete the old files we don't need.
  $dir = getcwd() . '/' . drupal_get_path('module', 'mediafront');

  // Delete the old views files.
  file_delete($dir . '/views/mediafront_handler_field_preset.inc');
  file_delete($dir . '/views/mediafront_plugin_display_block.inc');
  file_delete($dir . '/views/mediafront_plugin_display_page.inc');
  return $update;
}