invite_notifications.module in Invite 7.4
Main file for the Invite Notifications.
File
modules/invite_notifications/invite_notifications.moduleView source
<?php
/**
* @file
* Main file for the Invite Notifications.
*/
/**
* Implements hook_init().
*/
function invite_notifications_init() {
global $user;
if (user_is_anonymous()) {
return FALSE;
}
$account = user_load($user->uid);
if (isset($account->data['notification_received'])) {
$result = db_select('invite_notifications')
->fields('invite_notifications')
->condition('uid', $account->uid)
->condition('status', 1)
->execute()
->fetchAll();
foreach ($result as $row) {
drupal_set_message(invite_notifications_get_message($row->message_type, $row), 'status', FALSE);
}
$result = db_query('SELECT count(*) FROM {invite_notifications} WHERE uid = :uid and status = :status', array(
':uid' => $account->uid,
':status' => 1,
))
->fetchField();
if ($result == 0 && $account->data['notification_received']) {
user_save($account, array(
'data' => array(
'notification_received' => FALSE,
),
));
}
}
}
/**
* Invite Sending Controller.
*
* @return array
* Return a array with label.
*/
function invite_notifications_invite_sending_controller() {
return array(
'label' => t('In-site notification'),
);
}
/**
* Get Messages.
*/
function invite_notifications_get_message($message_type, $params) {
$message = $params;
$invitee = user_load($message->invitee);
$inviter = user_load($message->inviter);
$invitation = invite_load($message->iid);
$result = FALSE;
switch ($message_type) {
case 'inviter_notification':
$result = t('!user (@email) has joined!', array(
'!user' => theme('username', array(
'account' => $invitee,
)),
'@email' => $invitee->mail,
));
db_update('invite_notifications')
->fields(array(
'status' => 0,
))
->condition('iid', $invitation->iid, '=')
->execute();
break;
case 'registered_user_notification':
$result = t('!user has sent you invitation. You could !accept or !withdrawn it.', array(
'!user' => theme('username', array(
'account' => $inviter,
)),
'@email' => $invitee->mail,
'!accept' => l(t('accept'), 'invite/accept/' . $invitation->reg_code),
'!withdrawn' => l(t('withdraw'), 'invite/withdraw/' . $invitation->reg_code),
));
break;
}
return $result;
}
/**
* Invite Notifications Theme.
*/
function invite_notifications_theme() {
return array(
'invite_inviter_notification' => array(
'render element' => 'message',
),
'invite_registered_user_notification' => array(
'render element' => 'message',
),
);
}
/**
* Inviter Notification.
*/
function theme_invite_inviter_notification($variables) {
}
/**
* Registered User Notification.
*/
function theme_invite_registered_user_notification($variables) {
$message = $variables['message'];
$invitee = user_load($message->inviter);
$invitation = invite_load($message->iid);
return array(
'message' => array(
'#type' => 'markup',
'#markup' => t('!user has sent you invitation. You could !accept or !withdrawn it.', array(
'!user' => theme('username', array(
'account' => $invitee,
)),
'@email' => $invitee->mail,
'!accept' => l(t('accept'), 'invite/accept/' . $invitation->reg_code),
'!withdrawn' => l(t('accept'), 'invite/withdraw/' . $invitation->reg_code),
)),
),
);
}
/**
* Implements hook_invite_accept().
*/
function invite_notifications_invite_accept($invite) {
$message = array(
'iid' => $invite->iid,
'uid' => $invite->uid,
'inviter' => $invite->uid,
'invitee' => $invite->invitee,
'message_type' => 'inviter_notification',
);
// Flag the inviting user, this triggers status notifications and
// saves us some queries otherwise.
if (drupal_write_record('invite_notifications', $message) == SAVED_NEW) {
$inviter = user_load($invite->uid);
user_save($inviter, array(
'data' => array(
'notification_received' => TRUE,
),
));
$invitee = user_load($invite->invitee);
user_save($invitee, array(
'data' => array(
'notification_received' => FALSE,
),
));
db_update('invite_notifications')
->fields(array(
'status' => 0,
))
->condition('iid', $invite->iid, '=')
->execute();
}
}
/**
* Implements hook_entity_insert().
*/
function invite_notifications_entity_insert($invite, $type) {
if (!empty($invite->invitee) && $invite
->status() == INVITE_VALID) {
$message = array(
'iid' => $invite->iid,
'uid' => $invite->invitee,
'inviter' => $invite->uid,
'invitee' => $invite->invitee,
'message_type' => 'registered_user_notification',
);
// Flag the inviting user, this triggers status notifications and
// saves us some queries otherwise.
if (drupal_write_record('invite_notifications', $message) == SAVED_NEW) {
user_save($invite
->invitee(), array(
'data' => array(
'notification_received' => TRUE,
),
));
}
}
}
/**
* Invite Notifications.
*
* @param Invite $invite
* The Invite.
*
* @codingStandardsIgnoreStart
*/
function invite_notifications_invite_withdraw($invite) {
//@codingStandardsIgnoreEnd
if ($invite->invitee != 0) {
$uid = db_select('invite_notifications')
->fields('invite_notifications', array(
'uid',
))
->condition('iid', $invite->iid)
->execute()
->fetchField();
db_delete('invite_notifications')
->condition('iid', $invite->iid)
->execute();
$user = user_load($uid);
if (!_invite_notifications_user_messages($user)) {
user_save($user, array(
'data' => array(
'notification_received' => FALSE,
),
));
}
}
}
/**
* Helper function to retrieve notifications for user.
*
* @param object $user
* User.
*
* @return bool
* Return bool with result.
*/
function _invite_notifications_user_messages($user) {
$result = db_query('SELECT * FROM {invite_notifications} WHERE uid = :uid and status = :status', array(
':uid' => $user->uid,
':status' => 1,
))
->fetchAll();
return !empty($result) ? $result : FALSE;
}
Functions
Name | Description |
---|---|
invite_notifications_entity_insert | Implements hook_entity_insert(). |
invite_notifications_get_message | Get Messages. |
invite_notifications_init | Implements hook_init(). |
invite_notifications_invite_accept | Implements hook_invite_accept(). |
invite_notifications_invite_sending_controller | Invite Sending Controller. |
invite_notifications_invite_withdraw | Invite Notifications. |
invite_notifications_theme | Invite Notifications Theme. |
theme_invite_inviter_notification | Inviter Notification. |
theme_invite_registered_user_notification | Registered User Notification. |
_invite_notifications_user_messages | Helper function to retrieve notifications for user. |