| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id: lootz.module 5 2008-04-25 22:29:50Z jonathan $
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* Allow the use of [item] filters. The item databases are
|
| 8 |
* configured in the item_dbs directory. See item_dbs/wowdb.inc
|
| 9 |
* for an example.
|
| 10 |
*
|
| 11 |
* @todo
|
| 12 |
* Add more (eg, wowhead, thottbot, etc)
|
| 13 |
*/
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_filter()
|
| 17 |
*/
|
| 18 |
function lootz_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 19 |
switch ($op) {
|
| 20 |
case 'list':
|
| 21 |
return array(0 => t('MMORPG item filter'));
|
| 22 |
|
| 23 |
case 'description':
|
| 24 |
return _lootz_invoke_hook('filter_description');
|
| 25 |
|
| 26 |
case 'prepare' :
|
| 27 |
return $text;
|
| 28 |
case 'process':
|
| 29 |
return _lootz_filter_process($text, $format);
|
| 30 |
case 'settings' :
|
| 31 |
return _lootz_filter_settings();
|
| 32 |
}
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Implementation of hook_filter_tips()
|
| 37 |
*/
|
| 38 |
function lootz_filter_tips($delta, $format, $long) {
|
| 39 |
return _lootz_invoke_hook('filter_tips', $delta, $format, $long);
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Replace [item] tags with corresponding values, if found
|
| 44 |
*/
|
| 45 |
function _lootz_filter_process($text, $format) {
|
| 46 |
// find all links
|
| 47 |
if (preg_match_all('#\[item\]([0-9a-zA-Z ]+)\[/item\]#',$text, $links, PREG_SET_ORDER)) {
|
| 48 |
foreach ($links as $match) {
|
| 49 |
if ($new_link = _lootz_link_item($match[1])) {
|
| 50 |
$text = str_replace($match[0], $new_link, $text);
|
| 51 |
}
|
| 52 |
}
|
| 53 |
}
|
| 54 |
return $text;
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Implementation of hook_init()
|
| 59 |
*/
|
| 60 |
function lootz_init() {
|
| 61 |
_lootz_invoke_hook('init');
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* @param id string|int Item ID or Name
|
| 66 |
* @return string Link to item in configured item db
|
| 67 |
*/
|
| 68 |
function _lootz_link_item($id) {
|
| 69 |
if (is_numeric($id)) {
|
| 70 |
// find the name
|
| 71 |
$name = _lootz_name_lookup($id);
|
| 72 |
}
|
| 73 |
else {
|
| 74 |
// find id
|
| 75 |
$name = $id;
|
| 76 |
$id = _lootz_id_lookup($id);
|
| 77 |
}
|
| 78 |
return _lootz_invoke_hook('link_item', $name, $id);
|
| 79 |
}
|
| 80 |
|
| 81 |
/**
|
| 82 |
* Find an item name based on ID
|
| 83 |
* @param id int Item ID
|
| 84 |
* @return string Item name
|
| 85 |
*/
|
| 86 |
function _lootz_name_lookup($id) {
|
| 87 |
static $names = array();
|
| 88 |
|
| 89 |
if (empty($names)) {
|
| 90 |
$names = _lootz_cache_get('names');
|
| 91 |
}
|
| 92 |
|
| 93 |
if (!isset($names[$id])) {
|
| 94 |
$names[$id] = _lootz_invoke_hook('name_lookup', $id);
|
| 95 |
_lootz_cache_set('names', $names);
|
| 96 |
}
|
| 97 |
|
| 98 |
return $names[$id];
|
| 99 |
}
|
| 100 |
|
| 101 |
/**
|
| 102 |
* Find an item id based on name
|
| 103 |
* @param name string Item Name
|
| 104 |
* @return int Item ID
|
| 105 |
*/
|
| 106 |
function _lootz_id_lookup($name) {
|
| 107 |
static $ids = array();
|
| 108 |
if (empty($ids)) {
|
| 109 |
$ids = _lootz_cache_get('ids');
|
| 110 |
}
|
| 111 |
|
| 112 |
if (!isset($ids[$name])) {
|
| 113 |
$ids[$name] = _lootz_invoke_hook('id_lookup', $name);
|
| 114 |
_lootz_cache_set('ids', $ids);
|
| 115 |
}
|
| 116 |
|
| 117 |
return $ids[$name];
|
| 118 |
}
|
| 119 |
|
| 120 |
/**
|
| 121 |
* Invoke lootz hooks
|
| 122 |
*/
|
| 123 |
function _lootz_invoke_hook() {
|
| 124 |
$args = func_get_args();
|
| 125 |
$hook = array_shift($args);
|
| 126 |
|
| 127 |
// determine which vendor to use (default is wowdb)
|
| 128 |
$vendor = variable_get('lootz_item_database', 'wowdb');
|
| 129 |
require_once 'item_dbs/' . $vendor .'.inc';
|
| 130 |
|
| 131 |
$function = 'lootz_' . $vendor . '_' . $hook;
|
| 132 |
|
| 133 |
if (function_exists($function)) {
|
| 134 |
return call_user_func_array($function, $args);
|
| 135 |
}
|
| 136 |
}
|
| 137 |
|
| 138 |
/**
|
| 139 |
* Load lootz cache
|
| 140 |
*/
|
| 141 |
function _lootz_cache_get($cid) {
|
| 142 |
$cache = cache_get('lootz_' . $cid);
|
| 143 |
if ($cache) {
|
| 144 |
return $cache->data;
|
| 145 |
}
|
| 146 |
return array();
|
| 147 |
}
|
| 148 |
|
| 149 |
/**
|
| 150 |
* Store lootz cache
|
| 151 |
*/
|
| 152 |
function _lootz_cache_set($cid, $value) {
|
| 153 |
cache_set('lootz_' . $cid, $value, 'cache', CACHE_TEMPORARY);
|
| 154 |
}
|
| 155 |
|
| 156 |
/**
|
| 157 |
* Return settings form
|
| 158 |
*/
|
| 159 |
function _lootz_filter_settings() {
|
| 160 |
$form['lootz'] = array(
|
| 161 |
'#type' => 'fieldset',
|
| 162 |
'#tree' => TRUE,
|
| 163 |
'#title' => t('Lootz Settings'),
|
| 164 |
'#collapsible' => TRUE,
|
| 165 |
);
|
| 166 |
$form['lootz']['lootz_item_database'] = array(
|
| 167 |
'#type' => 'radios',
|
| 168 |
'#title' => t('Item Database'),
|
| 169 |
'#options' => _lootz_get_available_item_dbs(),
|
| 170 |
'#default_value' => variable_get('lootz_item_database', 'wowdb'),
|
| 171 |
'#description' => t('The item lookup database to use'),
|
| 172 |
);
|
| 173 |
return $form;
|
| 174 |
}
|
| 175 |
|
| 176 |
/**
|
| 177 |
* Get all available item dbs (as configured in item_dbs directory)
|
| 178 |
*/
|
| 179 |
function _lootz_get_available_item_dbs() {
|
| 180 |
// include all files
|
| 181 |
$dir = drupal_get_path('module', 'lootz') . '/item_dbs';
|
| 182 |
$available_item_dbs = file_scan_directory($dir, '\.inc$');
|
| 183 |
|
| 184 |
$item_dbs = array();
|
| 185 |
foreach (array_keys($available_item_dbs) as $file) {
|
| 186 |
if (preg_match('#/([a-z]*)\.inc$#', $file, $m)) {
|
| 187 |
// this is the name, eg wowhead, wowdb
|
| 188 |
$item_db = $m[1];
|
| 189 |
|
| 190 |
include_once $file;
|
| 191 |
|
| 192 |
$function = 'lootz_' . $item_db . '_item_db_info';
|
| 193 |
if (function_exists($function)) {
|
| 194 |
$details = call_user_func_array($function, array());
|
| 195 |
$item_dbs = array_merge($item_dbs, $details);
|
| 196 |
}
|
| 197 |
}
|
| 198 |
}
|
| 199 |
|
| 200 |
return $item_dbs;
|
| 201 |
}
|