123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269 |
- <?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];
- 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) {
-
- $chado_entity_table = chado_get_bundle_entity_table($bundle);
- $record = array(
- 'entity_id' => $entity->id,
- 'record_id' => $base_record_id,
- );
- $success = drupal_write_record($chado_entity_table, $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_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();
-
-
- 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();
- 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;
- }
- 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 = tripal_get_chado_semweb_column($chado_table, $condition['column']);
- $cquery->condition('base.' . $base_field , $condition['value'], $condition['operator']);
- }
- 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->order as $index => $sort) {
-
- if ($order['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 = tripal_get_chado_semweb_column($chado_table, $column);
- if ($table_column) {
- if ($chado_table == $base_table) {
- $cquery->orderBy('base.' . $table_column, $direction);
- }
- else {
-
-
-
- }
- }
- else {
-
- }
- }
- }
- }
- }
-
-
- $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 = $term->cvterm_id;
-
- $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('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;
- }
-
- $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 = array_keys($schema['foreign keys']['cvterm']['columns']);
- 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 = !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'
- ),
- );
-
- $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 = tripal_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']) {
- $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';
- }
- }
|