'Taxonomy Role Access', 'description' => 'Configure which vocabularies will use Taxonomy Role Access and which user roles will be controlled.', 'page callback' => 'drupal_get_form', 'page arguments' => array('taxonomy_tools_role_access_admin_form'), 'access arguments' => array('administer taxonomy role access configuration'), 'file' => 'taxonomy_tools_role_access.admin.inc', 'file path' => drupal_get_path('module', 'taxonomy_tools_role_access'), ); $items['taxonomy-role-access/nojs/%taxonomy_vocabulary_machine_name'] = array( 'page callback' => 'taxonomy_tools_role_access_change_access', 'page arguments' => array(1, 2, 3, 4, 5, 6), 'access arguments' => array('administer taxonomy role access'), 'type' => MENU_CALLBACK, ); $items['taxonomy-role-access/ajax/%taxonomy_vocabulary_machine_name'] = array( 'delivery callback' => 'ajax_deliver', ) + $items['taxonomy-role-access/nojs/%taxonomy_vocabulary_machine_name']; return $items; } /** * Implements hook_admin_paths(). */ function taxonomy_tools_role_access_admin_paths() { $paths = array( 'taxonomy-role-access/ajax/*' => TRUE, ); return $paths; } /** * Implements hook_permission(). */ function taxonomy_tools_role_access_permission() { return array( 'administer taxonomy role access' => array( 'title' => t('Administer Taxonomy Role Access'), 'description' => t('User is able to manage user role access to taxonomy terms.'), ), 'bypass taxonomy role access' => array( 'title' => t('Bypass Taxonomy Role Access restrictions'), 'description' => t('User is able to view taxonomy terms although the access is restricted by Taxonomy Role Access module.'), ), 'administer taxonomy role access configuration' => array( 'title' => t('Administer Taxonomy Role Access configuration'), 'description' => t('User is able to access and use Taxonomy Role Access configuration page.'), ), ); } /** * Ajax callback for processing changes in taxonomy term access rules. * * @param string $ajax * String representing if ajax is used; * "ajax" or "nojs". * @param stdClass $vocabulary * An object representing vocabulary. * @param int $tid * Taxonomy term ID. * @param int $rid * User role ID. * @param string $access * String representing how access is being changed; * "true" or "false". * @param int $parent * Parent taxonomy term ID. * * @return array * An array containing ajax commands. */ function taxonomy_tools_role_access_change_access($ajax, $vocabulary, $tid, $rid, $access, $parent = 0) { $is_ajax = $ajax === 'ajax'; $changed = FALSE; if ($access == 'false') { // Access is being changed to "deny". $query = db_insert('taxonomy_tools_role_access'); $query->fields(array('tid' => $tid, 'rid' => $rid)); $query->execute(); $changed = TRUE; } else { // Access is being changed to "allow". $query = db_delete('taxonomy_tools_role_access'); $query->condition(db_and()->condition('tid', $tid)->condition('rid', $rid)); $result = $query->execute(); if ($result) { $changed = TRUE; } } // Only if access settings have really changed. if ($changed) { // Rebuild node access records. taxonomy_tools_rebuild_access_grants($tid); // Allow other modules to act on term access change. module_invoke_all('taxonomy_tools_role_access_term_access_change', $tid); } if ($is_ajax) { $commands = array(); if ($changed) { $attributes = array( 'class' => array( 'use-ajax', 'access_' . $tid . '_' . $rid, $access, ), 'title' => t('change'), ); $opposite = $access === 'true' ? 'false' : 'true'; // Replace the link. $link = 'taxonomy-role-access/nojs/' . $vocabulary->machine_name . '/' . $tid . '/' . $rid . '/' . $opposite; if ($parent > 0) { $link .= '/' . $parent; } $element = array( '#type' => 'link', '#title' => '', '#href' => $link, '#attributes' => $attributes, '#access' => user_access('administer taxonomy role access'), ); $commands[] = ajax_command_replace('a.access_' . $tid . '_' . $rid, drupal_render($element)); } return array('#type' => 'ajax', '#commands' => $commands); } else { // Redirect back to overview page. $path = 'admin/structure/taxonomy/' . $vocabulary->machine_name . '/overview'; if ($parent > 0) { $path .= '/' . $parent; } drupal_goto($path); } } /** * Checks the taxonomy term availability to specified user role. * * @param string $tid * Taxonomy term identifcator * @param string $rid * User role identificator * * @return bool * TRUE if the taxonomy term is available to the user role, * otherwise - FALSE. */ function taxonomy_tools_role_access_get_access($tid, $rid) { $access = TRUE; $query = db_select('taxonomy_tools_role_access', 'foo'); $query->fields('foo'); $query->condition(db_and()->condition('foo.tid', $tid)->condition('foo.rid', $rid)); $result = $query->execute()->fetchObject(); if ($result) { $access = FALSE; } if ($access) { // Check parent access. $query = db_select('taxonomy_term_hierarchy', 'foo'); $query->addField('foo', 'parent'); $query->condition('foo.tid', $tid); $parent = $query->execute()->fetchField(); if ($parent) { $access = taxonomy_tools_role_access_get_access($parent, $rid); } } return $access; } /** * Implements hook_user_role_delete(). */ function taxonomy_tools_role_access_user_role_delete($role) { $r_config = variable_get('taxonomy_tools_role_access_role_config', array()); if (!empty($r_config) && in_array($role->rid, $r_config)) { // The deleted role must removed from Taxonomy Role Access settings. $v_config = variable_get('taxonomy_tools_role_access_vocab_config', array()); $form_state = array(); foreach ($v_config as $vid) { if ($vid > 0) { $form_state['values']['taxonomy_tools_role_access_vocab_config'][$vid] = $vid; } } unset($r_config[$role->rid]); foreach ($r_config as $rid) { if ($rid > 0) { $form_state['values']['taxonomy_tools_role_access_role_config'][$rid] = $rid; } } require_once drupal_get_path('module', 'taxonomy_tools_role_access') . '/taxonomy_tools_role_access.admin.inc'; drupal_form_submit('taxonomy_tools_role_access_admin_form', $form_state); } }