|
@@ -1,7 +1,18 @@
|
|
<?php
|
|
<?php
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* @file
|
|
* @file
|
|
* This API generates objects containing the full details of a record(s) in chado.
|
|
* This API generates objects containing the full details of a record(s) in chado.
|
|
|
|
+ *
|
|
|
|
+ * @ingroup tripal_chado
|
|
|
|
+ */
|
|
|
|
+/**
|
|
|
|
+ * @defgroup tripal_chado_variables_api Semantic Web
|
|
|
|
+ * @ingroup tripal_chado_api
|
|
|
|
+ *
|
|
|
|
+ * @{
|
|
|
|
+ * This API generates objects containing the full details of a record(s) in chado.
|
|
|
|
+ * @}
|
|
*/
|
|
*/
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -130,7 +141,7 @@
|
|
* base table). If the option 'return_array' is provided the function
|
|
* base table). If the option 'return_array' is provided the function
|
|
* always returns an array.
|
|
* always returns an array.
|
|
*
|
|
*
|
|
- * @ingroup tripal_chado_query_api
|
|
|
|
|
|
+ * @ingroup tripal_chado_variables_api
|
|
*/
|
|
*/
|
|
function chado_generate_var($table, $values, $base_options = array()) {
|
|
function chado_generate_var($table, $values, $base_options = array()) {
|
|
$all = new stdClass();
|
|
$all = new stdClass();
|
|
@@ -147,7 +158,7 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
if (array_key_exists('pager', $base_options)) {
|
|
if (array_key_exists('pager', $base_options)) {
|
|
$pager = $base_options['pager'];
|
|
$pager = $base_options['pager'];
|
|
}
|
|
}
|
|
- // get description for the current table----------------------------------------------------------
|
|
|
|
|
|
+ // get description for the current table-------------------------------------
|
|
$table_desc = chado_get_schema($table);
|
|
$table_desc = chado_get_schema($table);
|
|
if (!$table_desc or count($table_desc) == 0) {
|
|
if (!$table_desc or count($table_desc) == 0) {
|
|
tripal_report_error('tripal_chado', TRIPAL_ERROR,
|
|
tripal_report_error('tripal_chado', TRIPAL_ERROR,
|
|
@@ -163,7 +174,7 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
$table_primary_key = $table_desc['primary key'][0];
|
|
$table_primary_key = $table_desc['primary key'][0];
|
|
$table_columns = array_keys($table_desc['fields']);
|
|
$table_columns = array_keys($table_desc['fields']);
|
|
|
|
|
|
- // Expandable fields without value needed for criteria--------------------------------------------
|
|
|
|
|
|
+ // Expandable fields without value needed for criteria-----------------------
|
|
// Add in the default expandable arrays
|
|
// Add in the default expandable arrays
|
|
// These are used for later expanding fields, tables, foreign keys and nodes
|
|
// These are used for later expanding fields, tables, foreign keys and nodes
|
|
$all->expandable_fields = array();
|
|
$all->expandable_fields = array();
|
|
@@ -183,34 +194,36 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
|
|
|
|
// This allows modules to specify that some fields should be excluded by default
|
|
// This allows modules to specify that some fields should be excluded by default
|
|
// For example, tripal core provides a tripal_chado_exclude_field_from_feature_by_default()
|
|
// For example, tripal core provides a tripal_chado_exclude_field_from_feature_by_default()
|
|
- // which says that we usually don't want to include the residues field by default since
|
|
|
|
- // it can be very large and cause performance issues.
|
|
|
|
|
|
+ // which says that we usually don't want to include the residues field by
|
|
|
|
+ // default since it can be very large and cause performance issues.
|
|
|
|
|
|
- // If a field is excluded by default it can always be expanded at a later point by calling
|
|
|
|
- // chado_expand_var($chado_var, 'field', <field name as shown in expandable_fields array>);
|
|
|
|
|
|
+ // If a field is excluded by default it can always be expanded at a later
|
|
|
|
+ // point by calling chado_expand_var($chado_var, 'field',
|
|
|
|
+ // <field name as shown in expandable_fields array>);
|
|
|
|
|
|
// First get an array of all the fields to be removed for the current table
|
|
// First get an array of all the fields to be removed for the current table
|
|
- // module_invoke_all() is drupal's way of invoking all implementations of the specified
|
|
|
|
- // hook and merging all of the results.
|
|
|
|
|
|
+ // module_invoke_all() is drupal's way of invoking all implementations of the
|
|
|
|
+ // specified hook and merging all of the results.
|
|
|
|
|
|
// $fields_to_remove should be an array with the keys matching field names
|
|
// $fields_to_remove should be an array with the keys matching field names
|
|
- // and the values being strings to be executed using php_eval() to determine whether
|
|
|
|
- // to exclude the field (evaluates to TRUE) or not (evaluates to FALSE)
|
|
|
|
|
|
+ // and the values being strings to be executed using php_eval() to determine
|
|
|
|
+ // whether to exclude the field (evaluates to TRUE) or not (evaluates to FALSE)
|
|
$fields_to_remove = module_invoke_all('exclude_field_from_' . $table . '_by_default');
|
|
$fields_to_remove = module_invoke_all('exclude_field_from_' . $table . '_by_default');
|
|
|
|
|
|
// Now, for each field to be removed
|
|
// Now, for each field to be removed
|
|
foreach ($fields_to_remove as $field_name => $criteria) {
|
|
foreach ($fields_to_remove as $field_name => $criteria) {
|
|
|
|
|
|
- //replace <field_name> with the current field name
|
|
|
|
|
|
+ //Replace <field_name> with the current field name
|
|
$field_name_safe = preg_replace("/\'\"\\\/", '\\1', $field_name);
|
|
$field_name_safe = preg_replace("/\'\"\\\/", '\\1', $field_name);
|
|
$criteria = preg_replace('/<field_name> /', $field_name_safe, $criteria);
|
|
$criteria = preg_replace('/<field_name> /', $field_name_safe, $criteria);
|
|
- // if field_value needed we can't deal with this field yet
|
|
|
|
|
|
+ // If field_value needed we can't deal with this field yet.
|
|
if (preg_match('/<field_value> /', $criteria)) {
|
|
if (preg_match('/<field_value> /', $criteria)) {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
- //if criteria then remove from query
|
|
|
|
- // @coder-ignore: only module designers can populate $criteria -not a security risk
|
|
|
|
|
|
+ // If criteria then remove from query
|
|
|
|
+ // @coder-ignore: only module designers can populate $criteria -not a
|
|
|
|
+ // security risk.
|
|
$success = php_eval('<?php return ' . $criteria . '; ?>');
|
|
$success = php_eval('<?php return ' . $criteria . '; ?>');
|
|
if ($success) {
|
|
if ($success) {
|
|
unset($table_columns[array_search($field_name, $table_columns)]);
|
|
unset($table_columns[array_search($field_name, $table_columns)]);
|
|
@@ -222,40 +235,43 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
// Get fields to be removed by type................................
|
|
// Get fields to be removed by type................................
|
|
// This gets all implementations of hook_exclude_type_by_default().
|
|
// This gets all implementations of hook_exclude_type_by_default().
|
|
|
|
|
|
- // This allows modules to specify that some types of fields should be excluded by default
|
|
|
|
- // For example, tripal core provides a tripal_chado_exclude_type_by_default() which says
|
|
|
|
- // that text fields are often very large and if they are longer than 250 characters then
|
|
|
|
|
|
+ // This allows modules to specify that some types of fields should be excluded
|
|
|
|
+ // by default For example, tripal core provides a
|
|
|
|
+ // tripal_chado_exclude_type_by_default() which says that text fields are
|
|
|
|
+ // often very large and if they are longer than 250 characters then
|
|
// we want to exclude them by default
|
|
// we want to exclude them by default
|
|
|
|
|
|
- // If a field is excluded by default it can always be expanded at a later point by calling
|
|
|
|
- // chado_expand_var($chado_var, 'field', <field name as shown in expandable_fields array>);
|
|
|
|
|
|
+ // If a field is excluded by default it can always be expanded at a later
|
|
|
|
+ // point by calling chado_expand_var($chado_var, 'field',
|
|
|
|
+ //<field name as shown in expandable_fields array>);
|
|
|
|
|
|
- // First get an array of all the types of fields to be removed for the current table
|
|
|
|
- // module_invoke_all() is drupal's way of invoking all implementations of the specified
|
|
|
|
- // hook and merging all of the results.
|
|
|
|
|
|
+ // First get an array of all the types of fields to be removed for the current
|
|
|
|
+ // table module_invoke_all() is drupal's way of invoking all implementations
|
|
|
|
+ // of the specified hook and merging all of the results.
|
|
|
|
|
|
// $types_to_remove should be an array with the keys matching field names
|
|
// $types_to_remove should be an array with the keys matching field names
|
|
- // and the values being strings to be executed using php_eval() to determine whether
|
|
|
|
- // to exclude the field (evaluates to TRUE) or not (evaluates to FALSE)
|
|
|
|
|
|
+ // and the values being strings to be executed using php_eval() to determine
|
|
|
|
+ // whether to exclude the field (evaluates to TRUE) or not (evaluates to FALSE)
|
|
// (ie: array('text' => 'strlen("<field_value> ") > 100');
|
|
// (ie: array('text' => 'strlen("<field_value> ") > 100');
|
|
$types_to_remove = module_invoke_all('exclude_type_by_default');
|
|
$types_to_remove = module_invoke_all('exclude_type_by_default');
|
|
|
|
|
|
// Get a list of all the types of fields
|
|
// Get a list of all the types of fields
|
|
- // the key is the type of field and the value is an array of fields of this type
|
|
|
|
|
|
+ // the key is the type of field and the value is an array of fields of this
|
|
|
|
+ // type.
|
|
$field_types = array();
|
|
$field_types = array();
|
|
foreach ($table_desc['fields'] as $field_name => $field_array) {
|
|
foreach ($table_desc['fields'] as $field_name => $field_array) {
|
|
$field_types[$field_array['type']][] = $field_name;
|
|
$field_types[$field_array['type']][] = $field_name;
|
|
}
|
|
}
|
|
|
|
|
|
- // We want to use the types to remove in conjunction with our table field descriptions
|
|
|
|
- // to determine which fields might need to be removed
|
|
|
|
|
|
+ // We want to use the types to remove in conjunction with our table field
|
|
|
|
+ // descriptions to determine which fields might need to be removed.
|
|
foreach ($types_to_remove as $field_type => $criteria) {
|
|
foreach ($types_to_remove as $field_type => $criteria) {
|
|
|
|
|
|
- // if there are fields of that type to remove
|
|
|
|
|
|
+ // If there are fields of that type to remove.
|
|
if (isset($field_types[$field_type])) {
|
|
if (isset($field_types[$field_type])) {
|
|
|
|
|
|
// Do any processing needed on the php criteria
|
|
// Do any processing needed on the php criteria
|
|
- //replace <field_name> with the current field name
|
|
|
|
|
|
+ //replace <field_name> with the current field name.
|
|
$field_name_safe = preg_replace('/\'|"|\\\/', '\\1', $field_name);
|
|
$field_name_safe = preg_replace('/\'|"|\\\/', '\\1', $field_name);
|
|
$criteria = preg_replace('/<field_name> /', $field_name_safe, $criteria);
|
|
$criteria = preg_replace('/<field_name> /', $field_name_safe, $criteria);
|
|
foreach ($field_types[$field_type] as $field_name) {
|
|
foreach ($field_types[$field_type] as $field_name) {
|
|
@@ -265,19 +281,20 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
- // if criteria then remove from query
|
|
|
|
- // (as long as <field_value> is not needed for the criteria to be evaluated)
|
|
|
|
- // @coder-ignore: only module designers can populate $criteria -not a security risk
|
|
|
|
|
|
+ // If criteria then remove from query
|
|
|
|
+ // (as long as <field_value> is not needed for the criteria to be
|
|
|
|
+ // evaluated) @coder-ignore: only module designers can populate
|
|
|
|
+ //$criteria -not a security risk.
|
|
$success = php_eval('<?php return ' . $criteria . '; ?>');
|
|
$success = php_eval('<?php return ' . $criteria . '; ?>');
|
|
if ($success) {
|
|
if ($success) {
|
|
unset($table_columns[array_search($field_name, $table_columns)]);
|
|
unset($table_columns[array_search($field_name, $table_columns)]);
|
|
$all->expandable_fields[] = $table . '.' . $field_name;
|
|
$all->expandable_fields[] = $table . '.' . $field_name;
|
|
}
|
|
}
|
|
- } //end of foreach field of that type
|
|
|
|
|
|
+ } // End of foreach field of that type.
|
|
}
|
|
}
|
|
- } //end of foreach type to be removed
|
|
|
|
|
|
+ } // End of foreach type to be removed.
|
|
|
|
|
|
- // get the values for the record in the current table---------------------------------------------
|
|
|
|
|
|
+ // Get the values for the record in the current table-------------------------
|
|
$results = chado_select_record($table, $table_columns, $values, $base_options);
|
|
$results = chado_select_record($table, $table_columns, $values, $base_options);
|
|
|
|
|
|
if ($results) {
|
|
if ($results) {
|
|
@@ -285,7 +302,7 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
// Iterate through each result.
|
|
// Iterate through each result.
|
|
foreach ($results as $key => $object) {
|
|
foreach ($results as $key => $object) {
|
|
|
|
|
|
- // Add empty expandable_x arrays
|
|
|
|
|
|
+ // Add empty expandable_x arrays.
|
|
$object->expandable_fields = $all->expandable_fields;
|
|
$object->expandable_fields = $all->expandable_fields;
|
|
$object->expandable_foreign_keys = $all->expandable_foreign_keys;
|
|
$object->expandable_foreign_keys = $all->expandable_foreign_keys;
|
|
$object->expandable_tables = $all->expandable_tables;
|
|
$object->expandable_tables = $all->expandable_tables;
|
|
@@ -294,11 +311,12 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
$object->tablename = $table;
|
|
$object->tablename = $table;
|
|
|
|
|
|
// For Tripal v2 compatibility
|
|
// For Tripal v2 compatibility
|
|
- // check if the current table maps to a node type-----------------------------------------------
|
|
|
|
- // if this table is connected to a node there will be a chado_tablename table in drupal
|
|
|
|
|
|
+ // check if the current table maps to a node type-------------------------
|
|
|
|
+ // If this table is connected to a node there will be a chado_tablename
|
|
|
|
+ // table in drupal.
|
|
if (module_exists('tripal_core') and db_table_exists('chado_' . $table)) {
|
|
if (module_exists('tripal_core') and db_table_exists('chado_' . $table)) {
|
|
- // that has a foreign key to this one ($table_desc['primary key'][0]
|
|
|
|
- // and to the node table (nid)
|
|
|
|
|
|
+ // That has a foreign key to this one ($table_desc['primary key'][0]
|
|
|
|
+ // and to the node table (nid).
|
|
$sql = "
|
|
$sql = "
|
|
SELECT $table_primary_key, nid
|
|
SELECT $table_primary_key, nid
|
|
FROM {chado_$table}
|
|
FROM {chado_$table}
|
|
@@ -319,13 +337,14 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
$object->entity_id = $entity_id;
|
|
$object->entity_id = $entity_id;
|
|
}
|
|
}
|
|
|
|
|
|
- // remove any fields where criteria needs to be evalulated---------------------------------------
|
|
|
|
|
|
+ // Remove any fields where criteria needs to be evalulated----------------
|
|
// The fields to be removed can be populated by implementing either
|
|
// The fields to be removed can be populated by implementing either
|
|
- // hook_exclude_field_from_<table>_by_default() where <table> is the current table
|
|
|
|
- // OR hook_exclude_type_by_default() where there are fields of the specified type in the current table
|
|
|
|
- // It only reaches this point if the criteria specified for whether or not to
|
|
|
|
- // exclude the field includes <field_value> which means it has to be evaluated after
|
|
|
|
- // the query has been executed
|
|
|
|
|
|
+ // hook_exclude_field_from_<table>_by_default() where <table> is the
|
|
|
|
+ // current table OR hook_exclude_type_by_default() where there are fields
|
|
|
|
+ // of the specified type in the current table It only reaches this point
|
|
|
|
+ // if the criteria specified for whether or not to exclude the field
|
|
|
|
+ // includes <field_value> which means it has to be evaluated after
|
|
|
|
+ // the query has been executed.
|
|
foreach ($fields_to_remove as $field_name => $criteria) {
|
|
foreach ($fields_to_remove as $field_name => $criteria) {
|
|
|
|
|
|
// If the field is an object then we don't support exclusion of it
|
|
// If the field is an object then we don't support exclusion of it
|
|
@@ -334,7 +353,8 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
- // replace <field_value> with the actual value of the field from the query
|
|
|
|
|
|
+ // Replace <field_value> with the actual value of the field from the
|
|
|
|
+ // query.
|
|
$field_name_safe = preg_replace('/\'|"|\\\/', '\\1', $object->{$field_name});
|
|
$field_name_safe = preg_replace('/\'|"|\\\/', '\\1', $object->{$field_name});
|
|
$criteria = preg_replace('/<field_value>/', $field_name_safe, $criteria);
|
|
$criteria = preg_replace('/<field_value>/', $field_name_safe, $criteria);
|
|
|
|
|
|
@@ -348,34 +368,35 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // recursively follow foreign key relationships nesting objects as we go------------------------
|
|
|
|
|
|
+ // Recursively follow foreign key relationships nesting objects as we go------------------------
|
|
if (array_key_exists('foreign keys', $table_desc) and $table_desc['foreign keys']) {
|
|
if (array_key_exists('foreign keys', $table_desc) and $table_desc['foreign keys']) {
|
|
foreach ($table_desc['foreign keys'] as $foreign_key_array) {
|
|
foreach ($table_desc['foreign keys'] as $foreign_key_array) {
|
|
$foreign_table = $foreign_key_array['table'];
|
|
$foreign_table = $foreign_key_array['table'];
|
|
foreach ($foreign_key_array['columns'] as $foreign_key => $primary_key) {
|
|
foreach ($foreign_key_array['columns'] as $foreign_key => $primary_key) {
|
|
|
|
|
|
- // Note: Foreign key is the field in the current table whereas primary_key is the field in
|
|
|
|
- // the table referenced by the foreign key
|
|
|
|
- //Dont do anything if the foreign key is empty
|
|
|
|
|
|
+ // Note: Foreign key is the field in the current table whereas
|
|
|
|
+ // primary_key is the field in the table referenced by the foreign
|
|
|
|
+ // key, don't do anything if the foreign key is empty
|
|
if (empty($object->{$foreign_key})) {
|
|
if (empty($object->{$foreign_key})) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
if (is_array($include_fk)) {
|
|
if (is_array($include_fk)) {
|
|
- // don't recurse if the callee has supplied an $fk_include list and this
|
|
|
|
- // FK table is not in the list.
|
|
|
|
|
|
+ // Don't recurse if the callee has supplied an $fk_include list
|
|
|
|
+ // and this FK table is not in the list.
|
|
if (is_array($include_fk) and !array_key_exists($foreign_key, $include_fk)) {
|
|
if (is_array($include_fk) and !array_key_exists($foreign_key, $include_fk)) {
|
|
$object->expandable_foreign_keys[] = $table . '.' . $foreign_key . ' => ' . $foreign_table;
|
|
$object->expandable_foreign_keys[] = $table . '.' . $foreign_key . ' => ' . $foreign_table;
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- // if we have the option but it is not an array then we don't recurse any furutehr
|
|
|
|
|
|
+ // If we have the option but it is not an array then we don't
|
|
|
|
+ // recurse any further.
|
|
if ($include_fk === TRUE) {
|
|
if ($include_fk === TRUE) {
|
|
$object->expandable_foreign_keys[] = $table . '.' . $foreign_key . ' => ' . $foreign_table;
|
|
$object->expandable_foreign_keys[] = $table . '.' . $foreign_key . ' => ' . $foreign_table;
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
- // get the record from the foreign table
|
|
|
|
|
|
+ // Get the record from the foreign table.
|
|
$foreign_values = array($primary_key => $object->{$foreign_key});
|
|
$foreign_values = array($primary_key => $object->{$foreign_key});
|
|
$options = array();
|
|
$options = array();
|
|
if (is_array($include_fk)) {
|
|
if (is_array($include_fk)) {
|
|
@@ -384,9 +405,9 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
|
|
|
|
$foreign_object = chado_generate_var($foreign_table, $foreign_values, $options);
|
|
$foreign_object = chado_generate_var($foreign_table, $foreign_values, $options);
|
|
|
|
|
|
- // add the foreign record to the current object in a nested manner
|
|
|
|
|
|
+ // Add the foreign record to the current object in a nested manner.
|
|
$object->{$foreign_key} = $foreign_object;
|
|
$object->{$foreign_key} = $foreign_object;
|
|
- // Flatten expandable_x arrays so only in the bottom object
|
|
|
|
|
|
+ // Flatten expandable_x arrays so only in the bottom object.
|
|
if (property_exists($object->{$foreign_key}, 'expandable_fields') and
|
|
if (property_exists($object->{$foreign_key}, 'expandable_fields') and
|
|
is_array($object->{$foreign_key}->expandable_fields)) {
|
|
is_array($object->{$foreign_key}->expandable_fields)) {
|
|
$object->expandable_fields = array_merge(
|
|
$object->expandable_fields = array_merge(
|
|
@@ -426,26 +447,26 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // convert the results into an array
|
|
|
|
|
|
+ // Convert the results into an array.
|
|
$results_arr = array();
|
|
$results_arr = array();
|
|
foreach ($results as $record) {
|
|
foreach ($results as $record) {
|
|
$results_arr[] = $record;
|
|
$results_arr[] = $record;
|
|
}
|
|
}
|
|
- // check only one result returned
|
|
|
|
|
|
+ // Check only one result returned.
|
|
if (!$return_array) {
|
|
if (!$return_array) {
|
|
if (sizeof($results_arr) == 1) {
|
|
if (sizeof($results_arr) == 1) {
|
|
- // add results to object
|
|
|
|
|
|
+ // Add results to object.
|
|
return $results_arr[0];
|
|
return $results_arr[0];
|
|
}
|
|
}
|
|
elseif (!empty($results_arr)) {
|
|
elseif (!empty($results_arr)) {
|
|
return $results_arr;
|
|
return $results_arr;
|
|
}
|
|
}
|
|
else {
|
|
else {
|
|
- // no results returned
|
|
|
|
|
|
+ // No results returned.
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- // the caller has requested results are always returned as
|
|
|
|
- // an array
|
|
|
|
|
|
+ // The caller has requested results are always returned as
|
|
|
|
+ // an array.
|
|
else {
|
|
else {
|
|
if (!$results_arr) {
|
|
if (!$results_arr) {
|
|
return array();
|
|
return array();
|
|
@@ -550,16 +571,16 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
* - return_array:
|
|
* - return_array:
|
|
* Additionally, The option 'return_array' can be provided to force
|
|
* Additionally, The option 'return_array' can be provided to force
|
|
* the function to expand tables as an array. Default behavior is to expand
|
|
* the function to expand tables as an array. Default behavior is to expand
|
|
- * a table as single record if only one record exists or to expand as an array if
|
|
|
|
- * multiple records exist.
|
|
|
|
|
|
+ * a table as single record if only one record exists or to expand as an
|
|
|
|
+ * array if multiple records exist.
|
|
* - include_fk:
|
|
* - include_fk:
|
|
* an array of FK relationships to follow. By default, the
|
|
* an array of FK relationships to follow. By default, the
|
|
* chado_expand_var function will follow all FK relationships but this
|
|
* chado_expand_var function will follow all FK relationships but this
|
|
- * may generate more queries then is desired slowing down this function call when
|
|
|
|
- * there are lots of FK relationships to follow. Provide an array specifying the
|
|
|
|
- * fields to include. For example, if expanding a property table (e.g. featureprop)
|
|
|
|
- * and you want the CV and accession but do not want the DB the following
|
|
|
|
- * array would work:
|
|
|
|
|
|
+ * may generate more queries then is desired slowing down this function call
|
|
|
|
+ * when there are lots of FK relationships to follow. Provide an array
|
|
|
|
+ * specifying the fields to include. For example, if expanding a property
|
|
|
|
+ * table (e.g. featureprop) and you want the CV and accession but do not
|
|
|
|
+ * want the DB the following array would work:
|
|
* $table_options = array(
|
|
* $table_options = array(
|
|
* 'include_fk' => array(
|
|
* 'include_fk' => array(
|
|
* 'type_id' => array(
|
|
* 'type_id' => array(
|
|
@@ -583,21 +604,22 @@ function chado_generate_var($table, $values, $base_options = array()) {
|
|
* This options is only used where type=table and allows you to
|
|
* This options is only used where type=table and allows you to
|
|
* expand only a subset of results based on the given criteria. Criteria
|
|
* expand only a subset of results based on the given criteria. Criteria
|
|
* should provided as an array of [field name] => [value] similar to the
|
|
* should provided as an array of [field name] => [value] similar to the
|
|
- * values array provided to chado_generate_var(). For example, when expanding
|
|
|
|
- * the featureprop table for a feature, you will already get only properties
|
|
|
|
- * for that feature, this option allows you to further get only properties
|
|
|
|
- * of a given type by passing in array('type_id' => array('name' => [name of type]))
|
|
|
|
|
|
+ * values array provided to chado_generate_var(). For example, when
|
|
|
|
+ * expanding the featureprop table for a feature, you will already get only
|
|
|
|
+ * properties for that feature, this option allows you to further get only
|
|
|
|
+ * properties of a given type by passing in
|
|
|
|
+ * array('type_id' => array('name' => [name of type]))
|
|
* @return
|
|
* @return
|
|
- * A chado object supplemented with the field/table/node requested to be expanded.
|
|
|
|
- * If the type is a table and it has already been expanded no changes is made to the
|
|
|
|
- * returned object
|
|
|
|
|
|
+ * A chado object supplemented with the field/table/node requested to be
|
|
|
|
+ * expanded. If the type is a table and it has already been expanded no
|
|
|
|
+ * changes is made to the returned object
|
|
*
|
|
*
|
|
*
|
|
*
|
|
- * @ingroup tripal_chado_query_api
|
|
|
|
|
|
+ * @ingroup tripal_chado_variables_api
|
|
*/
|
|
*/
|
|
function chado_expand_var($object, $type, $to_expand, $table_options = array()) {
|
|
function chado_expand_var($object, $type, $to_expand, $table_options = array()) {
|
|
|
|
|
|
- // make sure we have a value
|
|
|
|
|
|
+ // Make sure we have a value.
|
|
if (!$object) {
|
|
if (!$object) {
|
|
tripal_report_error('tripal_chado',
|
|
tripal_report_error('tripal_chado',
|
|
TRIPAL_ERROR,
|
|
TRIPAL_ERROR,
|
|
@@ -606,7 +628,7 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
return $object;
|
|
return $object;
|
|
}
|
|
}
|
|
|
|
|
|
- // check to see if we are expanding an array of objects
|
|
|
|
|
|
+ // Check to see if we are expanding an array of objects.
|
|
if (is_array($object)) {
|
|
if (is_array($object)) {
|
|
foreach ($object as $index => $o) {
|
|
foreach ($object as $index => $o) {
|
|
$object[$index] = chado_expand_var($o, $type, $to_expand);
|
|
$object[$index] = chado_expand_var($o, $type, $to_expand);
|
|
@@ -614,20 +636,20 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
return $object;
|
|
return $object;
|
|
}
|
|
}
|
|
|
|
|
|
- // get the base table name
|
|
|
|
|
|
+ // Get the base table name.
|
|
$base_table = $object->tablename;
|
|
$base_table = $object->tablename;
|
|
|
|
|
|
switch ($type) {
|
|
switch ($type) {
|
|
- case "field": //--------------------------------------------------------------------------------
|
|
|
|
|
|
+ case "field": //------------------------------------------------------------
|
|
if (preg_match('/(\w+)\.(\w+)/', $to_expand, $matches)) {
|
|
if (preg_match('/(\w+)\.(\w+)/', $to_expand, $matches)) {
|
|
$tablename = $matches[1];
|
|
$tablename = $matches[1];
|
|
$fieldname = $matches[2];
|
|
$fieldname = $matches[2];
|
|
$table_desc = chado_get_schema($tablename);
|
|
$table_desc = chado_get_schema($tablename);
|
|
|
|
|
|
- // BASE CASE: the field is from the current table
|
|
|
|
|
|
+ // BASE CASE: the field is from the current table.
|
|
if ($base_table == $tablename) {
|
|
if ($base_table == $tablename) {
|
|
// Use the table description to fully describe the current object
|
|
// Use the table description to fully describe the current object
|
|
- // in a $values array to be used to select the field from chado
|
|
|
|
|
|
+ // in a $values array to be used to select the field from chado.
|
|
$values = array();
|
|
$values = array();
|
|
foreach ($table_desc['primary key'] as $key) {
|
|
foreach ($table_desc['primary key'] as $key) {
|
|
if(property_exists($object, $key)) {
|
|
if(property_exists($object, $key)) {
|
|
@@ -635,21 +657,21 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // Retrieve the field from Chado
|
|
|
|
|
|
+ // Retrieve the field from Chado.
|
|
$results = chado_select_record($tablename, array($fieldname), $values);
|
|
$results = chado_select_record($tablename, array($fieldname), $values);
|
|
|
|
|
|
- // Check that the field was retrieved correctly
|
|
|
|
|
|
+ // Check that the field was retrieved correctly.
|
|
if (isset($results[0])) {
|
|
if (isset($results[0])) {
|
|
$object->{$fieldname} = $results[0]->{$fieldname};
|
|
$object->{$fieldname} = $results[0]->{$fieldname};
|
|
$object->expanded = $to_expand;
|
|
$object->expanded = $to_expand;
|
|
}
|
|
}
|
|
- // If it wasn't retrieved correctly, we need to warn the administrator
|
|
|
|
|
|
+ // If it wasn't retrieved correctly, we need to warn the administrator.
|
|
|
|
|
|
}
|
|
}
|
|
- // RECURSIVE CASE: the field is in a nested object
|
|
|
|
|
|
+ // RECURSIVE CASE: the field is in a nested object.
|
|
else {
|
|
else {
|
|
// We want to look at each field and if it's an object then we want to
|
|
// We want to look at each field and if it's an object then we want to
|
|
- // attempt to expand the field in it via recursion
|
|
|
|
|
|
+ // attempt to expand the field in it via recursion.
|
|
foreach ((array) $object as $field_name => $field_value) {
|
|
foreach ((array) $object as $field_name => $field_value) {
|
|
if (is_object($field_value)) {
|
|
if (is_object($field_value)) {
|
|
$object->{$field_name} = chado_expand_var(
|
|
$object->{$field_name} = chado_expand_var(
|
|
@@ -658,11 +680,11 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
$to_expand
|
|
$to_expand
|
|
);
|
|
);
|
|
}
|
|
}
|
|
- } //end of for each field in the current object
|
|
|
|
|
|
+ } // End of for each field in the current object.
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Otherwise we weren't able to extract the parts of the field to expand
|
|
// Otherwise we weren't able to extract the parts of the field to expand
|
|
- // Thus we will warn the administrator
|
|
|
|
|
|
+ // Thus we will warn the administrator.
|
|
else {
|
|
else {
|
|
tripal_report_error('tripal_chado', TRIPAL_ERROR,
|
|
tripal_report_error('tripal_chado', TRIPAL_ERROR,
|
|
'chado_expand_var: Field (%field) not in the right format. " .
|
|
'chado_expand_var: Field (%field) not in the right format. " .
|
|
@@ -670,21 +692,22 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
|
|
|
|
- case "foreign_key": //--------------------------------------------------------------------------
|
|
|
|
|
|
+ case "foreign_key": //-----------------------------------------------------
|
|
if (preg_match('/(\w+)\.(\w+) => (\w+)/', $to_expand, $matches)) {
|
|
if (preg_match('/(\w+)\.(\w+) => (\w+)/', $to_expand, $matches)) {
|
|
$table_name = $matches[1];
|
|
$table_name = $matches[1];
|
|
$field_name = $matches[2];
|
|
$field_name = $matches[2];
|
|
$foreign_table = $matches[3];
|
|
$foreign_table = $matches[3];
|
|
$table_desc = chado_get_schema($table_name);
|
|
$table_desc = chado_get_schema($table_name);
|
|
|
|
|
|
- // BASE CASE: The foreign key is from the current table
|
|
|
|
|
|
+ // BASE CASE: The foreign key is from the current table.
|
|
if ($base_table == $table_name) {
|
|
if ($base_table == $table_name) {
|
|
|
|
|
|
// Get the value of the foreign key from the object
|
|
// Get the value of the foreign key from the object
|
|
$field_value = $object->{$field_name};
|
|
$field_value = $object->{$field_name};
|
|
|
|
|
|
- // Get the name of the field in the foreign table using the table description
|
|
|
|
- // For example, with the feature.type_id => cvterm.cvterm_id we need cvterm_id
|
|
|
|
|
|
+ // Get the name of the field in the foreign table using the table
|
|
|
|
+ // description For example, with the
|
|
|
|
+ // feature.type_id => cvterm.cvterm_id we need cvterm_id
|
|
$foreign_field_name = FALSE;
|
|
$foreign_field_name = FALSE;
|
|
foreach ($table_desc['foreign keys'][$foreign_table]['columns'] as $left => $right) {
|
|
foreach ($table_desc['foreign keys'][$foreign_table]['columns'] as $left => $right) {
|
|
if ($right == $field_name) {
|
|
if ($right == $field_name) {
|
|
@@ -692,23 +715,25 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // Check that we were able to determine the field name in the foreign table
|
|
|
|
|
|
+ // Check that we were able to determine the field name in the foreign
|
|
|
|
+ // table.
|
|
if ($foreign_field_name) {
|
|
if ($foreign_field_name) {
|
|
|
|
|
|
// Generate a chado variable of the foreign key
|
|
// Generate a chado variable of the foreign key
|
|
// For example, if the foreign key to expand is feature.type_id
|
|
// For example, if the foreign key to expand is feature.type_id
|
|
- // then we want to generate a chado cvterm variable that matches the feature.type_id
|
|
|
|
|
|
+ // then we want to generate a chado cvterm variable that matches the
|
|
|
|
+ // feature.type_id.
|
|
$foreign_var = chado_generate_var(
|
|
$foreign_var = chado_generate_var(
|
|
$foreign_table, // thus in the example above, generate a cvterm var
|
|
$foreign_table, // thus in the example above, generate a cvterm var
|
|
array($foreign_field_name => $field_value), // where the cvterm.cvterm_id = feature.type_id value
|
|
array($foreign_field_name => $field_value), // where the cvterm.cvterm_id = feature.type_id value
|
|
$table_options //pass in the same options given to this function
|
|
$table_options //pass in the same options given to this function
|
|
);
|
|
);
|
|
|
|
|
|
- // Check that the foreign object was returned
|
|
|
|
|
|
+ // Check that the foreign object was returned.
|
|
if ($foreign_var) {
|
|
if ($foreign_var) {
|
|
|
|
|
|
- // It was so now we can add this chado variable to our current object
|
|
|
|
- // in place of the key value
|
|
|
|
|
|
+ // It was so now we can add this chado variable to our current
|
|
|
|
+ // object in place of the key value.
|
|
$object->{$field_name} = $foreign_var;
|
|
$object->{$field_name} = $foreign_var;
|
|
$object->expanded = $to_expand;
|
|
$object->expanded = $to_expand;
|
|
|
|
|
|
@@ -721,7 +746,7 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
array('%fk' => $to_expand));
|
|
array('%fk' => $to_expand));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- // Else we were unable to determine the field name in the foreign table
|
|
|
|
|
|
+ // Else we were unable to determine the field name in the foreign table.
|
|
else {
|
|
else {
|
|
tripal_report_error('tripal_chado', TRIPAL_ERROR,
|
|
tripal_report_error('tripal_chado', TRIPAL_ERROR,
|
|
'chado_expand_var: unable to determine the field name in the table the foreign
|
|
'chado_expand_var: unable to determine the field name in the table the foreign
|
|
@@ -730,7 +755,7 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
- // RECURSIVE CASE: Check any nested objects
|
|
|
|
|
|
+ // RECURSIVE CASE: Check any nested objects.
|
|
else {
|
|
else {
|
|
|
|
|
|
foreach ((array) $object as $field_name => $field_value) {
|
|
foreach ((array) $object as $field_name => $field_value) {
|
|
@@ -741,12 +766,12 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
$to_expand
|
|
$to_expand
|
|
);
|
|
);
|
|
}
|
|
}
|
|
- } //end of for each field in the current object
|
|
|
|
|
|
+ } //End of for each field in the current object.
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- // Otherwise we weren't able to extract the parts of the foreign key to expand
|
|
|
|
- // Thus we will warn the administrator
|
|
|
|
|
|
+ // Otherwise we weren't able to extract the parts of the foreign key to
|
|
|
|
+ // expand thus we will warn the administrator.
|
|
else {
|
|
else {
|
|
tripal_report_error('tripal_chado', TRIPAL_ERROR,
|
|
tripal_report_error('tripal_chado', TRIPAL_ERROR,
|
|
'chado_expand_var: foreign_key (%fk) not in the right format. " .
|
|
'chado_expand_var: foreign_key (%fk) not in the right format. " .
|
|
@@ -754,7 +779,7 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
|
|
|
|
- case "table": //--------------------------------------------------------------------------------
|
|
|
|
|
|
+ case "table": //------------------------------------------------------------
|
|
$foreign_table = $to_expand;
|
|
$foreign_table = $to_expand;
|
|
|
|
|
|
// BASE CASE: don't expand the table it already is expanded
|
|
// BASE CASE: don't expand the table it already is expanded
|
|
@@ -763,15 +788,16 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
}
|
|
}
|
|
$foreign_table_desc = chado_get_schema($foreign_table);
|
|
$foreign_table_desc = chado_get_schema($foreign_table);
|
|
|
|
|
|
- // TODO: if we don't get a foreign_table (which could happen of a custom table
|
|
|
|
- // is not correctly defined or the table name is mispelled then we should return
|
|
|
|
- // gracefully.
|
|
|
|
|
|
+ // TODO: if we don't get a foreign_table (which could happen of a custom
|
|
|
|
+ // table is not correctly defined or the table name is mispelled then we
|
|
|
|
+ // should return gracefully.
|
|
|
|
|
|
// BASE CASE: If it's connected to the base table via a FK constraint
|
|
// BASE CASE: If it's connected to the base table via a FK constraint
|
|
- // then we have all the information needed to expand it now
|
|
|
|
|
|
+ // then we have all the information needed to expand it now.
|
|
if (array_key_exists($base_table, $foreign_table_desc['foreign keys'])) {
|
|
if (array_key_exists($base_table, $foreign_table_desc['foreign keys'])) {
|
|
foreach ($foreign_table_desc['foreign keys'][$base_table]['columns'] as $left => $right) {
|
|
foreach ($foreign_table_desc['foreign keys'][$base_table]['columns'] as $left => $right) {
|
|
- // if the FK value in the base table is not there then we can't expand it, so just skip it.
|
|
|
|
|
|
+ // if the FK value in the base table is not there then we can't expand
|
|
|
|
+ // it, so just skip it.
|
|
if (!$object->{$right}) {
|
|
if (!$object->{$right}) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
@@ -800,15 +826,17 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
$filter_criteria = array($left => $object->{$right});
|
|
$filter_criteria = array($left => $object->{$right});
|
|
}
|
|
}
|
|
|
|
|
|
- // generate a new object for this table using the FK values in the base table.
|
|
|
|
|
|
+ // Generate a new object for this table using the FK values in the
|
|
|
|
+ // base table.
|
|
$new_options = $table_options;
|
|
$new_options = $table_options;
|
|
$foreign_object = chado_generate_var($foreign_table, $filter_criteria, $new_options);
|
|
$foreign_object = chado_generate_var($foreign_table, $filter_criteria, $new_options);
|
|
|
|
|
|
- // if the generation of the object was successful, update the base object to include it.
|
|
|
|
|
|
+ // If the generation of the object was successful, update the base
|
|
|
|
+ // object to include it.
|
|
if ($foreign_object) {
|
|
if ($foreign_object) {
|
|
- // in the case where the foreign key relationship exists more
|
|
|
|
- // than once with the same table we want to alter the array structure to
|
|
|
|
- // include the field name.
|
|
|
|
|
|
+ // In the case where the foreign key relationship exists more
|
|
|
|
+ // than once with the same table we want to alter the array
|
|
|
|
+ // structure to include the field name.
|
|
if (count($foreign_table_desc['foreign keys'][$base_table]['columns']) > 1) {
|
|
if (count($foreign_table_desc['foreign keys'][$base_table]['columns']) > 1) {
|
|
if (!property_exists($object, $foreign_table)) {
|
|
if (!property_exists($object, $foreign_table)) {
|
|
$object->{$foreign_table} = new stdClass();
|
|
$object->{$foreign_table} = new stdClass();
|
|
@@ -825,11 +853,11 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
$object->expanded = $to_expand;
|
|
$object->expanded = $to_expand;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- // if the object returned is NULL then handle that
|
|
|
|
|
|
+ // If the object returned is NULL then handle that.
|
|
else {
|
|
else {
|
|
- // in the case where the foreign key relationship exists more
|
|
|
|
- // than once with the same table we want to alter the array structure to
|
|
|
|
- // include the field name.
|
|
|
|
|
|
+ // In the case where the foreign key relationship exists more
|
|
|
|
+ // than once with the same table we want to alter the array
|
|
|
|
+ // structure to include the field name.
|
|
if (count($foreign_table_desc['foreign keys'][$base_table]['columns']) > 1) {
|
|
if (count($foreign_table_desc['foreign keys'][$base_table]['columns']) > 1) {
|
|
if (!property_exists($object, $foreign_table)) {
|
|
if (!property_exists($object, $foreign_table)) {
|
|
$object->{$foreign_table} = new stdClass();
|
|
$object->{$foreign_table} = new stdClass();
|
|
@@ -842,30 +870,32 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- // RECURSIVE CASE: if the table is not connected directly to the current base table
|
|
|
|
- // through a foreign key relationship, then maybe it has a relationship to
|
|
|
|
- // one of the nested objects.
|
|
|
|
|
|
+ // RECURSIVE CASE: if the table is not connected directly to the current
|
|
|
|
+ // base table through a foreign key relationship, then maybe it has a
|
|
|
|
+ // relationship to one of the nested objects.
|
|
else {
|
|
else {
|
|
|
|
|
|
- // We need to recurse -the table has a relationship to one of the nested objects
|
|
|
|
- // We assume it's a nested object if the value of the field is an object
|
|
|
|
|
|
+ // We need to recurse -the table has a relationship to one of the nested
|
|
|
|
+ // objects. We assume it's a nested object if the value of the field is
|
|
|
|
+ // an object.
|
|
$did_expansion = 0;
|
|
$did_expansion = 0;
|
|
foreach ((array) $object as $field_name => $field_value) {
|
|
foreach ((array) $object as $field_name => $field_value) {
|
|
|
|
|
|
- // CASE #1: This field is an already expanded foreign key and the table to be
|
|
|
|
- // expanded is in the table referenced by the foreign key
|
|
|
|
|
|
+ // CASE #1: This field is an already expanded foreign key and the
|
|
|
|
+ // table to be expanded is in the table referenced by the foreign key.
|
|
|
|
|
|
// First of all it can only be expanded if it's an object
|
|
// First of all it can only be expanded if it's an object
|
|
- // And if it's a foreign key it should have a tablename property
|
|
|
|
|
|
+ // And if it's a foreign key it should have a tablename property.
|
|
if (is_object($field_value) AND property_exists($field_value, 'tablename')) {
|
|
if (is_object($field_value) AND property_exists($field_value, 'tablename')) {
|
|
$object->{$field_name} = chado_expand_var($field_value, 'table', $foreign_table);
|
|
$object->{$field_name} = chado_expand_var($field_value, 'table', $foreign_table);
|
|
}
|
|
}
|
|
|
|
|
|
- // CASE #2: This field is an already expanded object (ie: the field is actually
|
|
|
|
- // the expanded table name) and the table to be expanded si related to it
|
|
|
|
|
|
+ // CASE #2: This field is an already expanded object (ie: the field is
|
|
|
|
+ // actually the expanded table name) and the table to be expanded is
|
|
|
|
+ // related to it.
|
|
|
|
|
|
- // check to see if the $field_name is a valid chado table, we don't need
|
|
|
|
- // to call chado_expand_var on fields that aren't tables
|
|
|
|
|
|
+ // Check to see if the $field_name is a valid chado table, we don't
|
|
|
|
+ // need to call chado_expand_var on fields that aren't tables.
|
|
$check = chado_get_schema($field_name);
|
|
$check = chado_get_schema($field_name);
|
|
if ($check) {
|
|
if ($check) {
|
|
$did_expansion = 1;
|
|
$did_expansion = 1;
|
|
@@ -873,8 +903,8 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // if we did not expand this table we should return a message that the foreign table
|
|
|
|
- // could not be expanded
|
|
|
|
|
|
+ // If we did not expand this table we should return a message that the
|
|
|
|
+ // foreign tabl could not be expanded.
|
|
if (!$did_expansion) {
|
|
if (!$did_expansion) {
|
|
tripal_report_error('tripal_chado', TRIPAL_ERROR, 'chado_expand_var: Could not expand %table. ' .
|
|
tripal_report_error('tripal_chado', TRIPAL_ERROR, 'chado_expand_var: Could not expand %table. ' .
|
|
'The table is either not related to the base object through a foreign key relationships or ' .
|
|
'The table is either not related to the base object through a foreign key relationships or ' .
|
|
@@ -886,19 +916,21 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
|
|
|
|
- case "node": //---------------------------------------------------------------------------------
|
|
|
|
|
|
+ case "node": //-------------------------------------------------------------
|
|
|
|
|
|
- // BASE CASE: if the node to be expanded is for our base table, then just expand it
|
|
|
|
|
|
+ // BASE CASE: if the node to be expanded is for our base table, then just
|
|
|
|
+ // expand it.
|
|
if ($object->tablename == $to_expand) {
|
|
if ($object->tablename == $to_expand) {
|
|
|
|
|
|
- // Load the node based on the current objects nid (node primary key)
|
|
|
|
|
|
+ // Load the node based on the current objects nid (node primary key).
|
|
$node = NULL;
|
|
$node = NULL;
|
|
if (property_exists($object, 'nid')) {
|
|
if (property_exists($object, 'nid')) {
|
|
$node = node_load($object->nid);
|
|
$node = node_load($object->nid);
|
|
}
|
|
}
|
|
- // Try to get the nid based on the tablename
|
|
|
|
|
|
+ // Try to get the nid based on the tablename.
|
|
else {
|
|
else {
|
|
- // Invoke all hook_node_info to avoid hard-coding the chado_$table assumption
|
|
|
|
|
|
+ // Invoke all hook_node_info to avoid hard-coding the chado_$table
|
|
|
|
+ // assumption..
|
|
foreach (module_invoke_all('node_info') as $node_info) {
|
|
foreach (module_invoke_all('node_info') as $node_info) {
|
|
if (array_key_exists('chado_node_api', $node_info)) {
|
|
if (array_key_exists('chado_node_api', $node_info)) {
|
|
if ($node_info['chado_node_api']['base_table'] == $object->tablename) {
|
|
if ($node_info['chado_node_api']['base_table'] == $object->tablename) {
|
|
@@ -920,7 +952,7 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
// If we have successfully loaded the node...
|
|
// If we have successfully loaded the node...
|
|
if ($node) {
|
|
if ($node) {
|
|
|
|
|
|
- // Move expandable arrays from the object into the node
|
|
|
|
|
|
+ // Move expandable arrays from the object into the node.
|
|
$object->expanded = $to_expand;
|
|
$object->expanded = $to_expand;
|
|
$node->expandable_fields = $object->expandable_fields;
|
|
$node->expandable_fields = $object->expandable_fields;
|
|
unset($object->expandable_fields);
|
|
unset($object->expandable_fields);
|
|
@@ -930,13 +962,14 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
unset($object->expandable_nodes);
|
|
unset($object->expandable_nodes);
|
|
|
|
|
|
// The node becomes the base object with the obejct added to it.
|
|
// The node becomes the base object with the obejct added to it.
|
|
- // For example, we may start with a feature object with a name, uniquename , type, etc.
|
|
|
|
- // After expanding we will return the node and at $node->feature you will find the original object
|
|
|
|
|
|
+ // For example, we may start with a feature object with a name,
|
|
|
|
+ // uniquename , type, etc. After expanding we will return the node and
|
|
|
|
+ // at $node->feature you will find the original object.
|
|
$node->{$base_table} = $object;
|
|
$node->{$base_table} = $object;
|
|
$object = $node;
|
|
$object = $node;
|
|
|
|
|
|
}
|
|
}
|
|
- // Else we were unable to load the node
|
|
|
|
|
|
+ // Else we were unable to load the node.
|
|
else {
|
|
else {
|
|
|
|
|
|
// Warn the administrator
|
|
// Warn the administrator
|
|
@@ -947,14 +980,14 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
else {
|
|
else {
|
|
tripal_report_error('tripal_chado', TRIPAL_NOTICE, 'chado_expand_var: There is no node for the current object: <pre>%object</pre>', array('%object' => print_r($object,TRUE)));
|
|
tripal_report_error('tripal_chado', TRIPAL_NOTICE, 'chado_expand_var: There is no node for the current object: <pre>%object</pre>', array('%object' => print_r($object,TRUE)));
|
|
}
|
|
}
|
|
- } //end of if node
|
|
|
|
|
|
+ } //End of if node.
|
|
}
|
|
}
|
|
- // RECURSIVE CASE: check to see if the node to be expanded associates with a
|
|
|
|
- // chado table within one of the nested objects.
|
|
|
|
|
|
+ // RECURSIVE CASE: check to see if the node to be expanded associates with
|
|
|
|
+ // a chado table within one of the nested objects.
|
|
else {
|
|
else {
|
|
|
|
|
|
// We need to recurse -the node to expand is one of the nested objects
|
|
// We need to recurse -the node to expand is one of the nested objects
|
|
- // We assume it's a nested object if the field value is an object
|
|
|
|
|
|
+ // We assume it's a nested object if the field value is an object.
|
|
foreach ((array) $object as $field_name => $field_value) {
|
|
foreach ((array) $object as $field_name => $field_value) {
|
|
if (is_object($field_value)) {
|
|
if (is_object($field_value)) {
|
|
$object->{$field_name} = chado_expand_var(
|
|
$object->{$field_name} = chado_expand_var(
|
|
@@ -963,11 +996,11 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
$to_expand
|
|
$to_expand
|
|
);
|
|
);
|
|
}
|
|
}
|
|
- } //end of for each field in the current object
|
|
|
|
|
|
+ } // End of for each field in the current object.
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
|
|
|
|
- // The $type to be expanded is not yet supported
|
|
|
|
|
|
+ // The $type to be expanded is not yet supported.
|
|
default:
|
|
default:
|
|
tripal_report_error('tripal_chado', TRIPAL_ERROR, 'chado_expand_var: Unrecognized type (%type). Should be one of "field", "table", "node".',
|
|
tripal_report_error('tripal_chado', TRIPAL_ERROR, 'chado_expand_var: Unrecognized type (%type). Should be one of "field", "table", "node".',
|
|
array('%type' => $type));
|
|
array('%type' => $type));
|
|
@@ -975,22 +1008,22 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
}
|
|
}
|
|
|
|
|
|
// Move expandable arrays downwards -------------------------------
|
|
// Move expandable arrays downwards -------------------------------
|
|
- // If the type was either table or foreign key then a new chado variable was generated
|
|
|
|
- // this variable will have it's own expandable array's which need to be moved down
|
|
|
|
- // and merged with the base objects expandable arrays
|
|
|
|
|
|
+ // If the type was either table or foreign key then a new chado variable was
|
|
|
|
+ // generated this variable will have it's own expandable array's which need to
|
|
|
|
+ // be moved down and merged with the base objects expandable arrays.
|
|
|
|
|
|
// Thus, check all nested objects for expandable arrays
|
|
// Thus, check all nested objects for expandable arrays
|
|
- // and if they have them, move them downwards
|
|
|
|
|
|
+ // and if they have them, move them downwards.
|
|
foreach ( (array)$object as $field_name => $field_value) {
|
|
foreach ( (array)$object as $field_name => $field_value) {
|
|
if (is_object($field_value)) {
|
|
if (is_object($field_value)) {
|
|
|
|
|
|
- // The current nested object has expandable arrays
|
|
|
|
|
|
+ // The current nested object has expandable arrays.
|
|
if (isset($field_value->expandable_fields)) {
|
|
if (isset($field_value->expandable_fields)) {
|
|
|
|
|
|
- // Move expandable fields downwards
|
|
|
|
|
|
+ // Move expandable fields downwards.
|
|
if (isset($field_value->expandable_fields) and is_array($field_value->expandable_fields)) {
|
|
if (isset($field_value->expandable_fields) and is_array($field_value->expandable_fields)) {
|
|
|
|
|
|
- // If the current object has it's own expandable fields then merge them
|
|
|
|
|
|
+ // If the current object has it's own expandable fields then merge them.
|
|
if (isset($object->expandable_fields)) {
|
|
if (isset($object->expandable_fields)) {
|
|
$object->expandable_fields = array_merge(
|
|
$object->expandable_fields = array_merge(
|
|
$object->expandable_fields,
|
|
$object->expandable_fields,
|
|
@@ -999,7 +1032,7 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
unset($object->{$field_name}->expandable_fields);
|
|
unset($object->{$field_name}->expandable_fields);
|
|
|
|
|
|
}
|
|
}
|
|
- // Otherwise, just move the expandable fields downwards
|
|
|
|
|
|
+ // Otherwise, just move the expandable fields downwards.
|
|
else {
|
|
else {
|
|
$object->expandable_fields = $object->{$field_name}->expandable_fields;
|
|
$object->expandable_fields = $object->{$field_name}->expandable_fields;
|
|
unset($object->{$field_name}->expandable_fields);
|
|
unset($object->{$field_name}->expandable_fields);
|
|
@@ -1007,10 +1040,11 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- // Move expandable foreign keys downwards
|
|
|
|
|
|
+ // Move expandable foreign keys downwards.
|
|
if (isset($field_value->expandable_foreign_keys) and is_array($field_value->expandable_foreign_keys)) {
|
|
if (isset($field_value->expandable_foreign_keys) and is_array($field_value->expandable_foreign_keys)) {
|
|
|
|
|
|
- // If the current object has it's own expandable foreign keys then merge them
|
|
|
|
|
|
+ // If the current object has it's own expandable foreign keys then
|
|
|
|
+ // merge them.
|
|
if (isset($object->expandable_foreign_keys)) {
|
|
if (isset($object->expandable_foreign_keys)) {
|
|
$object->expandable_foreign_keys = array_merge(
|
|
$object->expandable_foreign_keys = array_merge(
|
|
$object->expandable_foreign_keys,
|
|
$object->expandable_foreign_keys,
|
|
@@ -1019,17 +1053,17 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
unset($object->{$field_name}->expandable_foreign_keys);
|
|
unset($object->{$field_name}->expandable_foreign_keys);
|
|
|
|
|
|
}
|
|
}
|
|
- // Otherwise, just move the expandable foreign keys downwards
|
|
|
|
|
|
+ // Otherwise, just move the expandable foreign keys downwards.
|
|
else {
|
|
else {
|
|
$object->expandable_foreign_keys = $object->{$field_name}->expandable_foreign_keys;
|
|
$object->expandable_foreign_keys = $object->{$field_name}->expandable_foreign_keys;
|
|
unset($object->{$field_name}->expandable_foreign_keys);
|
|
unset($object->{$field_name}->expandable_foreign_keys);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // Move expandable tables downwards
|
|
|
|
|
|
+ // Move expandable tables downwards.
|
|
if (isset($field_value->expandable_tables) and is_array($field_value->expandable_tables)) {
|
|
if (isset($field_value->expandable_tables) and is_array($field_value->expandable_tables)) {
|
|
|
|
|
|
- // If the current object has it's own expandable tables then merge them
|
|
|
|
|
|
+ // If the current object has it's own expandable tables then merge them.
|
|
if (isset($object->expandable_tables)) {
|
|
if (isset($object->expandable_tables)) {
|
|
$object->expandable_tables = array_merge(
|
|
$object->expandable_tables = array_merge(
|
|
$object->expandable_tables,
|
|
$object->expandable_tables,
|
|
@@ -1038,17 +1072,17 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
unset($object->{$field_name}->expandable_tables);
|
|
unset($object->{$field_name}->expandable_tables);
|
|
|
|
|
|
}
|
|
}
|
|
- // Otherwise, just move the expandable tables downwards
|
|
|
|
|
|
+ // Otherwise, just move the expandable tables downwards.
|
|
else {
|
|
else {
|
|
$object->expandable_tables = $object->{$field_name}->expandable_tables;
|
|
$object->expandable_tables = $object->{$field_name}->expandable_tables;
|
|
unset($object->{$field_name}->expandable_tables);
|
|
unset($object->{$field_name}->expandable_tables);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // Move expandable nodes downwards
|
|
|
|
|
|
+ // Move expandable nodes downwards.
|
|
if (isset($field_value->expandable_nodes) and is_array($field_value->expandable_nodes)) {
|
|
if (isset($field_value->expandable_nodes) and is_array($field_value->expandable_nodes)) {
|
|
|
|
|
|
- // If the current object has it's own expandable tables then merge them
|
|
|
|
|
|
+ // If the current object has it's own expandable tables then merge them.
|
|
if (isset($object->expandable_nodes)) {
|
|
if (isset($object->expandable_nodes)) {
|
|
$object->expandable_nodes = array_merge(
|
|
$object->expandable_nodes = array_merge(
|
|
$object->expandable_nodes,
|
|
$object->expandable_nodes,
|
|
@@ -1057,7 +1091,7 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
unset($object->{$field_name}->expandable_nodes);
|
|
unset($object->{$field_name}->expandable_nodes);
|
|
|
|
|
|
}
|
|
}
|
|
- // Otherwise, just move the expandable tables downwards
|
|
|
|
|
|
+ // Otherwise, just move the expandable tables downwards.
|
|
else {
|
|
else {
|
|
$object->expandable_nodes = $object->{$field_name}->expandable_nodes;
|
|
$object->expandable_nodes = $object->{$field_name}->expandable_nodes;
|
|
unset($object->{$field_name}->expandable_nodes);
|
|
unset($object->{$field_name}->expandable_nodes);
|
|
@@ -1069,20 +1103,20 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
|
|
|
|
// Move extended array downwards ----------------------------------
|
|
// Move extended array downwards ----------------------------------
|
|
// This tells us what we have expanded (ie: that we succeeded)
|
|
// This tells us what we have expanded (ie: that we succeeded)
|
|
- // and is needed to remove the entry from the expandable array
|
|
|
|
|
|
+ // and is needed to remove the entry from the expandable array.
|
|
|
|
|
|
- // If there is no expanded field in the current object then check any of the nested objects
|
|
|
|
- // and move it down
|
|
|
|
|
|
+ // If there is no expanded field in the current object then check any of the
|
|
|
|
+ // nested objects and move it down.
|
|
if (!property_exists($object, 'expanded')) {
|
|
if (!property_exists($object, 'expanded')) {
|
|
|
|
|
|
- // It's a nested object if the value is an object
|
|
|
|
|
|
+ // It's a nested object if the value is an object.
|
|
foreach ( (array)$object as $field_name => $field_value) {
|
|
foreach ( (array)$object as $field_name => $field_value) {
|
|
if (is_object($field_value)) {
|
|
if (is_object($field_value)) {
|
|
|
|
|
|
- // Check if the current nested object has an expanded array
|
|
|
|
|
|
+ // Check if the current nested object has an expanded array.
|
|
if (isset($field_value->expanded)) {
|
|
if (isset($field_value->expanded)) {
|
|
|
|
|
|
- // If so, then move it downwards
|
|
|
|
|
|
+ // If so, then move it downwards.
|
|
$object->expanded = $field_value->expanded;
|
|
$object->expanded = $field_value->expanded;
|
|
unset($field_value->expanded);
|
|
unset($field_value->expanded);
|
|
}
|
|
}
|
|
@@ -1091,10 +1125,11 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
|
|
}
|
|
}
|
|
|
|
|
|
// Check again if there is an expanded field in the current object
|
|
// Check again if there is an expanded field in the current object
|
|
- // We check again because it might have been moved downwards above
|
|
|
|
|
|
+ // We check again because it might have been moved downwards above.
|
|
if (property_exists($object, 'expanded')) {
|
|
if (property_exists($object, 'expanded')) {
|
|
|
|
|
|
- // If so, then remove the expanded identifier from the correct expandable array
|
|
|
|
|
|
+ // If so, then remove the expanded identifier from the correct expandable
|
|
|
|
+ // array..
|
|
$expandable_name = 'expandable_' . $type . 's';
|
|
$expandable_name = 'expandable_' . $type . 's';
|
|
if (property_exists($object, $expandable_name) and $object->{$expandable_name}) {
|
|
if (property_exists($object, $expandable_name) and $object->{$expandable_name}) {
|
|
$key_to_remove = array_search($object->expanded, $object->{$expandable_name});
|
|
$key_to_remove = array_search($object->expanded, $object->{$expandable_name});
|