123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <?php
- class chado_base__organism_id extends TripalField {
-
- public static $default_label = 'Organism';
-
- public static $default_description = 'A field for specifying an organism.';
-
-
-
-
- public static $default_settings = array(
- 'field_display_string' => '<i>[organism.genus] [organism.species]</i>',
- 'chado_table' => '',
- 'chado_column' => '',
- 'base_table' => '',
- 'semantic_web' => '',
- );
-
-
- public static $default_storage = 'field_chado_storage';
-
- public function createInfo() {
- if (!$this->can_attach) {
- return;
- }
- $table_name = $this->details['chado_table'];
- $type_table = $this->details['chado_type_table'];
- $type_field = $this->details['chado_type_column'];
- $cv_id = $this->details['chado_cv_id'];
- $cvterm_id = $this->details['chado_cvterm_id'];
- return array(
- 'field_name' => $this->field_name,
- 'type' => 'chado_base__organism_id',
- 'cardinality' => 1,
- 'locked' => FALSE,
- 'storage' => array(
- 'type' => 'field_chado_storage',
- ),
- 'settings' => array(
- 'chado_table' => $table_name,
- 'chado_column' => 'organism_id',
- 'semantic_web' => tripal_get_chado_semweb_term($table_name, 'organism_id'),
- ),
- );
- }
-
- public function createInstanceInfo() {
- if (!$this->can_attach) {
- return;
- }
- $table_name = $this->details['chado_table'];
- $type_table = $this->details['chado_type_table'];
- $type_field = $this->details['chado_type_column'];
- $cv_id = $this->details['chado_cv_id'];
- $cvterm_id = $this->details['chado_cvterm_id'];
- $is_required = FALSE;
- $schema = chado_get_schema($table_name);
- if (array_key_exists('not null', $schema['fields']['organism_id']) and
- $schema['fields']['organism_id']['not null']) {
- $is_required = TRUE;
- }
- return array(
- 'field_name' => $this->field_name,
- 'entity_type' => $this->entity_type,
- 'bundle' => $this->bundle->name,
- 'label' => 'Organism',
- 'description' => 'Select an organism.',
- 'required' => $is_required,
- 'settings' => array(
- 'auto_attach' => TRUE,
- ),
- 'widget' => array(
- 'type' => 'chado_base__organism_id_widget',
- 'settings' => array(
- 'display_label' => 1,
- ),
- ),
- 'display' => array(
- 'default' => array(
- 'label' => 'inline',
- 'type' => 'chado_base__organism_id_formatter',
- 'settings' => array(),
- ),
- ),
- );
- }
-
- public static function widgetInfo() {
- return array(
- 'chado_base__organism_id_widget' => array(
- 'label' => t('Organism Select'),
- 'field types' => array('chado_base__organism_id')
- ),
- );
- }
-
- public static function formatterInfo() {
- return array(
- 'chado_base__organism_id_formatter' => array(
- 'label' => t('Organism'),
- 'field types' => array('chado_base__organism_id'),
- 'settings' => array(
- ),
- ),
- );
- }
-
- public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
- if (count($items) > 0) {
- $content = $items[0]['value']['label'];
- if (array_key_exists('entity', $items[0]['value'])) {
- list($entity_type, $entity_id) = explode(':', $items[0]['value']['entity']);
- $content = l(strip_tags($items[0]['value']['label']), 'bio_data/' . $entity_id);
- }
-
-
- $element[0] = array(
- '#type' => 'markup',
- '#markup' => $content,
- );
- }
- }
-
- public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
- $settings = $this->field['settings'];
- $field_name = $this->field['field_name'];
- $field_type = $this->field['type'];
- $field_table = $this->field['settings']['chado_table'];
- $field_column = $this->field['settings']['chado_column'];
- $organism_id = 0;
- if (count($items) > 0 and array_key_exists($field_table . '__organism_id', $items[0])) {
- $organism_id = $items[0][$field_table . '__organism_id'];
- }
- $widget['value'] = array(
- '#type' => 'value',
- '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
- );
- $options = tripal_get_organism_select_options(FALSE);
- $widget[$field_table . '__organism_id'] = array(
- '#type' => 'select',
- '#title' => $element['#title'],
- '#description' => $element['#description'],
- '#options' => $options,
- '#default_value' => $organism_id,
- '#required' => $element['#required'],
- '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
- '#delta' => $delta,
- );
- }
-
- public function load($entity, $details = array()) {
- $record = $details['record'];
- $settings = $this->field['settings'];
- $field_name = $this->field['field_name'];
- $field_type = $this->field['type'];
- $field_table = $this->field['settings']['chado_table'];
- $field_column = $this->field['settings']['chado_column'];
-
- $entity->{$field_name}['und'][0] = array(
- 'value' => array(
- 'label' => '',
- 'genus' => '',
- 'species' => '',
- ),
- 'semantic_web' => array(
- 'label' => 'rdfs:label',
- 'genus' => tripal_get_chado_semweb_term('organism', 'genus'),
- 'species' => tripal_get_chado_semweb_term('organism', 'species'),
- 'infraspecific_name' => tripal_get_chado_semweb_term('organism', 'infraspecific_name'),
- 'infraspecific_type' => tripal_get_chado_semweb_term('organism', 'type_id'),
- ),
- 'chado_mapping' => array(
- 'genus' => 'organism_id,genus',
- 'species' => 'organism_id,genus',
- 'infraspecific_name' => 'organism_id,infraspecific_name',
- 'infraspecific_type' => 'organism_id,infraspecific_type',
- )
- );
- if ($record) {
- $organism = $record->organism_id;
- $string = $settings['field_display_string'];
- $label = tripal_replace_chado_tokens($string, $organism);
- $entity->{$field_name}['und'][0]['value'] = array(
- 'label' => $label,
- 'genus' => $organism->genus,
- 'species' => $organism->species,
- );
-
- if (property_exists($organism, 'infraspecific_name')) {
- $entity->{$field_name}['und'][0]['value']['infraspecific_type'] = NULL;
- $entity->{$field_name}['und'][0]['value']['infraspecific_name'] = $organism->infraspecific_name;
- if ($organism->type_id) {
- $entity->{$field_name}['und'][0]['value']['infraspecific_type'] = $organism->type_id->name;
- }
- }
- $entity->{$field_name}['und'][0][$field_table . '__organism_id'] = $organism->organism_id;
-
- if (property_exists($entity->chado_record->$field_column, 'entity_id')) {
- $fk_entity_id = $entity->chado_record->$field_column->entity_id;
- $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $fk_entity_id;
- }
- }
- }
-
- public static function fieldSettingsForm($field, $instance, $has_data) {
- $element = parent::fieldSettingsForm($field, $instance, $has_data);
- $settings = $this->field['settings'];
- $element['instructions'] = array(
- '#type' => 'item',
- '#markup' => 'You may rewrite the way this field is presented to the end-user.
- The Rewrite Value field allows you to use tokens to indicate how the
- value should be displayed. Tokens will be substituted with appriorate
- data from the database. See the Available tokens list for the
- tokens you may use.'
- );
- $element['field_display_string'] = array(
- '#type' => 'textfield',
- '#title' => 'Rewrite Value',
- '#description' => t('Provide a mixture of text and/or tokens for the format.
- For example: [organism.genus] [organism.species]. When displayed,
- the tokens will be replaced with the actual value.'),
- '#default_value' => $settings['field_display_string'],
- );
- $element['tokens'] = array(
- '#type' => 'fieldset',
- '#collapsed' => TRUE,
- '#collapsible' => TRUE,
- '#title' => 'Available Tokens'
- );
- $headers = array('Token', 'Description');
- $rows = array();
-
-
-
- $tokens = tripal_get_chado_tokens('organism');
- foreach ($tokens as $token) {
- $rows[] = array(
- $token['token'],
- $token['description'],
- );
- }
- $table_vars = array(
- 'header' => $headers,
- 'rows' => $rows,
- 'attributes' => array(),
- 'sticky' => FALSE,
- 'caption' => '',
- 'colgroups' => array(),
- 'empty' => 'There are no tokens',
- );
- $element['tokens']['list'] = array(
- '#type' => 'item',
- '#markup' => theme_table($table_vars),
- );
-
- $parent_elements = parent::settings_form($field, $instance, $has_data);
- $element = array_merge($element, $parent_elements);
- return $element;
- }
-
- public static function sort(&$data, $criteria = 'ASC') {
- }
- }
- function chado_base__organism_id_widget_validate($element, &$form_state) {
- $field_name = $element['#parents'][0];
- $field = $form_state['field'][$field_name]['und']['field'];
- $settings = $field['settings'];
- $field_name = $field['field_name'];
- $field_type = $field['type'];
- $field_table = $field['settings']['chado_table'];
- $field_column = $field['settings']['chado_column'];
-
-
-
- if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
- return;
- }
-
- if (!$organism_id) {
- form_error($element, t("Please specify an organism."));
- }
- }
|