Skip to content

Commit

Permalink
module files
Browse files Browse the repository at this point in the history
  • Loading branch information
jdittric committed May 31, 2012
1 parent 913d3f8 commit fd6b927
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
5 changes: 5 additions & 0 deletions privatemsg_unread.info
@@ -0,0 +1,5 @@
name = Privatemsg unread
description = Allow users to see private message read status (return receipt)
package = Mail
core = 6.x
dependencies[] = privatemsg
100 changes: 100 additions & 0 deletions privatemsg_unread.module
@@ -0,0 +1,100 @@
<?php

/**
* @file
* Module file for privatemsg_unread.module
* Allows a user to view whether a PM they sent has been read.
*/

/**
* Implements hook_perm().
*/
function privatemsg_unread_perm() {
$perms = array(
'access privatemsg unread',
);
return $perms;
}


function privatemsg_unread_fetch() {
//Get the $user object to examine the uid of the current user.
global $user;
$uid = $user->uid;
$result = db_query("
SELECT {users.name}, {pm_message.subject}, {pm_message.timestamp}

This comment has been minimized.

Copy link
@Berdir

Berdir Jun 1, 2012

You should add an alias for the tables (e.g {pm_message} pm and then use the alias everywhere else, like fields. Then you don't need {} there.

FROM {pm_index}
LEFT JOIN {pm_message}
ON {pm_index.mid} = {pm_message.mid}
LEFT JOIN {users}
ON {pm_index.recipient} = {users.uid}

This comment has been minimized.

Copy link
@Berdir

Berdir Jun 1, 2012

You at least need to add a pmi.type IN ('user', 'hidden') here or you could end up joining things like a role to a user with the same id.

WHERE {pm_index.is_new} = 1
AND {pm_message.author} = %d
AND {pm_index.recipient} != %d
ORDER BY {pm_message.timestamp} DESC" , $uid, $uid);

return $result;
}


/**
* Implements hook_menu().
*/
function privatemsg_unread_menu() {
$items['privatemsg/unread'] = array(
'title' => 'Unread PMs',
'description' => 'Allow users to see private message read status (return receipt).',
'access arguments' => array('access privatemsg unread'),
'page callback' => 'privatemsg_unread_view',
'type' => MENU_NORMAL_ITEM ,
);
return $items;
}


/**
* Implements hook_block().
*/
function privatemsg_unread_block($op = 'list', $delta = 0, $edit = array()) {
$blocks['unread private messages']= array (

This comment has been minimized.

Copy link
@Berdir

Berdir Jun 1, 2012

You define a block here, but you don't seem to be implementing hook_block_view()

'info' => t('Unread Private Messages'),
'cache' => DRUPAL_NO_CACHE
);
return $blocks;
}

/**
* Implements hook_view().
*/
function privatemsg_unread_view() {
if(user_access('access privatemsg unread')) {
$block = array();
$result = privatemsg_unread_fetch();

//UNSAFE: reportedly, this only works with mysqli

This comment has been minimized.

Copy link
@Berdir

Berdir Jun 1, 2012

The only save way is doing two queries. first a COUNT() and then actually fetch them.

$nummsgs = $result->num_rows;

//Display the summary table.
$output = "<table>";
$output .= "<tr><th>Recipient</th><th>Subject</th><th>Date</th><\tr>";
while ($row = db_fetch_array($result)) {
$timestamp = $row['timestamp'];
$msgdate = new DateTime("@$timestamp");
$row['timestamp'] = $msgdate->format('Y-m-d H:i:s');
$output .= "<tr><td>" . $row['name'] . "</td><td>" . $row['subject'] . "</td><td>" . $row['timestamp'] . "</td></tr>";
}
$output .= "</table>";

This comment has been minimized.

Copy link
@Berdir

Berdir Jun 1, 2012

You should use the theme system to display a table (http://api.drupal.org/theme_table) and t() to make the labels translatable.

Also, you should probably actually load the user using user_load() and then user theme_username(), so that modules like Realname have a chance of intercepting and displaying the real name of a user.


if ($nummsgs > 0){
//a crude hack to get rid of mystery output error
$pattern = '/<\s+[r]>/';
$replacement = '';
$output = preg_replace($pattern, $replacement, $output);

$output = "<h3>Unread private messages you have sent:</h3>" . $output;
}else{
$output = "<h3>All of your sent messages have been read.</h3>";
}
return $output;
}
}

0 comments on commit fd6b927

Please sign in to comment.