<?php

/**
 * @file
 * Vocabulary Permissions Per Role - administration UI
 */

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function vppr_form_admin_settings_vppr($form, &$form_state) {
  $form = array();
  $vocabs = taxonomy_get_vocabularies();
  $roles = user_roles(TRUE); // List of roles without 'anonymous'.
  $perms = variable_get('vppr_perms', array(array()));
  foreach ($vocabs as $vocab) {
    $form['vppr_vocabs'][$vocab->vid] = array(
      '#value' => $vocab->name,
    );
    foreach ($roles as $rid => $role) {
      $form['vppr_perms'][$vocab->vid][$rid] = array(
        '#type' => 'checkbox',
        '#default_value' => (isset($perms[$vocab->vid]) && isset($perms[$vocab->vid][$rid]) ? $perms[$vocab->vid][$rid] : 0),
      );
    }
    $form['vppr_perms']['#tree'] = TRUE;
  }
  foreach ($roles as $rid => $role) {
    $form['vppr_roles'][$rid] = array(
      '#value' => $role,
    );
  }
  $form['help'] = array(
    '#type' => 'item',
    '#description' => t('Tick a box above to allow editing that vocabulary for that role.'),
  );
  $form['buttons']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}

/**
 * Theme helper for vppr_form_admin_settings_vppr().
 */
function theme_vppr_form_admin_settings_vppr($variables) {
  $form = $variables['form'];
  $header = array(FALSE);
  foreach (element_children($form['vppr_roles']) as $rid) {
    $header[$rid] = drupal_render($form['vppr_roles'][$rid]);
  }
  $rows = array();
  foreach (element_children($form['vppr_vocabs']) as $vid) {
    $row = array(drupal_render($form['vppr_vocabs'][$vid]));
    foreach (element_children($form['vppr_perms'][$vid]) as $rid) {
      $row[$rid] = drupal_render($form['vppr_perms'][$vid][$rid]);
    }
    $rows[$vid] = $row;
  }
  $buttons = drupal_render($form['buttons']);
  return theme('table', array('header' => $header, 'rows' => $rows)) . drupal_render_children($form) . $buttons;
}

/**
 * Store VPPR perms to the DB.
 */
function vppr_form_admin_settings_vppr_submit($form, &$form_state) {
  $perms = array();
  foreach ($form_state['values']['vppr_perms'] as $vid => $values) {
    $values = array_filter($values);
    if (!empty($values)) {
      $perms[$vid] = $values;
    }
  }
  variable_set('vppr_perms', $perms);
}

