tripal_chado.field_storage.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <?php
  2. /**
  3. * Implements hook_field_storage_info().
  4. */
  5. function tripal_chado_field_storage_info() {
  6. return array(
  7. 'field_chado_storage' => array(
  8. 'label' => t('Chado'),
  9. 'description' => t('Stores fields in the local Chado database.'),
  10. 'settings' => array(),
  11. // The logo_url key is supported by Tripal. It's not a Drupal key. It's
  12. // used for adding a logo or picture for the data store to help make it
  13. // more easily recognized on the field_ui_field_overview_form. Ideally
  14. // the URL should point to a relative path on the local Drupal site.
  15. 'logo_url' => url(drupal_get_path('module', 'tripal') . '/theme/images/250px-ChadoLogo.png'),
  16. ),
  17. );
  18. }
  19. /**
  20. * Implements hook_field_storage_write().
  21. */
  22. function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
  23. // Get the bundle and the term for this entity.
  24. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  25. $term = entity_load('TripalTerm', array('id' => $entity->term_id));
  26. $term = reset($term);
  27. // Get the base table, type field and record_id from the entity.
  28. $base_table = $entity->chado_table;
  29. $type_field = $entity->chado_column;
  30. $record = $entity->chado_record;
  31. $record_id = $entity->chado_record_id;
  32. // Convert the fields into a key/value list of fields and their values.
  33. $field_vals = tripal_chado_field_storage_merge_fields($fields, $entity_type, $entity);
  34. dpm($field_vals);
  35. return;
  36. // Write the record for the base table.
  37. $record_id = tripal_chado_field_storage_write_table(array(
  38. 'entity' => $entity,
  39. 'term' => $term,
  40. 'op' => $op,
  41. 'field_vals' => $field_vals,
  42. 'base_table' => $base_table,
  43. 'tablename' => $base_table,
  44. 'type_field' => $type_field,
  45. 'record_id' => $record_id,
  46. ));
  47. // If this is an insert then add the chado_entity record.
  48. if ($op == FIELD_STORAGE_INSERT) {
  49. // Add a record to the chado_entity table so that the data for the
  50. // fields can be pulled from Chado when loaded the next time.
  51. $record = array(
  52. 'entity_id' => $entity->id,
  53. 'record_id' => $record_id,
  54. 'data_table' => $base_table,
  55. 'type_table' => $base_table,
  56. 'field' => $type_field,
  57. );
  58. $success = drupal_write_record('chado_entity', $record);
  59. if (!$success) {
  60. drupal_set_message('Unable to insert new Chado entity.', 'error');
  61. }
  62. }
  63. // Now that we have handled the base table, we need to handle linking tables.
  64. foreach ($fields as $field_id) {
  65. // Get the field using the id.
  66. $field = field_info_field_by_id($field_id);
  67. $field_name = $field['field_name'];
  68. // If the field has a chado_table setting then we can try to write.
  69. if (array_key_exists('settings', $field) and array_key_exists('chado_table', $field['settings'])) {
  70. // Skip fields that use the base table, as we've already handled those.
  71. if ($field['settings']['chado_table'] != $base_table){
  72. $field_table = $field['settings']['chado_table'];
  73. // Iterate through each record.
  74. if (array_key_exists($field_name, $field_vals)) {
  75. foreach ($field_vals[$field_name] as $delta => $fvals) {
  76. tripal_chado_field_storage_write_table(array(
  77. 'entity' => $entity,
  78. 'term' => $term,
  79. 'op' => $op,
  80. 'field_vals' => $fvals,
  81. 'base_table' => $base_table,
  82. 'tablename' => $field_table
  83. ));
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. *
  92. */
  93. function tripal_chado_field_storage_write_table($params) {
  94. $entity = $params['entity'];
  95. $term = $params['term'];
  96. $op = $params['op'];
  97. $field_vals = $params['field_vals'];
  98. $base_table = $params['base_table'];
  99. $tablename = $params['tablename'];
  100. $type_field = array_key_exists('type_field', $params) ? $params['type_field'] : NULL;
  101. $record_id = array_key_exists('record_id', $params) ? $params['record_id'] : NULL;
  102. $depth = array_key_exists('depth', $params) ? $params['depth'] : 0;
  103. // Intialize the values array.
  104. $values = array();
  105. // Get the schema for this table so that we can identify the primary key
  106. // and foreign keys.
  107. $schema = chado_get_schema($tablename);
  108. $pkey_field = $schema['primary key'][0];
  109. $fkey_fields = $schema['foreign keys'];
  110. $fkey_fields_list = array();
  111. $fkey_base_linker = NULL;
  112. // STEP 1: Recurse on the FK fields.
  113. // Loop through the foreign keys so that we can recurse on those first.
  114. foreach ($fkey_fields as $fk_table => $details) {
  115. foreach ($details['columns'] as $local_id => $remote_id) {
  116. // If this is the base table then do not recurse on the type_id.
  117. if ($tablename == $base_table && $local_id == $type_field) {
  118. $values[$local_id] = $term->details['cvterm']->cvterm_id;
  119. continue;
  120. }
  121. // If this is a linking table, do not recurse on the fields that
  122. // link back to the base table.
  123. if ($tablename != $base_table && $details['table'] == $base_table) {
  124. $fkey_base_linker = $local_id;
  125. continue;
  126. }
  127. // Get the value of the FK field as provided by the user.
  128. $fk_val = NULL;
  129. $fk_vals = array();
  130. $fk_field_name = $tablename . '__' . $local_id;
  131. if (array_key_exists($fk_field_name, $field_vals)) {
  132. $fk_val = $field_vals[$fk_field_name][0][$tablename . '__' . $local_id];
  133. $fk_vals = $field_vals[$fk_field_name][0];
  134. }
  135. // Don't recurse if the value of the FK field is set to NULL. The
  136. // Tripal Chado API value for NULL is '__NULL__'.
  137. if ($fk_val == "__NULL__") {
  138. $values[$local_id] = $fk_val;
  139. continue;
  140. }
  141. // Don't recuse if there are no fkvals.
  142. if (count(array_keys($fk_vals)) == 0) {
  143. continue;
  144. }
  145. // Keep track of the FK fields so that in STEP 2 we don't have to
  146. // loop through the $fk_fields again.
  147. $fkey_fields_list[] = $local_id;
  148. // Recurse on the FK field.
  149. $nparams = $params;
  150. $nparams['field_vals'] = $fk_vals;
  151. $nparams['tablename'] = $fk_table;
  152. $nparams['type_field'] = NULL;
  153. $nparams['record_id'] = $fk_val;
  154. $nparams['depth'] = $depth + 1;
  155. $fk_val = tripal_chado_field_storage_write_table($nparams);
  156. if (isset($fk_val) and $fk_val != '' and $fk_val != 0) {
  157. $values[$local_id] = $fk_val;
  158. }
  159. }
  160. }
  161. // STEP 2: Loop through the non FK fields.
  162. // Loop through the fields passed to the function and find any that
  163. // are for this table. Then add their values to the $values array.
  164. foreach ($field_vals as $field_name => $items) {
  165. if (preg_match('/^' . $tablename . '__(.*)/', $field_name, $matches)) {
  166. $chado_field = $matches[1];
  167. // Skip the PKey field. We won't ever insert a primary key and if
  168. // one is provided in the fields then we use it for matching on an
  169. // update. We don't add it to the $values array in either case.
  170. if ($chado_field == $pkey_field) {
  171. continue;
  172. }
  173. // Skip FK fields as those should already have been dealt with the
  174. // recursive code above.
  175. if (in_array($chado_field, $fkey_fields_list)) {
  176. continue;
  177. }
  178. // If the value is empty then exclude this field
  179. if (!$items[0][$tablename . '__' . $chado_field]) {
  180. continue;
  181. }
  182. // Add the value of the field to the $values arr for later insert/update.
  183. $values[$chado_field] = $items[0][$tablename . '__' . $chado_field];
  184. }
  185. }
  186. // STEP 3: Insert/Update the record.
  187. // If there are no values then return.
  188. if (count($values) == 0) {
  189. return $record_id;
  190. }
  191. dpm($values);
  192. // If we don't have an incoming record ID then this is an insert.
  193. if ($record_id == NULL) {
  194. // STEP 3a: Before inserting, we want to make sure the record does not
  195. // already exist. Using the unique constraint check for a matching record.
  196. $options = array('is_duplicate' => TRUE);
  197. $is_duplicate = chado_select_record($tablename, array('*'), $values, $options);
  198. if($is_duplicate) {
  199. $record = chado_select_record($tablename, array('*'), $values);
  200. return $record[0]->$pkey_field;
  201. }
  202. // STEP 3b: Insert the reocrd
  203. // Insert the values array as a new record in the table.
  204. $record = chado_insert_record($tablename, $values);
  205. if ($record === FALSE) {
  206. throw new Exception('Could not insert Chado record into table: "' . $tablename . '".');
  207. }
  208. $record_id = $record[$pkey_field];
  209. }
  210. // We have an incoming record_id so this is an update.
  211. else {
  212. // TODO: what if the unique constraint matches another record? That is
  213. // not being tested for here.
  214. $match[$pkey_field] = $record_id;
  215. if (!chado_update_record($tablename, $match, $values)) {
  216. drupal_set_message("Could not update Chado record in table: $tablename.", 'error');
  217. }
  218. }
  219. return $record_id;
  220. }
  221. /**
  222. * Implements hook_field_storage_load().
  223. *
  224. * Responsible for loading the fields from the Chado database and adding
  225. * their values to the entity.
  226. */
  227. function tripal_chado_field_storage_load($entity_type, $entities, $age,
  228. $fields, $options) {
  229. $load_current = $age == FIELD_LOAD_CURRENT;
  230. global $language;
  231. $langcode = $language->language;
  232. foreach ($entities as $id => $entity) {
  233. // Get the base table and record id for the fields of this entity.
  234. $details = db_select('chado_entity', 'ce')
  235. ->fields('ce')
  236. ->condition('entity_id', $entity->id)
  237. ->execute()
  238. ->fetchObject();
  239. if (!$details) {
  240. // TODO: what to do if record is missing!
  241. }
  242. // Get some values needed for loading the values from Chado.
  243. $base_table = $details->data_table;
  244. $type_field = $details->field;
  245. $record_id = $details->record_id;
  246. // Get this table's schema.
  247. $schema = chado_get_schema($base_table);
  248. $pkey_field = $schema['primary key'][0];
  249. // Get the base record if one exists
  250. $columns = array('*');
  251. $match = array($pkey_field => $record_id);
  252. $record = chado_generate_var($base_table, $match);
  253. $entity->chado_record = $record;
  254. // For now, expand all 'text' fields.
  255. // TODO: we want to be a bit smarter and allow the user to configure this
  256. // for now we'll expand.
  257. foreach ($schema['fields'] as $field_name => $details) {
  258. if ($schema['fields'][$field_name]['type'] == 'text') {
  259. $record = chado_expand_var($record, 'field', $base_table . '.' . $field_name);
  260. }
  261. }
  262. // Iterate through the entity's fields so we can get the column names
  263. // that need to be selected from each of the tables represented.
  264. $tables = array();
  265. foreach ($fields as $field_id => $ids) {
  266. // By the time this hook runs, the relevant field definitions have been
  267. // populated and cached in FieldInfo, so calling field_info_field_by_id()
  268. // on each field individually is more efficient than loading all fields in
  269. // memory upfront with field_info_field_by_ids().
  270. $field = field_info_field_by_id($field_id);
  271. $field_name = $field['field_name'];
  272. $field_type = $field['type'];
  273. $field_module = $field['module'];
  274. // Skip fields that don't map to a Chado table (e.g. kvproperty_adder).
  275. if (!array_key_exists('settings', $field) or !array_key_exists('chado_table', $field['settings'])) {
  276. continue;
  277. }
  278. // Get the Chado table and column for this field.
  279. $field_table = $field['settings']['chado_table'];
  280. $field_column = $field['settings']['chado_column'];
  281. // There are only two types of fields: 1) fields that represent a single
  282. // column of the base table, or 2) fields that represent a linked record
  283. // in a many-to-one relationship with the base table.
  284. // Type 1: fields from base tables.
  285. if ($field_table == $base_table) {
  286. // Set an empty value by default, and if there is a record, then update.
  287. $entity->{$field_name}['und'][0]['value'] = '';
  288. if ($record and property_exists($record, $field_column)) {
  289. // If the field column is an object then it's a FK to another table.
  290. // and because $record object is created by the chado_generate_var()
  291. // function we must go one more level deeper to get the value
  292. if (is_object($record->$field_column)) {
  293. $entity->{$field_name}['und'][0][$field_table . '__' . $field_column] = $record->$field_column->$field_column;
  294. }
  295. else {
  296. // For non FK fields we'll make the field value be the same
  297. // as the column value.
  298. $entity->{$field_name}['und'][0]['value'] = $record->$field_column;
  299. $entity->{$field_name}['und'][0][$field_table . '__' . $field_column] = $record->$field_column;
  300. }
  301. }
  302. // Allow the creating module to alter the value if desired. The
  303. // module should do this if the field has any other form elements
  304. // that need populationg besides the value which was set above.
  305. module_load_include('inc', $field_module, 'includes/fields/' . $field_type);
  306. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  307. $field_obj = new $field_type();
  308. $field_obj->load($field, $entity, array('record' => $record));
  309. }
  310. $load_function = $field_type . '_load';
  311. if (function_exists($load_function)) {
  312. $load_function($field, $entity, $base_table, $record);
  313. }
  314. }
  315. // Type 2: fields for linked records. These fields will have any number
  316. // of form elements that might need populating so we'll offload the
  317. // loading of these fields to the field itself.
  318. if ($field_table != $base_table) {
  319. // Set an empty value by default, and let the hook function update it.
  320. $entity->{$field_name}['und'][0]['value'] = '';
  321. $load_function = $field_type . '_load';
  322. module_load_include('inc', $field_module, 'includes/fields/' . $field_type);
  323. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  324. $field_obj = new $field_type();
  325. $field_obj->load($field, $entity, array('record' => $record));
  326. }
  327. if (function_exists($load_function)) {
  328. $load_function($field, $entity, $base_table, $record);
  329. }
  330. }
  331. } // end: foreach ($fields as $field_id => $ids) {
  332. } // end: foreach ($entities as $id => $entity) {
  333. }
  334. /**
  335. * Merges the values of all fields into a single array keyed by table name.
  336. */
  337. function tripal_chado_field_storage_merge_fields($fields, $entity_type, $entity) {
  338. $new_fields = array();
  339. // Iterate through all of the fields and organize them into a
  340. // new fields array keyed by the table name
  341. foreach ($fields as $field_id => $ids) {
  342. // Get the field name and information about it.
  343. $field = field_info_field_by_id($field_id);
  344. $field_name = $field['field_name'];
  345. // Some fields (e.g. chado_linker_cvterm_adder) don't add data to
  346. // Chado so they don't have a table, but they are still attached to the
  347. // entity. Just skip these.
  348. if (!array_key_exists('chado_table', $field['settings'])) {
  349. continue;
  350. }
  351. $chado_table = $field['settings']['chado_table'];
  352. $chado_column = $field['settings']['chado_column'];
  353. // Iterate through the field's items. Fields with cardinality ($delta) > 1
  354. // are multi-valued.
  355. $items = field_get_items($entity_type, $entity, $field_name);
  356. foreach ($items as $delta => $item) {
  357. foreach ($item as $item_name => $value) {
  358. $matches = array();
  359. if (preg_match('/^(.*?)__(.*?)$/', $item_name, $matches)) {
  360. $table_name = $matches[1];
  361. $column_name = $matches[2];
  362. // Is this a nested FK field? If so break it down into a sub array.
  363. if (preg_match('/^(.*?)--(.*?)$/', $column_name, $matches)) {
  364. $parent_item_name = $matches[1];
  365. $sub_item_name = $matches[2];
  366. $sub_item = tripal_chado_field_storage_expand_field($sub_item_name, $value);
  367. // If we've already encountered this table and column then we've
  368. // already seen the numeric FK value or we've already added a
  369. // subcolumn. If the former we want to convert this to an array
  370. // so we can add the details.
  371. if (array_key_exists($parent_item_name, $new_fields[$table_name][$delta]) and
  372. !is_array($new_fields[$table_name][$delta][$parent_item_name])) {
  373. $new_fields[$table_name][$delta][$parent_item_name] = array();
  374. }
  375. $new_fields[$table_name][$delta][$parent_item_name] += $sub_item;
  376. }
  377. else {
  378. // If not seen this table and column then just add it. If we've
  379. // already seen it then it means it's a FK field and we've already
  380. // added subfields so do nothing.
  381. if (!array_key_exists($table_name, $new_fields) or
  382. !array_key_exists($delta, $new_fields[$table_name]) or
  383. !array_key_exists($column_name, $new_fields[$table_name][$delta])) {
  384. $new_fields[$table_name][$delta][$column_name] = $value;
  385. }
  386. }
  387. }
  388. }
  389. // If there is no value set for the field using the
  390. // [table_name]__[field name] naming schema then check if a 'value' item
  391. // is present and if so use that.
  392. if ((!array_key_exists($chado_table, $new_fields) or
  393. !array_key_exists($delta, $new_fields[$chado_table]) or
  394. !array_key_exists($chado_column, $new_fields[$chado_table][$delta])) and
  395. array_key_exists('value', $items[$delta]) and
  396. !is_array($items[$delta]['value'])) {
  397. $new_fields[$chado_table][$delta][$chado_column] = $items[$delta]['value'];
  398. }
  399. }
  400. }
  401. return $new_fields;
  402. }
  403. /**
  404. * Recurses through a field's item name breaking it into a nested array.
  405. *
  406. *
  407. */
  408. function tripal_chado_field_storage_expand_field($item_name, $value) {
  409. $matches = array();
  410. if (preg_match('/^(.*?)--(.*?)$/', $item_name, $matches)) {
  411. $parent_item_name = $matches[1];
  412. $sub_item_name = $matches[2];
  413. $sub_item = tripal_chado_field_storage_expand_field($sub_item_name, $value);
  414. return array($parent_item_name => $sub_item);
  415. }
  416. else {
  417. return array($item_name => $value);
  418. }
  419. }
  420. /**
  421. * Implements hook_field_storage_query().
  422. *
  423. * Used by EntityFieldQuery to find the entities having certain entity
  424. * and field conditions and sort them in the given field order.
  425. *
  426. * NOTE: This function needs to exist or errors are triggered but so far it doesn't
  427. * appear to actually need to do anything...
  428. */
  429. function tripal_chado_field_storage_query($query) {
  430. $fieldConditions = $query->fieldConditions;
  431. foreach ($fieldConditions as $condition) {
  432. $field = $condition['field'];
  433. $field_name = $field['field_name'];
  434. $column = $condition['column'];
  435. }
  436. }