You are here

function brightcove_update_7005 in Brightcove Video Connect 7.5

Same name and namespace in other branches
  1. 7.3 brightcove.install \brightcove_update_7005()
  2. 7.4 brightcove.install \brightcove_update_7005()

Adding a "Display name" column. Move existing "Name" data into that and make "Name" machine-readable.

File

./brightcove.install, line 220
Installation file for Brightcove module.

Code

function brightcove_update_7005() {
  $default_player = variable_get('brightcove_player_pid', '');
  db_add_field('brightcove_player', 'display_name', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // We are not using brightcove_player_load_all() here because that, being
  // based on CTools Export, is using the table schema. But new fields might be
  // added to the table schema in future updates and that will break the
  // execution with an 'Unknown column' fatal error.
  $players = db_query("SELECT pid, name FROM {brightcove_player}")
    ->fetchAll();
  if (!empty($players)) {
    foreach ($players as $player) {

      // Make display name the name, and restrict the name to machine-readable.
      $player->display_name = $player->name;
      $player->name = preg_replace('/_+/', '_', preg_replace('/[^a-z0-9]+/', '_', strtolower($player->name)));

      // Save the new name. We are not using brightcove_player_save() because
      // $player is not a full CTools Export object.
      db_update('brightcove_player')
        ->fields(array(
        'name' => $player->name,
        'display_name' => $player->display_name,
      ))
        ->condition('pid', $player->pid)
        ->execute();

      //
      if ($player->pid == $default_player) {
        $default_player = $player->name;
      }
    }
  }
  variable_set('brightcove_player_default', $default_player);
}