Thursday, August 19, 2010

Example of format_plural combined with theme item_list

Below it the example of used of Drupal format_plural combined with theme item_list for showing notifications of new messages and friendship request using privatemsg module and user_relationships module.

<?php
global $user;
$uid = $user->uid;
$app = 0;
$row = db_fetch_object(db_query("SELECT  COUNT(*) AS counts FROM {user_relationships} ur WHERE ur.requestee_id = %d AND ur.approved = %d", $uid, $app));
$friend_req= $row->counts;
$message = privatemsg_unread_count();

$items = array();

if($friend_req >= 1){
    $friend_req_count = format_plural($friend_req, t('1 Friendship request'), t('@count Friendship requests'));
    $items[] = l($friend_req_count, 'relationships/requests', array('attributes' => array('class' => 'friend-requests')));
}

if($message >= 1){
    $message_count = format_plural($message, t('1 New message'), t('@count New messages'));
    $items[] = l($message_count, 'messages', array('attributes' => array('class' => 'messages')));
}

if(COUNT($items) >= 1){
    print theme('item_list', $items);
}

No comments:

Post a Comment