tripal_core.chado_variables.api.inc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. <?php
  2. /**
  3. * @file
  4. * This API generates objects containing the full details of a record(s) in chado.
  5. */
  6. /**
  7. * Implements hook_exclude_type_by_default()
  8. *
  9. * This hooks allows fields of a specified type that match a specified criteria to be excluded by
  10. * default from any table when chado_generate_var() is called. Keep in mind that if
  11. * fields are excluded by default they can always be expanded at a later date using
  12. * chado_expand_var().
  13. *
  14. * Criteria are php strings that evaluate to either TRUE or FALSE. These strings are evaluated using
  15. * drupal_eval() which suppresses syntax errors and throws watchdog entries of type php. There are
  16. * also watchdog entries of type tripal_core stating the exact criteria evaluated. Criteria can
  17. * contain the following tokens:
  18. * - &gt;field_name&lt;
  19. * Replaced by the name of the field to be excluded
  20. * - &gt;field_value&lt;
  21. * Replaced by the value of the field in the current record
  22. * Also keep in mind that if your criteria doesn't contain the &gt;field_value&lt; token then it will be
  23. * evaluated before the query is executed and if the field is excluded it won't be included in the
  24. * query.
  25. *
  26. * @return
  27. * An array of type => criteria where the type is excluded if the criteria evaluates to TRUE
  28. *
  29. * @ingroup tripal_chado_query_api
  30. */
  31. function tripal_core_exclude_type_by_default() {
  32. return array('text' => 'strlen("&gt;field_value&lt; ") > 100');
  33. }
  34. /**
  35. * Implements hook_exclude_field_from_<tablename>_by_default()
  36. *
  37. * This hooks allows fields from a specified table that match a specified criteria to be excluded by
  38. * default from any table when chado_generate_var() is called. Keep in mind that if
  39. * fields are excluded by default they can always be expanded at a later date using
  40. * chado_expand_var().
  41. *
  42. * Criteria are php strings that evaluate to either TRUE or FALSE. These strings are evaluated using
  43. * drupal_eval() which suppresses syntax errors and throws watchdog entries of type php. There are
  44. * also watchdog entries of type tripal_core stating the exact criteria evaluated. Criteria can
  45. * contain the following tokens:
  46. * - &gt;field_name&lt;
  47. * Replaced by the name of the field to be excluded
  48. * - &gt;field_value&lt;
  49. * Replaced by the value of the field in the current record
  50. * Also keep in mind that if your criteria doesn't contain the &gt;field_value&lt; token then it will be
  51. * evaluated before the query is executed and if the field is excluded it won't be included in the
  52. * query.
  53. *
  54. * @return
  55. * An array of type => criteria where the type is excluded if the criteria evaluates to TRUE
  56. *
  57. * @ingroup tripal_chado_query_api
  58. */
  59. function tripal_core_exclude_field_from_feature_by_default() {
  60. return array();
  61. }
  62. /**
  63. * Generates an object containing the full details of a record(s) in chado.
  64. *
  65. * This differs from the objects returned by chado_select_record in so far as all foreign key
  66. * relationships have been followed meaning you have more complete details. Thus this function
  67. * should be used whenever you need a full variable and chado_select_record should be used if
  68. * you only case about a few columns.
  69. *
  70. * @param $table
  71. * The name of the base table to generate a variable for
  72. * @param $values
  73. * A select values array that selects the records you want from the base table
  74. * (this has the same form as chado_select_record)
  75. * @param $base_options
  76. * An array containing options for the base table. For example, an
  77. * option of 'order_by' may be used to sort results in the base table
  78. * if more than one are returned. The options must be compatible with
  79. * the options accepted by the chado_select_record() function.
  80. * Additionally, These options are available for this function:
  81. * -return_array:
  82. * can be provided to force the function to always return an array. Default
  83. * behavior is to return a single record if only one record exists or to return
  84. * an array if multiple records exist.
  85. * - include_fk:
  86. * an array of FK relationships to follow. By default, the
  87. * chado_select_record function will follow all FK relationships but this
  88. * may generate more queries then is desired slowing down this function call when
  89. * there are lots of FK relationships to follow. Provide an array specifying the
  90. * fields to include. For example, if expanding a property table (e.g. featureprop)
  91. * and you want the CV and accession but do not want the DB the following
  92. * array would work:
  93. *
  94. * $table_options = array(
  95. * 'include_fk' => array(
  96. * 'type_id' => array(
  97. * 'cv_id' => 1,
  98. * 'dbxref_id' => 1,
  99. * )
  100. * )
  101. * );
  102. *
  103. * The above array will expand the 'type_id' of the property table but only
  104. * further expand the cv_id and the dbxref_id and will go no further.
  105. * - pager:
  106. * Use this option if it is desired to return only a subset of results
  107. * so that they may be shown within a Drupal-style pager. This should be
  108. * an array with two keys: 'limit' and 'element'. The value of 'limit'
  109. * should specify the number of records to return and 'element' is a
  110. * unique integer to differentiate between pagers when more than one
  111. * appear on a page. The 'element' should start with zero and increment by
  112. * one for each pager. This only works when type is a 'table'.
  113. * @return
  114. * Either an object (if only one record was selected from the base table)
  115. * or an array of objects (if more than one record was selected from the base table).
  116. * If the option 'return_array' is provided the function always returns an array.
  117. *
  118. * Example Usage:
  119. * @code
  120. $values = array(
  121. 'name' => 'Medtr4g030710'
  122. );
  123. $features = chado_generate_var('feature', $values);
  124. * @endcode
  125. * This will return an object if there is only one feature with the name Medtr4g030710 or it will
  126. * return an array of feature objects if more than one feature has that name.
  127. *
  128. * Note to Module Designers: Fields can be excluded by default from these objects by implementing
  129. * one of the following hooks:
  130. * - hook_exclude_field_from_tablename_by_default (where tablename is the name of the table):
  131. * This hook allows you to add fields to be excluded on a per table basis. Simply implement
  132. * this hook to return an array of fields to be excluded. For example:
  133. * @code
  134. mymodule_exclude_field_from_feature_by_default() {
  135. return array('residues' => TRUE);
  136. }
  137. * @endcode
  138. * will ensure that feature.residues is ecluded from a feature object by default.
  139. * - hook_exclude_type_by_default:
  140. * This hook allows you to exclude fields from all tables that are of a given postgresql field
  141. * type. Simply implement this hook to return an array of postgresql types mapped to criteria.
  142. * Then all fields of that type where the criteria supplied returns TRUE will be excluded from
  143. * any table. Tokens available in criteria are &gt;field_value&lt; and &gt;field_name&lt; . For example:
  144. * @code
  145. mymodule_exclude_type_by_default() {
  146. return array('text' => 'length(&gt;field_value&lt; ) > 50');
  147. }
  148. * @endcode
  149. * will exclude all text fields with a length > 50. Thus if $feature.residues is longer than 50 * it will be excluded, otherwise it will be added.
  150. *
  151. * @ingroup tripal_chado_query_api
  152. */
  153. function chado_generate_var($table, $values, $base_options = array()) {
  154. $all = new stdClass();
  155. $return_array = 0;
  156. if (array_key_exists('return_array', $base_options)) {
  157. $return_array = 1;
  158. }
  159. $include_fk = FALSE;
  160. if (array_key_exists('include_fk', $base_options)) {
  161. $include_fk = $base_options['include_fk'];
  162. }
  163. $pager = array();
  164. if (array_key_exists('pager', $base_options)) {
  165. $pager = $base_options['pager'];
  166. }
  167. // get description for the current table----------------------------------------------------------
  168. $table_desc = chado_get_schema($table);
  169. if (!$table_desc or count($table_desc) == 0) {
  170. tripal_report_error('tripal_core', TRIPAL_ERROR,
  171. "chado_generate_var: The table '%table' has not been defined. " .
  172. "and cannot be expanded. If this is a custom table, please add it using the Tripal " .
  173. "custom table interface.", array('%table' => $table));
  174. if ($return_array) {
  175. return array();
  176. }
  177. return FALSE;
  178. }
  179. $table_primary_key = $table_desc['primary key'][0];
  180. $table_columns = array_keys($table_desc['fields']);
  181. // Expandable fields without value needed for criteria--------------------------------------------
  182. $all->expandable_fields = array();
  183. $all->expandable_foreign_keys = array();
  184. if (array_key_exists('referring_tables', $table_desc) and $table_desc['referring_tables']) {
  185. $all->expandable_tables = $table_desc['referring_tables'];
  186. }
  187. else {
  188. $all->expandable_tables = array();
  189. }
  190. $all->expandable_nodes = array();
  191. /*
  192. // Get fields to be removed by name.................................
  193. $fields_to_remove = module_invoke_all('exclude_field_from_' . $table . '_by_default');
  194. foreach ($fields_to_remove as $field_name => $criteria) {
  195. //replace &gt;field_name&lt; with the current field name &
  196. $criteria = preg_replace('/&gt;field_name&lt; /', addslashes($field_name), $criteria);
  197. // if field_value needed we can't deal with this field yet
  198. if (preg_match('/&gt;field_value&lt; /', $criteria)) {
  199. break;
  200. }
  201. //if criteria then remove from query
  202. // @coder-ignore: only module designers can populate $criteria -not security risk
  203. $success = php_eval('<?php return ' . $criteria . '; ?>');
  204. if ($success) {
  205. unset($table_columns[array_search($field_name, $table_columns)]);
  206. unset($fields_to_remove[$field_name]);
  207. $all->expandable_fields[] = $table . '.' . $field_name;
  208. }
  209. }
  210. //Get fields to be removed by type................................
  211. $types_to_remove = module_invoke_all('exclude_type_by_default');
  212. $field_types = array();
  213. foreach ($table_desc['fields'] as $field_name => $field_array) {
  214. $field_types[$field_array['type']][] = $field_name;
  215. }
  216. foreach ($types_to_remove as $field_type => $criteria) {
  217. // if there are fields of that type to remove
  218. if (is_array($field_types[$field_type])) {
  219. //replace &gt;field_name&lt; with the current field name &
  220. $criteria = preg_replace('/&gt;field_name&lt; /', addslashes($field_name), $criteria);
  221. foreach ($field_types[$field_type] as $field_name) {
  222. // if field_value needed we can't deal with this field yet
  223. if (preg_match('/&gt;field_value&lt; /', $criteria)) {
  224. $fields_to_remove[$field_name] = $criteria;
  225. continue;
  226. }
  227. // if field_value needed we can't deal with this field yet
  228. if (preg_match('/&gt;field_value&lt; /', $criteria)) {
  229. break;
  230. }
  231. //if criteria then remove from query
  232. // @coder-ignore: only module designers can populate $criteria -not security risk
  233. $success = php_eval('<?php return ' . $criteria . '; ?>');
  234. if ($success) {
  235. unset($table_columns[array_search($field_name, $table_columns)]);
  236. $all->expandable_fields[] = $table . '.' . $field_name;
  237. }
  238. } //end of foreach field of that type
  239. }
  240. } //end of foreach type to be removed
  241. */
  242. // get the values for the record in the current table---------------------------------------------
  243. $results = chado_select_record($table, $table_columns, $values, $base_options);
  244. if ($results) {
  245. foreach ($results as $key => $object) {
  246. // Add empty expandable_x arrays
  247. $object->expandable_fields = $all->expandable_fields;
  248. $object->expandable_foreign_keys = $all->expandable_foreign_keys;
  249. $object->expandable_tables = $all->expandable_tables;
  250. $object->expandable_nodes = $all->expandable_nodes;
  251. // add curent table
  252. $object->tablename = $table;
  253. // check if the current table maps to a node type-----------------------------------------------
  254. // if this table is connected to a node there will be a chado_tablename table in drupal
  255. if (db_table_exists('chado_' . $table)) {
  256. // that has a foreign key to this one ($table_desc['primary key'][0]
  257. // and to the node table (nid)
  258. $sql = "
  259. SELECT $table_primary_key, nid
  260. FROM {chado_$table}
  261. WHERE $table_primary_key = :$table_primary_key
  262. ";
  263. $mapping = db_query($sql, array(":$table_primary_key" => $object->{$table_primary_key}))->fetchObject();
  264. if ($mapping and $mapping->$table_primary_key) {
  265. $object->nid = $mapping->nid;
  266. $object->expandable_nodes[] = $table;
  267. }
  268. }
  269. // remove any fields where criteria needs to be evalulated---------------------------------------
  270. /* foreach ($fields_to_remove as $field_name => $criteria) {
  271. if (!isset($object->{$field_name})) {
  272. break;
  273. }
  274. $criteria = preg_replace('/&gt;field_value&lt; /', addslashes($object->{$field_name}), $criteria);
  275. $success = php_eval('<?php return ' . $criteria . '; ?>');
  276. if ($success) {
  277. unset($object->{$field_name});
  278. $object->expandable_fields[] = $table . '.' . $field_name;
  279. }
  280. }
  281. */
  282. // recursively follow foreign key relationships nesting objects as we go------------------------
  283. if ($table_desc['foreign keys']) {
  284. foreach ($table_desc['foreign keys'] as $foreign_key_array) {
  285. $foreign_table = $foreign_key_array['table'];
  286. foreach ($foreign_key_array['columns'] as $foreign_key => $primary_key) {
  287. // Note: Foreign key is the field in the current table whereas primary_key is the field in
  288. // the table referenced by the foreign key
  289. //Dont do anything if the foreign key is empty
  290. if (empty($object->{$foreign_key})) {
  291. continue;
  292. }
  293. if (is_array($include_fk)) {
  294. // don't recurse if the callee has supplied an $fk_include list and this
  295. // FK table is not in the list.
  296. if (is_array($include_fk) and !array_key_exists($foreign_key, $include_fk)) {
  297. $object->expandable_foreign_keys[] = $table . '.' . $foreign_key . ' => ' . $foreign_table;
  298. continue;
  299. }
  300. }
  301. // if we have the option but it is not an array then we don't recurse any furutehr
  302. if ($include_fk === TRUE) {
  303. $object->expandable_foreign_keys[] = $table . '.' . $foreign_key . ' => ' . $foreign_table;
  304. continue;
  305. }
  306. // get the record from the foreign table
  307. $foreign_values = array($primary_key => $object->{$foreign_key});
  308. $options = array();
  309. if (is_array($include_fk)) {
  310. $options['include_fk'] = $include_fk[$foreign_key];
  311. }
  312. $foreign_object = chado_generate_var($foreign_table, $foreign_values, $options);
  313. // add the foreign record to the current object in a nested manner
  314. $object->{$foreign_key} = $foreign_object;
  315. // Flatten expandable_x arrays so only in the bottom object
  316. if (property_exists($object->{$foreign_key}, 'expandable_fields') and
  317. is_array($object->{$foreign_key}->expandable_fields)) {
  318. $object->expandable_fields = array_merge(
  319. $object->expandable_fields,
  320. $object->{$foreign_key}->expandable_fields
  321. );
  322. unset($object->{$foreign_key}->expandable_fields);
  323. }
  324. if (property_exists($object->{$foreign_key}, 'expandable_foreign_keys') and
  325. is_array($object->{$foreign_key}->expandable_foreign_keys)) {
  326. $object->expandable_foreign_keys = array_merge(
  327. $object->expandable_foreign_keys,
  328. $object->{$foreign_key}->expandable_foreign_keys
  329. );
  330. unset($object->{$foreign_key}->expandable_foreign_keys);
  331. }
  332. if (property_exists($object->{$foreign_key}, 'expandable_tables') and
  333. is_array($object->{$foreign_key}->expandable_tables)) {
  334. $object->expandable_tables = array_merge(
  335. $object->expandable_tables,
  336. $object->{$foreign_key}->expandable_tables
  337. );
  338. unset($object->{$foreign_key}->expandable_tables);
  339. }
  340. if (property_exists($object->{$foreign_key}, 'expandable_nodes') and
  341. is_array($object->{$foreign_key}->expandable_nodes)) {
  342. $object->expandable_nodes = array_merge(
  343. $object->expandable_nodes,
  344. $object->{$foreign_key}->expandable_nodes
  345. );
  346. unset($object->{$foreign_key}->expandable_nodes);
  347. }
  348. }
  349. }
  350. $results[$key] = $object;
  351. }
  352. }
  353. }
  354. // convert the results into an array
  355. $results_arr = array();
  356. foreach ($results as $record) {
  357. $results_arr[] = $record;
  358. }
  359. // check only one result returned
  360. if (!$return_array) {
  361. if (sizeof($results_arr) == 1) {
  362. // add results to object
  363. return $results_arr[0];
  364. }
  365. elseif (!empty($results_arr)) {
  366. return $results_arr;
  367. }
  368. else {
  369. // no results returned
  370. }
  371. }
  372. // the caller has requested results are always returned as
  373. // an array
  374. else {
  375. if (!$results_arr) {
  376. return array();
  377. }
  378. else {
  379. return $results_arr;
  380. }
  381. }
  382. }
  383. /**
  384. * Retrieves fields/tables/nodes that were excluded by default from a variable and adds them
  385. *
  386. * This function exists to allow chado_generate_var() to excldue some
  387. * fields/tables/nodes from the default form of a variable without making it extremely difficult for
  388. * the tripal admin to get at these variables if he/she wants them.
  389. *
  390. * @param $object
  391. * This must be an object generated using chado_generate_var()
  392. * @param $type
  393. * Must be one of 'field', 'table', 'node'. Indicates what is being expanded.
  394. * @param $to_expand
  395. * The name of the field/table/node to be expanded
  396. * @param $table_options
  397. * - order_by:
  398. * An array containing options for the base table. For example, an
  399. * option of 'order_by' may be used to sort results in the base table
  400. * if more than one are returned. The options must be compatible with
  401. * the options accepted by the chado_select_record() function.
  402. * - return_array:
  403. * Additionally, The option 'return_array' can be provided to force
  404. * the function to expand tables as an array. Default behavior is to expand
  405. * a table as single record if only one record exists or to expand as an array if
  406. * multiple records exist.
  407. * - include_fk:
  408. * an array of FK relationships to follow. By default, the
  409. * chado_expand_var function will follow all FK relationships but this
  410. * may generate more queries then is desired slowing down this function call when
  411. * there are lots of FK relationships to follow. Provide an array specifying the
  412. * fields to include. For example, if expanding a property table (e.g. featureprop)
  413. * and you want the CV and accession but do not want the DB the following
  414. * array would work:
  415. * $table_options = array(
  416. * 'include_fk' => array(
  417. * 'type_id' => array(
  418. * 'cv_id' => 1,
  419. * 'dbxref_id' => 1,
  420. * )
  421. * )
  422. * );
  423. *
  424. * The above array will expand the 'type_id' of the property table but only
  425. * further expand the cv_id and the dbxref_id and will go no further.
  426. * - pager:
  427. * Use this option if it is desired to return only a subset of results
  428. * so that they may be shown within a Drupal-style pager. This should be
  429. * an array with two keys: 'limit' and 'element'. The value of 'limit'
  430. * should specify the number of records to return and 'element' is a
  431. * unique integer to differentiate between pagers when more than one
  432. * appear on a page. The 'element' should start with zero and increment by
  433. * one for each pager. This only works when type is a 'table'.
  434. * @return
  435. * A chado object supplemented with the field/table/node requested to be expanded.
  436. * If the type is a table and it has already been expanded no changes is made to the
  437. * returned object
  438. *
  439. * Example Usage:
  440. * @code
  441. // Get a chado object to be expanded
  442. $values = array(
  443. 'name' => 'Medtr4g030710'
  444. );
  445. $features = chado_generate_var('feature', $values);
  446. // Expand the organism node
  447. $feature = chado_expand_var($feature, 'node', 'organism');
  448. // Expand the feature.residues field
  449. $feature = chado_expand_var($feature, 'field', 'feature.residues');
  450. // Expand the feature properties (featureprop table)
  451. $feature = chado_expand_var($feature, 'table', 'featureprop');
  452. * @endcode
  453. *
  454. * @ingroup tripal_chado_query_api
  455. */
  456. function chado_expand_var($object, $type, $to_expand, $table_options = array()) {
  457. // make sure we have a value
  458. if (!$object) {
  459. tripal_report_error('tripal_core', TRIPAL_ERROR,
  460. 'Cannot pass non array as argument, $object, to chado_expand_var function.', array());
  461. return $object;
  462. }
  463. // check to see if we are expanding an array of objects
  464. if (is_array($object)) {
  465. foreach ($object as $index => $o) {
  466. $object[$index] = chado_expand_var($o, $type, $to_expand);
  467. }
  468. return $object;
  469. }
  470. // get the base table name
  471. $base_table = $object->tablename;
  472. switch ($type) {
  473. case "field": //--------------------------------------------------------------------------------
  474. if (preg_match('/(\w+)\.(\w+)/', $to_expand, $matches)) {
  475. $tablename = $matches[1];
  476. $fieldname = $matches[2];
  477. $table_desc = chado_get_schema($tablename);
  478. // BASE CASE: the field is from the current table
  479. if ($base_table == $tablename) {
  480. // Use the table description to fully describe the current object
  481. // in a $values array to be used to select the field from chado
  482. $values = array();
  483. foreach ($table_desc['primary key'] as $key) {
  484. if(property_exists($object, $key)) {
  485. $values[$key] = $object->{$key};
  486. }
  487. }
  488. // Retrieve the field from Chado
  489. $results = chado_select_record($tablename, array($fieldname), $values);
  490. // Check that the field was retrieved correctly
  491. if (isset($results[0])) {
  492. $object->{$fieldname} = $results[0]->{$fieldname};
  493. $object->expanded = $to_expand;
  494. }
  495. // If it wasn't retrieved correctly, we need to warn the administrator
  496. }
  497. // RECURSIVE CASE: the field is in a nested object
  498. else {
  499. // We want to look at each field and if it's an object then we want to
  500. // attempt to expand the field in it via recursion
  501. foreach ((array) $object as $field_name => $field_value) {
  502. if (is_object($field_value)) {
  503. $object->{$field_name} = chado_expand_var(
  504. $field_value,
  505. 'field',
  506. $to_expand
  507. );
  508. }
  509. } //end of for each field in the current object
  510. }
  511. }
  512. // Otherwise we weren't able to extract the parts of the field to expand
  513. // Thus we will warn the administrator
  514. else {
  515. tripal_report_error('tripal_core', TRIPAL_ERROR,
  516. 'chado_expand_var: Field (%field) not in the right format. " .
  517. "It should be <tablename>.<fieldname>', array('%field' => $to_expand));
  518. }
  519. break;
  520. case "foreign_key": //--------------------------------------------------------------------------
  521. if (preg_match('/(\w+)\.(\w+) => (\w+)/', $to_expand, $matches)) {
  522. $table_name = $matches[1];
  523. $field_name = $matches[2];
  524. $foreign_table = $matches[3];
  525. $table_desc = chado_get_schema($table_name);
  526. // BASE CASE: The foreign key is from the current table
  527. if ($base_table == $table_name) {
  528. // Get the value of the foreign key from the object
  529. $field_value = $object->{$field_name};
  530. // Get the name of the field in the foreign table using the table description
  531. // For example, with the feature.type_id => cvterm.cvterm_id we need cvterm_id
  532. $foreign_field_name = FALSE;
  533. foreach ($table_desc['foreign keys'][$foreign_table]['columns'] as $left => $right) {
  534. if ($right == $field_name) {
  535. $foreign_field_name = $left;
  536. }
  537. }
  538. // Check that we were able to determine the field name in the foreign table
  539. if ($foreign_field_name) {
  540. // Generate a chado variable of the foreign key
  541. // For example, if the foreign key to expand is feature.type_id
  542. // then we want to generate a chado cvterm variable that matches the feature.type_id
  543. $foreign_var = chado_generate_var(
  544. $foreign_table, // thus in the example above, generate a cvterm var
  545. array($foreign_field_name => $field_value), // where the cvterm.cvterm_id = feature.type_id value
  546. $table_options //pass in the same options given to this function
  547. );
  548. // Check that the foreign object was returned
  549. if ($foreign_var) {
  550. // It was so now we can add this chado variable to our current object
  551. // in place of the key value
  552. $object->{$field_name} = $foreign_var;
  553. $object->expanded = $to_expand;
  554. }
  555. // Otherwise we weren't able to expand the foreign key
  556. else {
  557. tripal_report_error('tripal_core', TRIPAL_ERROR,
  558. 'chado_expand_var: unable to retrieve the object desribed by the foreign key
  559. while trying to expand %fk.',
  560. array('%fk' => $to_expand));
  561. }
  562. }
  563. // Else we were unable to determine the field name in the foreign table
  564. else {
  565. tripal_report_error('tripal_core', TRIPAL_ERROR,
  566. 'chado_expand_var: unable to determine the field name in the table the foreign
  567. key points to while trying to expand %fk.',
  568. array('%fk' => $to_expand));
  569. }
  570. }
  571. // RECURSIVE CASE: Check any nested objects
  572. else {
  573. foreach ((array) $object as $field_name => $field_value) {
  574. if (is_object($field_value)) {
  575. $object->{$field_name} = chado_expand_var(
  576. $field_value,
  577. 'foreign_key',
  578. $to_expand
  579. );
  580. }
  581. } //end of for each field in the current object
  582. }
  583. }
  584. // Otherwise we weren't able to extract the parts of the foreign key to expand
  585. // Thus we will warn the administrator
  586. else {
  587. tripal_report_error('tripal_core', TRIPAL_ERROR,
  588. 'chado_expand_var: foreign_key (%fk) not in the right format. " .
  589. "It should be <tablename>.<fieldname>', array('%fk' => $to_expand));
  590. }
  591. break;
  592. case "table": //--------------------------------------------------------------------------------
  593. $foreign_table = $to_expand;
  594. // RECURSIVE BASE CASE: don't expand the table it already is expanded
  595. if (array_key_exists($foreign_table, $object)) {
  596. return $object;
  597. }
  598. $foreign_table_desc = chado_get_schema($foreign_table);
  599. // BASE CASE: If it's connected to the base table via a FK constraint
  600. // then we have all the information needed to expand it now
  601. if (array_key_exists($base_table, $foreign_table_desc['foreign keys'])) {
  602. foreach ($foreign_table_desc['foreign keys'][$base_table]['columns'] as $left => $right) {
  603. // if the FK value in the base table is not there then we can't expand it, so just skip it.
  604. if (!$object->{$right}) {
  605. continue;
  606. }
  607. // generate a new object for this table using the FK values in the base table.
  608. $new_options = $table_options;
  609. $foreign_object = chado_generate_var($foreign_table, array($left => $object->{$right}), $new_options);
  610. // if the generation of the object was successful, update the base object to include it.
  611. if ($foreign_object) {
  612. // in the case where the foreign key relationship exists more
  613. // than once with the same table we want to alter the array structure to
  614. // include the field name.
  615. if (count($foreign_table_desc['foreign keys'][$base_table]['columns']) > 1) {
  616. if (!property_exists($object, $foreign_table)) {
  617. $object->{$foreign_table} = new stdClass();
  618. }
  619. $object->{$foreign_table}->{$left} = $foreign_object;
  620. $object->expanded = $to_expand;
  621. }
  622. else {
  623. if (!property_exists($object, $foreign_table)) {
  624. $object->{$foreign_table} = new stdClass();
  625. }
  626. $object->{$foreign_table} = $foreign_object;
  627. $object->expanded = $to_expand;
  628. }
  629. }
  630. // if the object returned is NULL then handle that
  631. else {
  632. // in the case where the foreign key relationship exists more
  633. // than once with the same table we want to alter the array structure to
  634. // include the field name.
  635. if (count($foreign_table_desc['foreign keys'][$base_table]['columns']) > 1) {
  636. if (!property_exists($object, $foreign_table)) {
  637. $object->{$foreign_table} = new stdClass();
  638. }
  639. $object->{$foreign_table}->{$left} = NULL;
  640. }
  641. else {
  642. $object->{$foreign_table} = NULL;
  643. }
  644. }
  645. }
  646. }
  647. // RECURSIVE CASE: if the table is not connected directly to the current base table
  648. // through a foreign key relationship, then maybe it has a relationship to
  649. // one of the nested objects.
  650. else {
  651. // We need to recurse -the table has a relationship to one of the nested objects
  652. // We assume it's a nested object if the value of the field is an object
  653. $did_expansion = 0;
  654. foreach ((array) $object as $field_name => $field_value) {
  655. // if we have a nested object ->expand the table in it
  656. // check to see if the $field_name is a valid chado table, we don't need
  657. // to call chado_expand_var on fields that aren't tables
  658. $check = chado_get_schema($field_name);
  659. if ($check) {
  660. $did_expansion = 1;
  661. $object->{$field_name} = chado_expand_var($field_value, 'table', $foreign_table);
  662. }
  663. }
  664. // if we did not expand this table we should return a message that the foreign table
  665. // could not be expanded
  666. if (!$did_expansion) {
  667. tripal_report_error('tripal_core', TRIPAL_ERROR, 'chado_expand_var: Could not expand table, %table. It is ' .
  668. 'not in a foreign key relationship with the base object nor with any other expanded table. ' .
  669. 'Check the table definition to ensure that a proper foreign key relationship is present.',
  670. array('%table' => $foreign_table));
  671. }
  672. }
  673. break;
  674. case "node": //---------------------------------------------------------------------------------
  675. // BASE CASE: if the node to be expanded is for our base table, then just expand it
  676. if ($object->tablename == $to_expand) {
  677. // Load the node based on the current objects nid (node primary key)
  678. $node = node_load($object->nid);
  679. // If we have successfully loaded the node...
  680. if ($node) {
  681. // Move expandable arrays from the object into the node
  682. $object->expanded = $to_expand;
  683. $node->expandable_fields = $object->expandable_fields;
  684. unset($object->expandable_fields);
  685. $node->expandable_tables = $object->expandable_tables;
  686. unset($object->expandable_tables);
  687. $node->expandable_nodes = $object->expandable_nodes;
  688. unset($object->expandable_nodes);
  689. // The node becomes the base object with the obejct added to it.
  690. // For example, we may start with a feature object with a name, uniquename , type, etc.
  691. // After expanding we will return the node and at $node->feature you will find the original object
  692. $node->{$base_table} = $object;
  693. $object = $node;
  694. }
  695. // Else we were unable to load the node
  696. else {
  697. // Warn the administrator
  698. tripal_report_error('tripal_core', TRIPAL_ERROR, 'chado_expand_var: No node matches the nid (%nid) supplied.',
  699. array('%nid' => $object->nid));
  700. } //end of if node
  701. }
  702. // RECURSIVE CASE: check to see if the node to be expanded associates with a
  703. // chado table within one of the nested objects.
  704. else {
  705. // We need to recurse -the node to expand is one of the nested objects
  706. // We assume it's a nested object if the field value is an object
  707. foreach ((array) $object as $field_name => $field_value) {
  708. if (is_object($field_value)) {
  709. $object->{$field_name} = chado_expand_var(
  710. $field_value,
  711. 'node',
  712. $to_expand
  713. );
  714. }
  715. } //end of for each field in the current object
  716. }
  717. break;
  718. // The $type to be expanded is not yet supported
  719. default:
  720. tripal_report_error('tripal_core', TRIPAL_ERROR, 'chado_expand_var: Unrecognized type (%type). Should be one of "field", "table", "node".',
  721. array('%type' => $type));
  722. return FALSE;
  723. }
  724. // Move expandable arrays downwards -------------------------------
  725. // If the type was either table or foreign key then a new chado variable was generated
  726. // this variable will have it's own expandable array's which need to be moved down
  727. // and merged with the base objects expandable arrays
  728. // Thus, check all nested objects for expandable arrays
  729. // and if they have them, move them downwards
  730. foreach ( (array)$object as $field_name => $field_value) {
  731. if (is_object($field_value)) {
  732. // The current nested object has expandable arrays
  733. if (isset($field_value->expandable_fields)) {
  734. // Move expandable fields downwards
  735. if (isset($field_value->expandable_fields) and is_array($field_value->expandable_fields)) {
  736. // If the current object has it's own expandable fields then merge them
  737. if (isset($object->expandable_fields)) {
  738. $object->expandable_fields = array_merge(
  739. $object->expandable_fields,
  740. $object->{$field_name}->expandable_fields
  741. );
  742. unset($object->{$field_name}->expandable_fields);
  743. }
  744. // Otherwise, just move the expandable fields downwards
  745. else {
  746. $object->expandable_fields = $object->{$field_name}->expandable_fields;
  747. unset($object->{$field_name}->expandable_fields);
  748. }
  749. }
  750. // Move expandable foreign keys downwards
  751. if (isset($field_value->expandable_foreign_keys) and is_array($field_value->expandable_foreign_keys)) {
  752. // If the current object has it's own expandable foreign keys then merge them
  753. if (isset($object->expandable_foreign_keys)) {
  754. $object->expandable_foreign_keys = array_merge(
  755. $object->expandable_foreign_keys,
  756. $object->{$field_name}->expandable_foreign_keys
  757. );
  758. unset($object->{$field_name}->expandable_foreign_keys);
  759. }
  760. // Otherwise, just move the expandable foreign keys downwards
  761. else {
  762. $object->expandable_foreign_keys = $object->{$field_name}->expandable_foreign_keys;
  763. unset($object->{$field_name}->expandable_foreign_keys);
  764. }
  765. }
  766. // Move expandable tables downwards
  767. if (isset($field_value->expandable_tables) and is_array($field_value->expandable_tables)) {
  768. // If the current object has it's own expandable tables then merge them
  769. if (isset($object->expandable_tables)) {
  770. $object->expandable_tables = array_merge(
  771. $object->expandable_tables,
  772. $object->{$field_name}->expandable_tables
  773. );
  774. unset($object->{$field_name}->expandable_tables);
  775. }
  776. // Otherwise, just move the expandable tables downwards
  777. else {
  778. $object->expandable_tables = $object->{$field_name}->expandable_tables;
  779. unset($object->{$field_name}->expandable_tables);
  780. }
  781. }
  782. // Move expandable nodes downwards
  783. if (isset($field_value->expandable_nodes) and is_array($field_value->expandable_nodes)) {
  784. // If the current object has it's own expandable tables then merge them
  785. if (isset($object->expandable_nodes)) {
  786. $object->expandable_nodes = array_merge(
  787. $object->expandable_nodes,
  788. $object->{$field_name}->expandable_nodes
  789. );
  790. unset($object->{$field_name}->expandable_nodes);
  791. }
  792. // Otherwise, just move the expandable tables downwards
  793. else {
  794. $object->expandable_nodes = $object->{$field_name}->expandable_nodes;
  795. unset($object->{$field_name}->expandable_nodes);
  796. }
  797. }
  798. }
  799. }
  800. }
  801. // Move extended array downwards ----------------------------------
  802. // This tells us what we have expanded (ie: that we succeeded)
  803. // and is needed to remove the entry from the expandable array
  804. // If there is no expanded field in the current object then check any of the nested objects
  805. // and move it down
  806. if (!property_exists($object, 'expanded')) {
  807. // It's a nested object if the value is an object
  808. foreach ( (array)$object as $field_name => $field_value) {
  809. if (is_object($field_value)) {
  810. // Check if the current nested object has an expanded array
  811. if (isset($field_value->expanded)) {
  812. // If so, then move it downwards
  813. $object->expanded = $field_value->expanded;
  814. unset($field_value->expanded);
  815. }
  816. }
  817. }
  818. }
  819. // Check again if there is an expanded field in the current object
  820. // We check again because it might have been moved downwards above
  821. if (property_exists($object, 'expanded')) {
  822. // If so, then remove the expanded identifier from the correct expandable array
  823. $expandable_name = 'expandable_' . $type . 's';
  824. if (property_exists($object, $expandable_name) and $object->{$expandable_name}) {
  825. $key_to_remove = array_search($object->expanded, $object->{$expandable_name});
  826. unset($object->{$expandable_name}[$key_to_remove]);
  827. unset($object->expanded);
  828. }
  829. }
  830. // Finally, Return the object!
  831. return $object;
  832. }