1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330 |
- <?php
- /**
- * @file
- * Provides administrative UI for workflow.
- * Why it's own module? Lower code footprint and better performance.
- * Additional creadit to gcassie ( http://drupal.org/user/80260 ) for
- * the initial push to split UI out of core workflow.
- * We're moving workflow in a API direction, so UI and the like - out.
- */
- define('WORKFLOW_ARROW', '→');
- /**
- * Implements hook_help().
- */
- 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.');
- }
- }
- /**
- * Implements hook_permission().
- */
- 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.'),
- ),
- );
- }
- /**
- * Implements hook_menu().
- */
- function workflow_admin_ui_menu() {
- $items['admin/config/workflow/workflow'] = array(
- 'title' => 'Workflow',
- 'access arguments' => array('administer workflow'),
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('workflow_admin_ui_types_form'),
- 'description' => 'Allows the creation and assignment of arbitrary workflows to node types.',
- );
- $items['admin/config/workflow/workflow/%workflow'] = array(
- '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(
- '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',
- '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',
- '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',
- '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',
- 'access arguments' => array('administer workflow'),
- 'page callback' => 'workflow_admin_ui_view_permissions',
- 'page arguments' => array(5),
- 'type' => MENU_CALLBACK,
- );
- return $items;
- }
- /**
- * Implements hook_theme().
- */
- 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_types_form' => array('render element' => 'form'),
- 'workflow_admin_ui_overview_form' => array('render element' => 'form'),
- );
- }
- /**
- * Form builder. Create the form for adding/editing a workflow.
- *
- * @param $name
- * Name of the workflow if editing.
- * @param $add
- * Boolean, if true edit workflow name.
- *
- * @return
- * HTML form.
- */
- function workflow_admin_ui_add_form($form, &$form_state, $name = NULL) {
- $form = array();
- $form['wf_name'] = array(
- '#type' => 'textfield',
- '#title' => t('Workflow Name'),
- '#maxlength' => '254',
- '#required' => TRUE,
- '#default_value' => $name,
- );
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Add Workflow'),
- );
- return $form;
- }
- /**
- * Validate the workflow add form.
- *
- * @see workflow_add_form()
- */
- function workflow_admin_ui_add_form_validate($form, &$form_state) {
- $workflow_name = $form_state['values']['wf_name'];
- // Make sure workflow name is not a duplicate.
- $workflows = array();
- foreach (workflow_get_workflows() as $data) {
- $workflows[$data->wid] = check_plain(t($data->name));
- }
- if (!empty($workflows)) {
- $workflows = array_flip($workflows);
- if (array_key_exists($workflow_name, $workflows)) {
- form_set_error('wf_name', t('A workflow with the name %name already exists. Please enter another name for your new workflow.',
- array('%name' => $workflow_name)));
- }
- }
- }
- /**
- * Submit handler for the workflow add form.
- *
- * @see workflow_add_form()
- */
- function workflow_admin_ui_add_form_submit($form, &$form_state) {
- $workflow_name = $form_state['values']['wf_name'];
- $workflow = array(
- 'name' => $workflow_name,
- 'tab_roles' => '',
- 'options' => serialize(array()),
- );
- workflow_update_workflows($workflow);
- watchdog('workflow', 'Created workflow %name', array('%name' => $workflow->name));
- drupal_set_message(t('The workflow %name was created. You should set the options for your workflow.',
- array('%name' => $workflow->name)), 'status');
- $form_state['wid'] = $workflow->wid;
- $form_state['redirect'] = 'admin/config/workflow/workflow/edit/' . $workflow->wid;
- }
- /**
- * Form builder. Create form for confirmation of workflow deletion.
- *
- * @param $wid
- * The workflow object to delete.
- * @return
- * Form definition array.
- *
- */
- function workflow_admin_ui_delete_form($form, &$form_state, $workflow) {
- // If we don't have a workflow that goes with this, return to the admin pg.
- if ($workflow) {
- // Let's add some breadcrumbs.
- workflow_admin_ui_breadcrumbs($workflow);
- $form['wid'] = array('#type' => 'value', '#value' => $workflow->wid);
- return confirm_form(
- $form,
- t('Are you sure you want to delete %title? All nodes that have a workflow state associated with this workflow will ' .
- 'have those workflow states removed.', array('%title' => $workflow->name)),
- !empty($_GET['destination']) ? $_GET['destination'] : 'admin/config/workflow/workflow',
- t('This action cannot be undone.'),
- t('Delete ' . $workflow->name), // This seems to get check_plain'ed.
- t('Cancel')
- );
- }
- else {
- drupal_goto('admin/config/workflow/workflow');
- }
- }
- /**
- * Submit handler for workflow deletion form.
- *
- * @see workflow_delete_form()
- */
- function workflow_admin_ui_delete_form_submit($form, &$form_state) {
- if ($form_state['values']['confirm'] == 1
- && $workflow = workflow_get_workflows_by_wid($form_state['values']['wid'])) {
- workflow_delete_workflows_by_wid($form_state['values']['wid']);
- watchdog('workflow', 'Deleted workflow %name with all its states', array('%name' => $workflow->name));
- drupal_set_message(t('The workflow %name with all its states was deleted.', array('%name' => $workflow->name)));
- $form_state['redirect'] = 'admin/config/workflow/workflow';
- }
- }
- /**
- * View workflow permissions by role
- *
- * @param $workflow
- * The workflow object.
- */
- function workflow_admin_ui_view_permissions($workflow) {
- // If we don't have a workflow at this point, go back to admin pg.
- if ($workflow) {
- // Place some breadcrumbs.
- workflow_admin_ui_breadcrumbs($workflow);
- $name = $workflow->name;
- $all = array();
- $roles = workflow_admin_ui_get_roles();
- foreach ($roles as $role => $value) {
- $all[$role]['name'] = $value;
- }
- // TODO return to this, this looks similar to actions stuff (transitions) - should be it's own function.
- $result = db_query(
- 'SELECT t.roles, s1.state AS state_name, s2.state AS target_state_name ' .
- 'FROM {workflow_transitions} t ' .
- 'INNER JOIN {workflow_states} s1 ON s1.sid = t.sid ' .
- 'INNER JOIN {workflow_states} s2 ON s2.sid = t.target_sid ' .
- 'WHERE s1.wid = :wid ' .
- 'ORDER BY s1.weight ASC , s1.state ASC , s2.weight ASC , s2.state ASC',
- array(':wid' => $workflow->wid));
- foreach ($result as $data) {
- foreach (explode(',', $data->roles) as $role) {
- $all[$role]['transitions'][] = array(check_plain(t($data->state_name)), WORKFLOW_ARROW, check_plain(t($data->target_state_name)));
- }
- }
- $header = array(t('From'), '', t('To'));
- $output = '';
- // TODO we should theme out the html here.
- foreach ($all as $role => $value) {
- if (!empty($value['name'])) {
- $output .= '<h3>' . t('%role may do these transitions:', array('%role' => $value['name'])) . '</h3>';
- }
- if (!empty($value['transitions'])) {
- $output .= theme('table', array('header' => $header, 'rows' => $value['transitions'])) . '<p></p>';
- }
- else {
- $output .= '<table><tbody><tr class="odd"><td>' . t('None') . '</td><td></tr></tbody></table><p></p>';
- }
- }
- return $output;
- }
- else {
- drupal_goto('admin/config/workflow/workflow');
- }
- }
- /**
- * Theme the workflow permissions view.
- */
- function theme_workflow_admin_ui_view_permissions($variables) {
- $header = $variables['header'];
- $all = $variables['all'];
- $output = '';
- foreach ($all as $role => $value) {
- if (!empty($value['name'])) {
- $output .= '<h3>' . t('%role may do these transitions:', array('%role' => $value['name'])) . '</h3>';
- }
- if (!empty($value['transitions'])) {
- $output .= theme('table', array('header' => $header, 'rows' => $value['transitions'])) . '<p></p>';
- }
- else {
- $output .= '<table><tbody><tr class="odd"><td>' . t('None') . '</td><td></tr></tbody></table><p></p>';
- }
- }
- return $output;
- }
- /**
- * Helper function. Create breadcrumbs.
- *
- * @param $workflow
- * The workflow object.
- * @param $extra (optional)
- * The link to the extra item to add to the end of the breadcrumbs.
- * @return
- * none.
- */
- 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(t($workflow->name), "admin/config/workflow/workflow/$workflow->wid");
- if ($extra) {
- $bc[] = $extra;
- }
- drupal_set_breadcrumb($bc);
- }
- /**
- * Menu callback. Edit a workflow's properties.
- *
- * @param $wid
- * The workflow object..
- * @return
- * HTML form and permissions table.
- */
- function workflow_admin_ui_edit_form($form, $form_state, $workflow = NULL) {
- $form = array();
- // If we don't have a workflow by this point, we need to go back
- // to creating one at admin/config/workflow/workflow/add
- // I think the menu loader won't allow this to happen.
- if (!$workflow) {
- drupal_goto('admin/config/workflow/workflow/add');
- }
- // Create breadcrumbs.
- workflow_admin_ui_breadcrumbs($workflow);
- $form['wid'] = array('#type' => 'value', '#value' => $workflow->wid);
- $form['#workflow'] = $workflow;
- $form['basic'] = array(
- '#type' => 'fieldset',
- '#title' => t('Workflow information'),
- );
- $form['basic']['wf_name'] = array(
- '#type' => 'textfield',
- '#default_value' => $workflow->name,
- '#title' => t('Workflow Name'),
- '#maxlength' => '254',
- '#required' => TRUE,
- );
- $form['basic']['name_as_title'] = array(
- '#type' => 'checkbox',
- '#title' => t('Use the workflow name as the title of the workflow form.'),
- '#default_value' => isset($workflow->options['name_as_title']) ? $workflow->options['name_as_title'] : 0,
- '#description' => t('The workflow section of the editing form is in its own fieldset. Checking the box will add the workflow ' .
- 'name as the title of workflow section of the editing form.'),
- );
- $form['comment'] = array(
- '#type' => 'fieldset',
- '#title' => t('Comment for Workflow Log'),
- );
- $form['comment']['comment_log_node'] = array(
- '#type' => 'checkbox',
- '#title' => t('Show a comment field in the workflow section of the editing form.'),
- '#default_value' => isset($workflow->options['comment_log_node']) ? $workflow->options['comment_log_node'] : 0,
- '#description' => t('On the node editing form, a Comment form can be shown so that the person making the state change can record ' .
- 'reasons for doing so. The comment is then included in the node\'s workflow history.'),
- );
- $form['comment']['comment_log_tab'] = array(
- '#type' => 'checkbox',
- '#title' => t('Show a comment field in the workflow section of the workflow tab form.'),
- '#default_value' => isset($workflow->options['comment_log_tab']) ? $workflow->options['comment_log_tab'] : 0,
- '#description' => t('On the workflow tab, a Comment form can be shown so that the person making the state change can record ' .
- 'reasons for doing so. The comment is then included in the node\'s workflow history.'),
- );
- $form['tab'] = array(
- '#type' => 'fieldset',
- '#title' => t('Workflow tab permissions'),
- '#collapsible' => TRUE,
- '#collapsed' => FALSE,
- );
- $form['tab']['tab_roles'] = array(
- '#type' => 'checkboxes',
- '#options' => workflow_admin_ui_get_roles(),
- '#default_value' => explode(',', $workflow->tab_roles),
- '#description' => t('Select any roles that should have access to the workflow tab on nodes that have a workflow.'),
- );
- $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
- return $form;
- }
- /**
- * Theme the workflow editing form.
- *
- * @see workflow_edit_form()
- */
- function theme_workflow_admin_ui_edit_form($variables) {
- $output = '';
- $form = $variables['form'];
- $wid = $form['wid']['#value'];
- $workflow = $form['#workflow'];
- // If we don't have a workflow here, we need to go back to admin.
- if ($workflow) {
- drupal_set_title(t('Edit workflow %name', array('%name' => $workflow->name)), PASS_THROUGH);
- $output .= drupal_render($form['wf_name']);
- $output .= drupal_render_children($form);
- return $output;
- }
- else {
- drupal_goto('admin/config/workflow/workflow');
- }
- }
- /**
- * Validate the workflow editing form.
- *
- * @see workflow_edit_form()
- */
- function workflow_admin_ui_edit_form_validate($form_id, $form_state) {
- $wid = $form_state['values']['wid'];
- // Make sure workflow name is not a duplicate.
- if (array_key_exists('wf_name', $form_state['values']) /*&& $form_state['values']['wf_name'] != ''*/) {
- $workflow_name = $form_state['values']['wf_name'];
- $workflows = array();
- foreach (workflow_get_workflows() as $data) {
- $workflows[$data->wid] = check_plain(t($data->name));
- }
- $workflows = array_flip($workflows);
- if (array_key_exists($workflow_name, $workflows) && $wid != $workflows[$workflow_name]) {
- form_set_error('wf_name', t('A workflow with the name %name already exists. Please enter another name for this workflow.',
- array('%name' => $workflow_name)));
- }
- }
- }
- /**
- * Submit handler for the workflow editing form.
- *
- * @see workflow_edit_form()
- */
- function workflow_admin_ui_edit_form_submit($form, &$form_state) {
- if (isset($form_state['values']['transitions'])) {
- workflow_update_transitions($form_state['values']['transitions']);
- }
- $options = array(
- 'comment_log_node' => $form_state['values']['comment_log_node'],
- 'comment_log_tab' => $form_state['values']['comment_log_tab'],
- 'name_as_title' => $form_state['values']['name_as_title'],
- );
- $workflow = array(
- 'wid' => $form_state['values']['wid'],
- 'name' => $form_state['values']['wf_name'],
- 'tab_roles' => array_filter($form_state['values']['tab_roles']),
- 'options' => serialize($options),
- );
- workflow_update_workflows($workflow);
- drupal_set_message(t('The workflow was updated.'));
- $form_state['redirect'] = 'admin/config/workflow/workflow/' . $form_state['values']['wid'];
- }
- /**
- * Menu callback. Edit a workflow's transitions.
- *
- * @param $wid
- * The workflow object.
- * @return
- * HTML form and permissions table.
- */
- function workflow_admin_ui_transitions_form($form, $form_state, $workflow) {
- // Make sure we have a workflow.
- if ($workflow) {
- // Create breadcrumbs.
- workflow_admin_ui_breadcrumbs($workflow);
- $form = array();
- $form['wid'] = array('#type' => 'value', '#value' => $workflow->wid);
- $form['#workflow'] = $workflow;
- $form['transitions'] = workflow_admin_ui_transition_grid_form($workflow);
- $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
- return $form;
- }
- }
- /**
- * Validate the workflow editing form.
- *
- * @see workflow_edit_form()
- */
- function workflow_admin_ui_transitions_form_validate($form, $form_state) {
- $wid = $form['wid']['#value'];
- $workflow = $form['#workflow'];
- // Make sure 'author' is checked for (creation) -> [something].
- $creation_id = workflow_get_creation_state_by_wid($wid);
- if (isset($form_state['values']['transitions'][$creation_id]) && is_array($form_state['values']['transitions'][$creation_id])) {
- foreach ($form_state['values']['transitions'][$creation_id] as $to => $roles) {
- if ($roles['author']) {
- $author_has_permission = TRUE;
- break;
- }
- }
- }
- $state_count = db_query('SELECT COUNT(sid) FROM {workflow_states} WHERE wid = :wid', array(':wid' => $wid))->fetchField();
- if (empty($author_has_permission) && $state_count > 1) {
- form_set_error('transitions', t('Please give the author permission to go from %creation to at least one state!',
- array('%creation' => '(creation)')));
- }
- }
- /**
- * Theme the workflow editing form.
- *
- * @see workflow_edit_form()
- */
- function theme_workflow_admin_ui_transitions_form($variables) {
- $output = '';
- $form = $variables['form'];
- $wid = $form['wid']['#value'];
- $workflow = $form['#workflow'];
- // If we don't have a workflow here, we need to go back to admin.
- if ($workflow) {
- drupal_set_title(t('Edit workflow %name transitions', array('%name' => $workflow->name)), PASS_THROUGH);
- $output .= drupal_render($form['wf_name']);
- $states = workflow_get_workflow_states_by_wid($wid, array('status' => 1));
- if ($states) {
- $roles = workflow_admin_ui_get_roles();
- $header = array(array('data' => t('From / To') . ' ' . WORKFLOW_ARROW));
- $rows = array();
- foreach ($states as $state) {
- $state_id = $state->sid;
- $name = $state->state;
- // Don't allow transition TO (creation).
- if ($name != t('(creation)')) {
- $header[] = array('data' => check_plain(t($name)));
- }
- $row = array(array('data' => check_plain(t($name))));
- foreach ($states as $nested_state) {
- $nested_state_id = $nested_state->sid;
- $nested_name = $nested_state->state;
- if ($nested_name == t('(creation)')) {
- // Don't allow transition TO (creation).
- continue;
- }
- if ($nested_state_id != $state_id) {
- // Need to render checkboxes for transition from $state to $nested_state.
- $from = $state_id;
- $to = $nested_state_id;
- $cell = '';
- foreach ($roles as $rid => $role_name) {
- $cell .= drupal_render($form['transitions'][$from][$to][$rid]);
- }
- $row[] = array('data' => $cell);
- }
- else {
- $row[] = array('data' => '');
- }
- }
- $rows[] = $row;
- }
- $output .= theme('table', array('header' => $header, 'rows' => $rows));
- }
- else {
- $output = t('There are no states defined for this workflow.');
- }
- $output .= drupal_render_children($form);
- return $output;
- }
- }
- /**
- * Submit handler for the workflow editing form.
- *
- * @see workflow_edit_form()
- */
- function workflow_admin_ui_transitions_form_submit($form, &$form_state) {
- $workflow = $form['#workflow'];
- if (isset($form_state['values']['transitions'])) {
- workflow_update_transitions($form_state['values']['transitions']);
- }
- drupal_set_message(t('The workflow was updated.'));
- $form_state['redirect'] = 'admin/config/workflow/workflow/' . $form_state['values']['wid'];
- }
- /**
- * Form builder. Build the grid of transitions for defining a workflow.
- *
- * @param int $wid
- * The workflow object.
- */
- function workflow_admin_ui_transition_grid_form($workflow) {
- $form = array('#tree' => TRUE);
- $roles = workflow_admin_ui_get_roles();
- $states = workflow_get_workflow_states_by_wid($workflow->wid, array('status' => 1));
- if (!$states) {
- $form['error'] = array(
- '#type' => 'markup',
- '#value' => t('There are no states defined for this workflow.'),
- );
- return $form;
- }
- foreach ($states as $state1) {
- $state_id = $state1->sid;
- $name = $state1->state;
- foreach ($states as $state2) {
- $nested_state_id = $state2->sid;
- $nested_name = $state2->state;
- if ($nested_name == t('(creation)')) {
- // Don't allow transition TO (creation).
- continue;
- }
- if ($nested_state_id != $state_id) {
- // Need to generate checkboxes for transition from $state to $nested_state.
- $from = $state_id;
- $to = $nested_state_id;
- foreach ($roles as $rid => $role_name) {
- $checked = FALSE;
- if ($transition = workflow_get_workflow_transitions_by_sid_target_sid($from, $to)) {
- $checked = workflow_transition_allowed($transition->tid, $rid);
- }
- $form[$from][$to][$rid] = array(
- '#type' => 'checkbox',
- '#title' => check_plain($role_name),
- '#default_value' => $checked,
- );
- }
- }
- }
- }
- return $form;
- }
- /**
- * Implements hook_workflow_operations().
- * Might as well eat our own cooking.
- */
- function workflow_admin_ui_workflow_operations($op, $workflow = NULL, $state = NULL) {
- switch ($op) {
- case 'top_actions':
- // Build a link to each workflow.
- $workflows = workflow_get_workflows();
- $alt = t('Add a new workflow');
- $actions = array(
- 'add-workflow' => array(
- 'title' => t('Add workflow'),
- // @TODO: It might be more sane to go to the "settings" page.
- 'href' => 'admin/config/workflow/workflow/add',
- 'attributes' => array('alt' => $alt, 'title' => $alt),
- ),
- );
- foreach ($workflows as $workflow) {
- $alt = t('Work with @wf', array('@wf' => $workflow->name));
- $actions[drupal_html_class($workflow->name)] = array(
- 'title' => t($workflow->name),
- 'href' => "admin/config/workflow/workflow/$workflow->wid",
- 'attributes' => array('alt' => $alt, 'title' => $alt),
- );
- }
- return $actions;
- case 'workflow':
- $actions = array(
- 'workflow_settings' => array(
- 'title' => t('Settings'),
- 'href' => "admin/config/workflow/workflow/edit/$workflow->wid",
- 'attributes' => array('alt' => t('Edit the @wf settings', array('@wf' => $workflow->name))),
- ),
- 'workflow_transitions' => array(
- 'title' => t('Transitions'),
- 'href' => "admin/config/workflow/workflow/transitions/$workflow->wid",
- 'attributes' => array('alt' => t('Edit the @wf transitios', array('@wf' => $workflow->name))),
- ),
- 'workflow_permission_summary' => array(
- 'title' => t('Summary'),
- 'href' => "admin/config/workflow/workflow/perm_summary/$workflow->wid",
- 'attributes' => array('alt' => t('See a summary of the @wf transitios', array('@wf' => $workflow->name))),
- ),
- 'workflow_overview_delete' => array(
- 'title' => t('Delete'),
- 'href' => "admin/config/workflow/workflow/delete/$workflow->wid",
- 'attributes' => array('alt' => t('Delete the @wf', array('@wf' => $workflow->name))),
- ),
- );
- foreach ($actions as $name => $link) {
- $actions[$name]['attributes']['title'] = $actions[$name]['attributes']['alt'];
- }
- return $actions;
- }
- }
- /**
- * Menu callback. Create the main workflow page, which gives an overview
- * of workflows and workflow states.
- * Replaced by http://drupal.org/node/1367530.
- */
- function workflow_admin_ui_overview_form($form, $form_state, $workflow) {
- $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');
- drupal_set_breadcrumb($bc);
- $form = array('#tree' => TRUE);
- $form['wid'] = array('#type' => 'value', '#value' => $workflow->wid);
- $form['#wf_name'] = $workflow->name;
- // Get the state objects and make the array keys the same as the sids.
- $states = workflow_get_workflow_states_by_wid($workflow->wid);
- $sids = array();
- foreach ($states as $state) {
- $sids[] = $state->sid;
- }
- $states = array_combine($sids, $states);
- // Save the list in the form for later.
- $form['#states'] = $states;
- // Allow modules to insert their own workflow operations.
- $links = module_invoke_all('workflow_operations', 'workflow', $workflow);
- drupal_set_title(t('Workflow: ') . t($workflow->name));
- $links_args = array(
- 'links' => $links,
- 'attributes' => array('class' => array('inline', 'action-links')),
- );
- $form['action-links'] = array(
- '#type' => 'markup',
- '#markup' => theme('links', $links_args),
- );
- // Build select options for reassigning states.
- // We put a blank state first for validation.
- $state_list = array('' => ' ');
- foreach ($states as $sid => $state) {
- // Leave out the creation state and any inactive states.
- if ($state->sysid == 0 && $state->status == 1) {
- $state_list[$sid] = $state->state;
- }
- }
- // Is this the last state available?
- $form['#last'] = count($state_list) == 2;
- // Dummy object for new state item.
- $states[] = (object) array(
- 'sid' => 0,
- 'state' => '',
- 'status' => 0,
- 'sysid' => 0,
- 'weight' => 99,
- );
- foreach ($states as $state) {
- // Allow modules to insert state operations.
- $state_links = module_invoke_all('workflow_operations', 'state', $workflow, $state);
- $form['states'][$state->sid] = array(
- 'state' => array(
- '#type' => 'textfield',
- '#size' => 30,
- '#maxlength' => 255,
- '#default_value' => $state->state,
- ),
- 'weight' => array(
- '#type' => 'weight',
- '#delta' => 20,
- '#default_value' => $state->weight,
- '#title-display' => 'invisible',
- '#attributes' => array('class' => array('state-weight')),
- ),
- 'status' => array(
- '#type' => 'checkbox',
- '#default_value' => $state->status,
- ),
- // Save the original status for the validation handler.
- 'orig_status' => array(
- '#type' => 'value',
- '#value' => $state->status,
- ),
- 'reassign' => array(
- '#type' => 'select',
- '#options' => $state_list,
- ),
- 'count' => array(
- '#type' => 'value',
- '#value' => count(workflow_get_workflow_node_by_sid($state->sid)),
- ),
- 'ops' => array(
- '#type' => 'markup',
- '#markup' => theme('links', array('links' => $state_links)),
- ),
- 'sysid' => array(
- '#type' => 'value',
- '#value' => 0,
- ),
- );
- // Don't let the creation state change weight or status.
- if ($state->state == '(creation)') {
- $form['states'][$state->sid]['weight']['#value'] = -50;
- $form['states'][$state->sid]['sysid']['#value'] = 1;
- $form['states'][$state->sid]['status']['#disabled'] = TRUE;
- $form['states'][$state->sid]['reassign']['#disabled'] = TRUE;
- }
- }
- $form['instructions'] = array(
- '#type' => 'markup',
- '#markup' => '<p>' . t('You may enter a new state name in the empty space.
- Check the "Active" box to make it effective. You may also drag it to the appropriate position.') . '</p>'
- . '<p>' . t('A state not marked as active will not be shown as available in the workflow settings.') . '</p>'
- . '<p>' . t('If you wish to inactivate a state that has content (i.e. count is not zero),
- then you need to select a state to which to reassign that content.') . '</p>'
- );
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Changes'));
- return $form;
- }
- function theme_workflow_admin_ui_overview_form($variables) {
- $form = $variables['form'];
- $output = '';
- $table_id = 'workflow_admin_ui_overview';
- $table = array(
- 'rows' => array(),
- 'header' => array(
- t('State'),
- t('Weight'),
- t('Active'),
- t('Reassign'),
- t('Count'),
- array('data' => t('Operations'), 'class' => 'state-ops'),
- ),
- 'attributes' => array('id' => $table_id, 'style' => 'width: auto;'),
- );
- // The out put needs to have the action links at the top.
- $output .= drupal_render($form['action-links']);
- // Iterate over each element in our $form['states'] array
- foreach (element_children($form['states']) as $id) {
- // We are now ready to add each element of our $form data to the rows
- // array, so that they end up as individual table cells when rendered
- // in the final table. We run each element through the drupal_render()
- // function to generate the final html markup for that element.
- $table['rows'][] = array(
- 'data' => array(
- // Add our 'name' column
- array('data' => drupal_render($form['states'][$id]['state']), 'class' => 'state-name'),
- // Add our 'weight' column
- drupal_render($form['states'][$id]['weight']),
- // Add our 'status' column
- array('data' => drupal_render($form['states'][$id]['status']), 'class' => 'state-status'),
- // Add our 'reassign' column
- array('data' => drupal_render($form['states'][$id]['reassign']), 'class' => 'state-reassign'),
- // Add our 'count' column
- array('data' => $form['states'][$id]['count']['#value'], 'class' => 'state-count'),
- // Add our 'operations' column
- array('data' => drupal_render($form['states'][$id]['ops']), 'class' => 'state-ops'),
- // Add our 'sysid' column
- drupal_render($form['states'][$id]['sysid']),
- ),
- // To support the tabledrag behaviour, we need to assign each row of the
- // table a class attribute of 'draggable'. This will add the 'draggable'
- // class to the <tr> element for that row when the final table is
- // rendered.
- 'class' => array('draggable'),
- );
- }
- $output .= theme('table', $table);
- // And then render any remaining form elements (such as our submit button)
- $output .= drupal_render_children($form);
- // We now call the drupal_add_tabledrag() function in order to add the
- // tabledrag.js goodness onto our page.
- //
- // For a basic sortable table, we need to pass it:
- // - the $table_id of our <table> element,
- // - the $action to be performed on our form items ('order'),
- // - a string describing where $action should be applied ('siblings'),
- // - and the class of the element containing our 'weight' element.
- drupal_add_tabledrag($table_id, 'order', 'sibling', 'state-weight');
- return $output;
- }
- /**
- * Validation handler for the state form.
- */
- function workflow_admin_ui_overview_form_validate($form, &$form_state) {
- // Get the workflow id.
- $wid = $form_state['values']['wid'];
- // Get the state objects.
- $states = $form['#states'];
- // Because the form elements were keyed with the item ids from the database,
- // we can simply iterate through the submitted values.
- foreach ($form_state['values']['states'] as $sid => $item) {
- // Do they want to deactivate the state?
- if (($item['status'] != $item['orig_status']) && ($item['status'] == 0)) {
- // Does that state have nodes in it?
- if ($item['count'] > 0 && empty($item['reassign'])) {
- if ($form['#last']) {
- drupal_set_message(t('Since you are deleting the last available workflow state
- in this workflow, all content items which are in that state will have their
- workflow state removed.'), 'warning');
- }
- else {
- form_set_error("states'][$sid]['reassign'",
- t('The %state state has content; you must reassign the content to another state.',
- array('%state' => $states[$sid]->state)));
- }
- }
- }
- }
- }
- /**
- * Submission handler for the state form.
- */
- function workflow_admin_ui_overview_form_submit($form, &$form_state) {
- // Get the workflow id, then save it for the next round.
- $wid = $form_state['values']['wid'];
- $wf_name = $form['#wf_name'];
- // Get the state objects.
- $states = $form['#states'];
- // Because the form elements were keyed with the item ids from the database,
- // we can simply iterate through the submitted values.
- foreach ($form_state['values']['states'] as $id => $item) {
- $item['sid'] = $id;
- $item['wid'] = $wid;
- // Is there not a new state name?
- if (empty($item['state'])) {
- // No new state entered, so skip it.
- continue;
- }
- // Is this a new state?
- if ($id == 0) {
- // Remove the key for the updating.
- unset($item['sid']);
- }
- // Do they want to deactivate the state?
- if ($item['status'] == 0) {
- // Does that state have nodes in it?
- if ($item['count'] > 0) {
- if ($form['#last']) {
- $new_sid = NULL;
- drupal_set_message(t('Removing workflow states from content in the %workflow.',
- array('%workflow' => $wf_name)));
- }
- else {
- // Prepare the state delete function.
- $new_sid = $item['reassign'];
- drupal_set_message(t('Reassigning content from %old_state to %new_state.',
- array('%old_state' => $states[$id]->state, '%new_state' => $states[$new_sid]->state)));
- }
- // Delete the old state without orphaning nodes, move them to the new state.
- workflow_delete_workflow_states_by_sid($id, $new_sid);
- $args = array(
- '%state' => $states[$id]->state,
- '%workflow' => $wf_name,
- );
- watchdog('workflow', 'Deactivated workflow state %state in %workflow.', $args);
- drupal_set_message(t('The workflow state %state was deleted from %workflow.', $args));
- }
- }
- // Call the update function.
- workflow_update_workflow_states($item);
- }
- $form_state['redirect'] = "admin/config/workflow/workflow/$wid";
- }
- /**
- * Form builder. Allow administrator to map workflows to content types
- * and determine placement.
- */
- function workflow_admin_ui_types_form($form) {
- $form = array();
- // Allow modules to insert their own action links.
- $links = module_invoke_all('workflow_operations', 'top_actions');
- $links_args = array(
- 'links' => $links,
- 'attributes' => array('class' => array('inline', 'action-links')),
- );
- $form['action-links'] = array(
- '#type' => 'markup',
- '#markup' => theme('links', $links_args),
- );
- $these_workflows = array();
- foreach (workflow_get_workflows() as $data) {
- // Don't allow workflows with no states.
- $states = workflow_get_workflow_states_by_wid($data->wid, array('status' => 1));
- // There should always be a creation state.
- if (count($states) == 1) {
- // That's all, so let's remind them to create some states.
- drupal_set_message(t('%workflow has no states defined, so it cannot be assigned to content yet.',
- array('%workflow' => ucwords($data->name))), 'warning');
- // Skip allowing this workflow.
- continue;
- }
- // Also check for transitions at least out of the creation state.
- // This always gets at least the "from" state.
- $transitions = workflow_allowable_transitions($data->creation_state, 'to');
- if (count($transitions) == 1) {
- // That's all, so let's remind them to create some transitions.
- drupal_set_message(t('%workflow has no transitions defined, so it cannot be assigned to content yet.',
- array('%workflow' => ucwords($data->name))), 'warning');
- // Skip allowing this workflow.
- continue;
- }
- // Add this workflow to the allowed list.
- $these_workflows[$data->wid] = t($data->name);
- }
- $workflows = array(t('None')) + $these_workflows;
- if (count($workflows) == 1) {
- drupal_set_message(t('You must create at least one workflow before content can be assigned to a workflow.'));
- return $form;
- }
- $type_map = workflow_get_workflow_type_map();
- $form['#tree'] = TRUE;
- $form['help'] = array(
- '#type' => 'item',
- '#title' => '<h3>' . t('Content Type Mapping') . '</h3>',
- '#markup' => t('Each content type may have a separate workflow. The form for changing workflow state can be '
- . 'displayed when editing a node, editing a comment for a node, or both.'),
- );
- $placement_options = array(
- 'node' => t('Post'),
- 'comment' => t('Comment'),
- );
- foreach (node_type_get_names() as $type => $name) {
- $form[$type]['workflow'] = array(
- '#type' => 'select',
- '#options' => $workflows,
- '#default_value' => isset($type_map[$type]) ? $type_map[$type] : 0,
- );
- $form[$type]['placement'] = array(
- '#type' => 'checkboxes',
- '#options' => $placement_options,
- '#default_value' => variable_get('workflow_' . $type, array()),
- );
- }
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Save workflow mapping'),
- );
- return $form;
- }
- /**
- * Theme the workflow type mapping form.
- */
- function theme_workflow_admin_ui_types_form($variables) {
- $output = '';
- $form = $variables['form'];
- // Do the action links at the top.
- $output .= drupal_render($form['action-links']);
- $header = array(t('Content Type'), t('Workflow'), t('Display Workflow Form on:'));
- $rows = array();
- foreach (node_type_get_names() as $type => $name) {
- $rows[] = array(
- check_plain(t($name)),
- drupal_render($form[$type]['workflow']),
- drupal_render($form[$type]['placement']),
- );
- }
- $output .= drupal_render($form['help']);
- $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('style' => 'width: auto;')));
- return $output . drupal_render_children($form);
- }
- /**
- * Submit handler for workflow type mapping form.
- *
- * @see workflow_types_form()
- */
- function workflow_admin_ui_types_form_submit($form, &$form_state) {
- workflow_admin_ui_types_save($form_state['values']);
- drupal_set_message(t('The workflow mapping was saved.'));
- }
- /**
- * Get a list of roles.
- *
- * @return
- * Array of role names keyed by role ID, including the 'author' role.
- */
- 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;
- }
- /**
- * Save mapping of workflow to node type. E.g., the story node type is using the Foo workflow.
- *
- * @param $form_state['values']
- */
- function workflow_admin_ui_types_save($form_values) {
- // Empty the table so that types no longer under workflow go away.
- 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 this type uses workflow, make sure pre-existing nodes are set
- // to the workflow's creation state.
- if ($form_values[$type]['workflow']) {
- workflow_initialize_nodes($type, $name);
- }
- }
- }
- /**
- * Initialize all pre-existing nodes of a type to their first state..
- *
- * @param $type - node type
- * @param $name - node type name
- */
- function workflow_initialize_nodes($type, $name) {
- // Build the select query.
- // We want all published nodes of this type that don't already have a workflow state.
- $query = db_select('node', 'n');
- $query->leftJoin('workflow_node', 'wn', 'wn.nid = n.nid');
- // Add the fields.
- $query->addField('n', 'nid');
- // Add conditions.
- $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.');
- // Get the initial state for this content type.
- $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);
- // Load them all up.
- $nodes = node_load_multiple($nids);
- foreach ($nodes as $node) {
- // Force it to transition to the first state and get a history record.
- 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)));
- }
- /**
- * Update the transitions for a workflow.
- *
- * @param array $transitions from values.
- * Transitions, for example:
- * 18 => array(
- * 20 => array(
- * 'author' => 1,
- * 1 => 0,
- * 2 => 1,
- * )
- * )
- * means the transition from state 18 to state 20 can be executed by
- * the node author or a user in role 2. The $transitions array should
- * contain ALL transitions for the workflow.
- */
- function workflow_update_transitions($transitions = array()) {
- // Empty string is sometimes passed in instead of an 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('');
- }
|