You are here

function privatemsg_new_folder_form_submit in Privatemsg 5.3

Same name and namespace in other branches
  1. 5 privatemsg.module \privatemsg_new_folder_form_submit()

File

./privatemsg.module, line 1221

Code

function privatemsg_new_folder_form_submit($form_id, $form_values) {
  global $user;
  $fid = db_next_id('{privatemsg_folder}_fid');
  db_query("INSERT INTO {privatemsg_folder} (fid, uid, name) VALUES (%d, %d, '%s')", $fid, $user->uid, $form_values['name']);
  if (isset($form_values['messages']) && is_array($form_values['messages'])) {
    unset($_SESSION['privatemsg']);
    foreach ($form_values['messages'] as $key => $value) {
      db_query("UPDATE {privatemsg} SET folder = %d WHERE id = %d AND recipient = %d", $fid, $key, $user->uid);
    }
    $link = l(t('%folder', array(
      '%folder' => $form_values['name'],
    )), 'privatemsg/list/' . $fid, array(), NULL, NULL, FALSE, TRUE);
    if (count($form_values['messages']) == 1) {
      drupal_set_message(t('Your message has been moved to !link.', array(
        '!link' => $link,
      )));
    }
    elseif (count($form_values['messages']) > 1) {
      drupal_set_message(t('Your messages have been moved to !link.', array(
        '!link' => $link,
      )));
    }
    $to_fid = $_SESSION['fromfolder'];
    unset($_SESSION['fromfolder']);
    if (empty($to_fid)) {
      drupal_goto('privatemsg');
    }
    elseif (substr($to_fid, 0, 4) == 'view') {
      drupal_goto('privatemsg/' . $to_fid);
    }
    elseif (is_numeric($to_fid)) {
      drupal_goto('privatemsg/list/' . $to_fid);
    }
    else {
      drupal_goto('privatemsg');
    }
  }
  else {
    drupal_set_message(t('Folder created successfully.'));
    if (isset($_SESSION['privatemsg'])) {
      unset($_SESSION['privatemsg']);
      $to_fid = $_SESSION['fromfolder'];
      unset($_SESSION['fromfolder']);
      if (empty($to_fid)) {
        drupal_goto('privatemsg');
      }
      else {
        drupal_goto('privatemsg/list/' . $to_fid);
      }
    }
  }
}