| 1 |
<?php
|
| 2 |
// $Id: rpg.loadcache.inc,v 1.17 2008/01/03 01:59:56 aaron Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* these internal calls control loading types, attributes, actions, and other arrays that rarely need to be built,
|
| 7 |
* instead being cached. Before calling any of these functions, you must call rpg_include_loadcache();
|
| 8 |
*/
|
| 9 |
|
| 10 |
function _rpg_load_resource_types() {
|
| 11 |
$resource_types = array();
|
| 12 |
foreach (module_implements('load_resource_types') as $module) {
|
| 13 |
$types = (array)module_invoke($module, 'load_resource_types');
|
| 14 |
foreach ($types as $key => $type) {
|
| 15 |
$types[$key]['module'] = $module;
|
| 16 |
}
|
| 17 |
$resource_types = array_merge($resource_types, $types);
|
| 18 |
}
|
| 19 |
return $resource_types;
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* implement hook_rpg_load_resource_types
|
| 24 |
*/
|
| 25 |
function rpg_rpg_load_resource_types() {
|
| 26 |
$resource_types = array();
|
| 27 |
$resource_types['node'] = array(
|
| 28 |
'type' => 'node',
|
| 29 |
'name' => t('Node'),
|
| 30 |
);
|
| 31 |
return $resource_types;
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* This will load types from the database, and populate them with attributes & actions
|
| 36 |
*/
|
| 37 |
function _rpg_type_load_types() {
|
| 38 |
$types = array();
|
| 39 |
|
| 40 |
// load _rpg_invoke_type_types_order
|
| 41 |
require_once(drupal_get_path('module', 'rpg') . '/inc/rpg.invoke.inc');
|
| 42 |
|
| 43 |
// load all attributes. force them to be reloaded
|
| 44 |
$all_attributes = rpg_attributes(NULL, false);
|
| 45 |
|
| 46 |
// load all actions. force them to be reloaded
|
| 47 |
$all_actions = rpg_actions(NULL, false);
|
| 48 |
// load all events. force them to be reloaded
|
| 49 |
$all_events = rpg_events(NULL, false);
|
| 50 |
|
| 51 |
$results = db_query("SELECT * FROM {rpg_types}");
|
| 52 |
while ($type = db_fetch_array($results)) {
|
| 53 |
// unserialize our arrays
|
| 54 |
$type['parents'] = unserialize($type['parents']);
|
| 55 |
|
| 56 |
// add the type to our array
|
| 57 |
$types[$type['type']] = $type;
|
| 58 |
|
| 59 |
// load any defined attributes
|
| 60 |
$types[$type['type']]['attributes']['defined'] = _rpg_type_load_defined_attributes($type, $all_attributes);
|
| 61 |
|
| 62 |
// load any defined actions
|
| 63 |
$types[$type['type']]['actions']['defined'] = _rpg_type_load_defined_actions($type, $all_actions);
|
| 64 |
|
| 65 |
// load any defined events
|
| 66 |
$types[$type['type']]['events']['defined'] = _rpg_type_load_defined_events($type, $all_events);
|
| 67 |
}
|
| 68 |
|
| 69 |
foreach ($types as $type) {
|
| 70 |
$types[$type['type']]['order'] = _rpg_invoke_type_types_order($type, true, $types);
|
| 71 |
}
|
| 72 |
|
| 73 |
foreach ($types as $type) {
|
| 74 |
$types[$type['type']]['attributes']['inherited'] = _rpg_type_load_inherited_attributes($type, $all_attributes, $types);
|
| 75 |
$types[$type['type']]['actions']['inherited'] = _rpg_type_load_inherited_actions($type, $all_actions, $types);
|
| 76 |
$types[$type['type']]['events']['inherited'] = _rpg_type_load_inherited_events($type, $all_events, $types);
|
| 77 |
}
|
| 78 |
|
| 79 |
return $types;
|
| 80 |
}
|
| 81 |
|
| 82 |
function _rpg_type_load_defined_attributes($type, $all_attributes) {
|
| 83 |
$attributes = array();
|
| 84 |
foreach ($all_attributes as $attribute) {
|
| 85 |
if (isset($attribute['types'][$type['type']])) {
|
| 86 |
$attributes[$attribute['attribute']] = $attribute;
|
| 87 |
$types_table = 'rpg_attribute_types_' . $attribute['attribute'];
|
| 88 |
$extras = db_fetch_array(db_query("SELECT * FROM {" . $types_table . "} WHERE type='%s'", $type['type']));
|
| 89 |
$extras['set'] = $extras['_set'];
|
| 90 |
unset($extras['_set']);
|
| 91 |
|
| 92 |
$class = rpg_attribute_classes($attribute['class']);
|
| 93 |
foreach ((array)module_invoke($class['module'], 'rpg_attribute_class_settings', 'save serialize', $class['class']) as $key => $column) {
|
| 94 |
$extras[$column] = unserialize($extras[$column]);
|
| 95 |
}
|
| 96 |
$attributes[$attribute['attribute']] = array_merge($attributes[$attribute['attribute']], $extras);
|
| 97 |
}
|
| 98 |
}
|
| 99 |
return $attributes;
|
| 100 |
}
|
| 101 |
|
| 102 |
function _rpg_type_load_inherited_attributes($type, $all_attributes, $types) {
|
| 103 |
$attributes = array();
|
| 104 |
foreach ($type['order'] as $ancestor) {
|
| 105 |
$ancestor = $types[$ancestor];
|
| 106 |
foreach ($all_attributes as $attribute) {
|
| 107 |
if (isset($attribute['types'][$ancestor['type']])) { // was also && !isset($attributes[$attribute['attribute']])
|
| 108 |
$attribute['inherited'] = $ancestor['type'];
|
| 109 |
$attributes[$attribute['attribute']] = $attribute;
|
| 110 |
$types_table = 'rpg_attribute_types_' . $attribute['attribute'];
|
| 111 |
$extras = db_fetch_array(db_query("SELECT * FROM {" . $types_table . "} WHERE type='%s'", $type['type']));
|
| 112 |
$extras['set'] = $extras['_set'];
|
| 113 |
unset($extras['_set']);
|
| 114 |
|
| 115 |
$class = rpg_attribute_classes($attribute['class']);
|
| 116 |
foreach ((array)module_invoke($class['module'], 'rpg_attribute_class_settings', 'save serialize', $class['class']) as $key => $column) {
|
| 117 |
$extras[$column] = unserialize($extras[$column]);
|
| 118 |
}
|
| 119 |
$attributes[$attribute['attribute']] = array_merge($attributes[$attribute['attribute']], $extras);
|
| 120 |
}
|
| 121 |
}
|
| 122 |
}
|
| 123 |
return $attributes;
|
| 124 |
}
|
| 125 |
|
| 126 |
function _rpg_type_load_defined_actions($type, $all_actions) {
|
| 127 |
$actions = array();
|
| 128 |
foreach ($all_actions as $action) {
|
| 129 |
if (isset($action['types'][$type['type']])) {
|
| 130 |
$actions[$action['action']] = $action;
|
| 131 |
$types_table = 'rpg_action_types_' . $action['action'];
|
| 132 |
$extras = db_fetch_array(db_query("SELECT * FROM {" . $types_table . "} WHERE type='%s'", $type['type']));
|
| 133 |
$actions[$action['action']] = array_merge($actions[$action['action']], $extras);
|
| 134 |
}
|
| 135 |
}
|
| 136 |
return $actions;
|
| 137 |
}
|
| 138 |
|
| 139 |
function _rpg_type_load_defined_events($type, $all_events) {
|
| 140 |
$events = array();
|
| 141 |
foreach ($all_events as $event) {
|
| 142 |
if (isset($event['types'][$type['type']])) {
|
| 143 |
$events[$event['event']] = $event;
|
| 144 |
$types_table = 'rpg_event_types_' . $event['event'];
|
| 145 |
$extras = db_fetch_array(db_query("SELECT * FROM {" . $types_table . "} WHERE type='%s'", $type['type']));
|
| 146 |
$events[$event['event']] = array_merge($events[$event['event']], $extras);
|
| 147 |
}
|
| 148 |
}
|
| 149 |
return $events;
|
| 150 |
}
|
| 151 |
|
| 152 |
function _rpg_type_load_inherited_actions($type, $all_actions, $types) {
|
| 153 |
$actions = array();
|
| 154 |
foreach ($type['order'] as $ancestor) {
|
| 155 |
$ancestor = $types[$ancestor];
|
| 156 |
foreach ($all_actions as $action) {
|
| 157 |
if (isset($action['types'][$ancestor['type']])) { // was also && !isset($attributes[$attribute['attribute']])
|
| 158 |
$action['inherited'] = $ancestor['type'];
|
| 159 |
$actions[$action['action']] = $action;
|
| 160 |
$types_table = 'rpg_action_types_' . $action['action'];
|
| 161 |
$extras = db_fetch_array(db_query("SELECT * FROM {" . $types_table . "} WHERE type='%s'", $type['type']));
|
| 162 |
$extras = is_array($extras) ? $extras : array();
|
| 163 |
$actions[$action['action']] = array_merge($actions[$action['action']], $extras);
|
| 164 |
}
|
| 165 |
}
|
| 166 |
}
|
| 167 |
return $actions;
|
| 168 |
}
|
| 169 |
|
| 170 |
function _rpg_type_load_inherited_events($type, $all_events, $types) {
|
| 171 |
$events = array();
|
| 172 |
foreach ($type['order'] as $ancestor) {
|
| 173 |
$ancestor = $types[$ancestor];
|
| 174 |
foreach ($all_events as $event) {
|
| 175 |
if (isset($event['types'][$ancestor['type']])) {
|
| 176 |
$event['inherited'] = $ancestor['type'];
|
| 177 |
$events[$event['event']] = $event;
|
| 178 |
$types_table = 'rpg_event_types_' . $event['event'];
|
| 179 |
$extras = db_fetch_array(db_query("SELECT * FROM {" . $types_table . "} WHERE type='%s'", $type['type']));
|
| 180 |
$extras = is_array($extras) ? $extras : array();
|
| 181 |
$events[$event['event']] = array_merge($events[$event['event']], $extras);
|
| 182 |
}
|
| 183 |
}
|
| 184 |
}
|
| 185 |
return $events;
|
| 186 |
}
|
| 187 |
|
| 188 |
/**
|
| 189 |
* This saves a type into the database, first deleting the row, if applicable.
|
| 190 |
*
|
| 191 |
* It doesn't matter if a type has been defined by a module, include file, or admin screen; they're saved
|
| 192 |
* the same in the database. After inserting the row, the defining module is called with an rpg_save_type hook.
|
| 193 |
* TODO: Not sure if the hook is actually needed, or if we want to broaden that to call all module hooks instead.
|
| 194 |
* TODO: attributes and actions defined on the type will be saved separately. not sure if it will call this
|
| 195 |
* function, or a new function specifically for those widgets.
|
| 196 |
*
|
| 197 |
* @param
|
| 198 |
* $type
|
| 199 |
* An array with the type definition to save.
|
| 200 |
*/
|
| 201 |
function rpg_save_type($type) {
|
| 202 |
db_query("DELETE FROM {rpg_types} WHERE type='%s'", $type['type']);
|
| 203 |
db_query("INSERT INTO {rpg_types} (type, name, module, parents, shortdesc, longdesc) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')", $type['type'], $type['name'], $type['module'], serialize($type['parents']), $type['shortdesc'], $type['longdesc']);
|
| 204 |
|
| 205 |
// make sure objects inherit new values, etc.
|
| 206 |
rpg_reset_cache();
|
| 207 |
// reload the type, clearing the cache
|
| 208 |
rpg_types($type['type'], false);
|
| 209 |
|
| 210 |
// in addition to adding new types to menu, we may have changed the type name
|
| 211 |
menu_rebuild();
|
| 212 |
}
|
| 213 |
|
| 214 |
function rpg_delete_type($type) {
|
| 215 |
db_query("DELETE FROM {rpg_types} WHERE type='%s'", $type['type']);
|
| 216 |
module_invoke($type['module'], 'rpg_delete_type', $type);
|
| 217 |
|
| 218 |
// make sure objects inherit new values, etc.
|
| 219 |
rpg_reset_cache();
|
| 220 |
// remove old type from menu
|
| 221 |
menu_rebuild();
|
| 222 |
|
| 223 |
// reload the type, clearing the cache. this should also revert previously overridden module-defined types
|
| 224 |
rpg_types($type['type'], false);}
|
| 225 |
|
| 226 |
/**
|
| 227 |
* implement hook_rpg_save_type
|
| 228 |
*/
|
| 229 |
function rpg_rpg_delete_type($type) {
|
| 230 |
rpg_include_invoke($type, 'delete_type');
|
| 231 |
}
|
| 232 |
|
| 233 |
function _rpg_attribute_edit($type, $attr) {
|
| 234 |
$type = rpg_types($type);
|
| 235 |
$attribute = rpg_attributes($attr['attribute']);
|
| 236 |
|
| 237 |
// TODO: move these to validation?
|
| 238 |
if (!$type['type']) {
|
| 239 |
drupal_set_message(t('Tried to edit attribute of non-existent type.'), 'error');
|
| 240 |
return;
|
| 241 |
}
|
| 242 |
else if (!$attribute['attribute']) {
|
| 243 |
drupal_set_message(t('Tried to edit non-existent attribute.'), 'error');
|
| 244 |
}
|
| 245 |
|
| 246 |
$class = rpg_attribute_classes($attribute['class']);
|
| 247 |
$types_table = "rpg_attribute_types_" . db_escape_string($attribute['attribute']);
|
| 248 |
|
| 249 |
$columns = array();
|
| 250 |
$values = array();
|
| 251 |
|
| 252 |
$columns[] = "description = '%s'";
|
| 253 |
$values[] = $attr['description'];
|
| 254 |
|
| 255 |
$columns[] = "get = '%s'";
|
| 256 |
$values[] = $attr['get'];
|
| 257 |
|
| 258 |
$columns[] = "verify = '%s'";
|
| 259 |
$values[] = $attr['verify'];
|
| 260 |
|
| 261 |
$columns[] = "_set = '%s'";
|
| 262 |
$values[] = $attr['set'];
|
| 263 |
|
| 264 |
$columns[] = "default_value = '%s'";
|
| 265 |
$values[] = $attr['default_value'];
|
| 266 |
|
| 267 |
$columns[] = "form_display = '%s'";
|
| 268 |
$values[] = $attr['form_display'];
|
| 269 |
|
| 270 |
foreach ((array)module_invoke($class['module'], 'rpg_attribute_class_settings', 'save', $class['class']) as $key => $column) {
|
| 271 |
$columns[] = $column . " = '%s'";
|
| 272 |
$values[$column] = $attr[$column];
|
| 273 |
}
|
| 274 |
foreach ((array)module_invoke($class['module'], 'rpg_attribute_class_settings', 'save serialize', $class['class']) as $key => $column) {
|
| 275 |
$values[$column] = serialize($values[$column]);
|
| 276 |
}
|
| 277 |
|
| 278 |
$values[] = $type['type'];
|
| 279 |
|
| 280 |
$col = implode(', ', $columns);
|
| 281 |
|
| 282 |
db_query("UPDATE {" . $types_table . "} SET $col WHERE type='%s'", $values);
|
| 283 |
|
| 284 |
$name = $edit['name'];
|
| 285 |
if ($name && $name != $attribute['name']) {
|
| 286 |
db_query("UPDATE {rpg_attributes} SET name = '%s' WHERE attribute='%s'", $name, $attribute['attribute']);
|
| 287 |
}
|
| 288 |
|
| 289 |
// rebuild the attribute & type caches
|
| 290 |
$attribute = rpg_attributes($attribute['attribute'], false);
|
| 291 |
rpg_types(NULL, false);
|
| 292 |
|
| 293 |
menu_rebuild();
|
| 294 |
return $attribute;
|
| 295 |
}
|
| 296 |
|
| 297 |
function _rpg_action_edit($type, $edit) { //$form_values) {
|
| 298 |
$type = rpg_types($type);
|
| 299 |
$action = rpg_actions($edit['action']);
|
| 300 |
// TODO: move these to validation
|
| 301 |
if (!$type['type']) {
|
| 302 |
drupal_set_message(t('Tried to edit action of non-existent type.'), 'error');
|
| 303 |
return;
|
| 304 |
}
|
| 305 |
else if (!$action['action']) {
|
| 306 |
drupal_set_message(t('Tried to edit non-existent action.'), 'error');
|
| 307 |
}
|
| 308 |
|
| 309 |
$types_table = "rpg_action_types_" . db_escape_string($action['action']);
|
| 310 |
|
| 311 |
$columns = array();
|
| 312 |
$values = array();
|
| 313 |
|
| 314 |
$columns[] = "description = '%s'";
|
| 315 |
$values[] = $edit['description'];
|
| 316 |
|
| 317 |
$columns[] = "php = '%s'";
|
| 318 |
$values[] = $edit['php'];
|
| 319 |
|
| 320 |
$columns[] = "expose = '%s'";
|
| 321 |
$values[] = $edit['expose'];
|
| 322 |
|
| 323 |
$values[] = $type['type'];
|
| 324 |
|
| 325 |
$col = implode(', ', $columns);
|
| 326 |
|
| 327 |
db_query("UPDATE {" . $types_table . "} SET $col WHERE type='%s'", $values);
|
| 328 |
|
| 329 |
$name = $edit['name'];
|
| 330 |
if ($name && $name != $action['name']) {
|
| 331 |
db_query("UPDATE {rpg_actions} SET name = '%s' WHERE action='%s'", $name, $action['action']);
|
| 332 |
}
|
| 333 |
|
| 334 |
// rebuild the action & type caches
|
| 335 |
$action = rpg_actions($action['action'], false);
|
| 336 |
rpg_types(NULL, false);
|
| 337 |
|
| 338 |
menu_rebuild();
|
| 339 |
return $action;
|
| 340 |
}
|
| 341 |
|
| 342 |
function _rpg_event_edit($type, $edit) {
|
| 343 |
$type = rpg_types($type);
|
| 344 |
$event = rpg_events($edit['event']);
|
| 345 |
// TODO: move these to validation
|
| 346 |
if (!$type['type']) {
|
| 347 |
drupal_set_message(t('Tried to edit event of non-existent type.'), 'error');
|
| 348 |
return;
|
| 349 |
}
|
| 350 |
else if (!$event['event']) {
|
| 351 |
drupal_set_message(t('Tried to edit non-existent event.'), 'error');
|
| 352 |
}
|
| 353 |
|
| 354 |
$types_table = "rpg_event_types_" . db_escape_string($event['event']);
|
| 355 |
|
| 356 |
$columns = array();
|
| 357 |
$values = array();
|
| 358 |
|
| 359 |
$columns[] = "description = '%s'";
|
| 360 |
$values[] = $edit['description'];
|
| 361 |
|
| 362 |
$columns[] = "php = '%s'";
|
| 363 |
$values[] = $edit['php'];
|
| 364 |
|
| 365 |
$values[] = $type['type'];
|
| 366 |
|
| 367 |
$col = implode(', ', $columns);
|
| 368 |
|
| 369 |
db_query("UPDATE {" . $types_table . "} SET $col WHERE type='%s'", $values);
|
| 370 |
|
| 371 |
$name = $edit['name'];
|
| 372 |
if ($name && $name != $event['name']) {
|
| 373 |
db_query("UPDATE {rpg_events_table} SET name = '%s' WHERE event='%s'", $name, $event['event']);
|
| 374 |
}
|
| 375 |
|
| 376 |
// rebuild the event & type caches
|
| 377 |
$event = rpg_events($event['event'], false);
|
| 378 |
rpg_types(NULL, false);
|
| 379 |
|
| 380 |
menu_rebuild();
|
| 381 |
return $event;
|
| 382 |
}
|
| 383 |
|
| 384 |
/**
|
| 385 |
* This adds a new attribute or override to a type.
|
| 386 |
* @param $type
|
| 387 |
* The name of the type to add the attribute to.
|
| 388 |
* @param $class
|
| 389 |
* The string of the class of attribute to add, such as integer or string.
|
| 390 |
* @param $attribute
|
| 391 |
* A machine-readable name for the attribute. There may only be one per type, although the same attribute may be
|
| 392 |
* added to further types or overridden in descended types. However, when overriding, it must be of the same class.
|
| 393 |
* @param $name
|
| 394 |
* The human-readable name of the attribute.
|
| 395 |
*/
|
| 396 |
// function rpg_attribute_add($type, $class, $attribute, $name, $build_types = false) {
|
| 397 |
function rpg_attribute_add($type, $attr, $build_types = false) {
|
| 398 |
// load the type from the type name given
|
| 399 |
$type = rpg_types($type);
|
| 400 |
|
| 401 |
// load the class from the class name stored in the attribute
|
| 402 |
$class = rpg_attribute_classes($attr['class']);
|
| 403 |
|
| 404 |
// make sure we can write field names to the db
|
| 405 |
$attribute = db_escape_string($attr['attribute']);
|
| 406 |
|
| 407 |
// get our attributes human-readable name
|
| 408 |
$name = $attr['name'];
|
| 409 |
|
| 410 |
// test to see if we're overriding an existing attribute for this type
|
| 411 |
$test_attribute = rpg_attributes($attribute);
|
| 412 |
|
| 413 |
// can't add an attribute if the type or class doesn't exist
|
| 414 |
if (!$type['type']) {
|
| 415 |
drupal_set_message(t('Tried to add attribute to non-existent type.'), 'error');
|
| 416 |
return;
|
| 417 |
}
|
| 418 |
else if (!$class['class']) {
|
| 419 |
drupal_set_message(t('Tried to add attribute of non-existent class @class to type @type.', array('@class' => $attr['class'], '@type' => $type['name'])), 'error');
|
| 420 |
return;
|
| 421 |
}
|
| 422 |
|
| 423 |
// new tables for the db
|
| 424 |
$types_table = "rpg_attribute_types_$attribute";
|
| 425 |
$objects_table = "rpg_attribute_rpg_$attribute";
|
| 426 |
|
| 427 |
// only create the table if it doesn't already exist
|
| 428 |
if (!$test_attribute['attribute']) {
|
| 429 |
_rpg_create_attribute_tables($attribute, $class, $name, $types_table, $objects_table);
|
| 430 |
}
|
| 431 |
|
| 432 |
// fail if we've already created this attribute for this type -- use _rpg_attribute_edit instead ???
|
| 433 |
if (db_result(db_query("SELECT type FROM {" . $types_table . "} WHERE type='%s'", $type['type']))) {
|
| 434 |
drupal_set_message(t('Tried to add the @attribute attribute to the @type type where it was already defined.', array('@attribute' => $attribute, '@type' => $type['name'])), 'error');
|
| 435 |
return;
|
| 436 |
}
|
| 437 |
|
| 438 |
// insert the type into the attribute's types table
|
| 439 |
db_query("INSERT INTO {" . $types_table. "} (type) VALUES ('%s')", $type['type']);
|
| 440 |
|
| 441 |
// rebuild the attribute & type caches
|
| 442 |
$attribute = rpg_attributes($attribute, false);
|
| 443 |
|
| 444 |
// we don't want a recursive call -- don't go here if we just came from here...
|
| 445 |
if (!$build_types) {
|
| 446 |
// make sure objects inherit new values, etc.
|
| 447 |
rpg_reset_cache();
|
| 448 |
rpg_types(NULL, false);
|
| 449 |
}
|
| 450 |
|
| 451 |
// add menu items for the new attribute
|
| 452 |
menu_rebuild();
|
| 453 |
return $attribute;
|
| 454 |
}
|
| 455 |
|
| 456 |
|
| 457 |
/**
|
| 458 |
* This adds a new action or override to a type.
|
| 459 |
* @param $type
|
| 460 |
* The name of the type to add the action to.
|
| 461 |
* @param $action
|
| 462 |
* An action array. There may only be one per type, although the same action may be
|
| 463 |
* added to further types or overridden in descended types.
|
| 464 |
*/
|
| 465 |
function _rpg_action_add($type, $action, $build_types = false) {
|
| 466 |
$type = rpg_types($type);
|
| 467 |
$action['action'] = db_escape_string($action['action']);
|
| 468 |
if (!$type['type']) {
|
| 469 |
drupal_set_message(t('Tried to add action to non-existent type.'), 'error');
|
| 470 |
return;
|
| 471 |
}
|
| 472 |
if (!$action['action']) {
|
| 473 |
drupal_set_message(t('Tried to add unnamed action.'), 'error');
|
| 474 |
return;
|
| 475 |
}
|
| 476 |
|
| 477 |
$test_action = rpg_actions($action['action']);
|
| 478 |
$types_table = "rpg_action_types_{$action['action']}";
|
| 479 |
|
| 480 |
// only create the table if it doesn't already exist
|
| 481 |
if (!$test_action['action']) {
|
| 482 |
_rpg_create_action_tables($action['action'], $action['name'], $types_table);
|
| 483 |
}
|
| 484 |
|
| 485 |
if (db_result(db_query("SELECT type FROM {" . $types_table . "} WHERE type='%s'", $type['type']))) {
|
| 486 |
drupal_set_message(t('Tried to add the @action action to the @type type where it was already defined.', array('@action' => $action['action'], '@type' => $type['name'])), 'error');
|
| 487 |
return;
|
| 488 |
}
|
| 489 |
|
| 490 |
// insert the type into the action's types table
|
| 491 |
db_query("INSERT INTO {" . $types_table. "} (type) VALUES ('%s')", $type['type']);
|
| 492 |
|
| 493 |
// rebuild the action & type caches
|
| 494 |
$action = rpg_actions($action['action'], false);
|
| 495 |
|
| 496 |
// we don't want a recursive call -- don't go here if we just came from here...
|
| 497 |
if (!$build_types) {
|
| 498 |
// make sure objects inherit new values, etc.
|
| 499 |
rpg_reset_cache();
|
| 500 |
rpg_types(NULL, false);
|
| 501 |
}
|
| 502 |
|
| 503 |
menu_rebuild();
|
| 504 |
return $action;
|
| 505 |
}
|
| 506 |
|
| 507 |
/**
|
| 508 |
* This adds a new event or override to a type.
|
| 509 |
* @param $type
|
| 510 |
* The name of the type to add the event to.
|
| 511 |
* @param $event
|
| 512 |
* An event array. There may only be one per type, although the same event may be
|
| 513 |
* added to further types or overridden in descended types.
|
| 514 |
*/
|
| 515 |
function _rpg_event_add($type, $event, $build_types = false) {
|
| 516 |
$type = rpg_types($type);
|
| 517 |
$event['event'] = db_escape_string($event['event']);
|
| 518 |
if (!$type['type']) {
|
| 519 |
drupal_set_message(t('Tried to add event to non-existent type.'), 'error');
|
| 520 |
return;
|
| 521 |
}
|
| 522 |
if (!$event['event']) {
|
| 523 |
drupal_set_message(t('Tried to add unnamed event.'), 'error');
|
| 524 |
return;
|
| 525 |
}
|
| 526 |
|
| 527 |
$test_event = rpg_events($event['event']);
|
| 528 |
$types_table = "rpg_event_types_{$event['event']}";
|
| 529 |
|
| 530 |
// only create the table if it doesn't already exist
|
| 531 |
if (!$test_event['event']) {
|
| 532 |
_rpg_create_event_tables($event['event'], $event['name'], $types_table);
|
| 533 |
}
|
| 534 |
|
| 535 |
if (db_result(db_query("SELECT type FROM {" . $types_table . "} WHERE type='%s'", $type['type']))) {
|
| 536 |
drupal_set_message(t('Tried to add the @event event to the @type type where it was already defined.', array('@event' => $event['event'], '@type' => $type['name'])), 'error');
|
| 537 |
return;
|
| 538 |
}
|
| 539 |
|
| 540 |
// insert the type into the event's types table
|
| 541 |
db_query("INSERT INTO {" . $types_table. "} (type) VALUES ('%s')", $type['type']);
|
| 542 |
|
| 543 |
// rebuild the event & type caches
|
| 544 |
$event = rpg_events($event['event'], false);
|
| 545 |
|
| 546 |
// we don't want a recursive call -- don't go here if we just came from here...
|
| 547 |
if (!$build_types) {
|
| 548 |
// make sure objects inherit new values, etc.
|
| 549 |
rpg_reset_cache();
|
| 550 |
rpg_types(NULL, false);
|
| 551 |
}
|
| 552 |
|
| 553 |
menu_rebuild();
|
| 554 |
return $event;
|
| 555 |
}
|
| 556 |
|
| 557 |
// TODO: examine data array
|
| 558 |
function _rpg_create_attribute_tables($attribute, $class, $name, $types_table, $objects_table) {
|
| 559 |
// add the attribute to the attribute table
|
| 560 |
db_query("INSERT INTO {rpg_attributes} (attribute, class, name) VALUES ('%s', '%s', '%s')", $attribute, $class['class'], $name);
|
| 561 |
// create a new table to store type data for this attribute
|
| 562 |
// $types_table is "rpg_attribute_types_$attribute"
|
| 563 |
switch ($GLOBALS['db_type']) {
|
| 564 |
case 'mysql':
|
| 565 |
case 'mysqli':
|
| 566 |
db_query("CREATE TABLE {" . $types_table . "} (
|
| 567 |
type varchar(128) NOT NULL default '',
|
| 568 |
description longtext NOT NULL,
|
| 569 |
get varchar(255) NOT NULL default '',
|
| 570 |
verify varchar(255) NOT NULL default '',
|
| 571 |
_set varchar(255) NOT NULL default '',
|
| 572 |
default_value varchar(255) NOT NULL default '',
|
| 573 |
form_display varchar(255) NOT NULL default '',
|
| 574 |
PRIMARY KEY (type)
|
| 575 |
) /*!40100 DEFAULT CHARACTER SET utf8 */");
|
| 576 |
db_query("CREATE TABLE {" . $objects_table . "} (
|
| 577 |
rid int(10) unsigned NOT NULL,
|
| 578 |
PRIMARY KEY (rid)
|
| 579 |
) /*!40100 DEFAULT CHARACTER SET utf8 */");
|
| 580 |
break;
|
| 581 |
|
| 582 |
case 'pgsql':
|
| 583 |
db_query("CREATE TABLE {" . $types_table . "} (
|
| 584 |
type varchar(128) NOT NULL default '',
|
| 585 |
description text NOT NULL default '',
|
| 586 |
get varchar(255) NOT NULL default '',
|
| 587 |
verify varchar(255) NOT NULL default '',
|
| 588 |
_set varchar(255) NOT NULL default '',
|
| 589 |
default_value varchar(255) NOT NULL default '',
|
| 590 |
form_display varchar(255) NOT NULL default '',
|
| 591 |
PRIMARY KEY (type)
|
| 592 |
)");
|
| 593 |
db_query("CREATE INDEX {" . $types_table . "}_type_idx ON {" . $types_table . "}(type);");
|
| 594 |
db_query("CREATE TABLE {" . $objects_table . "} (
|
| 595 |
rid integer NOT NULL default '0',
|
| 596 |
PRIMARY KEY (rid)
|
| 597 |
)");
|
| 598 |
db_query("CREATE INDEX {" . $objects_table . "}_rid_idx ON {" . $objects_table . "}(rid);");
|
| 599 |
break;
|
| 600 |
}
|
| 601 |
foreach (module_invoke($class['module'], 'rpg_attribute_class_settings', 'save', $class['class']) as $value) {
|
| 602 |
_rpg_db_add_column($types_table, $value, 'longtext');
|
| 603 |
}
|
| 604 |
foreach (module_invoke($class['module'], 'rpg_attribute_class_settings', 'data', $class['class']) as $column => $data) {
|
| 605 |
_rpg_db_add_column($objects_table, $column, $data['type'], $data);
|
| 606 |
}
|
| 607 |
}
|
| 608 |
|
| 609 |
function _rpg_create_action_tables($action, $name, $types_table) {
|
| 610 |
// add the action to the action table
|
| 611 |
db_query("INSERT INTO {rpg_actions} (action, name) VALUES ('%s', '%s')", $action, $name);
|
| 612 |
// create a new table to store type data for this action
|
| 613 |
// this will be named rpg_action_types_{$action['action']}, thus something like rpg_action_types_examine
|
| 614 |
switch ($GLOBALS['db_type']) {
|
| 615 |
case 'mysql':
|
| 616 |
case 'mysqli':
|
| 617 |
db_query("CREATE TABLE {" . $types_table . "} (
|
| 618 |
type varchar(128) NOT NULL default '',
|
| 619 |
description longtext NOT NULL,
|
| 620 |
php longtext NOT NULL,
|
| 621 |
expose longtext NOT NULL,
|
| 622 |
PRIMARY KEY (type)
|
| 623 |
) /*!40100 DEFAULT CHARACTER SET utf8 */");
|
| 624 |
break;
|
| 625 |
|
| 626 |
case 'pgsql':
|
| 627 |
db_query("CREATE TABLE {" . $types_table . "} (
|
| 628 |
type varchar(128) NOT NULL default '',
|
| 629 |
description text NOT NULL default '',
|
| 630 |
php text NOT NULL default '',
|
| 631 |
expose text NOT NULL default '',
|
| 632 |
PRIMARY KEY (type)
|
| 633 |
)");
|
| 634 |
db_query("CREATE INDEX {" . $types_table . "}_type_idx ON {" . $types_table . "}(type);");
|
| 635 |
break;
|
| 636 |
}
|
| 637 |
}
|
| 638 |
|
| 639 |
function _rpg_create_event_tables($event, $name, $types_table) {
|
| 640 |
// add the event to the event table
|
| 641 |
db_query("INSERT INTO {rpg_events_table} (event, name) VALUES ('%s', '%s')", $event, $name);
|
| 642 |
// create a new table to store type data for this event
|
| 643 |
// this will be named rpg_event_types_{$event['event']}, thus something like rpg_event_types_examine
|
| 644 |
switch ($GLOBALS['db_type']) {
|
| 645 |
case 'mysql':
|
| 646 |
case 'mysqli':
|
| 647 |
db_query("CREATE TABLE {" . $types_table . "} (
|
| 648 |
type varchar(128) NOT NULL default '',
|
| 649 |
description longtext NOT NULL,
|
| 650 |
php longtext NOT NULL,
|
| 651 |
PRIMARY KEY (type)
|
| 652 |
) /*!40100 DEFAULT CHARACTER SET utf8 */");
|
| 653 |
break;
|
| 654 |
|
| 655 |
case 'pgsql':
|
| 656 |
db_query("CREATE TABLE {" . $types_table . "} (
|
| 657 |
type varchar(128) NOT NULL default '',
|
| 658 |
description text NOT NULL default '',
|
| 659 |
php text NOT NULL default '',
|
| 660 |
PRIMARY KEY (type)
|
| 661 |
)");
|
| 662 |
db_query("CREATE INDEX {" . $types_table . "}_type_idx ON {" . $types_table . "}(type);");
|
| 663 |
break;
|
| 664 |
}
|
| 665 |
}
|
| 666 |
|
| 667 |
/**
|
| 668 |
* This loads all attributes from the database and returns the array.
|
| 669 |
*/
|
| 670 |
function _rpg_load_attributes() {
|
| 671 |
$attributes = array();
|
| 672 |
$results = db_query("SELECT * FROM {rpg_attributes}");
|
| 673 |
while ($attribute = db_fetch_array($results)) {
|
| 674 |
$attributes[$attribute['attribute']] = $attribute;
|
| 675 |
$attributes[$attribute['attribute']]['types'] = array();
|
| 676 |
$types = db_query("SELECT * FROM {rpg_attribute_types_" . $attribute['attribute']);
|
| 677 |
while ($type = db_fetch_array($types)) {
|
| 678 |
$type['set'] = $type['_set'];
|
| 679 |
unset($type['_set']);
|
| 680 |
$attributes[$attribute['attribute']]['types'][$type['type']] = $type;
|
| 681 |
}
|
| 682 |
}
|
| 683 |
return $attributes;
|
| 684 |
}
|
| 685 |
|
| 686 |
/**
|
| 687 |
* This loads all actions from the database and returns the array.
|
| 688 |
*/
|
| 689 |
function _rpg_load_actions() {
|
| 690 |
$actions = array();
|
| 691 |
$results = db_query("SELECT * FROM {rpg_actions}");
|
| 692 |
while ($action = db_fetch_array($results)) {
|
| 693 |
$actions[$action['action']] = $action;
|
| 694 |
$actions[$action['action']]['types'] = array();
|
| 695 |
$types = db_query("SELECT * FROM {rpg_action_types_" . $action['action']);
|
| 696 |
while ($type = db_fetch_array($types)) {
|
| 697 |
$actions[$action['action']]['types'][$type['type']] = $type;
|
| 698 |
}
|
| 699 |
}
|
| 700 |
return $actions;
|
| 701 |
}
|
| 702 |
|
| 703 |
/**
|
| 704 |
* This loads all events from the database and returns the array.
|
| 705 |
*/
|
| 706 |
function _rpg_load_events() {
|
| 707 |
$events = array();
|
| 708 |
$results = db_query("SELECT * FROM {rpg_events_table}");
|
| 709 |
while ($event = db_fetch_array($results)) {
|
| 710 |
$events[$event['event']] = $event;
|
| 711 |
$events[$event['event']]['types'] = array();
|
| 712 |
$types = db_query("SELECT * FROM {rpg_event_types_" . $event['event']);
|
| 713 |
while ($type = db_fetch_array($types)) {
|
| 714 |
$events[$event['event']]['types'][$type['type']] = $type;
|
| 715 |
}
|
| 716 |
}
|
| 717 |
return $events;
|
| 718 |
}
|
| 719 |
|
| 720 |
/**
|
| 721 |
* Add a column to a database table.
|
| 722 |
* This is lifted straight from cck -- content_admin.inc
|
| 723 |
*
|
| 724 |
* @param $table
|
| 725 |
* Name of the table, without {}
|
| 726 |
* @param $column
|
| 727 |
* Name of the column
|
| 728 |
* @param $type
|
| 729 |
* Type of column
|
| 730 |
* @param $attributes
|
| 731 |
* Additional optional attributes. Recognized attributes:
|
| 732 |
* not null => TRUE|FALSE
|
| 733 |
* default => NULL|FALSE|value (with or without '', it won't be added)
|
| 734 |
*/
|
| 735 |
function _rpg_db_add_column($table, $column, $type, $attributes = array()) {
|
| 736 |
switch ($GLOBALS['db_type']) {
|
| 737 |
case 'pgsql':
|
| 738 |
$mappings = array('int' => 'integer', 'mediumint' => 'integer', 'bigint' => 'integer',
|
| 739 |
'tinyint' => 'smallint',
|
| 740 |
'float' => 'float',
|
| 741 |
'varchar' => 'varchar',
|
| 742 |
'text' => 'text', 'mediumtext' => 'text', 'longtext' => 'text');
|
| 743 |
if (isset($mappings[$type])) {
|
| 744 |
$type = $mappings[$type];
|
| 745 |
}
|
| 746 |
else {
|
| 747 |
watchdog('database', t('No PostgreSQL mapping found for %type data type.', array('%type' => $type)), WATCHDOG_WARNING);
|
| 748 |
}
|
| 749 |
if ($type != 'varchar') {
|
| 750 |
unset($attributes['length']);
|
| 751 |
}
|
| 752 |
break;
|
| 753 |
}
|
| 754 |
|
| 755 |
if (array_key_exists('not null', $attributes) && $attributes['not null']) {
|
| 756 |
$not_null = 'NOT NULL';
|
| 757 |
}
|
| 758 |
if (array_key_exists('default', $attributes)) {
|
| 759 |
if (is_null($attributes['default'])) {
|
| 760 |
$default_val = 'NULL';
|
| 761 |
$default = 'default NULL';
|
| 762 |
}
|
| 763 |
elseif ($attributes['default'] === FALSE) {
|
| 764 |
$default = '';
|
| 765 |
}
|
| 766 |
else {
|
| 767 |
$default_val = "$attributes[default]";
|
| 768 |
$default = "default $attributes[default]";
|
| 769 |
}
|
| 770 |
}
|
| 771 |
if (array_key_exists('length', $attributes)) {
|
| 772 |
$type .= '('. $attributes['length'] .')';
|
| 773 |
}
|
| 774 |
if (array_key_exists('unsigned', $attributes) && $attributes['unsigned']) {
|
| 775 |
switch ($GLOBALS['db_type']) {
|
| 776 |
case 'pgsql':
|
| 777 |
$type = str_replace('integer', 'int_unsigned', $type);
|
| 778 |
break;
|
| 779 |
default:
|
| 780 |
$type .= ' unsigned';
|
| 781 |
break;
|
| 782 |
}
|
| 783 |
}
|
| 784 |
switch ($GLOBALS['db_type']) {
|
| 785 |
case 'pgsql':
|
| 786 |
db_query("ALTER TABLE {". $table ."} ADD $column $type");
|
| 787 |
if ($default) {
|
| 788 |
db_query("ALTER TABLE {". $table ."} ALTER $column SET $default");
|
| 789 |
}
|
| 790 |
if ($not_null) {
|
| 791 |
if ($default) {
|
| 792 |
db_query("UPDATE {". $table ."} SET $column = $default_val");
|
| 793 |
}
|
| 794 |
db_query("ALTER TABLE {". $table ."} ALTER $column SET NOT NULL");
|
| 795 |
}
|
| 796 |
break;
|
| 797 |
|
| 798 |
case 'mysql':
|
| 799 |
case 'mysqli':
|
| 800 |
// MySQL allows no DEFAULT value for text (and blob) columns
|
| 801 |
if (in_array($type, array('text', 'mediumtext', 'longtext'))) {
|
| 802 |
$default = '';
|
| 803 |
// We also allow NULL values to account for CCK's per field INSERTs
|
| 804 |
$not_null = '';
|
| 805 |
}
|
| 806 |
db_query('ALTER TABLE {'. $table .'} ADD COLUMN '. $column .' '. $type .' '. $not_null .' '. $default);
|
| 807 |
break;
|
| 808 |
}
|
| 809 |
}
|
| 810 |
|
| 811 |
/**
|
| 812 |
* Change a column definition.
|
| 813 |
*
|
| 814 |
* Remember that changing a column definition involves adding a new column
|
| 815 |
* and dropping an old one. This means that any indices, primary keys and
|
| 816 |
* sequences from serial-type columns are dropped and might need to be
|
| 817 |
* recreated.
|
| 818 |
*
|
| 819 |
* This is lifted straight from cck -- content_admin.inc
|
| 820 |
*
|
| 821 |
* @param $table
|
| 822 |
* Name of the table, without {}
|
| 823 |
* @param $column
|
| 824 |
* Name of the column to change
|
| 825 |
* @param $column_new
|
| 826 |
* New name for the column (set to the same as $column if you don't want to change the name)
|
| 827 |
* @param $type
|
| 828 |
* Type of column
|
| 829 |
* @param $attributes
|
| 830 |
* Additional optional attributes. Recognized attributes:
|
| 831 |
* not null => TRUE|FALSE
|
| 832 |
* default => NULL|FALSE|value (with or without '', it won't be added)
|
| 833 |
*/
|
| 834 |
function _rpg_db_change_column($table, $column, $column_new, $type, $attributes = array()) {
|
| 835 |
switch ($GLOBALS['db_type']) {
|
| 836 |
case 'pgsql':
|
| 837 |
$mappings = array('int' => 'integer', 'mediumint' => 'integer', 'bigint' => 'integer',
|
| 838 |
'tinyint' => 'smallint',
|
| 839 |
'float' => 'float',
|
| 840 |
'varchar' => 'varchar',
|
| 841 |
'text' => 'text', 'mediumtext' => 'text', 'longtext' => 'text');
|
| 842 |
if (isset($mappings[$type])) {
|
| 843 |
$type = $mappings[$type];
|
| 844 |
}
|
| 845 |
else {
|
| 846 |
watchdog('database', t('No PostgreSQL mapping found for %type data type.', array('%type' => $type)), WATCHDOG_WARNING);
|
| 847 |
}
|
| 848 |
if ($type != 'varchar') {
|
| 849 |
unset($attributes['length']);
|
| 850 |
}
|
| 851 |
break;
|
| 852 |
|
| 853 |
case 'mysql':
|
| 854 |
case 'mysqli':
|
| 855 |
break;
|
| 856 |
}
|
| 857 |
|
| 858 |
if (array_key_exists('not null', $attributes) and $attributes['not null']) {
|
| 859 |
$not_null = 'NOT NULL';
|
| 860 |
}
|
| 861 |
if (array_key_exists('default', $attributes)) {
|
| 862 |
if (is_null($attributes['default'])) {
|
| 863 |
$default_val = 'NULL';
|
| 864 |
$default = 'default NULL';
|
| 865 |
}
|
| 866 |
elseif ($attributes['default'] === FALSE) {
|
| 867 |
$default = '';
|
| 868 |
}
|
| 869 |
else {
|
| 870 |
$default_val = "$attributes[default]";
|
| 871 |
$default = "default $attributes[default]";
|
| 872 |
}
|
| 873 |
}
|
| 874 |
if (array_key_exists('length', $attributes)) {
|
| 875 |
$type .= '('. $attributes['length'] .')';
|
| 876 |
}
|
| 877 |
if (array_key_exists('unsigned', $attributes) && $attributes['unsigned']) {
|
| 878 |
switch ($GLOBALS['db_type']) {
|
| 879 |
case 'pgsql':
|
| 880 |
$type = str_replace('integer', 'int_unsigned', $type);
|
| 881 |
break;
|
| 882 |
default:
|
| 883 |
$type .= ' unsigned';
|
| 884 |
break;
|
| 885 |
}
|
| 886 |
}
|
| 887 |
|
| 888 |
switch ($GLOBALS['db_type']) {
|
| 889 |
case 'pgsql':
|
| 890 |
db_query("ALTER TABLE {". $table ."} RENAME $column TO ". $column ."_old");
|
| 891 |
db_query("ALTER TABLE {". $table ."} ADD $column_new $type");
|
| 892 |
db_query("UPDATE {". $table ."} SET $column_new = ". $column ."_old");
|
| 893 |
if ($default) {
|
| 894 |
db_query("ALTER TABLE {". $table ."} ALTER $column_new SET $default");
|
| 895 |
}
|
| 896 |
if ($not_null) {
|
| 897 |
db_query("ALTER TABLE {". $table ."} ALTER $column_new SET NOT NULL");
|
| 898 |
}
|
| 899 |
db_query("ALTER TABLE {". $table ."} DROP ". $column ."_old");
|
| 900 |
break;
|
| 901 |
|
| 902 |
case 'mysql':
|
| 903 |
case 'mysqli':
|
| 904 |
// MySQL allows no DEFAULT value for text (and blob) columns
|
| 905 |
if (in_array($type, array('text', 'mediumtext', 'longtext'))) {
|
| 906 |
$default = '';
|
| 907 |
// We also allow NULL values to account for CCK's per field INSERTs
|
| 908 |
$not_null = '';
|
| 909 |
}
|
| 910 |
db_query('ALTER TABLE {'. $table .'} CHANGE '. $column .' '. $column_new .' '. $type .' '. $not_null .' '. $default);
|
| 911 |
break;
|
| 912 |
}
|
| 913 |
}
|
| 914 |
|
| 915 |
|
| 916 |
/**
|
| 917 |
* This will allow an object to register itself in the database as being the player character of a user.
|
| 918 |
*
|
| 919 |
* @param $rid
|
| 920 |
* The rid of the object to register
|
| 921 |
* @param $uid
|
| 922 |
* The drupal user/account uid of the user to register
|
| 923 |
* @return
|
| 924 |
* TRUE or FALSE depending on whether successful or not
|
| 925 |
*/
|
| 926 |
function _rpg_register_object_as_pc($rid, $uid) {
|
| 927 |
$object = rpg_object($rid);
|
| 928 |
$account = user_load(array('uid' => $uid));
|
| 929 |
if ($account->uid && $object->rid) {
|
| 930 |
db_query("DELETE FROM {rpg_pc} WHERE uid=%d AND rid=%d", $account->uid, $object->rid);
|
| 931 |
db_query("INSERT INTO {rpg_pc} (uid, rid) VALUES (%d, %d)", $account->uid, $object->rid);
|
| 932 |
return TRUE;
|
| 933 |
}
|
| 934 |
return FALSE;
|
| 935 |
}
|
| 936 |
|
| 937 |
/**
|
| 938 |
* This will allow an object to remove itself from the database as being the player character of a user.
|
| 939 |
*
|
| 940 |
* @param $rid
|
| 941 |
* The rid of the object to register
|
| 942 |
* @param $uid
|
| 943 |
* The drupal user/account uid of the user to register
|
| 944 |
* @return
|
| 945 |
* TRUE or FALSE depending on whether successful or not
|
| 946 |
*/
|
| 947 |
function _rpg_remove_object_as_pc($rid, $uid) {
|
| 948 |
$object = rpg_object($rid);
|
| 949 |
$account = user_load(array('uid' => $uid));
|
| 950 |
if ($account->uid && $object->rid) {
|
| 951 |
db_query("DELETE FROM {rpg_pc} WHERE uid=%d AND rid=%d", $account->uid, $object->rid);
|
| 952 |
return TRUE;
|
| 953 |
}
|
| 954 |
return FALSE;
|
| 955 |
}
|
| 956 |
|