diff --git workbench_access.module workbench_access.module index 4e4500e..aa2bad3 100644 --- workbench_access.module +++ workbench_access.module @@ -476,15 +476,19 @@ function workbench_access_node_load($nodes, $types) { if (!isset($scheme)) { $scheme = variable_get('workbench_access', 'taxonomy'); } - $result = db_query("SELECT nid, access_id FROM {workbench_access_node} WHERE nid IN (:nid) AND access_scheme = :access_scheme", array(':nid' => array_keys($nodes), ':access_scheme' => $scheme))->fetchAllAssoc('nid'); + $result = db_query("SELECT nid, access_id FROM {workbench_access_node} WHERE nid IN (:nid) AND access_scheme = :access_scheme", array(':nid' => array_keys($nodes), ':access_scheme' => $scheme))->fetchAll(); + $data = array(); + foreach ($result as $obj) { + $data[$obj->nid][$obj->access_id] = $obj->access_id; + } foreach ($nodes as $node) { // Cannot load if the node has not been created yet. if (empty($node->nid)) { continue; } $nodes[$node->nid]->workbench_access = array(); - if (isset($result[$node->nid])) { - $nodes[$node->nid]->workbench_access[$result[$node->nid]->access_id] = $result[$node->nid]->access_id; + if (isset($data[$node->nid])) { + $nodes[$node->nid]->workbench_access = $data[$node->nid]; } } } @@ -497,13 +501,19 @@ function workbench_access_node_insert($node) { if (!isset($node->workbench_access_scheme['access_scheme'])) { return; } + dsm($node); workbench_access_node_delete($node); - $record = array( - 'nid' => $node->nid, - 'access_id' => $node->workbench_access_id, - 'access_scheme' => $node->workbench_access_scheme['access_scheme'], - ); - drupal_write_record('workbench_access_node', $record); + if (empty($node->workbench_access_id)) { + return; + } + foreach ($node->workbench_access_id as $id) { + $record = array( + 'nid' => $node->nid, + 'access_id' => $id, + 'access_scheme' => $node->workbench_access_scheme['access_scheme'], + ); + drupal_write_record('workbench_access_node', $record); + } } /** @@ -522,15 +532,19 @@ function workbench_access_node_view($node, $view_mode, $langcode) { } // Get the current assignment. $access_type = variable_get('workbench_access_scheme', 'taxonomy'); + $names = array(); $access_id = current($node->workbench_access); - $section = workbench_access_load($access_type, $access_id); + foreach ($node->workbench_access as $access_id) { + $section = workbench_access_load($access_type, $access_id); + $names[] = check_plain($section['name']); + } // Check editorial access. $access = node_access('update', $node); if ($access) { - drupal_set_message(t('This content belongs to the %section section. You may edit this content.', array('%section' => $section['name'])), 'status', FALSE); + drupal_set_message(t('This content belongs to the %section section(s). You may edit this content.', array('%section' => implode(', ', $names))), 'status', FALSE); } else { - drupal_set_message(t('This content belongs to the %section section. You are not a member of the %section editorial group.', array('%section' => $section['name'])), 'warning', FALSE); + drupal_set_message(t('This content belongs to the %section section(s). You are not a member of the %section editorial group.', array('%section' => implode(', ', $names))), 'warning', FALSE); } } @@ -1138,12 +1152,12 @@ function workbench_access_form_alter(&$form, $form_state, $form_id) { } // Generate options so we can check for access. $options = workbench_access_options($tree, $active['active']); - // TODO: multi-select - $default = NULL; + $default = array(); +dsm($form['#node']->workbench_access); if (!empty($form['#node']->workbench_access)) { - $default = current(array_keys($form['#node']->workbench_access)); + $default = array_keys($form['#node']->workbench_access); } - +dsm($default); // If there are no options and the 'workbench_access' variable has not been set // then it seems that Workbench Access has not been configured. if (empty($options) && !variable_get('workbench_access', FALSE)) { @@ -1160,20 +1174,27 @@ function workbench_access_form_alter(&$form, $form_state, $form_id) { '#options' => $options, '#required' => TRUE, '#default_value' => $default, + '#multiple' => TRUE, '#description' => t('Select the proper editorial group for this content.'), ); // If the default is set and is not in the user's range, then pass hidden and // display a message. - // TODO: $default might legitimately be zero in some edge cases. - if (!empty($default) && !isset($options[$default]) && isset($active['tree'][$default])) { - $name = check_plain($active['tree'][$default]['name']); - $element['#type'] = 'value'; - $element['#value'] = $element['#default_value']; - $form['workbench_access']['message'] = array( - '#type' => 'item', - '#title' => t('Workbench access'), - '#markup' => t('%title is assigned to the %section editorial group and may not be changed.', array('%title' => $form['#node']->title, '%section' => $name)), - ); + $names = array(); + if (!empty($default)) { + foreach ($default as $item) { + if (isset($active['tree'][$item]['name']) && !isset($options[$item])) { + $names[] = check_plain($active['tree'][$item]['name']); + } + } + if (!empty($names)) { + $element['#type'] = 'value'; + $element['#value'] = $element['#default_value']; + $form['workbench_access']['message'] = array( + '#type' => 'item', + '#title' => t('Workbench access'), + '#markup' => t('%title is assigned to the %section editorial group(s) and may not be changed.', array('%title' => $form['#node']->title, '%section' => implode(', ', $names))), + ); + } } workbench_access_alter_form('node_element', $element, $form_state); $form['workbench_access']['workbench_access_id'] = $element;