function ctools_export_load_object in Chaos Tool Suite (ctools) 6
Same name and namespace in other branches
- 7 includes/export.inc \ctools_export_load_object()
Load some number of exportable objects.
This function will cache the objects, load subsidiary objects if necessary, check default objects in code and properly set them up. It will cache the results so that multiple calls to load the same objects will not cause problems.
It attempts to reduce, as much as possible, the number of queries involved.
Parameters
$table: The name of the table to be loaded from. Data is expected to be in the schema to make all this work.
$type: A string to notify the loader what the argument is
- all: load all items. This is the default. $args is unused.
- names: $args will be an array of specific named objects to load.
- conditions: $args will be a keyed array of conditions. The conditions must be in the schema for this table or errors will result.
$args: An array of arguments whose actual use is defined by the $type argument.
9 calls to ctools_export_load_object()
- ctools_export_crud_load in includes/
export.inc - Load a single exportable object.
- ctools_export_crud_load_all in includes/
export.inc - Load all exportable objects of a given type.
- ctools_export_default_to_hook_code in includes/
export.inc - Default function to export objects to code.
- page_manager_load_task_handler in page_manager/
page_manager.module - Load a single task handler by name.
- page_manager_load_task_handlers in page_manager/
page_manager.module - Load all task handlers for a given task/subtask.
1 string reference to 'ctools_export_load_object'
- ctools_export_load_object_reset in includes/
export.inc - Reset all static caches in ctools_export_load_object() or static caches for a given table in ctools_export_load_object().
File
- includes/
export.inc, line 296 - Contains code to make it easier to have exportable objects.
Code
function ctools_export_load_object($table, $type = 'all', $args = array()) {
$cache =& ctools_static(__FUNCTION__);
$cached_database =& ctools_static('ctools_export_load_object_all');
$schema = ctools_export_get_schema($table);
if (empty($schema)) {
return array();
}
$join_schemas = array();
$export = $schema['export'];
if (!isset($cache[$table])) {
$cache[$table] = array();
}
// If fetching all and cached all, we've done so and we are finished.
if ($type == 'all' && !empty($cached_database[$table])) {
return $cache[$table];
}
$return = array();
// Don't load anything we've already cached.
if ($type == 'names' && !empty($args)) {
foreach ($args as $id => $name) {
if (isset($cache[$table][$name])) {
$return[$name] = $cache[$table][$name];
unset($args[$id]);
}
}
// If nothing left to load, return the result.
if (empty($args)) {
return $return;
}
}
// Build the query
$query = "SELECT * FROM {" . $table . "} t__0";
$alias_count = 1;
if (!empty($schema['join'])) {
foreach ($schema['join'] as $join_key => $join) {
$join_schema = drupal_get_schema($join['table']);
if (!empty($join_schema)) {
$query .= ' INNER JOIN {' . $join['table'] . '} t__' . $alias_count . ' ON t__0.' . $join['left_key'] . ' = ' . 't__' . $alias_count . '.' . $join['right_key'];
$alias_count++;
$join_schemas[$join['table']] = $join_schema;
if (!empty($join['extra'])) {
$query .= ' ' . $join['extra'];
}
}
}
}
$conditions = array();
$query_args = array();
// If they passed in names, add them to the query.
if ($type == 'names') {
if (!isset($export['key in table'])) {
$conditions[] = "{$export['key']} IN (" . db_placeholders($args, $schema['fields'][$export['key']]['type']) . ")";
}
else {
$conditions[] = "{$export['key']} IN (" . db_placeholders($args, $join_schemas[$export['key in table']]['fields'][$export['key']]['type']) . ")";
}
$query_args = $args;
}
else {
if ($type == 'conditions') {
foreach ($args as $key => $value) {
if (isset($schema['fields'][$key])) {
$conditions[] = "{$key} = " . db_type_placeholder($schema['fields'][$key]['type']);
$query_args[] = $value;
}
}
}
}
// Make a string out of the conditions.
if ($conditions) {
$query .= " WHERE " . implode(' AND ', $conditions);
}
$result = db_query($query, $query_args);
$status = variable_get($export['status'], array());
// Unpack the results of the query onto objects and cache them.
while ($data = db_fetch_object($result)) {
if (isset($schema['export']['object factory']) && function_exists($schema['export']['object factory'])) {
$object = $schema['export']['object factory']($schema, $data);
}
else {
$object = _ctools_export_unpack_object($schema, $data, $export['object']);
}
$object->table = $table;
$object->{$export['export type string']} = t('Normal');
$object->export_type = EXPORT_IN_DATABASE;
// Determine if default object is enabled or disabled.
if (isset($status[$object->{$export['key']}])) {
$object->disabled = $status[$object->{$export['key']}];
}
$cache[$table][$object->{$export['key']}] = $object;
if ($type == 'conditions') {
$return[$object->{$export['key']}] = $object;
}
}
// Load subrecords.
if (isset($export['subrecords callback']) && function_exists($export['subrecords callback'])) {
$export['subrecords callback']($cache[$table]);
}
if ($defaults = _ctools_export_get_defaults($table, $export)) {
foreach ($defaults as $object) {
if ($type == 'conditions') {
// if this does not match all of our conditions, skip it.
foreach ($args as $key => $value) {
if (!isset($object->{$key}) || $object->{$key} != $value) {
continue 2;
}
}
}
else {
if ($type == 'names') {
if (!in_array($object->{$export['key']}, $args)) {
continue;
}
}
}
// Determine if default object is enabled or disabled.
if (isset($status[$object->{$export['key']}])) {
$object->disabled = $status[$object->{$export['key']}];
}
if (!empty($cache[$table][$object->{$export['key']}])) {
$cache[$table][$object->{$export['key']}]->{$export['export type string']} = t('Overridden');
$cache[$table][$object->{$export['key']}]->export_type |= EXPORT_IN_CODE;
if ($type == 'conditions') {
$return[$object->{$export['key']}] = $cache[$table][$object->{$export['key']}];
}
}
else {
$object->{$export['export type string']} = t('Default');
$object->export_type = EXPORT_IN_CODE;
$object->in_code_only = TRUE;
$object->table = $table;
$cache[$table][$object->{$export['key']}] = $object;
if ($type == 'conditions') {
$return[$object->{$export['key']}] = $object;
}
}
}
}
// If fetching all, we've done so and we are finished.
if ($type == 'all') {
$cached_database[$table] = TRUE;
return $cache[$table];
}
if ($type == 'names') {
foreach ($args as $name) {
if (isset($cache[$table][$name])) {
$return[$name] = $cache[$table][$name];
}
}
}
// For conditions,
return $return;
}