You are here

workbench_workflows.install in Workbench Moderation 7.2

Database functions for workbench_workflows.

File

modules/workbench_workflows/workbench_workflows.install
View source
<?php

/**
 * @file
 * Database functions for workbench_workflows.
 */

/**
 * Implements hook_schema().
 */
function workbench_workflows_schema() {
  $schema = array();

  // Define the states schema.
  $schema["workbench_workflows_states"] = workbench_workflows_starter_schema('state');

  // Add fields to the states schema.
  $additional_state_fields = array(
    'entity_state_change' => array(
      'type' => 'int',
      'length' => 'small',
      'description' => 'Defines the state change on the entity level',
      'object default' => -1,
    ),
  );
  $schema['workbench_workflows_states']['fields'] = array_merge($schema['workbench_workflows_states']['fields'], $additional_state_fields);

  // Define the events schema.
  $schema["workbench_workflows_events"] = workbench_workflows_starter_schema('event');

  // Add fields to the events schema.
  $additional_event_fields = array(
    'target_state' => array(
      'type' => 'varchar',
      'length' => '255',
      'description' => 'target state',
    ),
    'origin_states' => array(
      'type' => 'text',
      'size' => 'big',
      'description' => 'Origin states',
      'serialize' => TRUE,
      'object default' => array(),
    ),
    'schedulable' => array(
      'type' => 'int',
      'size' => 'tiny',
      'description' => 'This state can be scheduled with state flow scheduler',
      'object default' => FALSE,
    ),
  );
  $schema["workbench_workflows_events"]['fields'] = array_merge($schema["workbench_workflows_events"]['fields'], $additional_event_fields);

  // Define the workflows schema
  $schema["workbench_workflows_workflows"] = workbench_workflows_starter_schema('workflow');
  $additional_workflow_fields = array(
    'category' => array(
      'type' => 'varchar',
      'length' => '64',
      'description' => 'The category this workbench workflow appears in.',
    ),
    'states' => array(
      'type' => 'text',
      'size' => 'big',
      'serialize' => TRUE,
      'object default' => array(),
      'description' => 'An array of states allowed in the workflow',
    ),
    'default_state' => array(
      'type' => 'text',
      'size' => 'big',
      'serialize' => FALSE,
      'object default' => '',
      'description' => 'The starter state of the workflow.',
    ),
    'events' => array(
      'type' => 'text',
      'size' => 'big',
      'serialize' => TRUE,
      'object default' => array(),
      'description' => 'An array of events allowed in the workflow',
    ),
  );
  $schema["workbench_workflows_workflows"]['fields'] = array_merge($schema["workbench_workflows_workflows"]['fields'], $additional_workflow_fields);
  return $schema;
}

/**
 * Helper function for defining schema.
 */
