drupal_module__wing/wing.module

286 lines
7.7 KiB
Text

<?php
/**
* @file
* Workflow - New Generation
*
* - States are entities
* - Worfklows are the bundle entities of states
* - Transitions are fields on states, storing a (rid, sid) pair per item
* - Any entity type can be subject to a workflow: association is represented by
* an entityreference field whose widget uses a specific view to list the
* relevant states
* - Workflow access is handled by field access using the permission associated
* with a state, one "view" or "edit" permission per state entity
*
* URL structure admin/structure/wing/...
* /: dashboard
* /settings: module settings
* /add: workflow creation form
* /workflow[/list]: list of workflows
* /workflow/add: state creation form
* /workflow/<wname>[/info]: info on workflow with list of states
* /workflow/<wname>/edit|clone|delete|export: workflow operations form
* /workflow/<wname>/fields: field UI for workflow as state bundle
* /workflow/<wname>/fields: view modes for workflow as state bundle
* /state[/list]: list of states
* /state/<sname>[/info]: info on state, with list of previous/next states
* and list of entities in state
* /state/<sname>/edit|clone|delete|export
* state operations form. Main point of interest is the transitions subform
*/
/**
* Implements hook_entity_info().
*
* Workflow is the bundle entity for state, but this concept is obsolete in D8,
* so we ignore it.
*/
function wing_entity_info() {
$defaults = array(
'entity keys' => array(
'label' => 'title',
'module' => 'module',
'name' => 'machine_name',
'status' => 'status',
),
'exportable' => TRUE,
'fieldable' => FALSE,
'label callback' => 'entity_class_label',
'module' => 'wing',
'uri callback' => 'entity_class_uri',
'view modes' => array(
'full' => array(
'label' => t('Full'),
'custom settings' => FALSE,
),
'summary' => array(
'label' => t('Summary'),
'custom settings' => FALSE,
),
'simple' => array(
'label' => t('Simple'),
'custom settings' => FALSE,
),
),
);
$ret = array();
$type = 'wing_workflow';
$class = 'WingWorkflow';
$ret[$type] = array_merge_recursive(array(
'access callback' => "{$type}_access",
'base table' => $type,
'controller class' => "{$class}Controller",
'entity class' => $class,
'entity keys' => array(
'id' => 'wid',
),
'export' => array(
'default hook' => "default_{$type}s",
),
'features controller class' => "{$class}FeaturesController",
'label' => t('Wing: Workflow'),
'load hook' => "{$type}_load", // Enable hook_wing_workflow_load().
'views controller class' => "{$class}ViewsController",
), $defaults);
$type = 'wing_state';
$class = 'WingState';
$ret[$type] = array_merge_recursive(array(
'access callback' => '{$type}_access',
'base table' => $type,
'bundle keys' => array('bundle' => 'workflow'),
'bundles' => array(),
'controller class' => "{$class}Controller",
'entity class' => $class,
'entity keys' => array(
'id' => 'sid',
'bundle' => 'workflow',
),
'export' => array(
'default_hook' => "default_{$type}s",
),
'features controller class' => "{$class}FeaturesController",
'label' => t('Wing: State'),
'load hook' => "{$type}_load", // Enable hook_wing_state_load().
'views controller class' => "{$class}ViewsController",
), $defaults);
// Add bundle info but bypass entity_load() as we cannot use it here.
$workflows = db_select('wing_workflow', 'ww')
->fields('ww')
->execute()
->fetchAllAssoc('machine_name');
foreach ($workflows as $workflow_name => $workflow) {
$ret[$type]['bundles'][$workflow_name] = array(
'label' => $workflow->title,
// 'admin' => array(
// 'path' => 'admin/content/wing/workflow/%entity_object',
// 'real path' => 'admin/content/wing/workflow/' . $workflow_name,
// 'bundle argument' => 4,
// 'access arguments' => array('administer wing'),
// ),
);
}
return $ret;
}
/**
* Implements hook_entity_info_alter().
*
* Sort initial Wing entity info keys for easier debugging.
*/
function wing_entity_info_alter(&$info) {
ksort($info['wing_state']);
ksort($info['wing_workflow']);
}
/**
* Implements hook_field_info().
*
* Define "wing_transition" as a UI-less field.
*/
function wing_field_info() {
$ret = array(
'wing_transition' => array(
'label' => t('Wing: Transition'),
'description' => t('Store access from one workflow state to another.'),
'settings' => array(),
'instance_settings' => array(),
'default_widget' => 'text_textfield',
'default_formatter' => 'text_default',
// 'default_widget' => 'wing_transition_widget',
// 'default_formatter' => 'wing_transition_default',
// 'no_ui' => FALSE,
),
);
dsm($ret, __FUNCTION__);
return $ret;
}
/**
* Implements hook_field_widget_info().
*/
function wing_field_widget_info() {
$ret = array(
'text_textfield' => array(
'label' => t('Text field'),
'field types' => array('text'),
'settings' => array('size' => 60),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
);
return $ret;
}
/**
* Implements hook_field_widget_info_alter().
*/
function wing_field_widget_info_alter(&$info) {
if (module_exists('text')) {
$info['text_textfield']['field types'][] = 'wing_transition';
}
}
/**
* Implements hook_menu().
*/
function wing_menu() {
$items = array();
$items['admin/content/wing'] = array(
'title' => 'Wing',
'access arguments' => array('administer wing'),
'page callback' => 'wing_overview',
'file' => 'wing.pages.inc',
);
$items['admin/content/wing/info'] = array(
'title' => 'Info',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/content/wing/settings'] = array(
'title' => 'Settings',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array('wing_settings_form'),
'file' => 'wing.admin.inc',
'access arguments' => array('administer wing'),
'weight' => 9,
);
return $items;
}
/**
* Implements hook_permission().
*/
function wing_permission() {
$ret = array(
'administer wing' => array(
'title' => t('Administer Wing'),
'description' => t('Perform administration tasks for Wing.'),
),
);
return $ret;
}
/**
* Entity access callback for wing_state.
*
* @param string $op
* 'create', 'update', 'delete' or 'view'
* @param WingState $entity
* @param stdClass $account
* @param string $entity_type
*
* @return boolean
*/
function wing_state_access($op, $entity, $account, $entity_type) {
// dsm(get_defined_vars(), __FUNCTION__);
return TRUE;
}
/**
* Implements hook_view_api().
*/
function wing_views_api() {
$path = drupal_get_path('module', 'wing');
$ret = array(
'api' => 3,
'path' => "$path/views",
'template path' => "$path/themes",
);
return $ret;
}
/**
* Entity access callback for wing_workflow.
*
* @param string $op
* 'create', 'update', 'delete' or 'view'
* @param WingWorkflow $entity
* @param stdClass $account
* @param string $entity_type
*
* @return boolean
*/
function wing_workflow_access($op, $entity, $account, $entity_type) {
$s_entity = isset($entity) ? "$entity" : 'NULL';
$s_account = isset($account) ? $account->uid : "'<anonymous>'";
// dsm(__FUNCTION__ . "('$op', $s_entity, $s_account, '$entity_type')");
return TRUE;
}
function wing_workflow_exists($machine_name) {
$ret = entity_load_multiple_by_name('wing_workflow', array($machine_name));
dsm($ret, __FUNCTION__);
return $ret;
}