You are here

function advpoll_electoral_list_form_submit in Advanced Poll 7.2

Same name and namespace in other branches
  1. 5 advpoll.module \advpoll_electoral_list_form_submit()
  2. 6.3 advpoll.module \advpoll_electoral_list_form_submit()
  3. 6 advpoll.module \advpoll_electoral_list_form_submit()
  4. 6.2 advpoll.module \advpoll_electoral_list_form_submit()
  5. 7.3 includes/advpoll.pages.inc \advpoll_electoral_list_form_submit()
  6. 7 includes/advpoll.pages.inc \advpoll_electoral_list_form_submit()

File

includes/advpoll.pages.inc, line 239

Code

function advpoll_electoral_list_form_submit($form, &$form_state) {
  $add_user = $form_state['values']['electoral_list']['add_user'];
  $nid = $form_state['values']['nid'];

  // using new db_merge method to avoid having to write multiple checks for insert.
  if ($add_user) {
    $uid = db_query('SELECT uid FROM {users} WHERE name = :user', array(
      ':user' => $add_user,
    ))
      ->fetchField();
    db_merge('advpoll_electoral_list')
      ->key(array(
      'nid' => $nid,
      'uid' => $uid,
    ))
      ->fields(array(
      'nid' => $nid,
      'uid' => $uid,
    ))
      ->execute();
    drupal_set_message(t('%user added to electoral list.', array(
      '%user' => $add_user,
    )));
  }
  $add_role = $form_state['values']['electoral_list']['add_role'];
  $roles = $form['electoral_list']['add_role']['#options'];
  if ($add_role) {
    $result = db_query("SELECT u.uid FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid WHERE ur.rid = :rid", array(
      ':rid' => $add_role,
    ));
    foreach ($result as $record) {
      db_merge('advpoll_electoral_list')
        ->key(array(
        'nid' => $nid,
        'uid' => $record->uid,
      ))
        ->fields(array(
        'nid' => $nid,
        'uid' => $record->uid,
      ))
        ->execute();
    }
    drupal_set_message(t('All users with %role role added to electoral list.', array(
      '%role' => $roles[$add_role],
    )));
  }
}