Skip to content

Instantly share code, notes, and snippets.

@dawehner
Created May 9, 2009 23:51
Show Gist options
  • Save dawehner/109447 to your computer and use it in GitHub Desktop.
Save dawehner/109447 to your computer and use it in GitHub Desktop.
<?php
// $Id$
function pm_role_admin_settings() {
$form = array();
$roles = user_roles(TRUE);
foreach ($roles as $rid1 => $role1) {
foreach ($roles as $rid2 => $role2) {
$form['foo_messages_role_sent_from'. $rid1 .'_to_'. $rid2] = array(
'#type' => 'checkbox',
'#title' => t('@role1 cannot sent to @role2'),
'#description' => t('@role1 is not allowed to sent messages to @role2'),
);
}
}
return system_settings_form($form);
}
; $Id$
name = Privatemsg Role permissions
description = Adds permissions between roles, wether they can sent mesages to each other.
core = 6.x
<?php
// $Id$
/**
* Implementation of hook_form_privatemsg_new_alter().
*/
function pm_role_form_privatemsg_new_alter(&$form, &$form_state) {
$form['#validate'][] = 'pm_role_form_privatemsg_new_validate';
}
function pm_role_form_privatemsg_new_validate(&$form, &$form_state) {
// @TODO
// set a form_set_error if the user has entered a not allowed user.
}
/**
* Implementation of hook_menu().
*/
function pm_role_menu() {
$items['admin/settings/pm_role_settings'] = array(
'title' => 'Pm Role settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('pm_role_admin_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'pm_role.admin.inc',
);
return $items;
}
/**
* Implementation of hook_menu_alter().
*/
function pm_role_menu_alter() {
$callbacks['messages/user-name-autocomplete']['page callback'] = 'foo_messages_autocomplete_callback';
}
/**
* This function does the same as the normal privatemsg autocomplete but only returns valid users.
* @see privatemsg_user_name_autocomplete
*
* @TODO
* Don't use the default one :)
*/
function pm_role_messages_autocomplete_callback($string) {
$names = array();
// 1: Parse $string and build list of valid user names.
$fragments = explode(',', $string);
foreach ($fragments as $index => $name) {
$name = trim($name);
if ($error = module_invoke('user', 'validate_name', $name)) {
// Do nothing if this name does not validate.
}
else {
$names[$name] = $name;
}
}
// By using user_validate_user we can ensure that names included in $names are at least logisticaly possible.
// 2: Find the next user name suggestion.
$fragment = array_pop($names);
if (!empty($fragment)) {
$query = "SELECT name FROM {users} u WHERE name like '%s%%'";
$query .= " AND name NOT IN ('". implode("', '", $names) ."')"; // This will prevent suggesting a name that is already in our list.
$query .= " AND status <> 0 ORDER BY name ASC";
$result = db_query_range($query, $fragment, 0, 10);
$prefix = count($names) ? implode(", ", $names) .", " : '';
// 3: Build proper suggestions and print.
$matches = array();
while ($user = db_fetch_object($result)) {
$matches[$prefix . $user->name .", "] = $user->name;
}
print drupal_to_js($matches);
exit();
}
}
<?php
// $Id$
class pmRoleTestCase extends DrupalWebTestCase {
static function getInfo() {
return array(
'name' => t('Privatemsg Role Permission.'),
'group' => t('Privatemsg'),
);
}
function setUp() {
parent::setUp('privatemsg', 'pm_role');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment