All pastes #54219 Raw Edit

Unnamed

public text v1 · immutable
#54219 ·published 2006-05-08 06:26 UTC
rendered paste body
<?php
// $Id: im.module,v 1.4 2005/01/17 13:08:18 killes Exp $

/**
 * Implementation of hook_perm().
 */
function im_perm() {
  return array('send instant messages', 'delete instant messages');
}


/**
 * Implementation of hook_help().
 */
function im_help($section) {
  switch ($section) {
    case 'admin/help#im':
      return t('');
    case 'admin/modules#description':
      return t('Enables users to send instant messages to each other.');
    case 'admin/settings/im':
      return t('');
  }
}


/**
 * Implementation of hook_menu().
 */
function im_menu($may_cache) {
  global $user;

  $items = array();

  if (!$may_cache) {
    $result = db_query("SELECT DISTINCT(i.mid), i.sid, i.message, i.timestamp, u.name FROM {im} i INNER JOIN {users} u ON i.sid = u.uid WHERE i.rid = %d ORDER BY i.timestamp", $user->uid);
    while ($msg = db_fetch_object($result)) {
      $form['destination'] = array(
  		'#type' => 'hidden',
 		'#value' => $_GET['q'],
	      );
      $form['mid'] = array(
  		 '#type' => 'hidden',
  		 '#value' => $msg->mid,
	       );
      $form['sid'] = array(
  		 '#type' => 'hidden',
  		 '#value' => $msg->sid,
	       );
      $form[] = array(
  		 '#type' => 'submit',
  		 '#value' => t('Confirm and delete'),
	       );
      $form[] = array(
  		 '#type' => 'submit',
  		 '#value' => t('Delete'),
	       ); 
      $form['#method'] = 'post';
      $form['#action'] = url('im/delete');
      $output = drupal_get_form('im', $form);
      drupal_set_message('<div class="im-user">'. t('%user says at %date: ', array('%user' => $msg->name, '%date' => format_date($msg->timestamp))) .'</div>'. $msg->message .'<div class="im-delete">'. $form  .'</div>','message');
    }
  }
  else {
    $items[] = array('path' => 'im/send', 'title' => t('Instant message sent'),
      'callback' => 'im_page',
      'access' => user_access('send instant messages'),
     'type' => MENU_CALLBACK);
    $items[] = array('path' => 'im/delete', 'title' => t('Delete instant message.'),
      'callback' => 'im_delete',
      'access' => user_access('delete instant messages'),
      'type' => MENU_CALLBACK);
  }

  return $items;
}

/**
 * Menu callback
 *
 */
function im_page() {
  global $user;
  $edit = $_POST['edit'];
  if (strlen($edit['message'])) {
    $edit['message'] = check_query($edit['message']);
    db_query("INSERT INTO {im} (sid, rid, message, timestamp) VALUES (%d, %d, '%s', %d)", $user->uid, $edit['receiver'], $edit['message'], time());
  }
  drupal_set_message(t('Message sent.'));
  drupal_goto($edit['destination']);
}

/**
 * Menu callback
 */
function im_delete() {
  $edit = $_POST['edit'];
  $op = $_POST['op'];
  global $user;

  switch ($op) {
    case t('Delete'):
      if (is_numeric($edit['mid'])) {
        db_query("DELETE FROM {im} WHERE mid = %d AND rid = %d", $edit['mid'], $user->uid);
      }
      unset($_SESSION['messages']);
      drupal_set_message(t('Message deleted.'));
      break;
    case t('Confirm and delete'):
      db_query("INSERT INTO {im} (sid, rid, message, timestamp) VALUES (%d, %d, '%s', %d)", $edit['sid'], $user->uid, t('Message confirmed.'), time());
      if (is_numeric($edit['mid'])) {
        db_query("DELETE FROM {im} WHERE mid = %d AND rid = %d", $edit['mid'], $user->uid);
      }
      unset($_SESSION['messages']);
      drupal_set_message(t('Message confirmed and deleted.'));
      break;
   }
  
  drupal_goto($edit['destination']);
}

/**
 * Implementation of hook_block().
 *
 * Displays the messaging form
 */
function im_block($op = 'list', $delta = 0) {

  if ($op == 'list') {
    $block['0']['info'] = t('IM block.');
    return $block;
  }
  else {
    $block = array();
    switch ($delta) {
      case '0':
        global $user;
        if (user_access('send instant messages')) {
          // Count users with activity in the past defined period.
          $time_period = variable_get('user_block_seconds_online', 2700);

          // Perform database queries to gather online user lists.
          $users = db_query('SELECT DISTINCT s.uid, u.name, MAX(s.timestamp) AS max_timestamp FROM {sessions} s INNER JOIN {users} u ON s.uid = u.uid WHERE s.timestamp >= %d AND s.uid != 0 AND s.uid != %d GROUP BY s.uid ORDER BY max_timestamp DESC', time() - $time_period, $user->uid);
          // Display a list of currently online users for selection.
          if (db_num_rows($users)) {
            $items = array();
            while ($uid = db_fetch_object($users)) {
              $items[] = array($uid->uid => $uid->name);
            }
          }
          if (count($items) > 1) {
            $form['message'] = array(
  		      '#type' => 'textfield',
  		      '#title' => t('Message'),
 		      '#default_value' => $edit['message'],
  		      '#size' => 20,
 		      '#maxlength' => 255,
		    );
            $form['receiver'] = array(
 		       '#type' => 'select',
 		       '#title' => t('Select user'),
		       '#default_value' => '',
  		       '#options' => $items,
		     );
            $form['destination'] = array(
  			'#type' => 'hidden',
 			'#value' => $_GET['q'],
	      	     );
            $form[] = array(
 		       '#type' => 'submit',
 		       '#value' => t('Go!'),
		     );
          }
          else {
            $form = t('No other user online');
          }
          $block['content'] = $form['#method'] = 'post';
			      $form['#action'] = url('im/send');
			      $output = drupal_get_form('im', $form);
        }
        $block['subject'] = t('Instant message');
      return $block;
    }
  }
}

?>