You are here

function party_hat_update_7004 in Party 7

Same name and namespace in other branches
  1. 8.2 modules/party_hat/party_hat.install \party_hat_update_7004()

Migrate data from {party_hat_data_set_rules} table to hat entity data.

File

modules/party_hat/party_hat.install, line 248
Contains install hooks for the party hat module..

Code

function party_hat_update_7004() {
  $party_hats = db_select('party_hat', 'ph')
    ->fields('ph', array(
    'hid',
    'name',
    'data',
  ))
    ->execute()
    ->fetchAllAssoc('hid');
  foreach ($party_hats as $hid => $hat) {

    // Prepare the hat data to be worked with.
    $hat->data = unserialize($hat->data);
    $results = db_select('party_hat_data_set_rules', 'c')
      ->fields('c', array(
      'data_set',
      'has',
      'multiple',
    ))
      ->condition('hat', $hat->name, '=')
      ->execute()
      ->fetchAllAssoc('data_set', PDO::FETCH_ASSOC);
    foreach ($results as $data_set_name => $data) {

      // Remove the data set name.
      unset($data['data_set']);
      $hat->data['data_sets'][$data_set_name] = $data;
    }

    // Re-serialize the hat data ready to be saved.
    $hat->data = serialize($hat->data);
    db_update('party_hat', 'ph')
      ->fields(array(
      'data' => $hat->data,
    ))
      ->condition('hid', $hid)
      ->execute();
  }
  db_delete('party_hat_data_set_rules');
}