You are here

function privatemsg_user in Privatemsg 6.2

Same name and namespace in other branches
  1. 5.3 privatemsg.module \privatemsg_user()
  2. 5 privatemsg.module \privatemsg_user()
  3. 6 privatemsg.module \privatemsg_user()

Related topics

File

./privatemsg.module, line 1321
Allows users to send private messages to other users.

Code

function privatemsg_user($op, &$edit, &$account, $category = NULL) {
  global $user;
  switch ($op) {
    case 'form':
      if ($category == 'account') {

        // Create array to be able to merge in fieldset and avoid overwriting
        // already added options.
        if (!isset($form['privatemsg'])) {
          $form['privatemsg'] = array();
        }

        // Always create the fieldset in case other modules want to add
        // Privatemsg-related settings through hook_form_alter(). If it's still
        // empty after the build process, the after build function will remove
        // it.
        $form['privatemsg'] += array(
          '#type' => 'fieldset',
          '#title' => t('Private messages'),
          '#collapsible' => TRUE,
          '#collapsed' => FALSE,
          '#weight' => 10,
          '#after_build' => array(
            'privatemsg_account_fieldset_remove_if_empty',
          ),
        );

        // We have to use user_acces() because privatemsg_user_access() would
        // return FALSE when privatemsg is disabled.
        if ((user_access('write privatemsg') || user_access('read privatemsg')) && user_access('allow disabling privatemsg')) {
          $form['privatemsg']['pm_enable'] = array(
            '#type' => 'checkbox',
            '#title' => t('Enable private messages'),
            '#default_value' => !privatemsg_is_disabled($account),
            '#description' => t('Disabling private messages prevents you from sending or receiving messages from other users.'),
            '#weight' => -10,
          );
        }
      }
      return $form;
    case 'submit':
      if (isset($edit['pm_enable']) && (user_access('write privatemsg') || user_access('read privatemsg')) && user_access('allow disabling privatemsg')) {
        $current = privatemsg_is_disabled($account);
        $disabled = !$edit['pm_enable'];
        unset($edit['pm_enable']);

        // only perform the save if the value has changed
        if ($current != $disabled) {
          privatemsg_set_setting('user', $account->uid, 'disabled', $disabled);
        }
      }
      break;
    case 'view':
      if (($url = privatemsg_get_link(array(
        $account,
      ))) && variable_get('privatemsg_display_profile_links', 1)) {
        $account->content['privatemsg_send_new_message'] = array(
          '#type' => 'markup',
          '#value' => l(t('Send this user a private message'), $url, array(
            'query' => drupal_get_destination(),
            'title' => t('Send this user a message'),
            'attributes' => array(
              'class' => 'privatemsg-send-link privatemsg-send-link-profile',
            ),
          )),
          '#weight' => 10,
        );
      }
      break;
    case 'login':
      if (variable_get('privatemsg_display_loginmessage', TRUE) && privatemsg_user_access()) {
        $count = privatemsg_unread_count();
        if ($count) {
          global $user;
          drupal_set_message(format_plural($count, 'You have <a href="@messages">1 unread message</a>.', 'You have <a href="@messages">@count unread messages</a>', array(
            '@messages' => url(privatemsg_get_dynamic_url_prefix($user->uid)),
          )));
        }
      }
      break;
    case 'delete':

      // Load all mids of the messages the user wrote.
      $result = db_query("SELECT mid FROM {pm_message} WHERE author = %d", $account->uid);
      $mids = array();
      while ($row = db_fetch_array($result)) {
        $mids[] = $row['mid'];
      }

      // Delete messages the user wrote.
      db_query('DELETE FROM {pm_message} WHERE author = %d', $account->uid);
      if (!empty($mids)) {

        // Delete recipient entries in {pm_index} of the messages the user wrote.
        db_query('DELETE FROM {pm_index} WHERE mid IN (' . db_placeholders($mids) . ')', $mids);
      }

      // Delete recipient entries of that user.
      db_query("DELETE FROM {pm_index} WHERE recipient = %d and type IN ('user', 'hidden')", $account->uid);

      // DELETE any disable flag for user.
      privatemsg_del_setting('user', $account->uid, 'disabled');
      break;
  }
}