You are here

function workbench_moderation_update_7006 in Workbench Moderation 7

Same name and namespace in other branches
  1. 7.3 workbench_moderation.install \workbench_moderation_update_7006()

Add machine names to moderation states.

File

./workbench_moderation.install, line 476
Install file for the Workbench Moderation module.

Code

function workbench_moderation_update_7006() {

  // Drop the unique key on name which will be converted to label.
  db_drop_unique_key('workbench_moderation_states', 'name');

  // Add the new field.
  $spec = array(
    'description' => 'A label for the moderation state.',
    'type' => 'varchar',
    'length' => '255',
    'not null' => FALSE,
  );
  db_add_field('workbench_moderation_states', 'label', $spec);

  // Transform names into machine_names, saving the original name into the new
  // 'label' field.
  $states = db_select('workbench_moderation_states', 'states')
    ->fields('states', array(
    'name',
    'label',
    'description',
    'weight',
  ))
    ->orderBy('states.weight', 'ASC')
    ->execute()
    ->fetchAllAssoc('name');
  foreach ($states as $state) {
    $machine_name = preg_replace('/[^a-z]+/', '_', strtolower($state->name));
    db_update('workbench_moderation_states')
      ->fields(array(
      'name' => $machine_name,
      'label' => $state->name,
    ))
      ->condition('name', $state->name)
      ->execute();
    db_update('workbench_moderation_transitions')
      ->fields(array(
      'from_name' => $machine_name,
    ))
      ->condition('from_name', $state->name)
      ->execute();
    db_update('workbench_moderation_transitions')
      ->fields(array(
      'to_name' => $machine_name,
    ))
      ->condition('to_name', $state->name)
      ->execute();
    db_update('workbench_moderation_node_history')
      ->fields(array(
      'from_state' => $machine_name,
    ))
      ->condition('from_state', $state->name)
      ->execute();
    db_update('workbench_moderation_node_history')
      ->fields(array(
      'state' => $machine_name,
    ))
      ->condition('state', $state->name)
      ->execute();
  }

  // Add a primary key on {workbench_moderation_states}.
  db_add_primary_key('workbench_moderation_states', array(
    'name',
  ));

  // Get content types where moderation is enabled and update
  // 'workbench_moderation_default_state_TYPE_NAME' variables
  $entity_info = entity_get_info('node');
  foreach (array_keys($entity_info['bundles']) as $content_type) {
    $options = variable_get("node_options_{$content_type}", array());
    if (in_array('revision', $options) && in_array('moderation', $options)) {
      $orig_name = variable_get('workbench_moderation_default_state_' . $content_type, 'Draft');
      $machine_name = preg_replace('/[^a-z]+/', '_', strtolower($orig_name));
      variable_set('workbench_moderation_default_state_' . $content_type, $machine_name);
    }
  }

  // Update transition permissions with new permission strings
  // Normally we would use workbench_moderation_states(), but we can't guarantee that workbench_moderation is enabled.
  $states = db_select('workbench_moderation_states', 'states')
    ->fields('states', array(
    'name',
    'label',
    'description',
    'weight',
  ))
    ->orderBy('states.weight', 'ASC')
    ->execute()
    ->fetchAllAssoc('name');

  // Normally we would use workbench_moderation_transitions(), but we can't guarantee that workbench_moderation is enabled.
  $query = db_select('workbench_moderation_transitions', 't')
    ->fields('t', array(
    'from_name',
    'to_name',
  ));
  $alias_from = $query
    ->addJoin('INNER', 'workbench_moderation_states', NULL, 't.from_name = %alias.name');
  $alias_to = $query
    ->addJoin('INNER', 'workbench_moderation_states', NULL, 't.to_name = %alias.name');
  $query
    ->orderBy("{$alias_from}.weight", 'ASC')
    ->orderBy("{$alias_to}.weight", 'ASC');
  $transitions = $query
    ->execute()
    ->fetchAll();
  foreach ($transitions as $transition) {
    $old_perm = "moderate content from {$states[$transition->from_name]->label} to {$states[$transition->to_name]->label}";
    $new_perm = "moderate content from {$transition->from_name} to {$transition->to_name}";
    db_update('role_permission')
      ->fields(array(
      'permission' => $new_perm,
    ))
      ->condition('permission', $old_perm)
      ->execute();
  }
  return t("Added column to store moderation state labels.");
}