diff --git a/privatemsg_groups/README b/privatemsg_groups/README new file mode 100644 index 0000000..3e8c40c --- /dev/null +++ b/privatemsg_groups/README @@ -0,0 +1,5 @@ +Must be enabled at least "write privatemsg to own groups" to be able to send messages. + +By default, activating the option before, all the users that belong to a group are able to send messages to group, to restrict this settings only to administrator, please go to "admin/settings/messages" and check "Restrict access to group administrators" option. + +Author: paolomainardi@gmail.com (http://drupal.org/user/302937) diff --git a/privatemsg_groups/privatemsg_groups.info b/privatemsg_groups/privatemsg_groups.info new file mode 100644 index 0000000..bb56b30 --- /dev/null +++ b/privatemsg_groups/privatemsg_groups.info @@ -0,0 +1,8 @@ +name = Privatemsg Groups +description = Allows to send message to all members of a group. +package = Mail +core = 6.x +dependencies[] = privatemsg +dependencies[] = og + + diff --git a/privatemsg_groups/privatemsg_groups.module b/privatemsg_groups/privatemsg_groups.module new file mode 100644 index 0000000..bc6426e --- /dev/null +++ b/privatemsg_groups/privatemsg_groups.module @@ -0,0 +1,207 @@ + array( + 'arguments' => array('group' => NULL, 'options' => array()), + ), + ); +} + +/** + * Implements hook_privatemsg_recipient_types_info(). + */ +function privatemsg_groups_privatemsg_recipient_type_info() { + return array( + 'group' => array( + 'name' => t('Group'), + 'description' => t('Enter the name of a group to write a message to all users that are suscribed to.'), + 'load' => 'privatemsg_groups_load_multiple', + 'format' => 'privatemsg_groups_format', + 'autocomplete' => 'privatemsg_groups_autocomplete', + 'generate recipients' => 'privatemsg_groups_load_recipients', + 'count' => 'privatemsg_groups_count_recipients', + 'write callback' => 'privatemsg_groups_access', + 'view callback' => 'privatemsg_groups_access', + ), + ); +} + +/** + * Implements hook_form_FORM_ID_alter(). + */ +function privatemsg_groups_form_privatemsg_admin_settings_alter(&$form, &$form_state) { + $form['groups'] = array( + '#type' => 'fieldset', + '#title' => t('Privatemsg groups'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#weight' => 25, + ); + + $form['groups']['privatemsg_group_admin_grant'] = array( + '#title' => t('Restrict access to group administrators'), + '#type' => 'checkbox', + '#default_value' => variable_get('privatemsg_group_admin_grant', 1), + '#description' => t("Enabling this setting will restrict message sending only to group administrator."), + '#weight' => -4, + ); +} + +/** + * Permission check for group recipients. + */ +function privatemsg_groups_access($recipient) { + global $user; + + // Check if user has permission to write to all groups. + if (user_access('write privatemsg to all groups')) { + return TRUE; + } + + // Check if user has permission to write to own groups. + if (!user_access('write privatemsg to own groups')) { + return FALSE; + } + + if ($recipient) { + // extract group nid from recipient, check if is an object or the nid passed by autocomplete function + $nid = (is_object($recipient) ? $recipient->recipient : $recipient); + + // if access is restricted to group admins + if (variable_get('privatemsg_group_admin_grant', FALSE)) { + $group = node_load($nid); + return og_is_group_admin($group); + } + + return og_is_group_member($nid); + } + + // give access only to users that have group subscriptions + return (bool) (count(og_get_subscriptions($user->uid))); +} + +/** + * Load a number of groups based on their nid. + */ +function privatemsg_groups_load_multiple($nids) { + $result = db_query("SELECT n.title, n.nid AS recipient FROM {node} n JOIN {og} og ON n.nid = og.nid WHERE n.status = 1 AND n.nid IN (" . db_placeholders($nids) . ")", $nids); + $groups = array(); + while ($group = db_fetch_object($result)) { + $group->type = 'group'; + $groups[privatemsg_recipient_key($group)] = $group; + } + + return $groups; +} + +/** + * Format a group to be displayed as a recipient. + */ +function theme_privatemsg_groups_format($group, $options = array()) { + if (!empty($options['plain'])) { + $name = $group->title; + if (!empty($options['unique'])) { + $name .= ' [group]'; + } + return $name.'(group)'; + } + return t('%group (group)', array('%group' => $group->title)); +} + +/** + * Loads users of a specific group. + */ +function privatemsg_groups_load_recipients($recipient, $limit, $offset) { + $recipients = array(); + $nid = isset($recipient->recipient) ? $recipient->recipient : $recipient->nid; + $result = db_query(og_list_users_sql(1, 0, null, false), $nid); + while ($row = db_fetch_object($result)) { + $recipients[] = $row->uid; + } + return $recipients; +} + +/** + * Return the number of users which have a given group. + */ +function privatemsg_groups_count_recipients($recipient) { + $nid = isset($recipient->recipient) ? $recipient->recipient : $recipient->nid; + return db_result(db_query(og_list_users_sql(1, 0, null, true), $nid)); +} + +/** + * Provides autocomplete suggestions for groups. + */ +function privatemsg_groups_autocomplete($fragment, $names, $limit) { + $query = _privatemsg_assemble_query(array('autocomplete_groups', 'privatemsg_groups'), $fragment, $names); + $result = db_query_range($query['query'], $fragment, 0, $limit); + // 3: Build proper suggestions and print. + $groups = array(); + while ($group = db_fetch_object($result)) { + if (user_access('write privatemsg to all groups') || (privatemsg_groups_access($group->nid))) { + $group->type = 'group'; + $group->recipient = $group->nid; + $groups[privatemsg_recipient_key($group)] = $group; + } + } + return $groups; +} + +/** + * Implements hook_privatemsg_name_lookup(). + */ +function privatemsg_groups_privatemsg_name_lookup($string) { + // Remove optonal group specifier. + $string = trim(str_replace(array(t('[group]'), '(group)'), '', $string)); + $result = db_query("SELECT n.*, n.nid AS recipient FROM {node} n JOIN {og} og ON og.nid = n.nid WHERE n.title = '%s' AND n.status = 1", trim($string)); + if ($group = db_fetch_object($result)) { + $group->type = 'group'; + return array(privatemsg_recipient_key($group) => $group); + } +} + +/** + * Query definition to search for username autocomplete suggestions. + * + * @param $fragments + * Query fragments array. + * @param $search + * Which search string is currently searched for. + * @param $names + * Array of names not to be used as suggestions. + */ +function privatemsg_groups_sql_autocomplete_groups(&$fragments, $search, $names) { + $fragments['primary_table'] = '{node} n'; + $fragments['select'][] = 'n.title, n.nid'; + + // Escape the % to get it through the placeholder replacement. + // Join another table. + $fragments['inner_join'][] = 'JOIN {og} og ON (n.nid = og.nid)'; + $fragments['where'][] = "n.title LIKE '%s' AND n.status = 1"; + $fragments['query_args']['where'][] = $search .'%%'; + if (!empty($names)) { + // If there are already names selected, exclude them from the suggestions. + $fragments['where'][] = "n.title NOT IN (". db_placeholders($names, 'text') .")"; + $fragments['query_args']['where'] += $names; + } + + $fragments['order_by'][] = 'n.title ASC'; +} diff --git a/privatemsg_groups/privatemsg_groups.test b/privatemsg_groups/privatemsg_groups.test new file mode 100644 index 0000000..a0943ed --- /dev/null +++ b/privatemsg_groups/privatemsg_groups.test @@ -0,0 +1,257 @@ + 'Privatemsg Group functionality', + 'description' => 'Tests sending messages to groups', + 'group' => 'Privatemsg', + ); + } + + function setUp() { + parent::setUp('privatemsg', 'privatemsg_groups', 'privatemsg_filter', 'pm_block_user', 'og'); + // Create a user with admin permissions. + $this->group_user_noperms = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg')); + $this->group_user = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to own groups')); + $this->super_admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to all groups', 'administer nodes', 'administer content types', 'access administration pages', 'administer site configuration', 'administer organic groups')); + } + + /** + * Helper function - create a group content. + * + * @param $type + * The type name of the content type. + * @param $og_type + * The og type - group or post. + * @param $edit + * An array of settings to add to the defaults. + * @param $keys + * An array with the keys that need to be present in the $node object + * after node_load(). + * @return + * The newly created node id. + */ + function _addOgContent($type, $og_type, $edit = array(), $keys = array()) { + $edit['title'] = $this->randomName(8); + $edit['body']= $this->randomName(32); + $type_hyphen = str_replace('_', '-', $type); + + $this->drupalPost('node/add/'. $type_hyphen, $edit, t('Save')); + + // Check that the form has been submitted. + $this->assertRaw(t('!type %title has been created.', array('!type' => $type, '%title' => $edit['title'])), t('%og_type created.', array('%og_type' => $og_type))); + + // Assert the node has loaded properly. + $node = node_load(array('title' => $edit['title'])); + $node = (array)$node; + $this->assertTrue($this->assertKeysExist($keys, $node), t('%og_type loaded properly.', array('%og_type' => $og_type))); + // Node was casted to an array. + $this->verbose('Group created: ' . var_export($node, TRUE)); + return $node; + } + + /** + * Helper function - create a group content. + * + */ + function _createGroup() { + // login as superadmin to create a group + $this->drupalLogin($this->super_admin); + + // create a group + $og_group_type = $this->drupalCreateContentType(); + variable_set('og_content_type_usage_'. $og_group_type->name, 'group'); + + // Create a group post content type. + $og_post_type = $this->drupalCreateContentType(); + variable_set('og_content_type_usage_'. $og_post_type->name, 'group_post_standard'); + + // Rebuild the menu so the new content types will appear in the menu. + menu_rebuild(); + + // Create a group node. + $group = (object) $this->addOgGroup($og_group_type->name); + $this->verbose('Group : ' . var_export($group, TRUE)); + return $group; + } + + /** + * Verify that deleting a user does not leave stale data behind. + */ + function testDeleteUser() { + // Create a group node. + $group = (object) $this->_createGroup(); + + // subscribe normal user to group + og_subscribe_user($group->nid, $this->group_user); + + $edit = array( + 'recipient' => $group->title . '[group]', + 'subject' => $this->randomName(10), + 'body' => $this->randomName(50), + ); + $this->drupalPost('messages/new', $edit, t('Send message')); + $this->assertText(t('A message has been sent to @group (group).', array('@group' => $group->title))); + + // check if normal user subscribed to group has received the message and have access to group write + $this->drupalLogin($this->group_user); + $this->drupalGet('messages'); + $this->assertRaw($edit['subject'], t('Message is displayed')); + + // Delete user and make sure that no recipient entries of him are left in + // table. + user_delete(array(), $this->group_user->uid); + $this->assertFalse(db_result(db_query_range("SELECT 1 FROM {pm_index} WHERE recipient = %d AND type IN ('group', 'hidden')", $this->group_user->uid, 0, 1))); + } + + /** + * Test the simple case of sending/receiving a message to group by priviliged users + */ + function testMessageToGroup() { + // Create a group node. + $group = (object) $this->_createGroup(); + + // subscribe normal user to group + og_subscribe_user($group->nid, $this->group_user); + + $edit = array( + 'recipient' => $group->title . '[group]', + 'subject' => $this->randomName(10), + 'body' => $this->randomName(50), + ); + $this->drupalPost('messages/new', $edit, t('Send message')); + $this->assertText(t('A message has been sent to @group (group).', array('@group' => $group->title))); + + + // check if normal user subscribed to group has received the message and have access to group write + $this->drupalLogin($this->group_user); + $this->drupalGet('messages'); + $this->assertRaw($edit['subject'] . ' new', t('Message is displayed as new')); + + // check if user has access to group as recipient + $this->clickLink($edit['subject']); + $this->assertText(t('@group (group)', array('@group' => $group->title))); + + // Reply message to group + $reply1 = array( + 'body' => $this->randomName(50), + ); + $this->drupalPost(NULL, $reply1, t('Send message')); + + // check if the message has been sent to group + $this->assertText(t('@group (group).', array('@group' => $group->title))); + + // try to send a new message to group (must be permitted) + $this->drupalPost('messages/new', $edit, t('Send message')); + $this->assertText(t('A message has been sent to @group (group).', array('@group' => $group->title))); + } + + + /** + * Test the simple case of sending a message by an administrator + * and receiving and reply a message by unprilived user subscribed to group. + */ + function testMessageToGroupWithNoPermissionUserToReplyToGroup() { + // Create a group node. + $group = (object) $this->_createGroup(); + + // subscribe normal user to group + og_subscribe_user($group->nid, $this->group_user_noperms); + + $edit = array( + 'recipient' => $group->title . '[group]', + 'subject' => $this->randomName(10), + 'body' => $this->randomName(50), + ); + $this->drupalPost('messages/new', $edit, t('Send message')); + $this->assertText(t('A message has been sent to @group (group).', array('@group' => $group->title))); + + // check if normal user subscribed to group has received the message + $this->drupalLogin($this->group_user_noperms); + $this->drupalGet('messages'); + $this->assertRaw($edit['subject'] . ' new', t('Message is displayed as new')); + + // check if user has access to group as recipient + $this->clickLink($edit['subject']); + $this->assertNoText(t('@group (group)', array('@group' => $group->title))); + + + // Reply message to group + $reply1 = array( + 'body' => $this->randomName(50), + ); + $this->drupalPost(NULL, $reply1, t('Send message')); + + // check if the message has been sent to group (they can't) + $this->assertNoText(t('@group (group).', array('@group' => $group->title))); + + // try to send a message to group (must be not permitted) + $this->drupalPost('messages/new', $edit, t('Send message')); + $this->assertText(t('You must include at least one valid recipient.')); + } + + + + /** + * Test the simple case of sending a message by an administrator + * and receiving and reply to this message by a priviliged user subscribed to group but with "grand to admin" setting activated. + */ + function testMessageToGroupWithPermOnlyGrantAdmin() { + variable_set('privatemsg_group_admin_grant', 1); + + // Create a group node. + $group = (object) $this->_createGroup(); + + // subscribe normal user to group + og_subscribe_user($group->nid, $this->group_user_noperms); + og_subscribe_user($group->nid, $this->group_user); + + $edit = array( + 'recipient' => $group->title . '[group]', + 'subject' => $this->randomName(10), + 'body' => $this->randomName(50), + ); + + $this->drupalPost('messages/new', $edit, t('Send message')); + $this->assertText(t('A message has been sent to @group (group).', array('@group' => $group->title))); + + // check if normal user subscribed to group has received the message + $accounts = array($this->group_user, $this->group_user_noperms); + foreach ($accounts as $account) { + $this->drupalLogin($account); + $this->drupalGet('messages'); + $this->assertRaw($edit['subject'] . ' new', t('Message is displayed as new')); + + // check if user has access to group as recipient + $this->clickLink($edit['subject']); + $this->assertNoText(t('@group (group)', array('@group' => $group->title))); + + // Reply message to group + $reply1 = array( + 'body' => $this->randomName(50), + ); + $this->drupalPost(NULL, $reply1, t('Send message')); + + // check if the message has been sent to group (not permitted) + $this->assertNoText(t('@group (group).', array('@group' => $group->title))); + + // send a new message to group (must be not permitted) + $this->drupalPost('messages/new', $edit, t('Send message')); + $this->assertText(t('You must include at least one valid recipient.')); + } + } +}