View source
<?php
define('WORKFLOW_ARROW', '→');
function workflow_admin_ui_help($path, $arg) {
switch ($path) {
case 'admin/config/workflow/workflow/edit/%':
return t('You are currently viewing the possible transitions to and from workflow states. The state is shown in the left column; ' . 'the state to be moved to is to the right. For each transition, check the box next to the role(s) that may initiate the transition. ' . 'For example, if only the "production editor" role may move a node from Review state to the Published state, check the box next to ' . '"production editor". The author role is built in and refers to the user who authored the node.');
case 'admin/config/workflow/workflow/add':
return t('To get started, provide a name for your workflow. This name will be used as a label when the workflow status is shown ' . 'during node editing.');
}
}
function workflow_admin_ui_permission() {
return array(
'administer workflow' => array(
'title' => t('Administer workflow'),
'description' => t('Administer workflow configurations.'),
),
'participate in workflow' => array(
'title' => t('Participate in workflow'),
'description' => t('Role is shown on workflow admin pages.'),
),
);
}
function workflow_admin_ui_menu() {
$items['admin/config/workflow/workflow'] = array(
'title' => 'Workflow',
'file' => 'workflow_admin_ui.pages.inc',
'access arguments' => array(
'administer workflow',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'workflow_admin_ui_type_map_form',
),
'description' => 'Allows the creation and assignment of arbitrary workflows to node types.',
);
$items['admin/config/workflow/workflow/%workflow'] = array(
'file' => 'workflow_admin_ui.pages.inc',
'access arguments' => array(
'administer workflow',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'workflow_admin_ui_overview_form',
4,
),
'type' => MENU_CALLBACK,
);
$items['admin/config/workflow/workflow/add'] = array(
'file' => 'workflow_admin_ui.pages.inc',
'access arguments' => array(
'administer workflow',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'workflow_admin_ui_add_form',
),
'type' => MENU_CALLBACK,
);
$items['admin/config/workflow/workflow/edit/%workflow'] = array(
'title' => 'Edit',
'file' => 'workflow_admin_ui.pages.inc',
'access arguments' => array(
'administer workflow',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'workflow_admin_ui_edit_form',
5,
),
'type' => MENU_CALLBACK,
);
$items["admin/config/workflow/workflow/delete/%workflow"] = array(
'title' => 'Delete',
'file' => 'workflow_admin_ui.pages.inc',
'access arguments' => array(
'administer workflow',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'workflow_admin_ui_delete_form',
5,
),
'type' => MENU_CALLBACK,
);
$items["admin/config/workflow/workflow/transitions/%workflow"] = array(
'title' => 'Transitions',
'file' => 'workflow_admin_ui.pages.inc',
'access arguments' => array(
'administer workflow',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'workflow_admin_ui_transitions_form',
5,
),
'type' => MENU_CALLBACK,
);
$items["admin/config/workflow/workflow/perm_summary/%workflow"] = array(
'title' => 'Permission Summary',
'file' => 'workflow_admin_ui.pages.inc',
'access arguments' => array(
'administer workflow',
),
'page callback' => 'workflow_admin_ui_view_permissions_form',
'page arguments' => array(
5,
),
'type' => MENU_CALLBACK,
);
return $items;
}
function workflow_admin_ui_theme() {
return array(
'workflow_admin_ui_transitions_form' => array(
'render element' => 'form',
),
'workflow_admin_ui_edit_form' => array(
'render element' => 'form',
),
'workflow_admin_ui_type_map_form' => array(
'render element' => 'form',
),
'workflow_admin_ui_overview_form' => array(
'render element' => 'form',
),
);
}
function workflow_admin_ui_user_role_insert($role) {
user_role_change_permissions($role->rid, array(
'participate in workflow' => 1,
));
}
function workflow_admin_ui_breadcrumbs($workflow, $extra = NULL) {
$bc = array(
l(t('Home'), '<front>'),
);
$bc[] = l(t('Configuration'), 'admin/config');
$bc[] = l(t('Workflow'), 'admin/config/workflow');
$bc[] = l(t('Workflow'), 'admin/config/workflow/workflow');
$bc[] = l($workflow
->label(), "admin/config/workflow/workflow/{$workflow->wid}");
if ($extra) {
$bc[] = $extra;
}
drupal_set_breadcrumb($bc);
}
function workflow_admin_ui_workflow_operations($op, $workflow = NULL, $state = NULL) {
switch ($op) {
case 'top_actions':
$alt = t('Add a new workflow');
$actions = array(
'add-workflow' => array(
'title' => t('Add workflow'),
'href' => 'admin/config/workflow/workflow/add',
'attributes' => array(
'alt' => $alt,
'title' => $alt,
),
),
);
foreach (Workflow::getWorkflows() as $workflow) {
$name = $workflow
->getName();
$wid = $workflow->wid;
$alt = t('Work with @wf', array(
'@wf' => $name,
));
$actions[drupal_html_class($name)] = array(
'title' => $workflow
->label(),
'href' => "admin/config/workflow/workflow/{$wid}",
'attributes' => array(
'alt' => $alt,
'title' => $alt,
),
);
}
return $actions;
case 'workflow':
$name = $workflow
->getName();
$wid = $workflow->wid;
$actions = array(
'workflow_settings' => array(
'title' => t('Settings'),
'href' => "admin/config/workflow/workflow/edit/{$wid}",
'attributes' => array(
'alt' => t('Edit the @wf settings', array(
'@wf' => $name,
)),
),
),
'workflow_transitions' => array(
'title' => t('Transitions'),
'href' => "admin/config/workflow/workflow/transitions/{$wid}",
'attributes' => array(
'alt' => t('Edit the @wf transitions', array(
'@wf' => $name,
)),
),
),
'workflow_permission_summary' => array(
'title' => t('Summary'),
'href' => "admin/config/workflow/workflow/perm_summary/{$wid}",
'attributes' => array(
'alt' => t('See a summary of the @wf transitions', array(
'@wf' => $name,
)),
),
),
'workflow_overview_delete' => array(
'title' => t('Delete'),
'href' => "admin/config/workflow/workflow/delete/{$wid}",
'attributes' => array(
'alt' => t('Delete the @wf workflow', array(
'@wf' => $name,
)),
),
),
);
foreach ($actions as $name => $link) {
$actions[$name]['attributes']['title'] = $actions[$name]['attributes']['alt'];
}
return $actions;
}
}
function workflow_admin_ui_get_roles() {
static $roles = NULL;
if (!$roles) {
$roles = array(
'author' => 'author',
);
$list = user_roles(FALSE, 'participate in workflow');
foreach ($list as $rid => $name) {
$roles[$rid] = check_plain($name);
}
}
return $roles;
}
function workflow_admin_ui_types_save($form_values) {
workflow_delete_workflow_type_map_all();
$node_types = node_type_get_names();
foreach ($node_types as $type => $name) {
$data = array(
'type' => $type,
'wid' => $form_values[$type]['workflow'],
);
workflow_insert_workflow_type_map($data);
variable_set('workflow_' . $type, array_keys(array_filter($form_values[$type]['placement'])));
if ($form_values[$type]['workflow']) {
_workflow_node_initialize_nodes($type, $name);
}
}
}
function _workflow_node_initialize_nodes($type, $name) {
$query = db_select('node', 'n');
$query
->leftJoin('workflow_node', 'wn', 'wn.nid = n.nid');
$query
->addField('n', 'nid');
$query
->condition('n.type', $type);
$query
->condition('n.status', 1);
$query
->isNull('wn.sid');
$nids = $query
->execute()
->fetchCol();
$how_many = count($nids);
if ($how_many == 0) {
return;
}
$comment = t('Pre-existing content set to initial state.');
$first_state = db_query("SELECT s.sid " . "FROM {workflow_type_map} m " . "INNER JOIN {workflow_states} s ON s.wid = m.wid " . "WHERE m.type = :type AND s.sysid <> :creation " . "ORDER BY s.weight ASC ", array(
':type' => $type,
':creation' => WORKFLOW_CREATION,
))
->fetchField(0);
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
workflow_execute_transition($node, $first_state, $comment, TRUE);
}
return;
drupal_set_message(t('!count @type nodes have been initialized.', array(
'@type' => $name,
'!count' => $how_many,
)));
}
function _workflow_admin_ui_update_configured_transitions($transitions = array()) {
if (!$transitions) {
return;
}
foreach ($transitions as $from => $to_data) {
foreach ($to_data as $to => $role_data) {
foreach ($role_data as $role => $can_do) {
if ($can_do) {
$transition = array(
'sid' => $from,
'target_sid' => $to,
'roles' => $role,
);
workflow_update_workflow_transitions($transition);
}
else {
$roles = array();
if ($transition = workflow_get_workflow_transitions_by_sid_target_sid($from, $to)) {
$roles = explode(',', $transition->roles);
$tid = $transition->tid;
if (($i = array_search($role, $roles)) !== FALSE) {
unset($roles[$i]);
workflow_update_workflow_transitions_roles($tid, $roles);
}
}
}
}
}
}
workflow_delete_workflow_transitions_by_roles('');
}