| 1 |
<?php
|
| 2 |
// $Id: user_titles.module,v 1.14.2.2 2009/01/05 05:19:14 agileware Exp $
|
| 3 |
/**
|
| 4 |
* @file user_titles.module
|
| 5 |
* Allows assignment of titles to a user based upon the number of posts the
|
| 6 |
* user has made.
|
| 7 |
*/
|
| 8 |
|
| 9 |
define('DEFAULT_IMAGE_DIR', 'user-titles-images');
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_help().
|
| 13 |
*/
|
| 14 |
function user_titles_help($path, $arg) {
|
| 15 |
switch ($path) {
|
| 16 |
case 'admin/user/user-titles':
|
| 17 |
return t('Click "Save" to change settings or use the "Add title" tab to add a new title.');
|
| 18 |
}
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Implementation of hook_perm().
|
| 23 |
*/
|
| 24 |
function user_titles_perm() {
|
| 25 |
return array('administer user titles');
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Implementation of hook_menu().
|
| 30 |
*/
|
| 31 |
function user_titles_menu() {
|
| 32 |
$items['admin/user/user-titles'] = array(
|
| 33 |
'title' => t('User titles'),
|
| 34 |
'description' => 'Configure user titles and number of posts required',
|
| 35 |
'page callback' => 'drupal_get_form',
|
| 36 |
'page arguments' => array('user_titles_settings_form'),
|
| 37 |
'access arguments' => array('administer user titles'),
|
| 38 |
);
|
| 39 |
$items['admin/user/user-titles/list'] = array(
|
| 40 |
'title' => t('List'),
|
| 41 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 42 |
'weight' => -10
|
| 43 |
);
|
| 44 |
$items['admin/user/user-titles/add/title'] = array(
|
| 45 |
'title' => t('Add title'),
|
| 46 |
'page callback' => 'drupal_get_form',
|
| 47 |
'page arguments' => array('user_titles_title_form'),
|
| 48 |
'access arguments' => array('administer user titles'),
|
| 49 |
'type' => MENU_LOCAL_TASK
|
| 50 |
);
|
| 51 |
$items['admin/user/user-titles/edit/title/%title'] = array(
|
| 52 |
'title' => t('Edit title'),
|
| 53 |
'page callback' => 'user_titles_edit_title',
|
| 54 |
'page arguments' => array(5),
|
| 55 |
'access arguments' => array('administer user titles'),
|
| 56 |
'type' => MENU_CALLBACK
|
| 57 |
);
|
| 58 |
$items['admin/user/user-titles/delete/title/%title'] = array(
|
| 59 |
'title' => t('Remove title'),
|
| 60 |
'page callback' => 'drupal_get_form',
|
| 61 |
'page arguments' => array('user_titles_delete_title_form', 5),
|
| 62 |
'access arguments' => array('administer user titles'),
|
| 63 |
'type' => MENU_CALLBACK
|
| 64 |
);
|
| 65 |
return $items;
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* User title wildcard loader.
|
| 70 |
*/
|
| 71 |
function title_load($tid) {
|
| 72 |
if (!is_numeric($tid)) {
|
| 73 |
return FALSE;
|
| 74 |
}
|
| 75 |
$title = user_titles_get_titles($tid);
|
| 76 |
if (!isset($title->tid)) {
|
| 77 |
return FALSE;
|
| 78 |
}
|
| 79 |
return $title;
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Determines what the current hook module implementation is, or '' if
|
| 84 |
* using built-in version. Ensures the module is loaded too.
|
| 85 |
*/
|
| 86 |
function user_titles_hook_module() {
|
| 87 |
$module = variable_get('user_titles_hook_module', $default = 'user_titles');
|
| 88 |
if (module_exists($module)) {
|
| 89 |
return $module;
|
| 90 |
}
|
| 91 |
else {
|
| 92 |
return $default;
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Form to determine titles and levels.
|
| 99 |
*/
|
| 100 |
function user_titles_settings_form(&$form_state) {
|
| 101 |
$form = array();
|
| 102 |
|
| 103 |
$types = user_titles_get_allowed_types();
|
| 104 |
|
| 105 |
foreach (node_get_types() as $type => $info) {
|
| 106 |
$nodes[$type] = $info->name;
|
| 107 |
}
|
| 108 |
|
| 109 |
$hook_module = user_titles_hook_module();
|
| 110 |
$hook_modules = module_invoke_all('user_titles', 'register');
|
| 111 |
|
| 112 |
|
| 113 |
$form['settings'] = array(
|
| 114 |
'#type' => 'fieldset',
|
| 115 |
'#title' => 'Settings',
|
| 116 |
);
|
| 117 |
|
| 118 |
$form['settings']['hook_module'] = array(
|
| 119 |
'#type' => 'radios',
|
| 120 |
'#title' => 'Point scheme',
|
| 121 |
'#default_value' => $hook_module,
|
| 122 |
'#options' => array(),
|
| 123 |
'#attributes' => array('class' => 'user-titles-hook-module'),
|
| 124 |
);
|
| 125 |
foreach ($hook_modules as $module) {
|
| 126 |
$form['settings']['hook_module']['#options'][$module] = array(
|
| 127 |
'name' => $name = module_invoke($module, 'user_titles', 'name'),
|
| 128 |
'description' => module_invoke($module, 'user_titles', 'description'),
|
| 129 |
'url' => module_invoke($module, 'user_titles', 'url'),
|
| 130 |
);
|
| 131 |
$name_index[$module] = $name;
|
| 132 |
}
|
| 133 |
|
| 134 |
array_multisort($name_index, SORT_ASC, SORT_STRING, $form['settings']['hook_module']['#options']);
|
| 135 |
|
| 136 |
// Implementation of user_titles' default behavior
|
| 137 |
$form['settings']['types'] = array(
|
| 138 |
'#type' => 'checkboxes',
|
| 139 |
'#title' => t('Counted node types'),
|
| 140 |
'#description' => t('Only the checked node types will be counted'),
|
| 141 |
'#options' => $nodes,
|
| 142 |
'#default_value' => $types,
|
| 143 |
'#disabled' => $hook_module !== 'user_titles',
|
| 144 |
'#attributes' => array('class' => 'user-titles-types'),
|
| 145 |
);
|
| 146 |
$form['settings']['types_disabled'] = array(
|
| 147 |
'#type' => 'hidden',
|
| 148 |
'#default_value' => $hook_module !== 'user_titles',
|
| 149 |
);
|
| 150 |
|
| 151 |
// Directory to save images
|
| 152 |
$form['settings']['image_dir'] = array(
|
| 153 |
'#type' => 'textfield',
|
| 154 |
'#title' => t('Image directory'),
|
| 155 |
'#description' => t('Subdirectory in the directory %dir where images will be stored.', array('%dir' => file_directory_path() .'/')),
|
| 156 |
'#default_value' => variable_get('user_titles_image_dir', DEFAULT_IMAGE_DIR),
|
| 157 |
'#size' => 35,
|
| 158 |
'#attributes' => array('class' => 'user-titles-image-dir'),
|
| 159 |
);
|
| 160 |
|
| 161 |
$form['settings']['submit'] = array(
|
| 162 |
'#type' => 'submit',
|
| 163 |
'#value' => t('Save'),
|
| 164 |
);
|
| 165 |
|
| 166 |
$titles = user_titles_get_titles();
|
| 167 |
|
| 168 |
$form['titles'] = array('#tree' => TRUE);
|
| 169 |
|
| 170 |
foreach ($titles as $i => $title) {
|
| 171 |
$form['titles'][$i]['value'] = array(
|
| 172 |
'#type' => 'markup',
|
| 173 |
'#value' => $title['value'],
|
| 174 |
);
|
| 175 |
$form['titles'][$i]['title'] = array(
|
| 176 |
'#type' => 'markup',
|
| 177 |
'#value' => $title['title'],
|
| 178 |
);
|
| 179 |
$form['titles'][$i]['image'] = array(
|
| 180 |
'#type' => 'markup',
|
| 181 |
'#value' => theme('user_titles_image', $title['image'], $title['image_title']),
|
| 182 |
);
|
| 183 |
$form['titles'][$i]['tid'] = array(
|
| 184 |
'#type' => 'hidden',
|
| 185 |
'#value' => $title['tid'],
|
| 186 |
);
|
| 187 |
}
|
| 188 |
|
| 189 |
$form['#submit'] = array('user_titles_settings_form_submit_handler');
|
| 190 |
|
| 191 |
return $form;
|
| 192 |
}
|
| 193 |
|
| 194 |
/**
|
| 195 |
* Implementation of hook_theme().
|
| 196 |
*/
|
| 197 |
function user_titles_theme($existing, $type, $theme, $path) {
|
| 198 |
return array(
|
| 199 |
'user_titles_settings_form' => array(
|
| 200 |
'arguments' => array('form' => NULL),
|
| 201 |
),
|
| 202 |
'user_titles_image' => array(
|
| 203 |
'arguments' => array('filepath' => NULL, 'alt' => NULL),
|
| 204 |
),
|
| 205 |
);
|
| 206 |
}
|
| 207 |
|
| 208 |
|
| 209 |
/**
|
| 210 |
* Theme function for the user title settings form.
|
| 211 |
*/
|
| 212 |
function theme_user_titles_settings_form($form) {
|
| 213 |
$path = drupal_get_path('module', 'user_titles');
|
| 214 |
drupal_add_css("$path/admin.css");
|
| 215 |
|
| 216 |
$units = module_invoke(user_titles_hook_module(), 'user_titles', 'units');
|
| 217 |
$header = array(
|
| 218 |
array('data' => $units, 'class' => 'num-posts'),
|
| 219 |
array('data' => t('Title'), 'class' => 'user-title'),
|
| 220 |
array('data' => t('Image'), 'class' => 'user-title-image'),
|
| 221 |
array('data' => t('Operations'), 'colspan' => 2),
|
| 222 |
);
|
| 223 |
|
| 224 |
$rows = array();
|
| 225 |
foreach (element_children($form['titles']) as $key) {
|
| 226 |
// set a reference so that the drupal_render gets remembered.
|
| 227 |
unset($elem);
|
| 228 |
$elem = &$form['titles'][$key];
|
| 229 |
|
| 230 |
$rows[] = array(
|
| 231 |
array('data' => drupal_render($elem['value']), 'class' => 'num-posts'),
|
| 232 |
array('data' => drupal_render($elem['title']), 'class' => 'user-title'),
|
| 233 |
array('data' => drupal_render($elem['image']), 'class' => 'user-title-image'),
|
| 234 |
drupal_render($elem['tid']),
|
| 235 |
'edit' => l(t('edit'), "admin/user/user-titles/edit/title/" . $elem['tid']['#value']),
|
| 236 |
'delete' => l(t('delete'), "admin/user/user-titles/delete/title/" . $elem['tid']['#value']),
|
| 237 |
);
|
| 238 |
}
|
| 239 |
|
| 240 |
$output = '<div class="title-table">';
|
| 241 |
$output .= theme('table', $header, $rows, array('id' => 'user-titles-settings'));
|
| 242 |
$output .= '</div>';
|
| 243 |
|
| 244 |
// Properly format hook_module
|
| 245 |
foreach ($form['settings']['hook_module']['#options'] as $module => $info) {
|
| 246 |
if ($info['url']) {
|
| 247 |
$name = l($info['name'], $info['url']);
|
| 248 |
}
|
| 249 |
else {
|
| 250 |
$name = check_plain($info['name']); drupal_get_form('user_titles_title_form', $title);
|
| 251 |
}
|
| 252 |
// drupal_render uses this area, not the original
|
| 253 |
$form['settings']['hook_module'][$module]['#title'] = '<strong>'. $name .'</strong><div class="description">'. $info['description'] .'</div>';
|
| 254 |
}
|
| 255 |
|
| 256 |
$output .= '<div class="settings">';
|
| 257 |
$output .= drupal_render($form['settings']);
|
| 258 |
$output .= '</div>';
|
| 259 |
|
| 260 |
$output .= '<div class="clearer"> </div>';
|
| 261 |
$output .= drupal_render($form);
|
| 262 |
return $output;
|
| 263 |
}
|
| 264 |
|
| 265 |
/**
|
| 266 |
* Submit the user titles setting form
|
| 267 |
*/
|
| 268 |
function user_titles_settings_form_submit_handler($form, &$form_state) {
|
| 269 |
// If they change the allowed types, wipe the existing counts so each one will be fresh.
|
| 270 |
$types = user_titles_get_allowed_types();
|
| 271 |
if ($types != $form_values['types']) {
|
| 272 |
db_query("TRUNCATE {user_titles_posts}");
|
| 273 |
}
|
| 274 |
|
| 275 |
if (!$form_state['values']['types_disabled']) {
|
| 276 |
user_titles_set_allowed_types(array_keys(array_filter($form_state['values']['types'])));
|
| 277 |
}
|
| 278 |
|
| 279 |
variable_set('user_titles_hook_module', $form_state['values']['hook_module']);
|
| 280 |
variable_set('user_titles_image_dir', $form_state['values']['image_dir']);
|
| 281 |
|
| 282 |
$image_path = file_create_path(variable_get('user_titles_image_dir', DEFAULT_IMAGE_DIR));
|
| 283 |
file_check_directory($image_path, 1, 'image_dir');
|
| 284 |
|
| 285 |
drupal_set_message(t('The configuration has been updated.'));
|
| 286 |
}
|
| 287 |
|
| 288 |
/**
|
| 289 |
* Form for adding/editing user titles.
|
| 290 |
*/
|
| 291 |
function user_titles_title_form(&$form_state, $edit = array()) {
|
| 292 |
$path = drupal_get_path('module', 'user_titles');
|
| 293 |
drupal_add_css("$path/admin.css");
|
| 294 |
|
| 295 |
// For file field to work.
|
| 296 |
$form['#attributes'] = array('enctype' => 'multipart/form-data');
|
| 297 |
|
| 298 |
$units = module_invoke(user_titles_hook_module(), 'user_titles', 'units');
|
| 299 |
$form['value'] = array(
|
| 300 |
'#type' => 'textfield',
|
| 301 |
'#title' => t($units),
|
| 302 |
'#size' => 5,
|
| 303 |
'#maxlength' => 10,
|
| 304 |
'#required' => TRUE,
|
| 305 |
'#default_value' => $edit['value'],
|
| 306 |
);
|
| 307 |
|
| 308 |
$form['title'] = array(
|
| 309 |
'#type' => 'textfield',
|
| 310 |
'#title' => t('Title'),
|
| 311 |
'#size' => 40,
|
| 312 |
'#maxlength' => 128,
|
| 313 |
'#required' => TRUE,
|
| 314 |
'#default_value' => $edit['title'],
|
| 315 |
);
|
| 316 |
|
| 317 |
$form['image'] = array(
|
| 318 |
'#type' => 'fieldset',
|
| 319 |
'#title' => t('Image'),
|
| 320 |
);
|
| 321 |
|
| 322 |
$form['image']['image_display'] = array(
|
| 323 |
'#type' => 'item',
|
| 324 |
'#value' => theme('user_titles_image', $edit['image'], $edit['image_title']),
|
| 325 |
);
|
| 326 |
|
| 327 |
if ($edit['image']) {
|
| 328 |
$form['image']['remove_image'] = array(
|
| 329 |
'#type' => 'checkbox',
|
| 330 |
'#title' => t('Remove image'),
|
| 331 |
'#description' => t('If checked this image will be removed from this title on submit.'),
|
| 332 |
);
|
| 333 |
}
|
| 334 |
|
| 335 |
$form['image']['image_title'] = array(
|
| 336 |
'#type' => 'textfield',
|
| 337 |
'#title' => t('Image title/alt text'),
|
| 338 |
'#size' => 40,
|
| 339 |
'#maxlength' => 255,
|
| 340 |
'#default_value' => $edit['image_title'],
|
| 341 |
'#description' => t('This text will be used as the title and alt text for the image when it is displayed.'),
|
| 342 |
);
|
| 343 |
|
| 344 |
$form['image']['image_upload'] = array(
|
| 345 |
'#type' => 'file',
|
| 346 |
'#title' => t('Upload new image'),
|
| 347 |
'#size' => 40,
|
| 348 |
'#description' => t('Upload an image that can be displayed with this user title. This image will replace any existing image attached to this title.'),
|
| 349 |
);
|
| 350 |
|
| 351 |
$form['submit'] = array(
|
| 352 |
'#type' => 'submit',
|
| 353 |
'#value' => t('Save'),
|
| 354 |
);
|
| 355 |
|
| 356 |
if ($edit['tid']) {
|
| 357 |
$form['delete'] = array(
|
| 358 |
'#type' => 'submit',
|
| 359 |
'#value' => t('Delete'));
|
| 360 |
$form['tid'] = array(
|
| 361 |
'#type' => 'value',
|
| 362 |
'#value' => $edit['tid'],
|
| 363 |
);
|
| 364 |
}
|
| 365 |
|
| 366 |
$form['#submit'] = array('user_titles_title_form_submit_handler');
|
| 367 |
|
| 368 |
return $form;
|
| 369 |
}
|
| 370 |
|
| 371 |
/**
|
| 372 |
* Implementation of hook_validate().
|
| 373 |
* Validate the add title form.
|
| 374 |
*/
|
| 375 |
function user_titles_title_form_validate($form, &$form_state) {
|
| 376 |
$existing_titles = user_titles_get_titles();
|
| 377 |
|
| 378 |
if (!is_numeric($form_state['values']['value'])) {
|
| 379 |
form_set_error('title', t('Number of posts must be a number!'));
|
| 380 |
}
|
| 381 |
|
| 382 |
foreach ($existing_titles as $title) {
|
| 383 |
if ($title['tid'] != $form_state['values']['tid']) {
|
| 384 |
if ($title['title'] == $form_state['values']['title']) {
|
| 385 |
form_set_error('title', t('This title already exists. Titles must be unique.'));
|
| 386 |
}
|
| 387 |
if ($title['value'] == $form_state['values']['value']) {
|
| 388 |
form_set_error('value', t('There is already a title for this number of posts. Posts must be unique.'));
|
| 389 |
}
|
| 390 |
}
|
| 391 |
}
|
| 392 |
}
|
| 393 |
|
| 394 |
/**
|
| 395 |
* Submit the add title form.
|
| 396 |
*/
|
| 397 |
function user_titles_title_form_submit_handler($form, &$form_state) {
|
| 398 |
// Do file processing if a file has been added.
|
| 399 |
$image = '';
|
| 400 |
|
| 401 |
// Get directory
|
| 402 |
$dir = file_create_path(variable_get('user_titles_image_dir', DEFAULT_IMAGE_DIR));
|
| 403 |
file_check_directory($dir, 1, 'image_upload');
|
| 404 |
|
| 405 |
// Save file
|
| 406 |
$validators = array(
|
| 407 |
'file_validate_is_image' => array(),
|
| 408 |
);
|
| 409 |
if ($file = file_save_upload('image_upload', $validators, $dir, TRUE)) {
|
| 410 |
$image = $file->filepath;
|
| 411 |
drupal_set_message(t('The file: <strong>"' . $file->filename . '"</strong> is successfully uploaded'));
|
| 412 |
}
|
| 413 |
// else if ($file === 0) {
|
| 414 |
// drupal_set_message(t('ERROR uploading the file to ' . $dir . '. The user title has been created without an image.'), 'error');
|
| 415 |
// }
|
| 416 |
|
| 417 |
// Save title information
|
| 418 |
if ($form_state['values']['tid']) {
|
| 419 |
// Only update image information if an image was uploaded so images don't get lost.
|
| 420 |
if ($image != '') {
|
| 421 |
db_query("UPDATE {user_titles} SET title = '%s', value = %d, image = '%s', image_title = '%s' WHERE tid = %d", $form_state['values']['title'], $form_state['values']['value'], $image, $form_state['values']['image_title'], $form_state['values']['tid']);
|
| 422 |
}
|
| 423 |
else if ($form_state['values']['remove_image']) {
|
| 424 |
db_query("UPDATE {user_titles} SET title = '%s', value = %d, image = '%s', image_title = '%s' WHERE tid = %d", $form_state['values']['title'], $form_state['values']['value'], '', '', $form_state['values']['tid']);
|
| 425 |
}
|
| 426 |
else {
|
| 427 |
db_query("UPDATE {user_titles} SET title = '%s', value = %d, image_title = '%s' WHERE tid = %d", $form_state['values']['title'], $form_state['values']['value'], $form_state['values']['image_title'], $form_state['values']['tid']);
|
| 428 |
}
|
| 429 |
drupal_set_message(t('Updated user title %title.', array('%title' => $form_state['values']['title'])));
|
| 430 |
$form_state['redirect'] = 'admin/user/user-titles';
|
| 431 |
}
|
| 432 |
else {
|
| 433 |
db_query("INSERT INTO {user_titles} (title, value, image, image_title) VALUES ('%s', %d, '%s', '%s')", $form_state['values']['title'], $form_state['values']['value'], $image, $form_state['values']['image_title']);
|
| 434 |
drupal_set_message(t('Created new user title %title.', array('%title' => $form_state['values']['title'])));
|
| 435 |
$form_state['redirect'] = 'admin/user/user-titles/add/title';
|
| 436 |
}
|
| 437 |
|
| 438 |
return;
|
| 439 |
}
|
| 440 |
|
| 441 |
/**
|
| 442 |
* Page to edit a user title.
|
| 443 |
*/
|
| 444 |
function user_titles_edit_title($title) {
|
| 445 |
if ($_POST['op'] == t('Delete') || $_POST['confirm']) {
|
| 446 |
return drupal_get_form('user_titles_delete_title_form', $title);
|
| 447 |
}
|
| 448 |
if ($title) {
|
| 449 |
return drupal_get_form('user_titles_title_form', $title);
|
| 450 |
}
|
| 451 |
return drupal_not_found();
|
| 452 |
}
|
| 453 |
|
| 454 |
/**
|
| 455 |
* Confirm deletion of user title form.
|
| 456 |
*/
|
| 457 |
function user_titles_delete_title_form(&$form_values, $title) {
|
| 458 |
|
| 459 |
$form = array();
|
| 460 |
$form['tid'] = array(
|
| 461 |
'#type' => 'hidden',
|
| 462 |
'#value' => $title['tid'],
|
| 463 |
);
|
| 464 |
|
| 465 |
$form['title'] = array(
|
| 466 |
'#type' => 'hidden',
|
| 467 |
'#value' => $title['title'],
|
| 468 |
);
|
| 469 |
|
| 470 |
$confirm_form = confirm_form($form,
|
| 471 |
t('Are you sure you want to delete the user title: %title?', array('%title' => $title['title'])),
|
| 472 |
'admin/user/user-titles',
|
| 473 |
t('This action cannot be undone.'),
|
| 474 |
t('Delete'),
|
| 475 |
t('Cancel'));
|
| 476 |
|
| 477 |
return $confirm_form;
|
| 478 |
}
|
| 479 |
|
| 480 |
/**
|
| 481 |
* Implementation of hook_submit().
|
| 482 |
* Submit the delete user titles form.
|
| 483 |
*/
|
| 484 |
function user_titles_delete_title_form_submit($form, &$form_state) {
|
| 485 |
if ($form_state['values']['tid'] && is_numeric($form_state['values']['tid'])) {
|
| 486 |
$success = db_query("DELETE FROM {user_titles} WHERE tid = %d", $form_state['values']['tid']);
|
| 487 |
}
|
| 488 |
|
| 489 |
if ($success) {
|
| 490 |
drupal_set_message("User title '" . $form_state['values']['title'] . "' has been deleted successfully.", 'status');
|
| 491 |
}
|
| 492 |
else {
|
| 493 |
drupal_set_message("Deletion of user title '" . $form_state['values']['title'] . "' has failed.", 'error');
|
| 494 |
}
|
| 495 |
|
| 496 |
$form_state['redirect'] = 'admin/user/user-titles';
|
| 497 |
return;
|
| 498 |
}
|
| 499 |
|
| 500 |
/**
|
| 501 |
* Implementation of hook_nodeapi
|
| 502 |
*/
|
| 503 |
function user_titles_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
|
| 504 |
if ($op == 'insert' || $op == 'delete') {
|
| 505 |
$allowed_types = user_titles_get_allowed_types();
|
| 506 |
if (in_array($node->type, $allowed_types)) {
|
| 507 |
$inc = ($op == 'insert') ? 1 : -1;
|
| 508 |
db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $node->uid, user_titles_get_posts($node->uid) + $inc);
|
| 509 |
}
|
| 510 |
}
|
| 511 |
}
|
| 512 |
|
| 513 |
/**
|
| 514 |
* Implementation of hook_comment.
|
| 515 |
*/
|
| 516 |
function user_titles_comment($a1, $op) {
|
| 517 |
if ($op == 'insert' || $op == 'delete') {
|
| 518 |
$a1 = (object)$a1; // sometimes an array is passed in
|
| 519 |
$uid = $a1->uid;
|
| 520 |
$inc = ($op == 'insert') ? 1 : -1;
|
| 521 |
db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $uid, user_titles_get_posts($uid) + $inc);
|
| 522 |
}
|
| 523 |
}
|
| 524 |
|
| 525 |
/**
|
| 526 |
* Load the number of posts for a user. If we don't have a stored count
|
| 527 |
* in the database, count and store that value.
|
| 528 |
*
|
| 529 |
* @todo When porting to 7.x, rename to user_title_get_points
|
| 530 |
*
|
| 531 |
* @param $uid
|
| 532 |
* The user to fetch the number of posts for.
|
| 533 |
*/
|
| 534 |
function user_titles_get_posts($uid) {
|
| 535 |
static $cache = array();
|
| 536 |
if (!isset($cache[$uid])) {
|
| 537 |
$module = user_titles_hook_module();
|
| 538 |
$cache[$uid] = module_invoke($module, 'user_titles', 'get', $uid);
|
| 539 |
}
|
| 540 |
return $cache[$uid];
|
| 541 |
}
|
| 542 |
|
| 543 |
/**
|
| 544 |
* Calculate a title based upon the number of posts.
|
| 545 |
*
|
| 546 |
* @param $posts
|
| 547 |
* The number of posts to find the title for.
|
| 548 |
*/
|
| 549 |
function user_titles_get_title($posts) {
|
| 550 |
$titles = user_titles_get_titles();
|
| 551 |
// This array will be sorted from highest to lowest prior to storing.
|
| 552 |
foreach ($titles as $title) {
|
| 553 |
if ($posts >= $title['value']) {
|
| 554 |
return $title;
|
| 555 |
}
|
| 556 |
}
|
| 557 |
}
|
| 558 |
|
| 559 |
/**
|
| 560 |
* Fetch a title for the given user.
|
| 561 |
*
|
| 562 |
* @param $account
|
| 563 |
* The user to fetch. May be a $user object or a $uid.
|
| 564 |
*/
|
| 565 |
function user_titles_get_user_title($account) {
|
| 566 |
$title = user_titles_get_user_title_info($account);
|
| 567 |
if (isset($title['title'])) {
|
| 568 |
return $title['title'];
|
| 569 |
}
|
| 570 |
}
|
| 571 |
|
| 572 |
/**
|
| 573 |
* Fetch the user title image path for the given user.
|
| 574 |
*
|
| 575 |
* @param $account
|
| 576 |
* The user to fetch. May be a $user object or a $uid.
|
| 577 |
*/
|
| 578 |
function user_titles_get_user_image_path($account) {
|
| 579 |
$title = user_titles_get_user_title_info($account);
|
| 580 |
if (isset($title['image'])) {
|
| 581 |
return $title['image'];
|
| 582 |
}
|
| 583 |
}
|
| 584 |
|
| 585 |
/**
|
| 586 |
* Fetch the themed user title image for the given user.
|
| 587 |
*
|
| 588 |
* @param $account
|
| 589 |
* The user to fetch. May be a $user object or a $uid.
|
| 590 |
*/
|
| 591 |
function user_titles_get_user_image($account) {
|
| 592 |
$title = user_titles_get_user_title_info($account);
|
| 593 |
if (isset($title['image'])) {
|
| 594 |
return theme('user_titles_image', $title['image'], $title['image_title']);
|
| 595 |
}
|
| 596 |
}
|
| 597 |
|
| 598 |
/**
|
| 599 |
* Fetch full title info for a given user.
|
| 600 |
*
|
| 601 |
* @param $uid
|
| 602 |
* The user to fetch. May be a $user object or a $uid.
|
| 603 |
*/
|
| 604 |
function user_titles_get_user_title_info($account) {
|
| 605 |
if (is_numeric($account)) {
|
| 606 |
$account = user_load(array('uid' => $account));
|
| 607 |
}
|
| 608 |
if (!$account) {
|
| 609 |
return;
|
| 610 |
}
|
| 611 |
|
| 612 |
if (!empty($account->user_title)) {
|
| 613 |
return array('title' => $account->user_title);
|
| 614 |
}
|
| 615 |
return user_titles_get_title(user_titles_get_posts($account->uid));
|
| 616 |
}
|
| 617 |
|
| 618 |
/**
|
| 619 |
* Update the post count for a given user.
|
| 620 |
*
|
| 621 |
* @param $uid
|
| 622 |
* The user id to update.
|
| 623 |
* @param $count
|
| 624 |
* The count to write. If not given, posts will be counted and that data
|
| 625 |
* written.
|
| 626 |
*/
|
| 627 |
function user_titles_update_post_count($uid, $count = NULL) {
|
| 628 |
if (is_null($count)) {
|
| 629 |
// fetch count from the database
|
| 630 |
$allowed_types = user_titles_get_allowed_types();
|
| 631 |
if ($allowed_types) {
|
| 632 |
$types = implode("','", $allowed_types);
|
| 633 |
$count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE uid = %d AND type IN ('$types')", $uid));
|
| 634 |
if (module_exists('comment')) {
|
| 635 |
$count += db_result(db_query("SELECT COUNT(*) FROM {comments} WHERE uid = %d", $uid));
|
| 636 |
}
|
| 637 |
}
|
| 638 |
else {
|
| 639 |
$count = 0;
|
| 640 |
}
|
| 641 |
}
|
| 642 |
// mysql only query
|
| 643 |
db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $uid, $count);
|
| 644 |
return $count;
|
| 645 |
}
|
| 646 |
|
| 647 |
/**
|
| 648 |
* Get allowed types from db.
|
| 649 |
*/
|
| 650 |
function user_titles_get_allowed_types() {
|
| 651 |
return variable_get('user_titles_types', array());
|
| 652 |
}
|
| 653 |
|
| 654 |
/**
|
| 655 |
* Set allowed types from db
|
| 656 |
*/
|
| 657 |
function user_titles_set_allowed_types($types) {
|
| 658 |
variable_set('user_titles_types', $types);
|
| 659 |
}
|
| 660 |
|
| 661 |
/**
|
| 662 |
* Get titles from db
|
| 663 |
* If tid is passed in only that title will be fetched, otherwise all titles will be fetched
|
| 664 |
*/
|
| 665 |
function user_titles_get_titles($tid = NULL) {
|
| 666 |
if ($tid && is_numeric($tid)) {
|
| 667 |
$result = db_fetch_array(db_query("SELECT * FROM {user_titles} WHERE tid = %d", $tid));
|
| 668 |
}
|
| 669 |
else {
|
| 670 |
$result = array();
|
| 671 |
$query = db_query("SELECT * FROM {user_titles} ORDER BY value DESC");
|
| 672 |
while ($row = db_fetch_array($query)) {
|
| 673 |
$result[] = $row;
|
| 674 |
}
|
| 675 |
}
|
| 676 |
|
| 677 |
return $result;
|
| 678 |
}
|
| 679 |
|
| 680 |
/**
|
| 681 |
* Implementation of hook_user()
|
| 682 |
*
|
| 683 |
* Add the 'edit user title' form to the edit user page.
|
| 684 |
*/
|
| 685 |
function user_titles_user($op, $edit, &$user, $category = NULL) {
|
| 686 |
switch ($op) {
|
| 687 |
case 'form':
|
| 688 |
if (user_access('administer user titles') && $category == 'account' && (isset($user->uid))) {
|
| 689 |
// when user tries to edit his own data
|
| 690 |
$form['user_titles'] = array(
|
| 691 |
'#type' => 'fieldset',
|
| 692 |
'#title' => t('User Title'),
|
| 693 |
'#collapsible' => TRUE,
|
| 694 |
'#weight' => 4);
|
| 695 |
$title_info = user_titles_get_user_title_info($user);
|
| 696 |
|
| 697 |
if (!isset($title_info['title'])) {
|
| 698 |
$title = t('No title set');
|
| 699 |
}
|
| 700 |
else {
|
| 701 |
$title = $title_info['title'];
|
| 702 |
}
|
| 703 |
|
| 704 |
$form['user_titles']['current_title'] = array(
|
| 705 |
'#value' => '<div><strong>'. t('Current user title:') .' </strong> '. filter_xss_admin($title) .'</div>',
|
| 706 |
);
|
| 707 |
|
| 708 |
if (isset($title_info['title']) && empty($title_info['tid'])) {
|
| 709 |
$default_title_info = user_titles_get_title(user_titles_get_posts($uid));
|
| 710 |
if (!isset($default_title_info['title'])) {
|
| 711 |
$default_title = t('No title set');
|
| 712 |
}
|
| 713 |
else {
|
| 714 |
$default_title = $default_title_info['title'];
|
| 715 |
}
|
| 716 |
$form['user_titles']['default_title'] = array(
|
| 717 |
'#value' => '<div><strong>'. t('Default user title:') .' </strong> '. filter_xss_admin($default_title) .'</div>',
|
| 718 |
);
|
| 719 |
}
|
| 720 |
|
| 721 |
$form['user_titles']['user_title'] = array(
|
| 722 |
'#type' => 'textfield',
|
| 723 |
'#title' => t('Override title'),
|
| 724 |
'#description' => t('Enter a title here to give this user a manually overridden title. Leave blank to use the default title.'),
|
| 725 |
'#default_value' => isset($user->user_title) ? $user->user_title : '',
|
| 726 |
);
|
| 727 |
|
| 728 |
return $form;
|
| 729 |
}
|
| 730 |
break;
|
| 731 |
case 'delete':
|
| 732 |
db_query('DELETE FROM {user_titles_posts} WHERE uid = %d', $user->uid);
|
| 733 |
break;
|
| 734 |
case 'view':
|
| 735 |
$title = user_titles_get_user_title($user);
|
| 736 |
if ($title) {
|
| 737 |
$user->content['user_titles'] = array(
|
| 738 |
'#type' => 'user_profile_category',
|
| 739 |
'#title' => t('User title'),
|
| 740 |
);
|
| 741 |
$user->content['user_titles']['title'] = array(
|
| 742 |
'#type' => 'user_profile_item',
|
| 743 |
'#value' => filter_xss_admin($title),
|
| 744 |
);
|
| 745 |
}
|
| 746 |
break;
|
| 747 |
}
|
| 748 |
}
|
| 749 |
|
| 750 |
/**
|
| 751 |
* Theme function for user titles images.
|
| 752 |
*/
|
| 753 |
function theme_user_titles_image($filepath, $alt) {
|
| 754 |
$image = theme('image', $filepath, $alt, $alt, '', FALSE);
|
| 755 |
|
| 756 |
$output = '<div class="user-titles-image">';
|
| 757 |
$output .= $image;
|
| 758 |
$output .= '</div>';
|
| 759 |
|
| 760 |
return $output;
|
| 761 |
}
|
| 762 |
|
| 763 |
/**
|
| 764 |
* Built-in hook implementation that counts nodes
|
| 765 |
*/
|
| 766 |
function user_titles_user_titles($op, $uid = NULL) {
|
| 767 |
switch ($op) {
|
| 768 |
case 'register':
|
| 769 |
return 'user_titles';
|
| 770 |
case 'name':
|
| 771 |
return t('Post count');
|
| 772 |
case 'description':
|
| 773 |
return t('Built-in, see below');
|
| 774 |
case 'units':
|
| 775 |
return t('Posts');
|
| 776 |
case 'url':
|
| 777 |
// No url implemented
|
| 778 |
return;
|
| 779 |
case 'get':
|
| 780 |
$res = db_result(db_query("SELECT posts FROM {user_titles_posts} WHERE uid = %d", $uid));
|
| 781 |
if ($res === FALSE) $res = user_titles_update_post_count($uid);
|
| 782 |
return $res;
|
| 783 |
}
|
| 784 |
}
|
| 785 |
|
| 786 |
/**
|
| 787 |
* Sample hook implementation for userpoints; should be placed in userpoints
|
| 788 |
*/
|
| 789 |
if (module_exists('userpoints') && !function_exists('userpoints_user_titles')) {
|
| 790 |
function userpoints_user_titles($op, $uid = NULL) {
|
| 791 |
switch ($op) {
|
| 792 |
case 'register':
|
| 793 |
return 'userpoints';
|
| 794 |
case 'name':
|
| 795 |
return t('User points');
|
| 796 |
case 'description':
|
| 797 |
return t('Different points values are assigned to user actions');
|
| 798 |
case 'units':
|
| 799 |
return t('Points');
|
| 800 |
case 'url':
|
| 801 |
return 'admin/help/userpoints';
|
| 802 |
case 'get':
|
| 803 |
return userpoints_get_current_points($uid);
|
| 804 |
}
|
| 805 |
}
|
| 806 |
}
|
| 807 |
|
| 808 |
/**
|
| 809 |
* Implementation of template_preprocess_node().
|
| 810 |
*/
|
| 811 |
function user_titles_preprocess_node(&$variables) {
|
| 812 |
$variables['user_title'] = user_titles_get_user_title($variables['node']->uid);
|
| 813 |
$variables['user_title_image'] = user_titles_get_user_image($variables['node']->uid);
|
| 814 |
}
|
| 815 |
|
| 816 |
/**
|
| 817 |
* Implementation of template_preprocess_comment().
|
| 818 |
*/
|
| 819 |
function user_titles_preprocess_comment(&$variables) {
|
| 820 |
$variables['user_title'] = user_titles_get_user_title($variables['comment']->uid);
|
| 821 |
$variables['user_title_image'] = user_titles_get_user_image($variables['comment']->uid);
|
| 822 |
}
|