tripal_entities.field_storage.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Implements hook_field_storage_info().
  4. */
  5. function tripal_entities_field_storage_info() {
  6. return array(
  7. 'field_chado_storage' => array(
  8. 'label' => t('Chado storage'),
  9. 'description' => t('Stores fields in the local Chado database.'),
  10. 'settings' => array(),
  11. ),
  12. );
  13. }
  14. /**
  15. * Implements hook_field_storage_write().
  16. */
  17. function tripal_entities_field_storage_write($entity_type, $entity, $op, $fields) {
  18. drupal_debug($entity_type);
  19. // Get the IDs for this entity.
  20. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  21. // Find out which table should receive the insert.
  22. $tablename = 'feature';
  23. $type_field = 'type_id';
  24. $schema = chado_get_schema($tablename);
  25. $pkey_field = $schema['primary key'][0];
  26. // Construct the values array that will be used to insert into the table.
  27. $values = array();
  28. foreach ($fields as $field_id) {
  29. $field = field_info_field_by_id($field_id);
  30. $field_name = $field['field_name'];
  31. $matches = array();
  32. if (preg_match('/^' . $tablename . '__(.*)/', $field_name, $matches)) {
  33. $chado_field = $matches[1];
  34. // Currently, we only support one language, but for for the sake of
  35. // thoroughness we'll iterate through all possible languages.
  36. $all_languages = field_available_languages($entity_type, $field);
  37. $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
  38. foreach ($field_languages as $langcode) {
  39. $items = (array) $entity->{$field_name}[$langcode];
  40. // The number of items is related to the cardinatily of the field.
  41. foreach ($items as $delta => $item) {
  42. $values[$chado_field] = $item['value'];
  43. }
  44. }
  45. }
  46. }
  47. // Add in the type_id field.
  48. $values[$type_field] = $entity->cvterm_id;
  49. $entity->storage = array();
  50. switch ($op) {
  51. case FIELD_STORAGE_INSERT:
  52. $record = chado_insert_record($tablename, $values);
  53. if ($record === FALSE) {
  54. drupal_set_message('Could not insert Chado record.', 'error');
  55. }
  56. $entity->storage['chado']['record_id'] = $record[$pkey_field];
  57. $entity->storage['chado']['data_table'] = $tablename;
  58. $entity->storage['chado']['type_table'] = $tablename;
  59. $entity->storage['chado']['field'] = $type_field;
  60. // Add a record to the chado_entity table so that the data for the
  61. // fields can be pulled from Chado when loaded the next time.
  62. $record = array(
  63. 'entity_id' => $entity->id,
  64. 'record_id' => $entity->storage['chado']['record_id'],
  65. 'data_table' => $entity->storage['chado']['data_table'],
  66. 'type_table' => $entity->storage['chado']['type_table'],
  67. 'field' => $entity->storage['chado']['field'],
  68. );
  69. $success = drupal_write_record('chado_entity', $record);
  70. if (!$success) {
  71. drupal_set_message('Unable to insert new data.', 'error');
  72. }
  73. break;
  74. case FIELD_STORAGE_UPDATE:
  75. $match[$pkey_field] = $entity->storage['chado']['record_id'];
  76. chado_update_record($tablename, $match, $values);
  77. break;
  78. }
  79. }
  80. /**
  81. * Implements hook_field_storage_load().
  82. *
  83. * Responsible for loading the fields from the Chado database and adding
  84. * their values to the entity.
  85. */
  86. function tripal_entities_field_storage_load($entity_type, $entities, $age, $fields, $options) {
  87. $load_current = $age == FIELD_LOAD_CURRENT;
  88. global $language;
  89. $langcode = $language->language;
  90. foreach ($entities as $id => $entity) {
  91. // Get the base table and record id for the fields of this entity.
  92. $details = db_select('chado_entity', 'ce')
  93. ->fields('ce')
  94. ->condition('entity_id', $entity->id)
  95. ->execute()
  96. ->fetchObject();
  97. if (!$details) {
  98. // TODO: what to do if record is missing!
  99. }
  100. $entity->storage['chado']['record_id'] = $details->record_id;
  101. $entity->storage['chado']['data_table'] = $details->data_table;
  102. $entity->storage['chado']['type_table'] = $details->type_table;
  103. $entity->storage['chado']['field'] = $details->field;
  104. // Find out which table should receive the insert.
  105. $tablename = $entity->storage['chado']['data_table'];
  106. $type_field = $entity->storage['chado']['field'];
  107. $schema = chado_get_schema($tablename);
  108. $pkey_field = $schema['primary key'][0];
  109. $record_id = $entity->storage['chado']['record_id'];
  110. // Iterate through the field names to get the list of tables and fields
  111. // that should be queried.
  112. $columns = array();
  113. foreach ($fields as $field_id => $ids) {
  114. // By the time this hook runs, the relevant field definitions have been
  115. // populated and cached in FieldInfo, so calling field_info_field_by_id()
  116. // on each field individually is more efficient than loading all fields in
  117. // memory upfront with field_info_field_by_ids().
  118. $field = field_info_field_by_id($field_id);
  119. $field_name = $field['field_name'];
  120. $matches = array();
  121. if (preg_match('/^(.*?)__(.*?)$/', $field_name, $matches)) {
  122. $table = $matches[1];
  123. $field = $matches[2];
  124. $columns[$table][] = $field;
  125. }
  126. }
  127. // Get the record
  128. $values = array($pkey_field => $record_id);
  129. $record = chado_select_record($tablename, $columns[$tablename], $values);
  130. // Now set the field values
  131. foreach ($fields as $field_id => $ids) {
  132. $field = field_info_field_by_id($field_id);
  133. $field_name = $field['field_name'];
  134. $matches = array();
  135. if (preg_match('/^(.*?)__(.*?)$/', $field_name, $matches)) {
  136. $table = $matches[1];
  137. $field = $matches[2];
  138. $entity->{$field_name}['und'][] = array('value' => $record[0]->$field);
  139. }
  140. }
  141. }
  142. }