| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_help()
|
| 6 |
*/
|
| 7 |
function signature_forum_help($section='') {
|
| 8 |
switch ($section) {
|
| 9 |
case 'admin/modules#description':
|
| 10 |
return t('Manages user\'s signatures in the style most forum users will be familiar with. Allows much longer signatures than the Drupal default users can also use BBCode or HTML in their signatures.');
|
| 11 |
};
|
| 12 |
}
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implementation of hook_perm()
|
| 16 |
*/
|
| 17 |
function signature_forum_perm() {
|
| 18 |
return array('administer signature');
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Implementation of hook_menu()
|
| 23 |
*/
|
| 24 |
function signature_forum_menu($may_cache) {
|
| 25 |
$items = array();
|
| 26 |
$items[] = array(
|
| 27 |
'path' => 'admin/settings/signature_forum',
|
| 28 |
'title' => t('Signatures'),
|
| 29 |
'description' => t('Manages users\' signatures.'),
|
| 30 |
'callback' => 'drupal_get_form',
|
| 31 |
'callback arguments' => array('signature_forum_admin_settings'),
|
| 32 |
'access' => user_access('administer signature'),
|
| 33 |
'type' => MENU_NORMAL_ITEM, // optional
|
| 34 |
);
|
| 35 |
return $items;
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Admin settings form
|
| 40 |
*/
|
| 41 |
function signature_forum_admin_settings() {
|
| 42 |
$form['signature'] = array(
|
| 43 |
'#type' => 'fieldset',
|
| 44 |
'#description' => t('Show signatures with nodes and comments for: '),
|
| 45 |
);
|
| 46 |
|
| 47 |
foreach (node_get_types('names') as $type => $name) {
|
| 48 |
$form['signature']['signature_forum_show_for_'.$type] = array(
|
| 49 |
'#type' => 'checkbox',
|
| 50 |
'#title' => $name,
|
| 51 |
'#return_value' => 1,
|
| 52 |
'#default_value' => variable_get('signature_forum_show_for_'.$type, '0'),
|
| 53 |
);
|
| 54 |
}
|
| 55 |
|
| 56 |
$form['signature']['signature_forum_template'] = array (
|
| 57 |
'#type' => 'textarea',
|
| 58 |
'#title' => 'Template for signatures:',
|
| 59 |
'#default_value' => variable_get('signature_forum_template', "__________________________\n<p>%s</p>"),
|
| 60 |
'#description' => '<strong>%s</strong> will be replaced with user\'s actual signature.',
|
| 61 |
);
|
| 62 |
|
| 63 |
$form['signature']['filter'] = filter_form(variable_get('signature_forum_format', FILTER_FORMAT_DEFAULT));
|
| 64 |
|
| 65 |
return system_settings_form($form);
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Admin settings form submit
|
| 70 |
*/
|
| 71 |
function signature_forum_admin_settings_submit($form_id, $form_values) {
|
| 72 |
foreach($form_values as $form_value_key => $form_value_value) {
|
| 73 |
if (substr($form_value_key, 0, strlen('signature_forum_show_for_')) == 'signature_forum_show_for_') {
|
| 74 |
variable_set($form_value_key, $form_value_value);
|
| 75 |
}
|
| 76 |
}
|
| 77 |
variable_set('signature_forum_template', $form_values['signature_forum_template']);
|
| 78 |
variable_set('signature_forum_format', $form_values['format']);
|
| 79 |
}
|
| 80 |
|
| 81 |
/**
|
| 82 |
* Implementation of hook_form_alter()
|
| 83 |
*/
|
| 84 |
function signature_forum_form_alter($form_id, &$form) {
|
| 85 |
// if signatures are disabled for all content types, hide signature field in user profile settings
|
| 86 |
if ($form_id == 'user_edit') {
|
| 87 |
$enabled = false;
|
| 88 |
foreach(node_get_types('names') as $type => $name) {
|
| 89 |
if (variable_get('signature_forum_show_for_'. $type, '0')) {
|
| 90 |
$enabled = true;
|
| 91 |
break;
|
| 92 |
}
|
| 93 |
}
|
| 94 |
if (!$enabled) {
|
| 95 |
unset($form['comment_settings']);
|
| 96 |
}
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Implementation of hook_user()
|
| 102 |
*/
|
| 103 |
function signature_forum_user($op, &$edit, &$account, $category = NULL) {
|
| 104 |
// Re-route the user signature to this module's signature table
|
| 105 |
if ($op == 'submit' && $category == 'account') {
|
| 106 |
// If the user has a signature set update it, otherwise create a new entry
|
| 107 |
if (db_result(db_query('SELECT uid FROM {users_signature} WHERE uid=%d', $account->uid)) != '') {
|
| 108 |
db_query('UPDATE {users_signature} SET signature=\'%s\' WHERE uid=%d', array($edit['signature'], $account->uid));
|
| 109 |
}
|
| 110 |
else {
|
| 111 |
db_query('INSERT INTO {users_signature} (uid, signature) VALUES (%d, \'%s\')', array($account->uid, $edit['signature']));
|
| 112 |
}
|
| 113 |
unset($edit['signature']);
|
| 114 |
}
|
| 115 |
else if ($op == 'load') {
|
| 116 |
static $signature;
|
| 117 |
// We keep this in memory because load is called several times
|
| 118 |
if (!isset($signature)) {
|
| 119 |
$signature = db_result(db_query('SELECT signature FROM {users_signature} WHERE uid=%d', $account->uid));
|
| 120 |
}
|
| 121 |
$account->signature = $signature;
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
/**
|
| 126 |
* Implementation of hook_nodeapi()
|
| 127 |
*/
|
| 128 |
function signature_forum_nodeapi(&$node, $op, $teaser, $page) {
|
| 129 |
if (variable_get('signature_forum_show_for_'.$node->type, '0')) {
|
| 130 |
$node->signature = db_result(db_query('SELECT signature FROM {users_signature} WHERE uid = %d', $node->uid));
|
| 131 |
switch ($op) {
|
| 132 |
case 'view':
|
| 133 |
// Add signature to node body text
|
| 134 |
$node->content['body']['#value'] = $node->content['body']['#value'] . theme('signature_forum', $node->signature);
|
| 135 |
break;
|
| 136 |
}
|
| 137 |
}
|
| 138 |
}
|
| 139 |
|
| 140 |
/**
|
| 141 |
* Implementation of hook_comment()
|
| 142 |
*/
|
| 143 |
function signature_forum_comment(&$comment, $op) {
|
| 144 |
static $node_type;
|
| 145 |
|
| 146 |
if (!isset($node_type)) {
|
| 147 |
$node_type = db_result(db_query('SELECT type FROM {node} WHERE nid = %d', $comment->nid));
|
| 148 |
}
|
| 149 |
|
| 150 |
if ($op == 'view' && variable_get('signature_forum_show_for_'. $node_type, '0')) {
|
| 151 |
static $cache;
|
| 152 |
if (!isset($cache)) {
|
| 153 |
$result = db_query('SELECT u.uid, u.signature FROM {comments} c
|
| 154 |
INNER JOIN {users_signature} u ON c.uid = u.uid
|
| 155 |
WHERE c.nid = %d AND c.status = 0 AND NOT(u.signature = "")
|
| 156 |
GROUP BY u.uid', $comment->nid);
|
| 157 |
$cache = array();
|
| 158 |
while($row = db_fetch_object($result)) {
|
| 159 |
$cache[$row->uid] = $row->signature;
|
| 160 |
}
|
| 161 |
}
|
| 162 |
|
| 163 |
if (isset($cache[$comment->uid])) {
|
| 164 |
$comment->comment .= theme('signature_forum', $cache[$comment->uid]);
|
| 165 |
}
|
| 166 |
}
|
| 167 |
}
|
| 168 |
|
| 169 |
/**
|
| 170 |
* Format user signature
|
| 171 |
*
|
| 172 |
* @ingroup themeable
|
| 173 |
*/
|
| 174 |
function theme_signature_forum($signature) {
|
| 175 |
$format = variable_get('signature_forum_format', FILTER_FORMAT_DEFAULT);
|
| 176 |
$template = variable_get('signature_forum_template', "__________________________\n<p>%s</p>");
|
| 177 |
$sig_temp = check_markup($signature, $format);
|
| 178 |
$sig_temp = trim(sprintf("\n". $template, $sig_temp));
|
| 179 |
|
| 180 |
return '<div class="signature">'. $sig_temp .'</div>';
|
| 181 |
}
|