You are here

function workflow_update_7007 in Workflow 7.2

Convert roles to entity-like arrays.

File

./workflow.install, line 652
Install, update and uninstall functions for the workflow module.

Code

function workflow_update_7007() {

  // For this update, do not use the Workflow API, since some table fields are
  // not present yet. Also, do not move to the 'floating' hook_update_N().
  $schema = workflow_schema();

  // Change length from 60 to 255, to create a 'standard' Roles field,
  // like workflow_transitions-roles.
  $table = 'workflows';
  $fields = $schema[$table]['fields'];
  db_change_field($table, 'tab_roles', 'tab_roles', $fields['tab_roles']);

  // Save field workflows-tab_roles in serialized array (using explode for the last time).
  $query = "SELECT * FROM {workflows} w ";
  $result = db_query($query);
  foreach ($result as $record) {

    // Replace role ID 'author' by '-1'.
    // Update workflow->tab_roles to serializable array.
    $roles = $record->tab_roles;

    // Allow reprocessing this hook, by checking if this is an array.
    if (!(strpos($roles, 'a:') === 0)) {
      $roles = str_replace('author', '-1', $roles);
      $record->tab_roles = empty($roles) ? array() : explode(',', $roles);
      $num_updated = db_update('workflows')
        ->fields(array(
        'tab_roles' => serialize($record->tab_roles),
      ))
        ->condition('wid', $record->wid, '=')
        ->execute();
    }
  }

  // Save field workflow_transitions-roles in serialized array (using explode for the last time).
  // Replace role ID 'author' by '-1'.
  $query = "SELECT wt.tid, wt.roles FROM {workflow_transitions} wt";
  $result = db_query($query);
  foreach ($result as $record) {
    $roles = $record->roles;

    // Allow reprocessing this hook, by checking if this is an array.
    if (!(strpos($roles, 'a:') === 0)) {
      $roles = str_replace('author', '-1', $roles);
      $record->roles = empty($roles) ? array() : explode(',', $roles);
      $num_updated = db_update('workflow_transitions')
        ->fields(array(
        'roles' => serialize($record->roles),
      ))
        ->condition('tid', $record->tid, '=')
        ->execute();
    }
  }
}