diff --git a/workbench_access.module b/workbench_access.module index 5632ab6..cacf5a6 100644 --- a/workbench_access.module +++ b/workbench_access.module @@ -2153,3 +2153,195 @@ function workbench_access_update_records($find, $replace, $scheme = NULL) { variable_set('workbench_access_' . $scheme, $new); drupal_set_message(t('Editorial sections have been updated.')); } +/** + * Implements hook_action_info(). + */ +function workbench_access_action_info() { + $info = array(); + + $info['workbench_access_assign_section_presave_action'] = array( + 'type' => 'node', + 'label' => t("Assign Section (Presave)"), + 'configurable' => TRUE, + 'behavior' => array('changes_property'), + 'triggers' => array('node_presave'), + ); + + $info['workbench_access_unassign_section_presave_action'] = array( + 'type' => 'node', + 'label' => t("Unassign Section (Presave)"), + 'configurable' => TRUE, + 'behavior' => array('changes_property'), + 'triggers' => array('node_presave'), + ); + + if (module_exists('workbench_moderation')){ + // Get all workbench transitions. + $transitions = workbench_moderation_transitions(); + + // Add a trigger for each transition. + foreach ($transitions as $transition_definition) { + $transition_string = 'wmt_' . $transition_definition->from_name . '__' . $transition_definition->to_name; + // Hash this string if it's longer than the db field size + if (strlen($transition_string) > 32) { + $transition_string = md5($transition_string); + } + + $info['workbench_access_assign_section_presave_action']['triggers'][] = $transition_string; + $info['workbench_access_unassign_section_presave_action']['triggers'][] = $transition_string; + } + } + + return $info; +} + +/** + * Define the section(s) to apply to the specified node(s). + * If multiple values allowed, the select box will allow multiple values to + * be selected and a "Add to existing sections" check box is added. + * Selection box for sections will be expanded up to 'workbench_access_max_select_size' + * variable value (or 10 if not defined) before slider shows up. + */ +function workbench_access_assign_section_presave_action_form($context) { + $form = array(); + $options = array(); + + $active = workbench_access_get_active_tree(); + if ($active ) { + $tree = $active['tree']; + workbench_access_build_tree($tree); + $options = workbench_access_options($tree, $active['active']); + } + + // display an error if workbench access module has not been configured + if (empty($options) && !variable_get('workbench_access', FALSE)) { + $message = workbench_access_configuration_needed_message(); + drupal_set_message($message, 'error', $repeat = FALSE); + } + + $form['section'] = array( + '#type' => 'select', + '#options' => $options, + '#default_value' => isset($context['section']) ? $context['section'] : '', + '#multiple' => variable_get('workbench_access_allow_multiple', 0) ? TRUE : FALSE, + '#size' => min( variable_get('workbench_access_max_select_size', 10), count($options)), + ); + + if (variable_get('workbench_access_allow_multiple', 0)){ + $form['add'] = array( + '#type' => 'checkbox', + '#title' => t('Add to existing sections'), + ); + } + + return $form; +} + +/** + * Process workbench_moderation_set_state_action_form form submissions. + */ +function workbench_access_assign_section_presave_action_submit($form, $form_state) { + $context = array( + 'section' => $form_state['values']['section'], + ); + if (variable_get('workbench_access_allow_multiple', 0)){ + $context['add'] = $form_state['values']['add']; + } + return $context; +} + +/** + * Adds the section(s) for a given node. + * When multiple sections are allowed and the add box was checked, + * this will append section(s) the existing section(s). + * When multiple sections are allowed and the add box was not checked, + * this will replace the existing section(s) with the specified sections(s). + * When multiple sections are disallowed, then this will replace the existing + * section with the given section. + * + * This is a presave function, so this neither performs a node_save() nor + * writes to the workbench_access database tables. + */ +function workbench_access_assign_section_presave_action($node, $context) { + if (empty($context['section']) || !variable_get('workbench_access_node_type_' . $node->type, 1)){ + return; + } + $sections = $context['section']; + if ( ! is_array($sections)) { + $sections = array($sections); + } + + if (variable_get('workbench_access_allow_multiple', 0) && $context['add']){ + if (empty($node->workbench_access) || !is_array($node->workbench_access)) { + $node->workbench_access = array(); + } + foreach( $sections as $section ) { + if (!in_array($section, $node->workbench_access)){ + $node->workbench_access[] = $section; + } + } + } + else { + $node->workbench_access = $sections; + } +} + +/** + * Form builder; Prepare a form for possible sections. + */ +function workbench_access_unassign_section_presave_action_form($context) { + $form = array(); + + $options = workbench_access_active_options(); + + // display an error if workbench access module has not been configured + if (empty($options) && !variable_get('workbench_access', FALSE)) { + $message = workbench_access_configuration_needed_message(); + drupal_set_message($message, 'error', $repeat = FALSE); + } + + $form['section'] = array( + '#type' => 'select', + '#options' => $options, + '#default_value' => isset($context['section']) ? $context['section'] : '', + ); + + return $form; +} + +/** + * Process workbench_moderation_set_state_action_form form submissions. + */ +function workbench_access_unassign_section_presave_action_submit($form, $form_state) { + return array('section' => $form_state['values']['section']); +} + +/** + * Removes a section for a given node. + * If the given section is not already assigned, then this does nothing. + * + * This is a presave function, so this neither performs a node_save() nor + * writes to the workbench_access database tables. + */ +function workbench_access_unassign_section_presave_action($node, $context) { + if (empty($context['section']) || !variable_get('workbench_access_node_type_' . $node->type, 1)){ + return; + } + + if (variable_get('workbench_access_allow_multiple', 0)){ + if (empty($node->workbench_access) || !is_array($node->workbench_access)) { + return; + } + + $key = array_search($context['section'], $node->workbench_access); + + if ($key !== FALSE){ + unset($node->workbench_access[$key]); + } + } + else { + if ($node->workbench_access == $context['section']){ + $node->workbench_access = array(); + } + } +} diff --git a/workbench_access.rules.inc b/workbench_access.rules.inc new file mode 100644 index 0000000..fcbdf72 --- /dev/null +++ b/workbench_access.rules.inc @@ -0,0 +1,227 @@ + t("Node"), + 'label' => t("Content is using workbench access control"), + 'base' => 'workbench_access_rules_condition_content_is_using_workbench_access', + 'parameter' => array( + 'node' => array('type' => 'node', 'label' => t("Content")), + ), + 'access callback' => 'rules_node_integration_access', + ); + + $items['contents_current_access_section'] = array( + 'group' => t("Node"), + 'label' => t("Content's current access control section"), + 'base' => 'workbench_access_rules_condition_contents_current_access_section', + 'parameter' => array( + 'node' => array('type' => 'node', 'label' => t("Content")), + 'access_section' => array( + 'type' => 'text', + 'label' => t("Workbench access section"), + 'options list' => 'workbench_access_active_options', + 'restriction' => 'input', + 'save' => TRUE, + ), + ), + 'access callback' => 'rules_node_integration_access', + ); + + return $items; +} + +/** + * Implements hook_rules_action_info() on behalf of the workbench_access module. + */ +function workbench_access_rules_action_info() { + $items = array(); + + $items['workbench_access_assign_access_section'] = array( + 'label' => t("Assign access control section (presave)"), + 'group' => t("Node"), + 'base' => 'workbench_access_assign_access_section_presave_rules_action', + + 'parameter' => array( + 'node' => array( + 'type' => 'node', + 'label' => t("Content"), + ), + 'access_section' => array( + 'type' => 'text', + 'label' => t("Workbench access control section"), + 'options list' => 'workbench_access_active_options', + 'restriction' => 'input', + ), + ), + ); + + $items['workbench_access_unassign_access_section'] = array( + 'label' => t("Unassign access control section (presave)"), + 'group' => t("Node"), + 'base' => 'workbench_access_unassign_access_section_presave_rules_action', + + 'parameter' => array( + 'node' => array( + 'type' => 'node', + 'label' => t("Content"), + ), + 'access_section' => array( + 'type' => 'text', + 'label' => t("Workbench access control section"), + 'options list' => 'workbench_access_active_options', + 'restriction' => 'input', + ), + ), + ); + + $items['workbench_access_load_access_section'] = array( + 'label' => t("Load access control section"), + 'group' => t("Node"), + 'base' => 'workbench_access_load_access_section_rules_action', + + 'parameter' => array( + 'node' => array( + 'type' => 'node', + 'label' => t("Content"), + ), + ), + 'provides' => array( + 'workbench_access_section' => array( + 'type' => 'list', + 'label' => t("Workbench Access States"), + ), + ), + ); + + return $items; +} + +/** + * Condition: Check if the content is using workbench access control. + * + * @param $node + * A node object + * + * @return + * TRUE/FALSE depending on if the content is using workbench access control. + */ +function workbench_access_rules_condition_content_is_using_workbench_access($node) { + if (!is_object($node) || !property_exists($node, 'workbench_access')) { + return FALSE; + } + + return variable_get('workbench_access_node_type_' . $node->type, 1) == 1; +} + +/** + * Condition: Check if workbench access section matched selected section. + * + * @param $node + * A node object + * @param $access_section + * A node object + * + * @return + * TRUE/FALSE depending on if the nodes current section matches selected section. + */ +function workbench_access_rules_condition_contents_current_access_section($node, $access_section) { + if (!is_object($node) || !property_exists($node, 'workbench_access')) { + return FALSE; + } + + if (is_array($node->workbench_access)) { + return in_array($access_section, $node->workbench_access); + } + + return $node->workbench_access == $access_section; +} + +/** + * Action: Assign an access section to a given node. + * + * $param $node + * A node object + * + * $param $access_section + * The desired access section to assign. + * + * @return + * An array containing the node object stored in the key called 'node'. + */ +function workbench_access_assign_access_section_presave_rules_action($node, $access_section) { + if (is_object($node) && !empty($access_section)) { + actions_do('workbench_access_assign_section_presave_action', $node, array('section' => $access_section)); + } + + return array('node' => $node); +} + +/** + * Action: Unassign an access section from a given node. + * + * $param $node + * A node object + * + * $param $access_section + * The desired access section to assign. + * + * @return + * An array containing the node object stored in the key called 'node'. + */ +function workbench_access_unassign_access_section_presave_rules_action($node, $access_section) { + if (is_object($node) && !empty($access_section)) { + actions_do('workbench_access_unassign_section_presave_action', $node, array('section' => $access_section)); + } + + return array('node' => $node); +} + +/** + * Action: Loads the workbench access section into a variable. + * + * $param $node + * A node object + * + * @return + * An array containing the node object stored in the key called 'node' and an array containing the access control section for the given node. + */ +function workbench_access_load_access_section_rules_action($node) { + $sections = array(); + + if (is_object($node)) { + if (property_exists($node, 'workbench_access_id')) { + if (is_array($node->workbench_access_id)) { + $sections = $node->workbench_access_id; + } + else { + $sections = array($node->workbench_access_id); + } + } + else if (property_exists($node, 'workbench_access')) { + if (is_array($node->workbench_access)) { + $sections = $node->workbench_access; + } + else { + $sections = array($node->workbench_access); + } + } + } + + return array('node' => $node, 'workbench_access_sections' => $sections); +}