| 1 |
<?php
|
| 2 |
// $Id: rpg.db.inc,v 1.11 2008/03/08 00:31:04 aaron Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* these are functions interfacing with the database for storing rpg objects
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* TODO: update this function for the new api
|
| 11 |
* this loads an rpg object into the global $rpg array as $rpg[$rid]
|
| 12 |
* this should rarely be called directly, as it returns nothing; instead, call rpg_object
|
| 13 |
* @param $rid
|
| 14 |
* the integer object id of the object to load
|
| 15 |
* @param $force
|
| 16 |
* optional -- if true, force the object to be reloaded. otherwise, stored in a static array.
|
| 17 |
*/
|
| 18 |
function _rpg_load_rpg($rid, $force = FALSE) {
|
| 19 |
global $rpg;
|
| 20 |
|
| 21 |
if ($rid && is_numeric($rid) && (!isset($rpg[$rid]) || $force)) {
|
| 22 |
$sql = "SELECT * FROM {rpg} WHERE rid=%d";
|
| 23 |
$results = db_query($sql, $rid);
|
| 24 |
$object = db_fetch_object($results);
|
| 25 |
if ($object->rid) {
|
| 26 |
$object->types = array();
|
| 27 |
$sql = "SELECT type from {rpg_object_types} WHERE rid=%d";
|
| 28 |
$results = db_query($sql, $object->rid);
|
| 29 |
while ($type = db_fetch_array($results)) {
|
| 30 |
$object->types[] = $type['type'];
|
| 31 |
}
|
| 32 |
// TODO: change this to drupal_alter for d6
|
| 33 |
|
| 34 |
// does this actually need to happen?
|
| 35 |
// rpg_invoke_object($object, 'load');
|
| 36 |
}
|
| 37 |
$rpg[$rid] = $object;
|
| 38 |
rpg_drupal_alter('rpg_object', $object, 'load');
|
| 39 |
}
|
| 40 |
else if ($rid === 0) {
|
| 41 |
$rpg[$rid] = rpg_limbo();
|
| 42 |
}
|
| 43 |
}
|
| 44 |
|
| 45 |
/**
|
| 46 |
* TODO: update this function for the new api.
|
| 47 |
* TODO: it's not actually called anywhere any more.
|
| 48 |
*/
|
| 49 |
function rpg_limbo() {
|
| 50 |
static $limbo;
|
| 51 |
if (!isset($limbo)) {
|
| 52 |
$limbo = array(
|
| 53 |
'types' => array(),
|
| 54 |
'uid' => 1,
|
| 55 |
'rid' => 0,
|
| 56 |
'data' => array(),
|
| 57 |
// 'location' => 0,
|
| 58 |
// 'contents' => array(),
|
| 59 |
// 'name' => t('limbo'),
|
| 60 |
// 'data' => array('proper' => true,),
|
| 61 |
);
|
| 62 |
$limbo = (object) $limbo;
|
| 63 |
}
|
| 64 |
return $limbo;
|
| 65 |
}
|
| 66 |
|
| 67 |
/**
|
| 68 |
* TODO: update this function for the new api.
|
| 69 |
* this creates a new blank rpg object of $type, created by $account->uid. called by rpg_create_object_page, and
|
| 70 |
* may be called programatically
|
| 71 |
* @param $type
|
| 72 |
* the type array returned by rpg_types
|
| 73 |
* @param $owner
|
| 74 |
* the account of the owning user
|
| 75 |
* @param $auto_save
|
| 76 |
* optional; if true, then we'll automatically save w/ new rid
|
| 77 |
*/
|
| 78 |
function rpg_new_rpg_object($type, $owner, $auto_save = false) {
|
| 79 |
global $rpg;
|
| 80 |
|
| 81 |
$object = array(
|
| 82 |
'types' => array($type['type']),
|
| 83 |
'uid' => $owner->uid,
|
| 84 |
'is_new' => true,
|
| 85 |
'rid' => 'temp:' . rand(),
|
| 86 |
'data' => array(),
|
| 87 |
);
|
| 88 |
|
| 89 |
// set up initial attribute data. note that this allows for objects to inherit from multiple types
|
| 90 |
// also, since default values of inherited types are set first, they will automatically be overridden
|
| 91 |
// and the same when inheriting from multiple types. although we should TODO do this from the flattened
|
| 92 |
// type array ultimately... see that function in rpg.invoke.inc...
|
| 93 |
foreach ($object['types'] as $type_str) {
|
| 94 |
$type = rpg_types($type_str);
|
| 95 |
foreach ($type['attributes']['inherited'] as $attr => $attribute) {
|
| 96 |
foreach ($attribute['types'] as $parent) {
|
| 97 |
$object['data'][$attr] = $parent['default_value'];
|
| 98 |
}
|
| 99 |
}
|
| 100 |
foreach ($type['attributes']['defined'] as $attr => $attribute) {
|
| 101 |
$object['data'][$attr] = $attribute['default_value'];
|
| 102 |
}
|
| 103 |
}
|
| 104 |
|
| 105 |
// turn our object into an object
|
| 106 |
$object = (object) $object;
|
| 107 |
|
| 108 |
// if we call this function with autosave, then automatically put it into the db.
|
| 109 |
// this may be used to quickly add one programatically. not called when creating one with a form.
|
| 110 |
if ($auto_save) {
|
| 111 |
$object = _rpg_insert_new_rpg($object);
|
| 112 |
}
|
| 113 |
|
| 114 |
$rpg[$object->rid] = $object;
|
| 115 |
|
| 116 |
// return the object object to be put into the global $rpg array
|
| 117 |
return $object;
|
| 118 |
}
|
| 119 |
|
| 120 |
|
| 121 |
/**
|
| 122 |
* this inserts a newly created rpg object into the database, assigning a new rid,
|
| 123 |
* adding it to the global $rpg array, and returning the revised object (with the new rid).
|
| 124 |
* this is called by rpg_new_rpg_object when autosave is set. also called from rpg_form_object_submit
|
| 125 |
*
|
| 126 |
* @param $object
|
| 127 |
* an object with the basic fields filled in (from a previously called rpg_invoke('create', $object))
|
| 128 |
* @return
|
| 129 |
* the object with the new ->rid
|
| 130 |
*/
|
| 131 |
function _rpg_insert_new_rpg($object) {
|
| 132 |
global $rpg;
|
| 133 |
$object->rid = db_next_id('{rpg}_rid');
|
| 134 |
$rpg[$object->rid] = $object;
|
| 135 |
// rpg_invoke_object($object, 'insert');
|
| 136 |
unset($rpg[$object->rid]->is_new);
|
| 137 |
$object = $rpg[$object->rid];
|
| 138 |
db_query("INSERT INTO {rpg} (rid, uid, created, changed) VALUES (%d, %d, %d, %d)", $object->rid, $object->uid, $object->created, $object->changed);
|
| 139 |
foreach($object->types as $type) {
|
| 140 |
db_query("INSERT INTO {rpg_object_types} (rid, type) VALUES (%d, '%s')", $object->rid, $type);
|
| 141 |
}
|
| 142 |
|
| 143 |
// make sure each attribute will be initially saved, preserving defaults
|
| 144 |
foreach (rpg_object_attributes($object) as $type) {
|
| 145 |
if (!$object->data[$type['attribute']] && $type['default_value']) {
|
| 146 |
$object->data[$type['attribute']] = $type['default_value'];
|
| 147 |
}
|
| 148 |
}
|
| 149 |
foreach ($object->data as $attr => $value) {
|
| 150 |
rpg_set($attr, $object->rid, $value);
|
| 151 |
}
|
| 152 |
|
| 153 |
// rpg_trigger('on_create', $object->rid);
|
| 154 |
|
| 155 |
return $object;
|
| 156 |
}
|
| 157 |
|
| 158 |
/**
|
| 159 |
* TODO: update this function for the new api.
|
| 160 |
* this updates the object, saving its new values into the database.
|
| 161 |
* it does NOT save attributes. that happens in _rpg_save_all_marked_objects.
|
| 162 |
* It is called from rpg_form_object_submit
|
| 163 |
* @param
|
| 164 |
* the object or rid to be saved
|
| 165 |
* @return
|
| 166 |
* NULL
|
| 167 |
*/
|
| 168 |
function rpg_save_rpg($object) {//print_r($object);
|
| 169 |
global $rpg;
|
| 170 |
rpg_invoke_object($object, 'update');//print_r($object);
|
| 171 |
$rpg[$object->rid] = $object;
|
| 172 |
db_query("UPDATE {rpg} SET uid=%d, created=%d, changed=%d WHERE rid=%d", $object->uid, $object->created, $object->changed, $object->rid);
|
| 173 |
db_query("DELETE FROM {rpg_object_types} WHERE rid=%d", $object->rid);
|
| 174 |
foreach($object->types as $type) {
|
| 175 |
db_query("INSERT INTO {rpg_object_types} (rid, type) VALUES (%d, '%s')", $object->rid, $type);
|
| 176 |
}
|
| 177 |
// reset attributes according to new types
|
| 178 |
rpg_object_attributes($object, NULL, FALSE);
|
| 179 |
}
|
| 180 |
|
| 181 |
/**
|
| 182 |
* this will save all objects marked with ->save, unsetting that field.
|
| 183 |
*
|
| 184 |
* generally, this ensures we save only objects whose values have changed. this function will unset all the save arrays,
|
| 185 |
* so they won't get saved again if called again. this function should only be called once per page, at the end of
|
| 186 |
* page load, or if the global $rpg array is to be reset.
|
| 187 |
*/
|
| 188 |
function _rpg_save_all_marked_objects() {
|
| 189 |
global $rpg;//print_r($rpg);
|
| 190 |
if (is_array($rpg['save'])) {
|
| 191 |
foreach ($rpg['save'] as $rid => $attributes) {
|
| 192 |
if (is_array($attributes)) {
|
| 193 |
// this will give us the object & attributes we need to save...
|
| 194 |
foreach ($attributes as $property => $value) {
|
| 195 |
_rpg_set_object_value_to_db($rpg[$rid], rpg_attributes($property));
|
| 196 |
}
|
| 197 |
unset($rpg[$rid]->save);
|
| 198 |
}
|
| 199 |
}
|
| 200 |
}
|
| 201 |
unset($rpg['save']);
|
| 202 |
}
|
| 203 |
|
| 204 |
/**
|
| 205 |
* This will set the value of the object's attribute in the database.
|
| 206 |
*
|
| 207 |
* The class has a chance to modify the value first. This function is to be ONLY called by rpg_save_all_marked_objects,
|
| 208 |
* which should only be called once per page load (or possibly when resetting the global rpg, if that ever happens).
|
| 209 |
*
|
| 210 |
* @param $object
|
| 211 |
* This is the object prepopulated with the correct values. However, the value will be pulled from the global $rpg
|
| 212 |
* before sending to the class for possible modification.
|
| 213 |
* @param $attribute
|
| 214 |
* This is the proper attribute array.
|
| 215 |
* @param $insert
|
| 216 |
* If true, then the value will be inserted into the database. Used for new objects. Otherwise, it's updated instead.
|
| 217 |
* TODO: $insert is NOT CURRENTLY USED -- NEED TO FIGURE BEST WAY TO CALL THAT...
|
| 218 |
*/
|
| 219 |
function _rpg_set_object_value_to_db($object, $attribute, $insert = false) {
|
| 220 |
global $rpg;
|
| 221 |
if ($object->rid && is_numeric($object->rid) && isset($rpg[$object->rid])) {
|
| 222 |
$class = rpg_attribute_classes($attribute['class']);
|
| 223 |
$value = $rpg[$object->rid]->data[$attribute['attribute']];
|
| 224 |
$test_value = module_invoke($class['module'], 'rpg_attribute_class_settings', 'db_set', $class['class'], $value);
|
| 225 |
if (isset($test_value)) {
|
| 226 |
$value = $test_value;
|
| 227 |
}
|
| 228 |
foreach (module_invoke($class['module'], 'rpg_attribute_class_settings', 'data', $class['class']) as $data_type => $data) {
|
| 229 |
$table = '{rpg_attribute_rpg_' . $attribute['attribute'] . '}';
|
| 230 |
if ($data['type'] == 'int') {
|
| 231 |
$query_type = '%d';
|
| 232 |
}
|
| 233 |
else {
|
| 234 |
$query_type = '"%s"';
|
| 235 |
}
|
| 236 |
db_query("DELETE FROM $table WHERE rid=%d", $object->rid);
|
| 237 |
db_query("INSERT INTO $table (rid, $data_type) VALUES (%d, $query_type)", $object->rid, $value);
|
| 238 |
// if ($insert) {
|
| 239 |
// db_queryd("INSERT INTO $table (rid, $data_type) VALUES (%d, $query_type)", $object->rid, $value);
|
| 240 |
// }
|
| 241 |
// else {
|
| 242 |
// db_queryd("UPDATE $table SET $data_type=$query_type WHERE rid=%d", $value, $object->rid);
|
| 243 |
// }
|
| 244 |
}
|
| 245 |
}
|
| 246 |
}
|
| 247 |
|