I'm completely new to Drupal and trying to learn it, but it's pretty hard.
The Node Access module is great and I want to make functionalty for it for bulk grant for several users. The idea is to have a form grid with several users in columns and nodes and grants in rows. It will look like that:
What I've come to so far:
function table_test_form($form, &$form_state, $node) {
$form_values = &$form_state['values'];
if (!$form_values) {
$form_values = array();
// Get node titles for the first column
$results = db_query("SELECT DISTINCT title, node.nid
FROM node_access
LEFT JOIN node ON node_access.nid = node.nid
WHERE node.nid < :nid
ORDER BY node.nid", array(
':nid' => 5,
));
foreach ($results as $noderow) {
// make an array for every row
$form_values['noderowid'][$noderow->nid] = array(
'name' => $noderow->title,
);
$current_nid = $noderow->nid;
//Get users and grants for the current node
$results_grants = db_query("SELECT uid, name, grant_view
FROM node_access
LEFT JOIN users ON uid = gid
WHERE nid = :nid AND uid < :uid
ORDER BY uid ", array(
':nid' => $current_nid,
':uid' => 5,
));
// Tried this but didn't work
// if ($results_grants) {
// while ($row = $results_grants->fetchAssoc()) {
// $items_grants[] = array(
// $row['name'] => $row['grant_view']
// );
// }
// }
$items_grants = $results_grants->fetchAllKeyed(1,2);
// add the grants for every user to the node row
$form_values['noderowid'][$noderow->nid][] = $items_grants;
}
}
if (!isset($form_values['noderowid'])) {
$form_values['noderowid'] = array();
}
$noderows = $form_values['noderowid'];
$form['nid'] = array(
'#type' => 'hidden',
'#value' => $node->nid,
);
// the table.
if (is_array($noderows)) {
$form['noderowid'] = array('#tree' => TRUE);
foreach ($noderows as $key => $field) {
if(!isset($form['noderowid'][$key])){
$form['noderowid'][$key] = array();
}
$form['noderowid'][$key]['name'] = array(
'#type' => 'hidden',
'#value' => $field['name'],
);
$arr = $form['noderowid'][$key];
//add checkboxes
foreach ($arr as $nodetitle => $f){
if($nodetitle!='name'){
$form['noderowid'][$key][$nodetitle] = array(
'#type' => 'checkbox',
'#default_value' => $f[$nodetitle],
);
}
}
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Grants'),
);
return $form;
}
function theme_table_test_form($variables) {
$output = '';
$form = $variables['form'];
$rows = array();
//get the names of the users and put them in the header
$uid = 5;
$sql = "SELECT name FROM users WHERE uid < :uid ORDER BY uid ASC";
$result = db_query($sql, array(':uid' => $uid));
if ($result) {
while ($row = $result->fetchAssoc()) {
$items[] = array(
'data' => t($row['name'])
);
}
}
// make table.
unset($rows);
$noderows = element_children($form['noderowid']);
if (count($noderows) > 0) {
$header = $items;
$rows = array();
foreach ($noderows as $key) {
$row = array();
// first column
$row[] = $form['noderowid'][$key]['name']['#value'];
// other columns
foreach(array_slice($header, 1) as $usr){
if(!isset($form['noderowid'][$key][$usr])){
$form['noderowid'][$key][$usr] = array();
}
$row[] = drupal_render($form['noderowid'][$key][$usr]);
}
$rows[] = $row;
}
$output .= theme('table', array('header' => $header, 'rows' => $rows));
}
$output .= drupal_render_children($form);
return $output;
}
It renders the users and nodes names, but not the checkboxes.
I guess it's a pretty bad code and there are too many queries to the database, but I want to just make it work for now.
Any suggestions?