123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636 |
- <?php
- function tripal_chado_field_storage_info() {
- return array(
- 'field_chado_storage' => array(
- 'label' => t('Chado'),
- 'description' => t('Stores fields in the local Chado database.'),
- 'settings' => array(),
-
-
-
-
- 'logo_url' => url(drupal_get_path('module', 'tripal') . '/theme/images/250px-ChadoLogo.png'),
- ),
- );
- }
- function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
-
- $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
- $term = entity_load('TripalTerm', array('id' => $entity->term_id));
- $term = reset($term);
-
- $dbxref = tripal_get_dbxref(array('accession' => $term->accession, 'db_id' => array('name' => $term->vocab->vocabulary)));
- $cvterm = tripal_get_cvterm(array('dbxref_id' => $dbxref->dbxref_id));
-
- $base_table = $entity->chado_table;
- $type_field = $entity->chado_column;
- $record = $entity->chado_record;
- $record_id = $entity->chado_record_id;
- $base_schema = chado_get_schema($base_table);
- $base_pkey = $base_schema['primary key'][0];
-
- $field_vals = tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $entity);
-
-
-
- $values = $field_vals[$base_table][0];
- if ($record_id) {
- $values[$base_pkey] = $record_id;
- }
- elseif ($type_field) {
- $values[$type_field] = $cvterm->cvterm_id;
- }
- $base_record_id = tripal_chado_field_storage_write_table($base_table, $values);
-
- if ($op == FIELD_STORAGE_INSERT) {
-
-
- $record = array(
- 'entity_id' => $entity->id,
- 'record_id' => $base_record_id,
- 'data_table' => $base_table,
- 'type_table' => $base_table,
- 'field' => $type_field,
- );
- $success = drupal_write_record('chado_entity', $record);
- if (!$success) {
- drupal_set_message('Unable to insert new Chado entity.', 'error');
- }
- }
-
- foreach ($field_vals as $table_name => $details) {
-
- if ($table_name == $base_table) {
- continue;
- }
- foreach ($details as $delta => $values) {
- $record_id = tripal_chado_field_storage_write_table($table_name, $values);
- }
- }
- }
- function tripal_chado_field_storage_write_table($table_name, $values) {
- $schema = chado_get_schema($table_name);
- $fkeys = $schema['foreign keys'];
- $pkey = $schema['primary key'][0];
-
-
-
- $num_empty = 0;
- foreach ($values as $column => $value) {
- if (!$value) {
- $num_empty++;
- }
- }
- if ($num_empty == count(array_keys($values))) {
- return '';
- }
-
-
- if (array_key_exists($pkey, $values) and $values[$pkey]) {
- $num_vals = 0;
- foreach ($values as $value) {
- if ($value) {
- $num_vals++;
- }
- }
- if ($num_vals == 1) {
- $new_vals[$pkey] = $values[$pkey];
- if (!chado_delete_record($table_name, $new_vals)) {
- throw new Exception('Could not delete record from table: "' . $table_name . '".');
- }
- return '';
- }
- }
-
- if (!array_key_exists($pkey, $values) or !$values[$pkey] or !isset($values[$pkey])) {
-
-
- $options = array('is_duplicate' => TRUE);
- $is_duplicate = chado_select_record($table_name, array('*'), $values, $options);
- if($is_duplicate) {
- $record = chado_select_record($table_name, array('*'), $values);
- return $record[0]->$pkey;
- }
-
-
- $new_vals = $values;
- unset($new_vals[$pkey]);
- $record = chado_insert_record($table_name, $new_vals);
- if ($record === FALSE) {
- throw new Exception('Could not insert Chado record into table: "' . $table_name . '".');
- }
- return $record[$pkey];
- }
-
-
-
- $match[$pkey] = $values[$pkey];
- if (!chado_update_record($table_name, $match, $values)) {
- drupal_set_message("Could not update Chado record in table: $table_name.", 'error');
- }
- return $values[$pkey];
- }
- function tripal_chado_field_storage_load($entity_type, $entities, $age,
- $fields, $options) {
- $load_current = $age == FIELD_LOAD_CURRENT;
- global $language;
- $langcode = $language->language;
- foreach ($entities as $id => $entity) {
- if (property_exists($entity, 'chado_table')) {
-
- $base_table = $entity->chado_table;
- $type_field = $entity->chado_column;
- $record_id = $entity->chado_record_id;
- }
- else {
-
- $details = db_select('chado_entity', 'ce')
- ->fields('ce')
- ->condition('entity_id', $entity->id)
- ->execute()
- ->fetchObject();
- if (!$details) {
-
- }
-
- $base_table = isset($details->data_table) ? $details->data_table : '';
- $type_field = isset($details->field) ? $details->field : '';
- $record_id = isset($details->record_id) ? $details->record_id : '';
- }
-
- $schema = chado_get_schema($base_table);
- $pkey_field = $schema['primary key'][0];
-
- $columns = array('*');
- $match = array($pkey_field => $record_id);
- $record = chado_generate_var($base_table, $match);
- $entity->chado_record = $record;
-
-
-
- if (isset($schema['fields'])) {
- foreach ($schema['fields'] as $field_name => $details) {
- if ($schema['fields'][$field_name]['type'] == 'text') {
- $record = chado_expand_var($record, 'field', $base_table . '.' . $field_name);
- }
- }
- }
-
-
- $tables = array();
- foreach ($fields as $field_id => $ids) {
-
-
-
-
- $field = field_info_field_by_id($field_id);
- $field_name = $field['field_name'];
- $field_type = $field['type'];
- $field_module = $field['module'];
-
- if (!array_key_exists('settings', $field) or !array_key_exists('chado_table', $field['settings'])) {
- continue;
- }
-
- $field_table = $field['settings']['chado_table'];
- $field_column = $field['settings']['chado_column'];
-
-
-
-
- if ($field_table == $base_table) {
-
- $entity->{$field_name}['und'][0]['value'] = '';
- if ($record and property_exists($record, $field_column)) {
-
-
-
- if (is_object($record->$field_column)) {
- $entity->{$field_name}['und'][0]['chado-' . $field_table . '__' . $field_column] = $record->$field_column->$field_column;
- }
- else {
-
-
- $entity->{$field_name}['und'][0]['value'] = $record->$field_column;
- $entity->{$field_name}['und'][0]['chado-' . $field_table . '__' . $field_column] = $record->$field_column;
- }
- }
-
-
-
- tripal_load_include_field_type($field_type);
- if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
- $tfield = new $field_type($field);
- $tfield->load($entity, array('record' => $record));
- }
- }
-
-
-
- if ($field_table != $base_table) {
-
- $entity->{$field_name}['und'][0]['value'] = '';
- tripal_load_include_field_type($field_type);
- if (class_exists($field_type) && method_exists($field_type, 'load')) {
- $tfield = new $field_type($field);
- $tfield->load($entity, array('record' => $record));
- }
- }
- }
- }
- }
- function tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $entity) {
- $new_fields = array();
-
-
- foreach ($fields as $field_id => $ids) {
-
- $field = field_info_field_by_id($field_id);
- $field_name = $field['field_name'];
-
-
-
- if (!array_key_exists('chado_table', $field['settings'])) {
- continue;
- }
- $chado_table = $field['settings']['chado_table'];
- $chado_column = $field['settings']['chado_column'];
-
-
- $items = field_get_items($entity_type, $entity, $field_name);
- $temp = array();
- foreach ($items as $delta => $item) {
-
-
-
- foreach ($item as $item_name => $value) {
- $matches = array();
- if (preg_match('/^chado-(.*?)__(.*?)$/', $item_name, $matches)) {
- $table_name = $matches[1];
- $column_name = $matches[2];
- $temp[$table_name][$delta][$column_name] = $value;
- }
- }
-
-
-
- if ((!array_key_exists($chado_table, $temp) or
- !array_key_exists($delta, $temp[$chado_table]) or
- !array_key_exists($chado_column, $temp[$chado_table][$delta])) and
- array_key_exists('value', $items[$delta]) and
- !is_array($items[$delta]['value'])) {
- $temp[$chado_table][$delta][$chado_column] = $items[$delta]['value'];
- }
- }
-
- foreach ($temp as $table_name => $details) {
- foreach ($details as $delta => $list) {
- $new_fields[$table_name][] = $list;
- }
- }
- }
- return $new_fields;
- }
- function tripal_chado_field_storage_expand_field($item_name, $value) {
- $matches = array();
- if (preg_match('/^(.*?)--(.*?)$/', $item_name, $matches)) {
- $parent_item_name = $matches[1];
- $sub_item_name = $matches[2];
- $sub_item = tripal_chado_field_storage_expand_field($sub_item_name, $value);
- return array($parent_item_name => $sub_item);
- }
- else {
- return array($item_name => $value);
- }
- }
- function tripal_chado_field_storage_query($query) {
-
-
- $filters = array();
-
-
- foreach ($query->fieldConditions as $index => $condition) {
- $field = $condition['field'];
-
- if ($field['storage']['type'] != 'field_chado_storage') {
- continue;
- }
- $column = $condition['column'];
- $value = $condition['value'];
- $field_type = $field['type'];
- $field_module = $field['module'];
- $settings = $field['settings'];
- $chado_table = $settings['chado_table'];
- $chado_column = $settings['chado_column'];
-
- $subfields = explode('.', $column);
-
- if (count($subfields) > 1) {
-
-
-
-
-
- $subfield1 = tripal_get_chado_semweb_term($chado_table, $chado_column, array('return_object' => TRUE));
- $subfields[0] = strtolower(preg_replace('/ /', '_', $subfield1->name));
- $value = tripal_chado_field_storage_recurse_subfilters($chado_table, $subfields, $value);
- $value = array_shift($value);
- }
- else {
- $value = $condition['value'];
- }
-
- $operator = $condition['operator'] ? $condition['operator'] : '=';
- switch ($operator) {
- case '=':
- $filters[$chado_table][$chado_column] = $value;
- break;
- case '>':
- case '>=':
- case '<':
- case '<=':
- $filters[$chado_table][$chado_column] = array(
- 'op' => $operator,
- 'data' => $value,
- );
- break;
- case '<>':
- $filters[$chado_table][$chado_column] = array(
- 'op' => '<>',
- 'data' => $value,
- );
- break;
- case 'CONTAINS':
- $filters[$chado_table][$chado_column] = array(
- 'op' => 'LIKE',
- 'data' => '%' . $value . '%',
- );
- break;
- case 'NOT':
- $filters[$chado_table][$chado_column] = array(
- 'op' => 'NOT LIKE',
- 'data' => '%' . $value . '%',
- );
- break;
- case 'STARTS WITH':
- $filters[$chado_table][$chado_column] = array(
- 'op' => 'LIKE',
- 'data' => $value . '%',
- );
- break;
- case 'NOT STARTS':
- $filters[$chado_table][$chado_column] = array(
- 'op' => 'NOT LIKE',
- 'data' => $value . '%',
- );
- break;
- case 'ENDS WITH':
- $filters[$chado_table][$chado_column] = array(
- 'op' => 'LIKE',
- 'data' => '%' . $value,
- );
- break;
- case 'NOT ENDS':
- $filters[$chado_table][$chado_column] = array(
- 'op' => 'NOT LIKE',
- 'data' => '%' . $value,
- );
- break;
- default:
-
- break;
- }
- }
-
- foreach ($query->order as $index => $sort) {
- $field = $sort['specifier']['field'];
-
- if ($field['storage']['type'] != 'field_chado_storage') {
- continue;
- }
- $direction = $sort['direction'];
- $field_type = $field['type'];
- $field_module = $field['module'];
- $settings = $field['settings'];
- $chado_table = $settings['chado_table'];
- $chado_column = $settings['chado_column'];
- $sorting[$chado_table][$chado_column] = $direction;
- }
-
- $entity_ids = array();
- foreach ($filters as $chado_table => $values) {
-
-
-
- $schema = chado_get_schema($chado_table);
- $pkey = $schema['primary key'][0];
- $results = chado_select_record($chado_table, array($pkey), $values);
- $record_ids = array();
- foreach ($results as $result) {
- $record_ids[] = $result->$pkey;
- }
-
- $filter_ids = array();
- if (count($record_ids) > 0) {
- $select = db_select('chado_entity', 'CE');
- $select->join('tripal_entity', 'TE', 'TE.id = CE.entity_id');
- $select->fields('CE', array('entity_id'));
- $select->fields('TE', array('bundle'));
- $select->condition('record_id', $record_ids);
-
- if (array_key_exists('bundle', $query->entityConditions)) {
- $select->condition('bundle', $query->entityConditions['bundle']);
- }
- $results = $select->execute();
- while ($result = $results->fetchObject()) {
- $entity_ids[] = array($result->entity_id, 0, $result->bundle);
- }
- }
- }
- $result = array(
- 'TripalEntity' => array()
- );
- foreach ($entity_ids as $ids) {
- $result['TripalEntity'][$ids[0]] = entity_create_stub_entity('TripalEntity', $ids);
- }
- return $result;
- }
- function tripal_chado_field_storage_recurse_subfilters($chado_table, $subfields, $value) {
- $sub_value = array();
-
- $subfield = array_shift($subfields);
-
- $columns = db_select('chado_semweb', 'CS')
- ->fields('CS', array('chado_column', 'cvterm_id'))
- ->condition('chado_table', $chado_table)
- ->execute();
-
-
- $chado_column = '';
- while($column = $columns->fetchObject()) {
- $cvterm_id = $column->cvterm_id;
- $cvterm = tripal_get_cvterm(array('cvterm_id' => $cvterm_id));
-
-
- $term_name = strtolower(preg_replace('/ /', '_', $cvterm->name));
- if ($subfield == $term_name) {
- $chado_column = $column->chado_column;
- }
- }
-
-
- if (count($subfields) > 0) {
-
- $schema = chado_get_schema($chado_table);
- $fkeys = $schema['foreign keys'];
-
- foreach ($fkeys as $fk_table => $details) {
- foreach ($details['columns'] as $lkey => $rkey) {
- if ($lkey == $chado_column) {
- $sub_value = tripal_chado_field_storage_recurse_subfilters($fk_table, $subfields, $value);
- return array($chado_column => $sub_value);
- }
- }
- }
- }
- return array($chado_column => $value);
- }
|