123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804 |
- <?php
- function tripal_chado_field_info() {
- $info = array();
-
- $fields_path = drupal_get_path('module', 'tripal_chado') . '/includes/fields';
- $field_files = file_scan_directory($fields_path, '/^chado_.*\.inc$/');
-
- foreach ($field_files as $file) {
- $field_type = $file->name;
- module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
- if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
- $field_obj = new $field_type();
- $info[$field_type] = $field_obj->field_info();
- }
- $function = $field_type . '_info';
- if (function_exists($function)) {
- $info[$field_type] = $function();
- }
- }
- return $info;
- }
- function tripal_chado_field_widget_info() {
- $widgets = array();
- $fields = field_info_fields();
- foreach ($fields as $field) {
- $field_type = $field['type'];
- if ($field['storage']['type'] == 'field_chado_storage') {
- module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
- if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
- $field_obj = new $field_type();
- $widgets[$field_type . '_widget'] = $field_obj->widget_info();
- }
- $function = $field_type . '_widget_info';
- if (function_exists($function)) {
- $widgets[$field_type . '_widget'] = $function();
- }
- }
- }
- return $widgets;
- }
- function tripal_chado_field_formatter_info() {
- $formatters = array();
- $fields = field_info_fields();
- foreach ($fields as $field) {
- $field_type = $field['type'];
- if ($field['storage']['type'] == 'field_chado_storage') {
- module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
- if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
- $field_obj = new $field_type();
- $formatters[$field_type . '_formatter'] = $field_obj->formatter_info();
- }
- $function = $field_type . '_formatter_info';
- if (function_exists($function)) {
- $formatters[$field_type . '_formatter'] = $function();
- }
- }
- }
- return $formatters;
- }
- function tripal_chado_field_settings_form($field, $instance, $has_data) {
- $form = '';
- $field_type = $field['type'];
- module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
- if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
- $field_obj = new $field_type();
- $form = $field_obj->settings_form($field, $instance, $has_data);
- }
- $function = $field_type . '_settings_form';
- if (function_exists($function)) {
- $form = $function($field, $instance, $has_data);
- }
- return $form;
- }
- function tripal_chado_field_formatter_settings_summary($field, $instance, $view_mode) {
- $summary = '';
- $field_type = $field['type'];
- module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
- if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
- $field = new $field_type();
- $summary = $field->formatter_settings_summary($field, $instance, $view_mode);
- }
- $function = $field_type . '_formatter_settings_summary';
- if (function_exists($function)) {
- $summary = $function($field, $instance, $view_mode);
- }
- return $summary;
- }
- function tripal_chado_field_formatter_settings_form($field, $instance,
- $view_mode, $form, &$form_state) {
- $element = array();
- $field_type = $field['type'];
- form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/' . $field_type);
- module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
- if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
- $field_obj = new $field_type();
- $element = $field_obj->formatter_settings_form($field, $instance, $view_mode, $form, $form_state);
- }
- $function = $field_type . '_formatter_settings_form';
- if (function_exists($function)) {
- $element = $function($field, $instance, $view_mode, $form, $form_state);
- }
- return $element;
- }
- function tripal_chado_field_formatter_view($entity_type, $entity, $field,
- $instance, $langcode, $items, $display) {
- $element = array();
- $field_type = $field['type'];
- module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
- if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
- $field_obj = new $field_type();
- $field_obj->formatter_view($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);
- }
- $function = $display['type'];
- if (function_exists($function)) {
- $function($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);
- }
- return $element;
- }
- function tripal_chado_field_widget_form(&$form, &$form_state, $field,
- $instance, $langcode, $items, $delta, $element) {
- $widget = $element;
- $field_name = $instance['field_name'];
- $field_type = $field['type'];
- form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/' . $field_type);
- module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_name);
- if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
- $field_obj = new $field_type();
- $field_obj->widget_form($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
- }
- $function = $field_type . '_widget';
- if (function_exists($function)) {
- $function($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
- }
- return $widget;
- }
- function tripal_chado_get_field_form_values($field_name, $form_state, $delta = 0, $child = NULL) {
- $value = NULL;
-
- if (!array_key_exists('values', $form_state)) {
- return $value;
- }
-
- if (!array_key_exists($field_name, $form_state['values'])) {
- return $value;
- }
-
- foreach ($form_state['values'][$field_name] as $langcode => $items) {
- if (!array_key_exists($delta, $items)) {
- continue;
- }
- $item = $items[$delta];
- if ($child){
- if(array_key_exists($child, $item) and $item[$child] != '') {
- $value = $item[$child];
- }
- }
- else {
- $value = $item['value'];
- }
- }
- return $value;
- }
- function tripal_chado_set_field_form_values($field_name, &$form_state, $newvalue, $delta = 0, $child = NULL) {
-
- if (!array_key_exists('values', $form_state)) {
- return FALSE;
- }
-
- if (!array_key_exists($field_name, $form_state['values'])) {
- return FALSE;
- }
- foreach ($form_state['values'][$field_name] as $langcode => $items) {
- if ($child) {
- $form_state['values'][$field_name][$langcode][$delta][$child] = $newvalue;
- }
- else {
- $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
- }
- }
- return TRUE;
- }
- function tripal_chado_field_widget_form_alter(&$element, &$form_state, $context) {
- if (array_key_exists('#field_name', $element)) {
- $field_name = $element['#field_name'];
- $matches = array();
- if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
- $tablename = $matches[1];
- $colname = $matches[2];
- $schema = chado_get_schema($tablename);
-
-
-
- if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
-
- $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
- $element['#date_items']['value'] = $element['#default_value']['value'];
- }
-
-
-
- if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
- $element['#theme_wrappers'] = array('tripal_chado_date_combo');
- }
- }
- }
- }
- function tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $schema, $column_name) {
- $details = $schema['fields'][$column_name];
-
- $field = array(
- 'field_type' => '',
- 'widget_type' => '',
- 'description' => '',
- 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
- 'is_required' => 0,
- 'storage' => 'field_chado_storage',
- 'widget_settings' => array(
- 'display_label' => 1
- ),
- 'field_settings' => array(
-
- 'chado_table' => $table_name,
-
- 'chado_column' => $column_name,
- 'semantic_web' => array(
-
- 'name' => '',
-
- 'accession' => '',
-
- 'ns' => '',
-
-
- 'nsurl' => '',
- ),
- ),
- );
-
- switch($details['type']) {
- case 'char':
- $field['field_type'] = 'text';
- $field['widget_type'] = 'text_textfield';
- $field['field_settings']['max_length'] = $details['length'];
- break;
- case 'varchar':
- $field['field_type'] = 'text';
- $field['widget_type'] = 'text_textfield';
- $field['field_settings']['max_length'] = $details['length'];
- break;
- case 'text':
- $field['field_type'] = 'text';
- $field['widget_type'] = 'text_textarea';
- $field['field_settings']['max_length'] = 17179869184;
- $field['field_settings']['text_processing'] = 1;
- $field['format'] = filter_default_format();
- break;
- case 'blob':
-
- continue;
- break;
- case 'int':
- $field['field_type'] = 'number_integer';
- $field['widget_type'] = 'number';
- break;
- case 'float':
- $field['field_type'] = 'number_float';
- $field['widget_type'] = 'number';
- $field['field_settings']['precision'] = 10;
- $field['field_settings']['scale'] = 2;
- $field['field_settings']['decimal_separator'] = '.';
- break;
- case 'numeric':
- $field['field_type'] = 'number_decimal';
- $field['widget_type'] = 'number';
- break;
- case 'serial':
-
- break;
- case 'boolean':
- $field['field_type'] = 'list_boolean';
- $field['widget_type'] = 'options_onoff';
- $field['field_settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
- break;
- case 'datetime':
-
- $field['field_type'] = 'datetime';
- $field['widget_type'] = 'date_select';
- $field['widget_settings']['increment'] = 1;
- $field['widget_settings']['tz_handling'] = 'none';
- $field['widget_settings']['collapsible'] = TRUE;
-
-
- break;
- }
-
- if ($column_name == 'name') {
- $field['field_settings']['semantic_web']['name'] = 'name';
- $field['field_settings']['semantic_web']['accession'] = 'name';
- $field['field_settings']['semantic_web']['ns'] = 'foaf';
- $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
- }
- if ($column_name == 'description' or $column_name == 'definition' or
- $column_name == 'comment') {
- $field['field_settings']['semantic_web']['name'] = 'description';
- $field['field_settings']['semantic_web']['accession'] = 'description';
- $field['field_settings']['semantic_web']['ns'] = 'hydra';
- $field['field_settings']['semantic_web']['nsurl'] = 'http://www.w3.org/ns/hydra/core#';
- }
- if ($field['label'] == 'Timeaccessioned') {
- $field['label'] = 'Time Accessioned';
- $field['description'] = 'Please enter the time that this record was first added to the database.';
- }
- elseif ($field['label'] == 'Timelastmodified') {
- $field['label'] = 'Time Last Modified';
- $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
- }
-
-
-
- elseif ($field['field_settings']['chado_table'] == 'organism' and $field['field_settings']['chado_column'] == 'comment') {
- $field['label'] = 'Description';
- }
-
-
-
- elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'uniquename') {
- $field['field_type'] = 'text';
- $field['widget_type'] = 'text_textfield';
- $field['field_settings']['text_processing'] = 0;
- $field['field_settings']['semantic_web']['name'] = 'name';
- $field['field_settings']['semantic_web']['accession'] = 'name';
- $field['field_settings']['semantic_web']['ns'] = 'foaf';
- $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
- }
-
-
-
- elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'program') {
- $field['field_settings']['semantic_web']['name'] = 'SoftwareApplication';
- $field['field_settings']['semantic_web']['accession'] = 'SoftwareApplication';
- $field['field_settings']['semantic_web']['ns'] = 'schema';
- $field['field_settings']['semantic_web']['nsurl'] = 'https://schema.org/';
- $field['description'] = 'The program name (e.g. blastx, blastp, sim4, genscan. If the analysis was not derived from a software package then provide a very brief description of the pipeline, workflow or method.';
- $field['label'] = 'Program, Pipeline, Workflow or Method Name.';
- }
- elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceuri') {
- $field['field_type'] = 'text';
- $field['widget_type'] = 'text_textfield';
- $field['field_settings']['text_processing'] = 0;
- $field['label'] = 'Source URL';
- $field['description'] = 'The URL where the original source data was derived. Ideally, this should link to the page where more information about the source data can be found.';
- }
- elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourcename') {
- $field['label'] = 'Source Name';
- $field['description'] = 'The name of the source data. This could be a file name, data set or a small description for how the data was collected. For long descriptions use the larger description field.';
- }
- elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceversion') {
- $field['label'] = 'Source Version';
- $field['description'] = 'If hte source data set has a version include it here.';
- }
- elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'algorithm') {
- $field['label'] = 'Source Version';
- $field['description'] = 'The name of the algorithm used to produce the dataset if different from the program.';
- }
- elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'programversion') {
- $field['label'] = 'Program Version';
- $field['description'] = 'The version of the program used to perform this analysis. (e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter "n/a" if no version is available or applicable.';
- }
-
-
-
- elseif ($field['field_settings']['chado_table'] == 'project' and $field['field_settings']['chado_column'] == 'description') {
- $field['label'] = 'Short Description';
- }
- return $field;
- }
- function tripal_chado_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
-
-
- $fields_names = element_children($form['fields']);
- foreach ($fields_names as $field_name) {
- $field_info = field_info_field($field_name);
- if ($field_info['type'] == 'kvproperty_adder') {
- unset($form['fields'][$field_name]);
- }
- if ($field_info['type'] == 'cvterm_class_adder') {
- unset($form['fields'][$field_name]);
- }
- }
- }
- function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
-
-
- $fields_names = element_children($form['fields']);
- foreach ($fields_names as $field_name) {
- $field_info = field_info_field($field_name);
- if ($field_info['type'] == 'kvproperty_adder') {
- unset($form['fields'][$field_name]);
- }
- if ($field_info['type'] == 'cvterm_class_adder') {
- unset($form['fields'][$field_name]);
- }
- }
- }
- function tripal_chado_field_is_empty($item, $field) {
-
- if (!array_key_exists('value', $item)) {
- return TRUE;
- }
-
-
- foreach ($item as $form_field_name => $value) {
- if (isset($value) and $value != NULL and $value != '') {
- return FALSE;
- }
- }
-
- return TRUE;
- }
- function tripal_chado_update_bundle_fields($entity_type, $bundle, $term) {
-
-
- $bundle_name = $bundle->name;
- $fields = tripal_chado_get_bundle_fields($entity_type, $bundle, $term);
- foreach ($fields as $field_name => $field_info) {
- tripal_update_bundle_field($field_name, $field_info, $entity_type, $bundle_name);
- }
- }
- function tripal_chado_add_bundle_fields($entity_type, $bundle, $term) {
-
-
- $bundle_name = $bundle->name;
- $fields = tripal_chado_get_bundle_fields($entity_type, $bundle, $term);
- foreach ($fields as $field_name => $field_info) {
- tripal_add_bundle_field($field_name, $field_info, $entity_type, $bundle_name);
- }
- }
- function tripal_chado_get_bundle_fields($entity_type, $bundle, $term) {
- $fields = array();
- $bundle_name = $bundle->name;
-
- $bundle_data = array();
-
- $vocab = entity_load('TripalVocab', array($term->vocab_id));
- $vocab = reset($vocab);
- $match = array(
- 'dbxref_id' => array(
- 'db_id' => array(
- 'name' => $vocab->namespace,
- ),
- 'accession' => $term->accession
- ),
- );
- $cvterm = chado_generate_var('cvterm', $match);
-
-
- if ($cvterm->name == 'organism') {
- $bundle_data = array(
- 'cv_id' => $cvterm->cv_id->cv_id,
- 'cvterm_id' => $cvterm->cvterm_id,
- 'data_table' => 'organism',
- 'type_table' => 'organism',
- 'field' => '',
- );
- }
-
-
- else if ($cvterm->name == 'analysis') {
- $bundle_data = array(
- 'cv_id' => $cvterm->cv_id->cv_id,
- 'cvterm_id' => $cvterm->cvterm_id,
- 'data_table' => 'analysis',
- 'type_table' => 'analysis',
- 'field' => '',
- );
- }
- else if ($cvterm->name == 'project') {
- $bundle_data = array(
- 'cv_id' => $cvterm->cv_id->cv_id,
- 'cvterm_id' => $cvterm->cvterm_id,
- 'data_table' => 'project',
- 'type_table' => 'project',
- 'field' => '',
- );
- }
- else {
-
-
-
- $default = db_select('tripal_cv_defaults', 't')
- ->fields('t')
- ->condition('cv_id', $cvterm->cv_id->cv_id)
- ->execute()
- ->fetchObject();
- if ($default) {
- $bundle_data = array(
- 'cv_id' => $cvterm->cv_id->cv_id,
- 'cvterm_id' => $cvterm->cvterm_id,
- 'data_table' => $default->table_name,
- 'type_table' => $default->table_name,
- 'field' => $default->field_name,
- );
- }
- }
-
-
- tripal_set_bundle_variable('chado_cvterm_id', $bundle->id, $bundle_data['cvterm_id']);
- tripal_set_bundle_variable('chado_table', $bundle->id, $bundle_data['data_table']);
- tripal_set_bundle_variable('chado_column', $bundle->id, $bundle_data['field']);
-
- $fields_path = drupal_get_path('module', 'tripal_chado') . '/includes/fields';
- $field_files = file_scan_directory($fields_path, '/^chado_.*\.inc$/');
-
- foreach ($field_files as $file) {
- $field_type = $file->name;
- module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
- if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
- $field_obj = new $field_type();
- $field_info = $field_obj->attach_info($entity_type, $bundle, $bundle_data);
- }
- $function = $field_type . '_attach_info';
- if (function_exists($function)) {
- $field_info = $function($entity_type, $bundle, $bundle_data);
- }
- if (!is_array($field_info) or count(array_keys($field_info)) == 0) {
- continue;
- }
- $field_name = $field_info['field_name'];
- $fields[$field_name] = $field_info;
- }
-
-
- tripal_chado_add_bundle_fields_base__fields($fields, $entity_type, $bundle_name, $bundle_data);
- return $fields;
- }
- function tripal_chado_add_bundle_fields_base__fields(&$fields, $entity_type_name, $bundle_name, $bundle_data) {
- $table_name = $bundle_data['data_table'];
- $type_table = $bundle_data['type_table'];
- $type_field = $bundle_data['field'];
-
-
- $schema = chado_get_schema($table_name);
- $columns = $schema['fields'];
- foreach ($columns as $column_name => $details) {
- $field_name = $table_name . '__' . $column_name;
-
- if ($column_name == $schema['primary key'][0]) {
- continue;
- }
-
- if ($table_name == $type_table and $column_name == $type_field) {
- continue;
- }
-
- $field_info = tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $schema, $column_name);
-
-
-
- if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
- $field_info['is_required'] = array_key_exists('default', $details) ? 0 : 1;
- }
-
- if (!$field_info['field_type']) {
-
-
- if ($field_info['is_required'] and !array_key_exists('default', $details)) {
- throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
- array('%table' => $table_name, '%field' => $column_name, '%type' => $details['type'])));
- }
- continue;
- }
-
- $is_fk = FALSE;
- if (array_key_exists('foreign keys', $schema)) {
- foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
- if (array_key_exists($column_name, $fk_details['columns'])) {
- $is_fk = TRUE;
- }
- }
- }
-
- $fields[$field_name] = $field_info;
-
- }
- }
- function tripal_chado_field_attach_validate($entity_type, $entity, &$errors) {
- $bundle_name = $entity->bundle;
- $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
- $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
-
- if ($term->name == 'organism') {
- }
- }
|