You are here

function vppn_node_form_submit in View Permission Per Node 7

Submission for the node form that has VPPN enabled.

1 string reference to 'vppn_node_form_submit'
vppn_form_node_form_alter in ./vppn.module
Implements hook_form_BASE_FORM_ID_alter().

File

./vppn.module, line 128
Configuration for VPPN.

Code

function vppn_node_form_submit(&$form, &$form_state) {

  // Bail out if no nid.
  if (empty($form_state['nid'])) {
    return;
  }

  // Bail out if no roles.
  if (empty($form_state['values']['vppn_roles'])) {
    return;
  }

  // Get the defaults.
  $defaults = $form['vppn']['vppn_roles']['#default_value'];

  // Each role.
  foreach ($form_state['values']['vppn_roles'] as $role_id => $value) {

    // Check if there was a default.
    $exists = in_array($role_id, $defaults);

    // Check if there was no change.
    if (!$exists && $value === 0 || $exists && $value !== 0) {

      // Bail out if no change.
      continue;
    }

    // Delete the record if it was changed & removed.
    if ($value === 0) {

      // Remove the role from the viewable roles.
      db_delete('vppn')
        ->condition('nid', $form_state['nid'])
        ->condition('rid', $role_id)
        ->execute();
    }
    else {

      // Add the role to the viewable roles.
      db_insert('vppn')
        ->fields(array(
        'nid' => $form_state['nid'],
        'rid' => $value,
      ))
        ->execute();
    }
  }
}