| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Enables the use of contact forms for Organic Groups
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_help().
|
| 11 |
*/
|
| 12 |
function og_contact_help($path, $arg) {
|
| 13 |
switch ($path) {
|
| 14 |
case 'admin/help#og_contact':
|
| 15 |
$output = '<p>'. t('The OG contact module enables the use of contact module style contact forms on a per group basis. The emails are sent to the administrators of the Organic Group and/or a recipientes list. Users can specify a subject and message in the contact form, and also request that a copy of the e-mail be sent to their own address.') .'</p>';
|
| 16 |
return $output;
|
| 17 |
case 'admin/og/contact':
|
| 18 |
$output = '<p>'. t('This page lets you set up contact forms for Organic Groups. To do so, <a href="@groups">add one or more groups</a>. The recipients for each group can either be the administrators of that group, a list of recipients, or both. On the <a href="@settings">settings page</a>, you can customize the information shown above the group contact forms, as well as some settings for the module. These settings apply to all group contact forms.', array('@groups' => url('admin/og/contact/add'), '@settings' => url('admin/og/contact/settings'))) .'</p>';
|
| 19 |
$output .= '<p>'. t('Groups that have contact forms enabled will get a contact page with a URL like "node/nid/contact."') .'</p>';
|
| 20 |
return $output;
|
| 21 |
}
|
| 22 |
}
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implementation of hook_perm().
|
| 26 |
*/
|
| 27 |
function og_contact_perm() {
|
| 28 |
return array('access og contact form', 'administer og contact form', 'administer group contact form', 'receive og contact submissions');
|
| 29 |
}
|
| 30 |
|
| 31 |
/**
|
| 32 |
* Implementation of hook_menu().
|
| 33 |
*/
|
| 34 |
function og_contact_menu() {
|
| 35 |
$items = array();
|
| 36 |
$items['admin/og/contact'] = array(
|
| 37 |
'title' => 'OG Contact form',
|
| 38 |
'description' => 'Create a contact module like form for Organic Groups.',
|
| 39 |
'page callback' => 'og_contact_admin_groups',
|
| 40 |
'access arguments' => array('administer og contact form'),
|
| 41 |
'file' => 'og_contact.admin.inc',
|
| 42 |
);
|
| 43 |
$items['admin/og/contact/list'] = array(
|
| 44 |
'title' => 'List',
|
| 45 |
'page callback' => 'og_contact_admin_groups',
|
| 46 |
'access arguments' => array('administer og contact form'),
|
| 47 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 48 |
'file' => 'og_contact.admin.inc',
|
| 49 |
);
|
| 50 |
$items['admin/og/contact/add'] = array(
|
| 51 |
'title' => 'Add Group',
|
| 52 |
'page callback' => 'drupal_get_form',
|
| 53 |
'page arguments' => array('og_contact_admin_add'),
|
| 54 |
'access arguments' => array('administer og contact form'),
|
| 55 |
'type' => MENU_LOCAL_TASK,
|
| 56 |
'weight' => 1,
|
| 57 |
'file' => 'og_contact.admin.inc',
|
| 58 |
);
|
| 59 |
$items['admin/og/contact/edit/%og_contact_admin'] = array(
|
| 60 |
'title' => 'Edit Group contact',
|
| 61 |
'page callback' => 'drupal_get_form',
|
| 62 |
'page arguments' => array('og_contact_admin_edit'),
|
| 63 |
'access arguments' => array('administer og contact form'),
|
| 64 |
'type' => MENU_CALLBACK,
|
| 65 |
'file' => 'og_contact.admin.inc',
|
| 66 |
);
|
| 67 |
$items['admin/og/contact/delete/%og_contact_del_group'] = array(
|
| 68 |
// $items['admin/og/contact/delete/%og_contact_admin'] = array(
|
| 69 |
'title' => 'Delete contact',
|
| 70 |
'page callback' => 'drupal_get_form',
|
| 71 |
'page arguments' => array('og_contact_admin_delete'),
|
| 72 |
'access arguments' => array('administer og contact form'),
|
| 73 |
'type' => MENU_CALLBACK,
|
| 74 |
'file' => 'og_contact.admin.inc',
|
| 75 |
);
|
| 76 |
$items['admin/og/contact/settings'] = array(
|
| 77 |
'title' => 'Settings',
|
| 78 |
'page callback' => 'drupal_get_form',
|
| 79 |
'page arguments' => array('og_contact_admin_settings'),
|
| 80 |
'access arguments' => array('administer og contact form'),
|
| 81 |
'type' => MENU_LOCAL_TASK,
|
| 82 |
'weight' => 4,
|
| 83 |
'file' => 'og_contact.admin.inc',
|
| 84 |
);
|
| 85 |
$items['admin/og/contact/addall'] = array(
|
| 86 |
'title' => 'Add all groups',
|
| 87 |
'page callback' => 'drupal_get_form',
|
| 88 |
'page arguments' => array('og_contact_admin_add_all'),
|
| 89 |
'access arguments' => array('administer og contact form'),
|
| 90 |
'type' => MENU_LOCAL_TASK,
|
| 91 |
'weight' => 3,
|
| 92 |
'file' => 'og_contact.admin.inc',
|
| 93 |
);
|
| 94 |
$items['admin/og/contact/deleteall'] = array(
|
| 95 |
'title' => 'Delete all groups',
|
| 96 |
'page callback' => 'drupal_get_form',
|
| 97 |
'page arguments' => array('og_contact_admin_delete_all'),
|
| 98 |
'access arguments' => array('administer og contact form'),
|
| 99 |
'type' => MENU_LOCAL_TASK,
|
| 100 |
'weight' => 3,
|
| 101 |
'file' => 'og_contact.admin.inc',
|
| 102 |
);
|
| 103 |
|
| 104 |
// contact tab on group node
|
| 105 |
$items['node/%og_contact_page/contact'] = array(
|
| 106 |
'title' => 'Contact',
|
| 107 |
'page callback' => 'og_contact_page',
|
| 108 |
'access arguments' => array('access og contact form'),
|
| 109 |
'type' => variable_get('og_contact_display_tab', 1) ? MENU_LOCAL_TASK : MENU_CALLBACK,
|
| 110 |
'weight' => 6,
|
| 111 |
'file' => 'og_contact.pages.inc',
|
| 112 |
);
|
| 113 |
$items['node/%og_contact_admin_single/contact/edit'] = array(
|
| 114 |
'title' => 'Edit',
|
| 115 |
'page callback' => 'drupal_get_form',
|
| 116 |
'page arguments' => array('og_contact_admin_edit'),
|
| 117 |
'access callback' => 'og_contact_can_edit_group_settings',
|
| 118 |
'type' => MENU_CALLBACK,
|
| 119 |
'file' => 'og_contact.admin.inc',
|
| 120 |
);
|
| 121 |
$items['ogcontact/autocomplete'] = array(
|
| 122 |
'title' => 'Autocomplete recipients',
|
| 123 |
'page callback' => 'og_contact_recipients_autocomplete',
|
| 124 |
'access callback' => 'og_contact_can_edit_group_settings',
|
| 125 |
'type' => MENU_CALLBACK,
|
| 126 |
'file' => 'og_contact.admin.inc',
|
| 127 |
);
|
| 128 |
$items['ogcontact/grouprecipients'] = array(
|
| 129 |
'title' => 'Group recipients return',
|
| 130 |
'page callback' => 'og_contact_group_recipients_js',
|
| 131 |
'access callback' => 'og_contact_can_edit_group_settings',
|
| 132 |
'type' => MENU_CALLBACK,
|
| 133 |
'file' => 'og_contact.admin.inc',
|
| 134 |
);
|
| 135 |
$items['ogcontact/removerecipient'] = array(
|
| 136 |
'title' => 'Group recipient remove',
|
| 137 |
'page callback' => 'og_contact_recipients_remove_js',
|
| 138 |
'access callback' => 'og_contact_can_edit_group_settings',
|
| 139 |
'type' => MENU_CALLBACK,
|
| 140 |
'file' => 'og_contact.admin.inc',
|
| 141 |
);
|
| 142 |
return $items;
|
| 143 |
}
|
| 144 |
|
| 145 |
/**
|
| 146 |
* Implementation of hook_nodeapi().
|
| 147 |
*/
|
| 148 |
function og_contact_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
|
| 149 |
switch ($op) {
|
| 150 |
case 'insert':
|
| 151 |
if (og_is_group_type($node->type) == 1 && variable_get('og_contact_enable_all', 0)) {
|
| 152 |
db_query("INSERT INTO {og_contact} (gid, reply) VALUES ('%d','%s')", $node->nid, '');
|
| 153 |
drupal_set_message(t('Contact form for %group added.', array('%group' => $node->title)));
|
| 154 |
watchdog('mail', 'OG Contact: Form for %group added.', array('%group' => $node->title), WATCHDOG_NOTICE, l(t('view'), 'admin/og/contact'));
|
| 155 |
}
|
| 156 |
break;
|
| 157 |
case 'delete':
|
| 158 |
if (og_is_group_type($node->type) == 1) {
|
| 159 |
db_query("DELETE FROM {og_contact} WHERE gid = %d", $node->nid);
|
| 160 |
drupal_set_message(t('The group contact form for %group has been deleted.', array('%group' => $node->title)));
|
| 161 |
watchdog('mail', 'OG Contact: Form for %group was deleted.', array('%group' => $node->title), WATCHDOG_NOTICE);
|
| 162 |
}
|
| 163 |
break;
|
| 164 |
}
|
| 165 |
}
|
| 166 |
|
| 167 |
/**
|
| 168 |
* Implementation of hook_mail().
|
| 169 |
*/
|
| 170 |
function og_contact_mail($key, &$message, $params) {
|
| 171 |
// print '<pre>'; print_r($params); print '<br/>'; print_r($message); die;
|
| 172 |
$language = $message['language'];
|
| 173 |
|
| 174 |
$group = og_contact_group_load($params['gid']);
|
| 175 |
|
| 176 |
switch ($key) {
|
| 177 |
case 'page_mail':
|
| 178 |
case 'page_copy':
|
| 179 |
$contact = $params['contact'];
|
| 180 |
|
| 181 |
if (variable_get('og_contact_group_name_in_subject', 0)) {
|
| 182 |
$message['subject'] = t('[!groupname] !subject', array('!groupname' => $group->name, '!subject' => $params['subject']), $language->language);
|
| 183 |
}
|
| 184 |
else {
|
| 185 |
$message['subject'] = t('!subject', array('!subject' => $params['subject']), $language->language);
|
| 186 |
}
|
| 187 |
$message['body'][] = t("!name sent a message using the contact form at !form.", array('!name' => $params['name'], '!form' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language))), $language->language);
|
| 188 |
$message['body'][] = $params['message'];
|
| 189 |
break;
|
| 190 |
case 'page_autoreply':
|
| 191 |
$contact = $params['contact'];
|
| 192 |
if (variable_get('og_contact_group_name_in_subject', 0)) {
|
| 193 |
$message['subject'] = t('[!groupname] !subject', array('!groupname' => $group->name, '!subject' => $params['subject']), $language->language);
|
| 194 |
}
|
| 195 |
else {
|
| 196 |
$message['subject'] = t('!subject', array('!subject' => $params['subject']), $language->language);
|
| 197 |
}
|
| 198 |
$message['body'][] = $group->reply;
|
| 199 |
break;
|
| 200 |
}
|
| 201 |
}
|
| 202 |
|
| 203 |
/**
|
| 204 |
* Check if user can edit the contact form settings
|
| 205 |
*/
|
| 206 |
function og_contact_can_edit_group_settings() {
|
| 207 |
if (user_access('administer group contact form') || user_access('administer og contact form')) {
|
| 208 |
return TRUE;
|
| 209 |
}
|
| 210 |
}
|
| 211 |
|
| 212 |
/**
|
| 213 |
* Generate an options array for form selects of groups that don't have
|
| 214 |
* contact forms.
|
| 215 |
*/
|
| 216 |
function og_contact_group_select_form_options() {
|
| 217 |
$groups = og_all_groups_options();
|
| 218 |
$options = array();
|
| 219 |
foreach ($groups as $gid => $name) {
|
| 220 |
if (!og_contact_group_has_form($gid)) {
|
| 221 |
$options[$gid] = $name;
|
| 222 |
}
|
| 223 |
}
|
| 224 |
return $options;
|
| 225 |
}
|
| 226 |
|
| 227 |
/**
|
| 228 |
* Check if group already has form
|
| 229 |
*/
|
| 230 |
function og_contact_group_has_form($gid) {
|
| 231 |
$query = db_query('SELECT COUNT(*) FROM {og_contact} WHERE gid=%d', $gid);
|
| 232 |
if (db_result($query) > 0) {
|
| 233 |
return TRUE;
|
| 234 |
}
|
| 235 |
}
|
| 236 |
|
| 237 |
/**
|
| 238 |
* Check if a group contact form is private
|
| 239 |
*/
|
| 240 |
function og_contact_group_contact_private($gid) {
|
| 241 |
$private = db_result(db_query('SELECT notpublic FROM {og_contact} WHERE gid=%d', $gid));
|
| 242 |
if ($private == 1) {
|
| 243 |
return TRUE;
|
| 244 |
}
|
| 245 |
}
|
| 246 |
|
| 247 |
/**
|
| 248 |
* Check if user can view private form
|
| 249 |
*/
|
| 250 |
function og_contact_can_view_contact($uid, $gid) {
|
| 251 |
if (og_contact_group_contact_private($gid) && og_is_group_member($gid, TRUE, $uid)) {
|
| 252 |
return TRUE;
|
| 253 |
}
|
| 254 |
elseif (!og_contact_group_contact_private($gid)) {
|
| 255 |
return TRUE;
|
| 256 |
}
|
| 257 |
}
|
| 258 |
|
| 259 |
/**
|
| 260 |
* Get group name from nid/gid
|
| 261 |
*/
|
| 262 |
function og_contact_get_group_name($gid) {
|
| 263 |
$name = db_result(db_query('SELECT title FROM {node} WHERE nid=%d', $gid));
|
| 264 |
return $name;
|
| 265 |
}
|
| 266 |
|
| 267 |
/**
|
| 268 |
* Format contact form recipient addresses
|
| 269 |
*/
|
| 270 |
function og_contact_get_recipients($gid) {
|
| 271 |
// pull in recipients and recipient settings for the groups
|
| 272 |
$group_rules = db_fetch_object(db_query('SELECT noadmin,recipients,grouprecipients FROM {og_contact} WHERE gid=%d', $gid));
|
| 273 |
|
| 274 |
// get group admins when enabled
|
| 275 |
if ($group_rules->noadmin == 0) {
|
| 276 |
$admin_query = db_query('SELECT uid FROM {og_uid} WHERE nid=%d AND is_admin=1 AND is_active=1', $gid);
|
| 277 |
$admin_uids = array();
|
| 278 |
while ($admin = db_fetch_object($admin_query)) {
|
| 279 |
$admin_uids[$admin->uid] = $admin->uid;
|
| 280 |
}
|
| 281 |
}
|
| 282 |
// get group recipients when enabled
|
| 283 |
if ($group_rules->grouprecipients == 1) {
|
| 284 |
$group_uids = array();
|
| 285 |
$group_uids = og_contact_return_group_recipients($gid);
|
| 286 |
}
|
| 287 |
|
| 288 |
// build and array of user recipient uids
|
| 289 |
if (count($admin_uids) && count($group_uids)) {
|
| 290 |
$uids = array_merge($admin_uids, $group_uids);
|
| 291 |
}
|
| 292 |
elseif (count($admin_uids)) {
|
| 293 |
$uids = $admin_uids;
|
| 294 |
}
|
| 295 |
elseif (count($group_uids)) {
|
| 296 |
$uids = $group_uids;
|
| 297 |
}
|
| 298 |
|
| 299 |
// get each user recipients mail address
|
| 300 |
if (count($uids)) {
|
| 301 |
$i = 0;
|
| 302 |
foreach ($uids as $key => $uid) {
|
| 303 |
$mail = db_result(db_query('SELECT mail FROM {users} WHERE uid=%d', $uid));
|
| 304 |
// don't append a comma to the first address
|
| 305 |
// and don't append the address if its already in the recipients list
|
| 306 |
if ($i == 0 && !strstr($group_rules->recipients, $mail)) {
|
| 307 |
$mails = $mail;
|
| 308 |
$i++;
|
| 309 |
}
|
| 310 |
elseif (!strstr($group_rules->recipients, $mail)) {
|
| 311 |
$mails .= ', '. $mail;
|
| 312 |
}
|
| 313 |
}
|
| 314 |
// append group recipients when sent with other user recipients
|
| 315 |
if ($group_rules->recipients) {
|
| 316 |
$mails .= ','. $group_rules->recipients;
|
| 317 |
}
|
| 318 |
}
|
| 319 |
elseif ($group_rules->recipients) {
|
| 320 |
$mails = $group_rules->recipients;
|
| 321 |
}
|
| 322 |
return $mails;
|
| 323 |
}
|
| 324 |
|
| 325 |
/**
|
| 326 |
* Load OG Contact information about a group
|
| 327 |
*/
|
| 328 |
function og_contact_group_load($gid) {
|
| 329 |
// Load the group information:
|
| 330 |
$group = db_fetch_object(db_query("SELECT * FROM {og_contact} WHERE gid = %d", $gid));
|
| 331 |
$groupname = og_contact_get_group_name($gid);
|
| 332 |
$group->name = $groupname;
|
| 333 |
return $group;
|
| 334 |
}
|
| 335 |
|
| 336 |
/**
|
| 337 |
* Load admin pages
|
| 338 |
*/
|
| 339 |
function og_contact_admin_load($arg) {
|
| 340 |
if (!is_numeric($arg)) {
|
| 341 |
return FALSE;
|
| 342 |
}
|
| 343 |
if ($node = node_load($arg)) {
|
| 344 |
if (og_is_group_type($node->type) && og_contact_group_has_form($node->nid)) {
|
| 345 |
return $node;
|
| 346 |
}
|
| 347 |
}
|
| 348 |
return FALSE;
|
| 349 |
}
|
| 350 |
|
| 351 |
/**
|
| 352 |
* Load single admin pages
|
| 353 |
*/
|
| 354 |
function og_contact_admin_single_load($arg) {
|
| 355 |
if (!is_numeric($arg)) {
|
| 356 |
return FALSE;
|
| 357 |
}
|
| 358 |
if ($node = node_load($arg)) {
|
| 359 |
if (og_is_group_type($node->type) && variable_get('og_contact_group_admin_single', 1) == 1 && og_contact_group_has_form($node->nid)) {
|
| 360 |
return $node;
|
| 361 |
}
|
| 362 |
}
|
| 363 |
return FALSE;
|
| 364 |
}
|
| 365 |
|
| 366 |
/**
|
| 367 |
* Load contact form pages
|
| 368 |
*/
|
| 369 |
function og_contact_page_load($arg) {
|
| 370 |
if (!is_numeric($arg)) {
|
| 371 |
return FALSE;
|
| 372 |
}
|
| 373 |
if ($node = node_load($arg)) {
|
| 374 |
if (og_is_group_type($node->type) && og_contact_group_has_form($node->nid)) {
|
| 375 |
return $node;
|
| 376 |
}
|
| 377 |
}
|
| 378 |
return FALSE;
|
| 379 |
}
|
| 380 |
|
| 381 |
/**
|
| 382 |
* Load contact form delete pages
|
| 383 |
*/
|
| 384 |
function og_contact_del_group_load($arg) {
|
| 385 |
if (!is_numeric($arg)) {
|
| 386 |
return FALSE;
|
| 387 |
}
|
| 388 |
if ($node = node_load($arg)) {
|
| 389 |
if (og_is_group_type($node->type) && og_contact_group_has_form($node->nid)) {
|
| 390 |
return $node->nid;
|
| 391 |
}
|
| 392 |
}
|
| 393 |
return FALSE;
|
| 394 |
}
|
| 395 |
|
| 396 |
/**
|
| 397 |
* Check if a group has eligible non-admin recipients
|
| 398 |
*/
|
| 399 |
function og_contact_check_group_recipients($gid) {
|
| 400 |
$query = db_query('SELECT uid FROM {og_uid} WHERE nid=%d AND is_active=%d AND is_admin=%d', $gid, 1, 0);
|
| 401 |
$recipient_count = 0;
|
| 402 |
while ($uid = db_fetch_object($query)) {
|
| 403 |
if (user_access('receive og contact submissions', user_load($uid->uid))) {
|
| 404 |
$recipient_count++;
|
| 405 |
}
|
| 406 |
}
|
| 407 |
if ($recipient_count > 0) {
|
| 408 |
return TRUE;
|
| 409 |
}
|
| 410 |
}
|
| 411 |
|
| 412 |
/**
|
| 413 |
* Rerturn an array of group recipients
|
| 414 |
*/
|
| 415 |
function og_contact_return_group_recipients($gid) {
|
| 416 |
$query = db_query('SELECT uid FROM {og_uid} WHERE nid=%d AND is_active=%d AND is_admin=%d', $gid, 1, 0);
|
| 417 |
$users = array();
|
| 418 |
while ($uid = db_fetch_object($query)) {
|
| 419 |
if (user_access('receive og contact submissions', user_load($uid->uid))) {
|
| 420 |
$users[$uid->uid] = $uid->uid;
|
| 421 |
}
|
| 422 |
}
|
| 423 |
return $users;
|
| 424 |
}
|