12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427 |
- <?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(
- 'tripal_storage_api' => TRUE,
- ),
-
-
-
-
- '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 = chado_get_dbxref(array('accession' => $term->accession, 'db_id' => array('name' => $term->vocab->vocabulary)));
- $cvterm = chado_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;
- $linker = property_exists($entity, 'chado_linker') ? $entity->chado_linker : '';
- $base_schema = chado_get_schema($base_table);
- $base_pkey = $base_schema['primary key'][0];
-
- list($field_vals, $field_items) = tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $entity);
-
-
-
- $values = $field_vals[$base_table];
- if ($record_id) {
- $values[$base_pkey] = $record_id;
- }
- elseif ($type_field and !$linker) {
- $values[$type_field] = $cvterm->cvterm_id;
- }
- $base_record_id = tripal_chado_field_storage_write_table($base_table, $values, $base_table);
- if (!$base_record_id) {
- throw new Exception('Unable to write fields to Chado: ' . print_r($field_items, TRUE));
- }
-
- if ($op == FIELD_STORAGE_INSERT) {
-
- $chado_entity_table = chado_get_bundle_entity_table($bundle);
- $record_id = db_insert($chado_entity_table)
- ->fields(array(
- 'entity_id' => $entity->id,
- 'record_id' => $base_record_id
- ))
- ->execute();
- if (!$record_id) {
- throw new Exception('Unable to insert new Chado entity.');
- }
- }
-
- 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, $base_table, $base_pkey, $base_record_id);
- }
- }
- }
- function tripal_chado_field_storage_write_table($table_name, $values, $base_table, $base_pkey = NULL, $base_record_id = NULL) {
- $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;
- }
-
-
-
- if (array_key_exists($base_table, $fkeys) and $base_table != $table_name) {
- foreach ($fkeys[$base_table]['columns'] as $lkey => $rkey) {
- if ($rkey == $base_pkey and !array_key_exists($lkey, $values) or empty($values[$lkey])) {
- $values[$lkey] = $base_record_id;
- }
- }
- }
-
-
- $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 . '": ' . print_r($new_vals, TRUE));
- }
- 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_pre_load($entity_type, $entities, $age,
- $fields, $options) {
-
- foreach ($entities as $id => $entity) {
-
- if ($entity_type != 'TripalEntity') {
- continue;
- }
- $record_id = NULL;
- if (property_exists($entity, 'chado_table')) {
-
- $base_table = $entity->chado_table;
- $type_field = $entity->chado_column;
- $record_id = $entity->chado_record_id;
- }
- else {
- $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
- $base_table = $bundle->data_table;
- $type_field = $bundle->type_column;
-
- $chado_entity_table = chado_get_bundle_entity_table($bundle);
- $details = db_select($chado_entity_table, 'ce')
- ->fields('ce')
- ->condition('entity_id', $entity->id)
- ->execute()
- ->fetchObject();
- if ($details) {
- $record_id = isset($details->record_id) ? $details->record_id : '';
- }
- $entity->chado_table = $base_table;
- $entity->chado_record_id = $record_id;
- $entity->chado_column = $type_field;
- }
- if ($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;
- }
- }
- }
- 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) {
- $base_table = $entity->chado_table;
- $type_field = $entity->chado_column;
- $record_id = $entity->chado_record_id;
- $record = $entity->chado_record;
- $schema = chado_get_schema($base_table);
-
-
- $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'];
-
-
-
-
- $instance = field_info_instance($entity_type, $field_name, $entity->bundle);
- if (!$instance) {
- continue;
- }
-
- if (!array_key_exists('settings', $instance) or
- !array_key_exists('chado_table', $instance['settings'])) {
- continue;
- }
-
- $field_table = $instance['settings']['chado_table'];
- $field_column = $instance['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)) {
- $fkey_column = $field_column;
- foreach($schema['foreign keys'] as $table => $fk_details) {
- foreach($fk_details['columns'] as $lfkey => $rfkey) {
- if ($lfkey == $field_column) {
- $fkey_column = $rfkey;
- }
- }
- }
- $entity->{$field_name}['und'][0]['chado-' . $field_table . '__' . $field_column] = $record->$field_column->$fkey_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_class($field_type);
- if (class_exists($field_type) and is_subclass_of($field_type, 'TripalField')) {
- $tfield = new $field_type($field, $instance);
- $tfield->load($entity, array('record' => $record));
- }
-
-
- else {
- if ($schema['fields'][$field_column]['type'] == 'text') {
- $record = chado_expand_var($record, 'field', "$field_table.$field_column");
- $entity->{$field_name}['und'][0]['value'] = $record->$field_column;
-
-
- if (array(key_exists('text_processing', $instance['settings']) and
- $instance['settings']['text_processing'] == 1)) {
-
-
- $entity->{$field_name}['und'][0]['format'] = array_key_exists('format', $instance['settings']) ? $instance['settings']['format'] : 'full_html';
- }
- }
- }
- }
-
-
-
- if ($field_table != $base_table) {
-
- $entity->{$field_name}['und'][0]['value'] = '';
- tripal_load_include_field_class($field_type);
- if (class_exists($field_type) && method_exists($field_type, 'load')) {
- $tfield = new $field_type($field, $instance);
- $tfield->load($entity, array('record' => $record));
- }
- }
- }
- }
- }
- function tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $entity) {
- $all_fields = array();
- $base_fields = array();
- $field_items = array();
-
-
- foreach ($fields as $field_id => $ids) {
-
- $field = field_info_field_by_id($field_id);
- $field_name = $field['field_name'];
- $instance = field_info_instance('TripalEntity', $field['field_name'], $entity->bundle);
-
-
-
- if (!array_key_exists('chado_table', $instance['settings'])) {
- continue;
- }
- $chado_table = $instance['settings']['chado_table'];
- $chado_column = $instance['settings']['chado_column'];
- $base_table = $instance['settings']['base_table'];
-
-
- $items = field_get_items($entity_type, $entity, $field_name);
- $temp = array();
- $field_items[$field_name] = $items;
- foreach ($items as $delta => $item) {
-
-
-
- $value_set = FALSE;
- foreach ($item as $item_name => $value) {
- $matches = array();
- if (preg_match('/^chado-(.*?)__(.*?)$/', $item_name, $matches)) {
- $table_name = $matches[1];
- $column_name = $matches[2];
-
-
- if ($table_name == $base_table) {
- $base_fields[$table_name][$column_name] = $value;
- }
- else {
- $temp[$table_name][$delta][$column_name] = $value;
- }
- $value_set = TRUE;
- }
- }
-
-
-
- if (!$value_set and array_key_exists('value', $items[$delta]) and
- !is_array($items[$delta]['value'])) {
-
-
- if ($base_table == $chado_table) {
- $base_fields[$chado_table][$chado_column] = $item['value'];
- }
- else {
- $temp[$chado_table][$delta][$chado_column] = $item['value'];
- }
- }
- }
-
- foreach ($temp as $table_name => $details) {
- foreach ($details as $delta => $list) {
- $all_fields[$table_name][] = $list;
- }
- }
- }
- $all_fields = array_merge($base_fields, $all_fields);
- return [$all_fields, $field_items];
- }
- function tripal_chado_field_storage_query($query) {
-
- $result = array(
- 'TripalEntity' => array()
- );
-
-
-
- if (!array_key_exists('bundle', $query->entityConditions)) {
- return $result;
- }
- $bundle = tripal_load_bundle_entity(array('name' => $query->entityConditions['bundle']));
- if (!$bundle) {
- return;
- }
- $data_table = $bundle->data_table;
- $type_column = $bundle->type_column;
- $type_id = $bundle->type_id;
- $schema = chado_get_schema($data_table);
- $pkey = $schema['primary key'][0];
-
- $cquery = chado_db_select($data_table, 'base');
- $cquery->fields('base', array($pkey));
-
-
- foreach ($query->fieldConditions as $index => $condition) {
- $field = $condition['field'];
- $field_name = $field['field_name'];
- $field_type = $field['type'];
-
- if ($field['storage']['type'] != 'field_chado_storage') {
- continue;
- }
-
-
-
- foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
-
-
- if (array_key_exists('bundle', $query->entityConditions)) {
- if (strtolower($query->entityConditions['bundle']['operator']) == 'in' and
- !array_key_exists($bundle_name, $query->entityConditions['bundle']['value'])) {
- continue;
- }
- else if ($query->entityConditions['bundle']['value'] != $bundle_name) {
- continue;
- }
- }
-
- $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle_name);
- if (tripal_load_include_field_class($field_type)) {
- $field_obj = new $field_type($field, $instance);
- $field_obj->query($cquery, $condition);
- }
-
-
- else {
- $alias = $field['field_name'];
- $chado_table = $instance['settings']['chado_table'];
- $base_table = $instance['settings']['base_table'];
- $bschema = chado_get_schema($base_table);
- $bpkey = $bschema['primary key'][0];
- if ($chado_table == $base_table) {
-
-
- $base_field = chado_get_semweb_column($chado_table, $condition['column']);
- $matches = array();
- $key = $condition['value'];
- $op = array_key_exists('operator', $condition) ? $condition['operator'] : '=';
- if (preg_match('/^(.+);(.+)$/', $key, $matches)) {
- $key = $matches[1];
- $op = $matches[2];
- }
- switch ($op) {
- case 'eq':
- $op = '=';
- break;
- case 'gt':
- $op = '>';
- break;
- case 'gte':
- $op = '>=';
- break;
- case 'lt':
- $op = '<';
- break;
- case 'lte':
- $op = '<=';
- break;
- case '<>':
- case 'ne':
- $op = '!=';
- break;
- case 'LIKE':
- case 'contains':
- $op = 'LIKE';
- if (!preg_match('/\%/', $key)) {
- $key = '%' . $key . '%';
- }
- break;
- case 'NOT LIKE':
- $op = 'NOT LIKE';
- if (!preg_match('/\%/', $key)) {
- $key = '%' . $key . '%';
- }
- break;
- case 'starts':
- $op = 'LIKE';
- $key = $key . '%';
- break;
- default:
- $op = '=';
- }
- $cquery->condition('base.' . $base_field , $key, $op);
- }
- if ($chado_table != $base_table) {
-
-
-
- }
- }
- }
- }
-
- $chado_entity_table = chado_get_bundle_entity_table($bundle);
- $cquery->join($chado_entity_table, 'CE', "CE.record_id = base.$pkey");
- $cquery->join('tripal_entity', 'TE', "CE.entity_id = TE.id");
- $cquery->fields('CE', array('entity_id'));
- $cquery->fields('TE', array('bundle'));
- if (array_key_exists('start', $query->range)) {
- $cquery->range($query->range['start'], $query->range['length']);
- }
-
- $cquery->condition('TE.bundle', $query->entityConditions['bundle']['value']);
-
-
- foreach ($query->propertyConditions as $index => $condition) {
- $cquery->condition('TE.' . $condition['column'], $condition['value']);
- }
-
- if (array_key_exists('entity_id', $query->entityConditions)) {
- $value = $query->entityConditions['entity_id']['value'];
- $op = '';
- if (array_key_exists('op', $query->entityConditions['entity_id'])) {
- $op = $query->entityConditions['entity_id']['op'];
- }
-
- if (!is_array($value)) {
- switch ($op) {
- case 'CONTAINS':
- $op = 'LIKE';
- $value = '%' . $value . '%';
- break;
- case 'STARTS_WITH':
- $op = 'LIKE';
- $value = $value . '%';
- break;
- case 'BETWEEN':
-
-
- $op = '=';
- default:
- $op = $op ? $op : '=';
- }
- }
-
- else {
- switch ($op) {
- case 'CONTAINS':
- $op = 'IN';
- break;
- case 'STARTS_WITH':
- $op = 'IN';
- break;
- case 'BETWEEN':
- $op = 'BETWEEN';
- default:
- $op = 'IN';
- }
- }
- if ($op == 'BETWEEN') {
- $cquery->condition('TE.id', $value[0], '>');
- $cquery->condition('TE.id', $value[1], '<');
- }
- else {
- $cquery->condition('TE.id', $value, $op);
- }
- }
-
- foreach ($query->order as $index => $sort) {
-
- if ($sort['type'] == 'property') {
-
- }
-
- if ($sort['type'] == 'field') {
- $field = $sort['specifier']['field'];
- $field_type = $field['type'];
- $field_name = $field['field_name'];
-
- if ($field['storage']['type'] != 'field_chado_storage') {
- continue;
- }
- $column = $sort['specifier']['column'];
- $direction = $sort['direction'];
-
-
-
- foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
-
-
- if (array_key_exists('bundle', $query->entityConditions)) {
- if (strtolower($query->entityConditions['bundle']['operator']) == 'in' and
- !array_key_exists($bundle_name, $query->entityConditions['bundle']['value'])) {
- continue;
- }
- else if ($query->entityConditions['bundle']['value'] != $bundle_name) {
- continue;
- }
- }
-
-
- $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
- if (tripal_load_include_field_class($field_type)) {
- $field_obj = new $field_type($field, $instance);
- $field_obj->queryOrder($cquery, array('column' => $column, 'direction' => $direction));
- }
-
- else {
- $base_table = $instance['settings']['base_table'];
- $chado_table = $instance['settings']['chado_table'];
- $table_column = chado_get_semweb_column($chado_table, $column);
- if ($table_column) {
- if ($chado_table == $base_table) {
- $cquery->orderBy('base.' . $table_column, $direction);
- }
- else {
-
-
-
- }
- }
- else {
-
- }
- }
- }
- }
- }
-
-
-
-
-
- if (property_exists($query, 'deleted') and $query->deleted) {
-
-
- $cquery->where('1=0');
- }
-
-
-
- $records = $cquery->execute();
-
- $result = array();
- while ($record = $records->fetchObject()) {
- $ids = array($record->entity_id, 0, $record->bundle);
- $result['TripalEntity'][$record->entity_id] = entity_create_stub_entity('TripalEntity', $ids);
- }
- return $result;
- }
- function tripal_chado_field_storage_bundle_mapping_form($form, &$form_state,
- $term, &$submit_disabled) {
- $selected_term_id = (is_object($term)) ? $term->cvterm_id : NULL;
-
- $form = array();
-
- $default = array(
- 'table' => '',
- 'has_all' => 'No',
- 'use_cvterm' => 'cv',
- 'cv_id' => '',
- 'use_linker' => 'Yes',
- 'use_prop' => 'Yes',
- 'type_column' => '',
- 'prop_term_name' => '',
- 'prop_term_value' => '',
- );
- if (array_key_exists('base_chado_table', $form_state['values'])
- and $form_state['values']['base_chado_table']) {
- $default['table'] = $form_state['values']['base_chado_table'];
- }
- else {
- $mapped_table = chado_get_cvterm_mapping(array('cvterm_id' => $selected_term_id));
- if ($mapped_table) {
- $default['table'] = $mapped_table->chado_table;
- }
- }
- if (array_key_exists('chado_table_has_all', $form_state['values'])
- and $form_state['values']['chado_table_has_all']) {
- $default['has_all'] = $form_state['values']['chado_table_has_all'];
- }
- if (array_key_exists('chado_type_use_linker', $form_state['values'])
- and $form_state['values']['chado_type_use_linker']) {
- $default['use_linker'] = $form_state['values']['chado_type_use_linker'];
- }
- if (array_key_exists('chado_type_use_prop', $form_state['values'])
- and $form_state['values']['chado_type_use_prop']) {
- $default['use_prop'] = $form_state['values']['chado_type_use_prop'];
- }
- if (array_key_exists('type_column', $form_state['values'])
- and $form_state['values']['type_column']) {
- $default['type_column'] = $form_state['values']['type_column'];
- }
- if (array_key_exists('type_column_ignore', $form_state['values'])
- and $form_state['values']['type_column_ignore']) {
- $default['type_column_ignore'] = $form_state['values']['type_column_ignore'];
- }
- if (array_key_exists('prop_term_name', $form_state['values'])
- and $form_state['values']['prop_term_name']) {
- $default['prop_term_name'] = $form_state['values']['prop_term_name'];
- }
- if (array_key_exists('prop_term_value', $form_state['values'])
- and $form_state['values']['prop_term_value']) {
- $default['prop_term_value'] = $form_state['values']['prop_term_value'];
- }
- if (array_key_exists('chado_type_use_cv', $form_state['values'])
- and $form_state['values']['chado_type_use_cv']) {
- $default['use_cvterm'] = $form_state['values']['chado_type_use_cv'];
- }
- if (array_key_exists('chado_type_cv_id', $form_state['values'])
- and $form_state['values']['chado_type_cv_id']) {
- $default['cv_id'] = $form_state['values']['chado_type_cv_id'];
- }
- $form['selected_cvterm_id'] = array(
- '#type' => 'value',
- '#value' => $selected_term_id,
- );
- $base_tables = chado_get_base_tables();
- $options = array(0 => '-- Select table --');
- foreach ($base_tables AS $tablename) {
- $options[$tablename] = $tablename;
- }
- $form['base_chado_table'] = array(
- '#type' => 'select',
- '#title' => 'Chado table',
- '#options' => $options,
- '#description' => 'Select the Chado table into which the primary records for this content type will be stored.',
- '#default_value' => $default['table'],
- '#ajax' => array(
- 'callback' => "tripal_admin_add_type_form_ajax_callback",
- 'wrapper' => "tripal-vocab-select-form",
- 'effect' => 'fade',
- 'method' => 'replace'
- ),
- );
- $form_state['input']['base_chado_table'] = $default['table'];
-
- if (!$default['table']) {
- return $form;
- }
-
- $schema = chado_get_schema($default['table']);
-
- if ($default['table'] == 'cvterm') {
- tripal_chado_field_storage_bundle_mapping_form_add_cvterm($form,
- $form_state, $term, $default);
- if ($default['use_cvterm'] == 'cv' and $default['cv_id']) {
- $submit_disabled = FALSE;
- }
- if ($default['use_cvterm'] == 'parent') {
- $submit_disabled = FALSE;
- }
- return $form;
- }
-
- tripal_chado_field_storage_bundle_mapping_form_add_allrecs($form,
- $form_state, $term, $default);
-
- if ($default['has_all'] == 'Yes') {
- $submit_disabled = FALSE;
- return $form;
- }
-
-
- tripal_chado_field_storage_bundle_mapping_form_add_type($form,
- $form_state, $term, $default);
-
-
- if (!empty($default['type_column'])) {
- $submit_disabled = FALSE;
- return $form;
- }
-
-
-
- if (!$default['type_column_ignore'] and empty($default['type_column'])) {
- return $form;
- }
-
-
- $linker_table = $default['table'] . '_cvterm';
- $prop_table = $default['table']. 'prop';
- $linker_exists = chado_table_exists($linker_table);
- $prop_exists = chado_table_exists($prop_table);
-
- if ($prop_exists) {
- tripal_chado_field_storage_bundle_mapping_form_add_prop($form,
- $form_state, $term, $prop_table, $default);
-
- if ($default['use_prop'] == 'Yes') {
- $submit_disabled = FALSE;
- return $form;
- }
-
- if ($linker_exists) {
- tripal_chado_field_storage_bundle_mapping_form_add_linker($form,
- $form_state, $term, $linker_table, $default);
-
- if ($default['use_linker'] == 'Yes') {
- $submit_disabled = FALSE;
- return $form;
- }
- }
- }
-
-
- else if ($linker_exists) {
- tripal_chado_field_storage_bundle_mapping_form_add_prop($form,
- $form_state, $term, $prop_table, $default);
-
- if ($default['use_linker'] == 'Yes') {
- $submit_disabled = FALSE;
- return $form;
- }
- }
- $form['notice'] = array(
- '#type' => 'item',
- '#markup' => '<font color="red">Unfortunately, the options selected above do not allow for mapping of this content type to records in Chado.</font>'
- );
- return $form;
- }
- function tripal_chado_field_storage_bundle_mapping_form_add_type(&$form,
- &$form_state, $term, &$default){
- $term_name = $term->name;
-
- $schema = chado_get_schema($default['table']);
- $column_options = array('none' => '--None--');
- $cvt_fkeys = (isset($schema['foreign keys']['cvterm'])) ? array_keys($schema['foreign keys']['cvterm']['columns']) : array();
- foreach ($schema['fields'] as $column_name => $column_details) {
- if (in_array($column_name, $cvt_fkeys)) {
- $column_options[$column_name] = $column_name;
- }
- }
-
- if (count($column_options) == 1) {
- $default['type_column'] = '';
- return;
- }
-
- $default_column_ignore = !empty($default['type_column_ignore']) ? $default['type_column_ignore'] : 0;
- $default_column = !empty($default['type_column']) ? $default['type_column'] : 'type_id';
-
- $form['type_column'] = array(
- '#type' => 'select',
- '#title' => 'Type Column',
- '#options' => $column_options,
- '#description' => 'Please select the column in the "' .
- $default['table'] . '" table that will identify a record as being of type "' .
- $term_name . '". If you select "--None--" then you will be asked
- if you the type can be identified using a property or linker table.
- Only fields that have a foreign key to the cvterm table will be listed.',
- '#default_value' => $default_column,
- '#ajax' => array(
- 'callback' => "tripal_admin_add_type_form_ajax_callback",
- 'wrapper' => "tripal-vocab-select-form",
- 'effect' => 'fade',
- 'method' => 'replace'
- ),
- '#disabled' => $default_column_ignore,
- );
- $form['type_column_ignore'] = array(
- '#type' => 'checkbox',
- '#title' => 'The type column does not distinguish the type',
- '#default_value' => $default_column_ignore,
- '#ajax' => array(
- 'callback' => "tripal_admin_add_type_form_ajax_callback",
- 'wrapper' => "tripal-vocab-select-form",
- 'effect' => 'fade',
- 'method' => 'replace'
- ),
- );
-
- $default['type_column'] = $default_column;
- if ($default['type_column'] == 'none') {
- $default['type_column'] = '';
- }
- }
- function tripal_chado_field_storage_bundle_mapping_form_add_allrecs(&$form,
- &$form_state, $term, $default) {
- $term_name = $term->name;
-
- $form['chado_table_has_all'] = array(
- '#type' => 'radios',
- '#options' => array(
- 'Yes' => 'Yes',
- 'No' => 'No'
- ),
- '#title' => 'Are all records in the "' . $default['table'] .
- '" table of type "'. $term_name . '"?',
- '#description' => 'Select "No" if the "' .
- $default['table'] . '" table houses more than just data of type "' .
- $term_name . '".',
- '#default_value' => $default['has_all'],
- '#ajax' => array(
- 'callback' => "tripal_admin_add_type_form_ajax_callback",
- 'wrapper' => "tripal-vocab-select-form",
- 'effect' => 'fade',
- 'method' => 'replace'
- ),
- );
- }
- function tripal_chado_field_storage_bundle_mapping_form_add_linker(&$form,
- &$form_state, $term, $linker_table, $default){
- $form['chado_type_use_linker'] = array(
- '#type' => 'radios',
- '#title' => 'Do you want to use the "' . $linker_table . '" table
- to distinguish between content types?',
- '#options' => array(
- 'Yes' => 'Yes',
- 'No' => 'No'
- ),
- '#description' => t('Sometimes records can be distringuished
- using a linker table, especially if there is no column
- in the specified Chado table to identify the
- record type. In these cases the linker table can be used.'),
- '#default_value' => $default['use_linker'],
- '#ajax' => array(
- 'callback' => "tripal_admin_add_type_form_ajax_callback",
- 'wrapper' => "tripal-vocab-select-form",
- 'effect' => 'fade',
- 'method' => 'replace'
- ),
- );
- }
- function tripal_chado_field_storage_bundle_mapping_form_add_prop(&$form,
- &$form_state, $term, $prop_table, $default){
- $form['chado_type_use_prop'] = array(
- '#type' => 'radios',
- '#title' => 'Do you want to use the "' . $prop_table . '" table
- to distinguish between content types?',
- '#options' => array(
- 'Yes' => 'Yes',
- 'No' => 'No'
- ),
- '#description' => t('Sometimes records can be distringuished
- using a value in property table, especially if there is no column
- in the specified Chado table to identify the
- record type. In these cases the property table can be used.'),
- '#default_value' => $default['use_prop'],
- '#ajax' => array(
- 'callback' => "tripal_admin_add_type_form_ajax_callback",
- 'wrapper' => "tripal-vocab-select-form",
- 'effect' => 'fade',
- 'method' => 'replace'
- ),
- );
- if ($default['use_prop'] == 'Yes') {
- $prop_term_name = $default['prop_term_name'];
- $prop_term_value = $default['prop_term_value'];
- $form['prop_term_name'] = array(
- '#title' => t('Property Type'),
- '#type' => 'textfield',
- '#description' => t('The content type "' . $term->name . '" must be
- associated with a property type. The property type must be the
- name of a term in a controlled vocabulary and the controlled
- vocabulary should already be loaded into Tripal. Please select
- the property type that will be used to identify records of this
- type'),
- '#required' => TRUE,
- '#default_value' => $prop_term_name,
- '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
- );
- $form['prop_term_select_button'] = array(
- '#type' => 'submit',
- '#value' => t('Lookup Term'),
- '#name' => 'select_prop_cvterm',
- '#ajax' => array(
- 'callback' => "tripal_admin_add_type_form_ajax_callback",
- 'wrapper' => "tripal-vocab-select-form",
- 'effect' => 'fade',
- 'method' => 'replace'
- ),
- );
-
-
- if ($prop_term_name) {
- $form['prop_terms_list'] = array(
- '#type' => 'fieldset',
- '#title' => t('Matching Terms'),
- '#description' => t('Please select the term the best matches the
- property type.')
- );
- $match = array(
- 'name' => $prop_term_name,
- );
- $pterms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
- $pterms = chado_expand_var($pterms, 'field', 'cvterm.definition');
- $num_terms = 0;
-
-
- foreach ($pterms as $pterm) {
-
-
- $default = FALSE;
- $attrs = array();
- if ($num_terms == 0 and count($pterms) == 1) {
- $default = TRUE;
- $attrs = array('checked' => 'checked');
- }
- $term_element_name = 'prop_term-' . $pterm->cvterm_id;
- $form['prop_terms_list'][$term_element_name] = array(
- '#type' => 'checkbox',
- '#title' => $pterm->name,
- '#default_value' => $default,
- '#attributes' => $attrs,
- '#description' => '<b>Vocabulary:</b> ' . $pterm->cv_id->name . ' (' . $pterm->dbxref_id->db_id->name . ') ' . $pterm->cv_id->definition .
- '<br><b>Term: </b> ' . $pterm->dbxref_id->db_id->name . ':' . $pterm->dbxref_id->accession . '. ' .
- '<br><b>Definition:</b> ' . $pterm->definition,
- '#ajax' => array(
- 'callback' => "tripal_admin_add_type_form_ajax_callback",
- 'wrapper' => "tripal-vocab-select-form",
- 'effect' => 'fade',
- 'method' => 'replace'
- ),
- );
- $num_terms++;
- }
- $form['prop_term_value'] = array(
- '#type' => 'textfield',
- '#title' => 'Property Value',
- '#description' => t('All properties have a type and a value. Above you
- indicated the type of property. Now you must specify the value
- that this properly must have in order to be considered of
- type "' . $term->name . '".'),
- '#default_value' => $prop_term_value,
- '#required' => TRUE,
- );
- }
- }
- }
- function tripal_chado_field_storage_bundle_mapping_form_add_cvterm(&$form,
- &$form_state, $term, &$default){
- $form['chado_type_use_cv'] = array(
- '#type' => 'radios',
- '#title' => 'How do you want to distinguish the "' . $term->name . '" records
- within the cvterm table?',
- '#options' => array(
- 'cv' => 'All records that belong to a single controlled vocabulary?',
- 'parent' => 'All child records of the specified term?'
- ),
- '#description' => t('Records in the cvterm table are often not used for content
- types. The records in the cvterm table are meant to be vocabulary terms.
- However, there are times when records in the cvterm table can be
- used for a content type. One example is for trait "pages" that
- use phenotype values stored in the phenotype table. Because records in
- the cvterm table are vocabulary terms they do not have "types" so the
- only ways to distinguish them are 1) to use the controlled vocabulary to
- distinguish the records or 2) to use all children records of the
- selected term. Please select the appropriate value for this case.'),
- '#default_value' => $default['use_cvterm'],
- '#ajax' => array(
- 'callback' => "tripal_admin_add_type_form_ajax_callback",
- 'wrapper' => "tripal-vocab-select-form",
- 'effect' => 'fade',
- 'method' => 'replace'
- ),
- );
- if ($default['use_cvterm'] == 'cv') {
- $cvs = chado_get_cv_select_options();
- $form['chado_type_cv_id'] = array(
- '#type' => select,
- '#options' => $cvs,
- '#title' => t('Select a controlled vocabulary'),
- '#default_value' => $default['cv_id'],
- '#ajax' => array(
- 'callback' => "tripal_admin_add_type_form_ajax_callback",
- 'wrapper' => "tripal-vocab-select-form",
- 'effect' => 'fade',
- 'method' => 'replace'
- ),
- );
- }
- }
- function tripal_chado_field_storage_bundle_mapping_form_validate($form, &$form_state) {
-
- $default = array(
- 'table' => '',
- 'has_all' => 'No',
- 'use_cvterm' => 'cv',
- 'cv_id' => '',
- 'use_linker' => 'Yes',
- 'use_prop' => 'Yes',
- 'type_column' => '',
- 'prop_term_name' => '',
- 'prop_term_value' => '',
- );
- if (array_key_exists('base_chado_table', $form_state['values'])
- and $form_state['values']['base_chado_table']) {
- $default['table'] = $form_state['values']['base_chado_table'];
- }
- if (array_key_exists('chado_table_has_all', $form_state['values'])
- and $form_state['values']['chado_table_has_all']) {
- $default['has_all'] = $form_state['values']['chado_table_has_all'];
- }
- if (array_key_exists('chado_type_use_linker', $form_state['values'])
- and $form_state['values']['chado_type_use_linker']) {
- $default['use_linker'] = $form_state['values']['chado_type_use_linker'];
- }
- if (array_key_exists('chado_type_use_prop', $form_state['values'])
- and $form_state['values']['chado_type_use_prop']) {
- $default['use_prop'] = $form_state['values']['chado_type_use_prop'];
- }
- if (array_key_exists('type_column', $form_state['values'])
- and $form_state['values']['type_column']) {
- $default['type_column'] = $form_state['values']['type_column'];
- }
- if (array_key_exists('prop_term_name', $form_state['values'])
- and $form_state['values']['prop_term_name']) {
- $default['prop_term_name'] = $form_state['values']['prop_term_name'];
-
- $num_selected = 0;
- foreach ($form_state['values'] as $key => $value) {
- $matches = array();
- if (preg_match("/^prop_term-(\d+)$/", $key, $matches) and
- $form_state['values']['prop_term-' . $matches[1]]) {
- $cvterm_id = $matches[1];
- $term = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
- $num_selected++;
- }
- }
- if ($num_selected == 0) {
- form_set_error('', 'Please select at least one property term.');
- }
- else if ($num_selected > 1) {
- form_set_error('prop_term-' . $cvterm_id, 'Please select only one property term from the list below.');
- }
- $form_state['prop_term'] = $term;
- }
- if (array_key_exists('prop_term_value', $form_state['values'])
- and $form_state['values']['prop_term_value']) {
- $default['prop_term_value'] = $form_state['values']['prop_term_value'];
- }
- if (array_key_exists('chado_type_use_cv', $form_state['values'])
- and $form_state['values']['chado_type_use_cv']) {
- $default['use_cvterm'] = $form_state['values']['chado_type_use_cv'];
- }
- if (array_key_exists('chado_type_cv_id', $form_state['values'])
- and $form_state['values']['chado_type_cv_id']) {
- $default['cv_id'] = $form_state['values']['chado_type_cv_id'];
- }
-
- if (!$default['table']) {
- form_set_error('base_chado_table', 'Please select a default table.');
- }
- }
- function tripal_chado_field_storage_bundle_mapping_form_submit($form,
- &$form_state, $term, &$storage_args) {
-
- $default = array(
- 'table' => '',
- 'has_all' => 'No',
- 'use_cvterm' => '',
- 'cv_id' => '',
- 'use_linker' => 'No',
- 'use_prop' => 'No',
- 'type_column' => '',
- 'prop_term_name' => '',
- 'prop_term_value' => '',
- );
- if (array_key_exists('base_chado_table', $form_state['values'])
- and $form_state['values']['base_chado_table']) {
- $default['table'] = $form_state['values']['base_chado_table'];
- }
- if (array_key_exists('chado_table_has_all', $form_state['values'])
- and $form_state['values']['chado_table_has_all']) {
- $default['has_all'] = $form_state['values']['chado_table_has_all'];
- }
- if (array_key_exists('chado_type_use_linker', $form_state['values'])
- and $form_state['values']['chado_type_use_linker']) {
- $default['use_linker'] = $form_state['values']['chado_type_use_linker'];
- }
- if (array_key_exists('chado_type_use_prop', $form_state['values'])
- and $form_state['values']['chado_type_use_prop']) {
- $default['use_prop'] = $form_state['values']['chado_type_use_prop'];
- }
- if (array_key_exists('type_column', $form_state['values'])
- and $form_state['values']['type_column']) {
- $default['type_column'] = $form_state['values']['type_column'];
- }
- if (array_key_exists('prop_term_name', $form_state['values'])
- and $form_state['values']['prop_term_name']) {
- $default['prop_term_name'] = $form_state['values']['prop_term_name'];
- }
- if (array_key_exists('prop_term_value', $form_state['values'])
- and $form_state['values']['prop_term_value']) {
- $default['prop_term_value'] = $form_state['values']['prop_term_value'];
- }
- if (array_key_exists('chado_type_use_cv', $form_state['values'])
- and $form_state['values']['chado_type_use_cv']) {
- $default['use_cvterm'] = $form_state['values']['chado_type_use_cv'];
- }
- if (array_key_exists('chado_type_cv_id', $form_state['values'])
- and $form_state['values']['chado_type_cv_id']) {
- $default['cv_id'] = $form_state['values']['chado_type_cv_id'];
- }
-
-
- if ($default['use_cvterm'] == 'cv' and !empty($default['cv_id'])) {
- $storage_args['data_table'] = $default['table'];
- $storage_args['type_id'] = $form_state['values']['selected_cvterm_id'];
- $storage_args['type_value'] = '';
- if ($default['cv_id']) {
- $storage_args['type_value'] = $default['cv_id'];
- }
- $storage_args['type_column'] = '';
- return;
- }
-
-
- if ($default['type_column'] and $default['type_column'] != 'none') {
- $storage_args['data_table'] = $default['table'];
- $storage_args['type_column'] = $default['type_column'];
- return;
- }
-
- if ($default['has_all'] == 'Yes') {
- $storage_args['data_table'] = $default['table'];
- $storage_args['type_id'] = $form_state['values']['selected_cvterm_id'];
- $storage_args['type_linker_table'] = '';
- $storage_args['type_column'] = '';
- $storage_args['type_value'] = '';
- }
-
-
- else if ($default['use_prop'] == 'Yes') {
- $storage_args['data_table'] = $default['table'];
- $storage_args['type_linker_table'] = $default['table'] . 'prop';
- $storage_args['type_column'] = 'type_id';
- $storage_args['type_id'] = $form_state['prop_term']->cvterm_id;
- $storage_args['type_value'] = $default['prop_term_value'];
- }
-
-
- else if ($default['use_linker'] == 'Yes') {
- $storage_args['data_table'] = $default['table'];
- $storage_args['type_id'] = $form_state['values']['selected_cvterm_id'];
- $storage_args['type_linker_table'] = $default['table'] . '_cvterm';
- $storage_args['type_column'] = 'cvterm_id';
- }
- }
|