function workbench_workflows_starter_schema($type) {
  $plural = $type . 's';
  $schema = array(
    "description" => "Contains exportable customized {$plural}.",
    "export" => array(
      "identifier" => "{$type}",
      "bulk export" => TRUE,
      "primary key" => $type . "id",
      "api" => array(
        "owner" => "workbench_workflows",
        "api" => "workbench_workflows",
        "minimum_version" => 1,
        "current_version" => 1,
      ),
    ),
    "fields" => array(
      $type . "id" => array(
        "type" => "serial",
        "description" => "A database primary key to ensure uniqueness",
        "not null" => TRUE,
        "no export" => TRUE,
      ),
      "name" => array(
        "type" => "varchar",
        "length" => "255",
        "description" => "Unique ID for this {$type}. Used to identify it programmatically.",
      ),
      "editor_title" => array(
        "type" => "varchar",
        "length" => "255",
        "description" => "Administrative title for this {$type}.",
      ),
      "admin_title" => array(
        "type" => "varchar",
        "length" => "255",
        "description" => "Administrative title for this {$type}.",
      ),
      "admin_description" => array(
        "type" => "text",
        "size" => "big",
        "description" => "Administrative description for this {$type}.",
        "object default" => "",
      ),
      "requiredcontexts" => array(
        "type" => "text",
        "size" => "big",
        "description" => "Any required contexts for this {$type}.",
        "serialize" => TRUE,
        "object default" => array(),
      ),
      "contexts" => array(
        "type" => "text",
        "size" => "big",
        "description" => "Any embedded contexts for this {$type}.",
        "serialize" => TRUE,
        "object default" => array(),
      ),
      "relationships" => array(
        "type" => "text",
        "size" => "big",
        "description" => "Any relationships for this {$type}.",
        "serialize" => TRUE,
        "object default" => array(),
      ),
      "access" => array(
        "type" => "text",
        "size" => "big",
        "description" => "The actual group of access plugins for this {$type}.",
        "serialize" => TRUE,
        "object default" => array(),
      ),
      'weight' => array(
        'type' => 'int',
        'description' => 'Weight',
      ),
    ),
    "primary key" => array(
      $type . "id",
    ),
    'unique keys' => array(
      'name' => array(
        'name',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_requirements().
 */
function workbench_workflows_requirements($phase) {
  $requirements = array();
  if ($phase == 'runtime') {
    $states = workbench_workflows_load_all('states');
    $events = workbench_workflows_load_all('events');
    $workflows = workbench_workflows_load_all('workflows');
    if (empty($states) && empty($events) && empty($workflows)) {
      $form = drupal_get_form('workbench_workflows_install_default_workflow_form');
      $requirements['workbench_workflows_default'] = array(
        'title' => t('Workbench Workflows default workflow'),
        'value' => t('Install the default workflow'),
        'description' => drupal_render($form),
        'severity' => REQUIREMENT_WARNING,
      );
    }
  }
  return $requirements;
}

/**
 * Provides a form to install the default workflow.
 */
function workbench_workflows_install_default_workflow_form($form, &$form_state) {
  $form = array();

  // @todo Would it be overkill to do a theme function for this form so that <p> tags
  // are not added in a normal function?
  $form['message'] = array(
    '#markup' => '<p>' . t('There are no states, events, or workflows yet on this site. Would you like to install a default configuration?') . '</p>',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Install the default workflow'),
  );
  return $form;
}

/**
 * Actually installs the default configuration.
 *
 * submit handler for workbench_workflows_install_default_workflow_form().
 */
function workbench_workflows_install_default_workflow_form_submit($form, &$form_state) {
  module_load_include('inc', 'workbench_workflows', 'includes/workbench_workflows.starter');
  workbench_workflows_import_starter_exportables();

  // Notify the user that the default workflow is installed.
  drupal_set_message(t('A default workflow with the states and events of Draft, Needs Review, and Published.'));
}

/**
 * Helper to do db updates.
 *
 * Supported actions based on the schema hook:
 * - Add new tables
 * - Add new fields
 * - Drop tables
 *
 * @param string $table
 *   The name of the table to update.
 */
function workbench_workflows_db_update($table) {
  $schema = workbench_workflows_schema();

  // If schema contains requested table handle it, else drop it.
  if (isset($schema[$table])) {

    // If table exists check existing items, else create table.
    if (db_table_exists($table)) {
      if (isset($schema[$table]['fields'])) {
        foreach ($schema[$table]['fields'] as $field => $field_schema) {
          if (!db_field_exists($table, $field)) {
            db_add_field($table, $field, $field_schema);
          }
        }
      }
    }
    else {
      db_create_table($table, $schema[$table]);
    }
  }
  elseif (db_table_exists($table)) {
    db_drop_table($table);
  }
}

/**
 * Add new fields for state and event plugin to support event scheduling.
 */
function workbench_workflows_update_7000() {
  workbench_workflows_db_update('workbench_workflows_states');
}

/**
 * Add new fields for workflow to have default state per workflows.
 */
function workbench_workflows_update_7001() {
  workbench_workflows_db_update('workbench_workflows_workflows');
}

Functions

Namesort descending Description
workbench_workflows_db_update Helper to do db updates.
workbench_workflows_install_default_workflow_form Provides a form to install the default workflow.
workbench_workflows_install_default_workflow_form_submit Actually installs the default configuration.
workbench_workflows_requirements Implements hook_requirements().
workbench_workflows_schema Implements hook_schema().
workbench_workflows_starter_schema Helper function for defining schema.
workbench_workflows_update_7000 Add new fields for state and event plugin to support event scheduling.
workbench_workflows_update_7001 Add new fields for workflow to have default state per workflows.