$name) { $rows[] = array($name, array('data'=>l(t("edit"),"admin/user/taxonomy_access/$rid"), 'align'=>'right')); } return theme('table', $header, $rows); } /** * Generating the category permissions form for choosen user role. */ function _taxonomy_access_permissions_form($rid) { $node_grant_types = array('view', 'update', 'delete'); // Get all category permissions $perm = taxonomy_access_get_grants($rid); $default = taxonomy_access_get_default_grants($rid); $form['taxonomy_access'] = array( '#tree' => TRUE); // Do the row for uncategorized nodes foreach ($node_grant_types as $grant) { $form['taxonomy_access'][0]['term'][0][$grant] = array( '#type' => 'checkbox', '#default_value' => $perm[0][$grant], ); } $vocabs = taxonomy_get_vocabularies(); $options1 = array( '3' => '--', '1' => t('Allow all'), '0' => t('Ignore all'), '2' => t('Deny all')); $options2 = array( '3' => '--', '1' => t('Select all'), '0' => t('Deselect all')); //Radios' values: '1' => t('Allow'), '0' => t('Ignore'), '2' => t('Deny') $radios = array('1' => '', '0' => '', '2' => ''); foreach ($vocabs as $vocab) { $form['taxonomy_access'][$vocab->vid] = array( '#title' => $vocab->name, '#tree' => TRUE, ); foreach ($node_grant_types as $grant) { $form['taxonomy_access'][$vocab->vid]['vocab'][$grant] = array( '#type' => 'select', '#options' => $options1, '#default_value' => -1, ); $form['taxonomy_access'][$vocab->vid]['default'][$grant] = array( '#type' => 'radios', '#options' => $radios, '#default_value' => isset($default[$vocab->vid][$grant]) ? $default[$vocab->vid][$grant] : 0, ); } foreach (array('create', 'list') as $grant) { $form['taxonomy_access'][$vocab->vid]['vocab'][$grant] = array( '#type' => 'select', '#options' => $options2, ); $form['taxonomy_access'][$vocab->vid]['default'][$grant] = array( '#type' => 'checkbox', '#default_value' => isset($default[$vocab->vid][$grant]) ? $default[$vocab->vid][$grant] : 0, ); } // Defining form elements for each term in vocabulary $terms = array(); $terms = taxonomy_get_tree($vocab->vid); if ($terms) { foreach ($terms as $term) { foreach ($node_grant_types as $grant) { $form['taxonomy_access'][$vocab->vid]['term'][$term->tid][$grant] = array( '#type' => 'radios', '#options' => $radios, '#default_value' => isset($perm[$term->tid][$grant]) ? $perm[$term->tid][$grant] : 0, ); } foreach (array('create', 'list') as $grant) { $form['taxonomy_access'][$vocab->vid]['term'][$term->tid][$grant] = array ( '#type' => 'checkbox', '#default_value' => isset($perm[$term->tid][$grant]) ? $perm[$term->tid][$grant] : 0, ); } } } } $form['taxonomy_access']['submit'] = array( '#type' => 'submit', '#value' => t('Save category permissions'), '#name' => 'save', '#button_type' => 'submit', ); return $form; } /** * Renders the permission matrix user form for choosen user role. */ function theme__taxonomy_access_permissions_form($form) { global $tac_user_roles; $rid = arg(3); $vids = array_keys($form['taxonomy_access']); $node_grant_types = array('view', 'update', 'delete'); $header = array(); $header[] = array( 'data'=>t('Category')); $header[] = array( 'data'=>t('View'), 'colspan'=>4); $header[] = array( 'data'=>t('Update'), 'colspan'=>4); $header[] = array( 'data'=>t('Delete'), 'colspan'=>4); $header[] = array( 'data'=>t('Create')); $header[] = array( 'data'=>t('List')); $sub_header[] = ' '.t('A').''; $sub_header[] = ' '.t('I').''; $sub_header[] = ' '.t('D').''; $sub_header[] = ' '; $radios = array('1' => t('Allow'), '0' => t('Ignore'), '2' => t('Deny')); $output = '
"; foreach ($vids as $vid) { if (is_integer($vid)) { $rows = array(); if ($vid == 0) { // Do the row for uncategorized nodes $row = array(); $row[] = ''.t('Uncategorized nodes').''; foreach ($node_grant_types as $grant) { $row[] = array('data' => drupal_render($form['taxonomy_access'][0]['term'][0][$grant]), 'colspan'=>4); } $row[] = ' '; $row[] = ' '; $rows[] = $row; $output_table = theme('table', $header, $rows); $output .= theme_fieldset(array('#title'=>'Uncategorized nodes', '#value'=>$output_table, '#collapsible' => TRUE, '#collapsed' => TRUE)); } else { //1st row: 'Allow all'... 'Select All'... unset($row); $row = array(); $row[] = array('data' => ''.$form['taxonomy_access'][$vid]['#title'].''); foreach ($node_grant_types as $grant) { $row[] = array('data' => drupal_render($form['taxonomy_access'][$vid]['vocab'][$grant]), 'colspan'=>4); } foreach (array('create', 'list') as $grant) { $row[] = array('data' => drupal_render($form['taxonomy_access'][$vid]['vocab'][$grant])); } $rows[] = $row; //2nd row: 'A' 'I' 'D' $row = array(); $row[] = ' '; foreach ($node_grant_types as $grant) { $row = array_merge($row, $sub_header); } $row[] = array('data'=>' ', 'colspan'=>2); $rows[] = $row; //3rd row: Defaults $row = array(); $row[] = '
'.t('Default').''; foreach (array('view', 'update', 'delete') as $grant) { foreach (array_keys($radios) as $key) { // I need this hack to display radio buttons horizontally (instead of standard form 'radios') $row[] = array('data' => drupal_render($form['taxonomy_access'][$vid]['default'][$grant][$key])); } $row[] = ' '; } foreach (array('create', 'list') as $grant) { $row[] = array('data' => drupal_render($form['taxonomy_access'][$vid]['default'][$grant]), 'align' => 'center'); } $rows[] = $row; //4th row: Empty row $row = array(); $row[] = array('data'=>' ', 'colspan'=>15); $rows[] = $row; // Rows for each term in vocabulary $terms = array(); $terms = taxonomy_get_tree($vid); if ($terms) { foreach ($terms as $term) { $row = array(); $row[] = str_repeat('-',$term->depth).$term->name; foreach ($node_grant_types as $grant) { // I need this hack to display radio buttons horizontally (instead of standard form 'radios') foreach (array_keys($radios) as $key) { $row[] = drupal_render($form['taxonomy_access'][$vid]['term'][$term->tid][$grant][$key]); } $row[] = ' '; } foreach (array('create', 'list') as $grant) { $row[] = array('data' => drupal_render($form['taxonomy_access'][$vid]['term'][$term->tid][$grant]), 'align' => 'center'); } $rows[] = $row; } } $output_table = theme('table', $header, $rows); $output .= theme_fieldset(array('#title'=>$form['taxonomy_access'][$vid]['#title'], '#value'=>$output_table, '#collapsible' => TRUE, '#collapsed' => TRUE)); } } } $output .= drupal_render($form); return $output; } /** * Saves the category permissions matrix for choosen user role, after editing. */ function _taxonomy_access_permissions_form_submit($form_id, $form_values) { global $tac_user_roles; $vids = array_keys($form_values['taxonomy_access']); $rid = arg(3); $grant_types = array('view', 'update', 'delete', 'create', 'list'); $edit = $form_values['taxonomy_access']; foreach ($vids as $vid) { if (is_numeric($vid)) { // Process input data and use it to make changes to the database. if (is_array($edit[$vid]['term'])) { $tids = array_keys($edit[$vid]['term']); foreach ($tids as $tid) { // The $edit array is filled out with zeroes here to // reduce code complexity when we update the term_access table. if (!$edit[$vid]['term'][$tid]) { foreach ($grant_types as $grant) { $edit[$vid]['term'][$tid][$grant] = 0; } } else { foreach ($grant_types as $grant) { if (!isset($edit[$vid][$tid][$grant])) { // Enter a '0' for any permission that has not been set or for any checkbox that has been left unchecked by the user $edit[$vid][$tid][$grant] = 0; } else { if ($grant == 'create' || $grant == 'list') { // For any permission checkbox checked by user, set it to a value of '1' $edit[$vid][$tid][$grant] = 1; } } } } // Make the changes to the term_access table taxonomy_access_grant_update($tid, $rid, $edit[$vid]['term'][$tid]); } } // Now to the same for vocabularies if ($vid !=0) { // The $edit array is filled out with zeroes here to // reduce code complexity when we update the term_access table. $changed = false; if (!array_key_exists($vid, $edit)) { foreach ($grant_types as $grant) { $edit[$vid]['vocab'][$grant] = 0; } } else { foreach ($grant_types as $grant) { if (($edit[$vid]['vocab'][$grant] === NULL) || !in_array($edit[$vid]['vocab'][$grant], array( 0, 1, 2))) { $edit[$vid]['vocab'][$grant] = 3; } else { $changed = true; } // Enter a '0' for any permission that has been left unchecked by the user // For any permission checked by user, set it to a value of '1' if ($grant == 'create' || $grant == 'list') { $edit[$vid]['default'][$grant] = $edit[$vid]['default'][$grant] ? 1 : 0; } else { $edit[$vid]['default'][$grant] = $edit[$vid]['default'][$grant][0]; } } } // Make the changes to the term_access table if ($changed) { taxonomy_access_grant_vocab_update($vid, $rid, $edit); } // set the defaults taxonomy_access_defaults_update($vid,$rid,$edit[$vid]['default']); } } } // Force to rebuild the node_access table according changes made to term_access table //Todo: 1. Possibly optimizing when values changes for only one user role (permission matrix submit) // _refresh_node_access_table($rid); // 2. Run this when changes were made to 'View/Update/Delete' permissions node_access_rebuild(); // redirect to main category permissions menu // $roles = user_roles(); drupal_set_message(t("Your permission settings for '%role' role have been saved.", array('%role' => $tac_user_roles[$rid])), 'status'); drupal_goto('admin/user/taxonomy_access'); return; }