123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- class obi__organism extends ChadoField {
- // The default lable for this field.
- public static $default_label = 'Organism';
- // The default description for this field.
- public static $description = 'The organism to which this resource is associated.';
- // Provide a list of instance specific settings. These can be access within
- // the instanceSettingsForm. When the instanceSettingsForm is submitted
- // then Drupal with automatically change these settings for the instnace.
- // It is recommended to put settings at the instance level whenever possible.
- // If you override this variable in a child class be sure to replicate the
- // term_name, term_vocab, term_accession and term_fixed keys as these are
- // required for all TripalFields.
- public static $default_instance_settings = array(
- // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
- 'term_vocabulary' => 'OBI',
- // The name of the term.
- 'term_name' => 'organism',
- // The unique ID (i.e. accession) of the term.
- 'term_accession' => '0100026',
- // Set to TRUE if the site admin is allowed to change the term
- // type. This will create form elements when editing the field instance
- // to allow the site admin to change the term settings above.
- 'term_fixed' => FALSE,
- // The format for display of the organism.
- 'field_display_string' => '<i>[organism.genus] [organism.species]</i>',
- );
- // The default widget for this field.
- public static $default_widget = 'OBI__organism_widget';
- // The default formatter for this field.
- public static $default_formatter = 'OBI__organism_formatter';
- /**
- * @see TripalField::validate()
- */
- public function validate($entity_type, $entity, $langcode, $items, &$errors) {
- // If we don't have an entity then we don't want to validate. The case
- // where this could happen is when a user is editing the field settings
- // and trying to set a default value. In that case there's no entity and
- // we don't want to validate. There will always be an entity for creation
- // and update operations of a content type.
- if (!$entity) {
- return;
- }
- $settings = $this->field['settings'];
- $field_name = $this->field['field_name'];
- $field_type = $this->field['type'];
- $field_table = $this->instance['settings']['chado_table'];
- $field_column = $this->instance['settings']['chado_column'];
- // Set the linker field appropriately.
- if ($field_table == 'biomaterial') {
- $linker_field = 'chado-biomaterial__taxon_id';
- }
- else {
- $linker_field = 'chado-' . $field_table . '__organism_id';
- }
- // Get the field values.
- foreach ($items as $delta => $values) {
- // Get the field values.
- $organism_id = $values[$linker_field];
- if (!$organism_id or $organism_id == 0) {
- $errors[$field_name]['und'][0][] = array(
- 'message' => t("Please specify an organism."),
- 'error' => 'obi__organism_id'
- );
- }
- }
- }
- /**
- * @see TripalField::load()
- */
- public function load($entity) {
- $record = $entity->chado_record;
- $settings = $this->instance['settings'];
- $field_name = $this->field['field_name'];
- $field_type = $this->field['type'];
- $field_table = $this->instance['settings']['chado_table'];
- $field_column = $this->instance['settings']['chado_column'];
- // Get the terms for each of the keys for the 'values' property.
- $label_term = 'rdfs:label';
- $genus_term = chado_get_semweb_term('organism', 'genus');
- $species_term = chado_get_semweb_term('organism', 'species');
- $infraspecific_name_term = chado_get_semweb_term('organism', 'infraspecific_name');
- $infraspecific_type_term = chado_get_semweb_term('organism', 'type_id');
- // Set the linker field appropriately.
- if ($field_table == 'biomaterial') {
- $linker_field = 'chado-biomaterial__taxon_id';
- }
- else {
- $linker_field = 'chado-' . $field_table . '__organism_id';
- }
- // Set some defaults for the empty record.
- $entity->{$field_name}['und'][0] = array(
- 'value' => array(),
- );
- if ($record) {
- if ($field_table == 'biomaterial') {
- $organism = $record->taxon_id;
- }
- else {
- $organism = $record->organism_id;
- }
- $string = $settings['field_display_string'];
- $label = chado_replace_tokens($string, $organism);
- $entity->{$field_name}['und'][0]['value'] = array(
- $label_term => $label,
- $genus_term => $organism->genus,
- $species_term => $organism->species,
- );
- // The infraspecific fields were introduced in Chado v1.3.
- if (property_exists($organism, 'infraspecific_name')) {
- $entity->{$field_name}['und'][0]['value'][$infraspecific_type_term] = NULL;
- $entity->{$field_name}['und'][0]['value'][$infraspecific_name_term] = $organism->infraspecific_name;
- if ($organism->type_id) {
- $entity->{$field_name}['und'][0]['value'][$infraspecific_type_term] = $organism->type_id->name;
- }
- }
- $entity->{$field_name}['und'][0][$linker_field] = $organism->organism_id;
- // Is there a published entity for this organism?
- if (property_exists($record->{$field_column}, 'entity_id')) {
- $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $record->{$field_column}->entity_id;
- }
- }
- }
- /**
- * @see TripalField::globalSettingsForm()
- */
- public function instanceSettingsForm() {
- $element = parent::instanceSettingsForm();
- $settings = $this->instance['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();
- // Here we use the chado_get_tokens rather than the
- // tripal_get_entity_tokens because we can't gurantee that all organisms
- // have entities.
- $tokens = chado_get_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),
- );
- return $element;
- }
- /**
- * @see TripalField::elementInfo()
- */
- public function elementInfo() {
- $field_term = $this->getFieldTermID();
- $genus_term = chado_get_semweb_term('organism', 'genus');
- $species_term = chado_get_semweb_term('organism', 'species');
- $infraspecific_name_term = chado_get_semweb_term('organism', 'infraspecific_name');
- $infraspecific_type_term = chado_get_semweb_term('organism', 'type_id');
- return array(
- $field_term => array(
- 'operations' => array('eq', 'contains', 'starts'),
- 'sortable' => TRUE,
- 'searchable' => TRUE,
- 'readonly' => FALSE,
- 'type' => 'xs:complexType',
- 'elements' => array(
- 'rdfs:label' => array(
- 'searchable' => TRUE,
- 'name' => 'scientific_name',
- 'operations' => array('eq', 'ne', 'contains', 'starts'),
- 'sortable' => FALSE,
- 'type' => 'xs:string',
- 'readonly' => TRUE,
- 'required' => FALSE,
- ),
- $genus_term => array(
- 'searchable' => TRUE,
- 'name' => 'genus',
- 'operations' => array('eq', 'ne', 'contains', 'starts'),
- 'sortable' => TRUE,
- 'readonly' => FALSE,
- 'type' => 'xs:string',
- 'required' => TRUE,
- ),
- $species_term => array(
- 'searchable' => TRUE,
- 'name' => 'species',
- 'operations' => array('eq', 'ne', 'contains', 'starts'),
- 'sortable' => TRUE,
- 'readonly' => FALSE,
- 'type' => 'xs:string',
- 'required' => TRUE,
- ),
- $infraspecific_name_term => array(
- 'searchable' => TRUE,
- 'name' => 'infraspecies',
- 'operations' => array('eq', 'ne', 'contains', 'starts'),
- 'sortable' => TRUE,
- 'readonly' => FALSE,
- 'type' => 'xs:string',
- 'required' => FALSE,
- ),
- $infraspecific_type_term => array(
- 'searchable' => TRUE,
- 'name' => 'infraspecific_type',
- 'operations' => array('eq', 'ne', 'contains', 'starts'),
- 'sortable' => TRUE,
- 'readonly' => FALSE,
- 'type' => 'xs:integer',
- 'required' => FALSE,
- ),
- 'entity' => array(
- 'searchable' => FALSE,
- ),
- ),
- ),
- );
- }
- /**
- * @see ChadoField::query()
- */
- public function query($query, $condition) {
- $alias = $this->field['field_name'];
- $operator = $condition['operator'];
- $field_term_id = $this->getFieldTermID();
- $genus_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'genus');
- $species_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'species');
- $infraspecific_name_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'infraspecific_name');
- $infraspecific_type_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'type_id');
- // Join to the organism table for this field.
- $this->queryJoinOnce($query, 'organism', $alias, "base.organism_id = $alias.organism_id");
- // If the column is the field name then we're during a search on the full
- // scientific name.
- if ($condition['column'] == $field_term_id or
- $condition['column'] == $field_term_id . ',rdfs:label') {
- if (chado_get_version() <= 1.3) {
- $query->where("CONCAT($alias.genus, ' ', $alias.species) $operator :full_name", array(':full_name' => $condition['value']));
- }
- else {
- $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', 'base.infraspecific_type = ' . $alias . '_cvterm.type_id', 'LEFT OUTER');
- $query->where("CONCAT($alias.genus, ' ', $alias.species, ' ', " . $alias . "'_cvterm.name', ' ', $alias.infraspecific_name) $operator :full_name", array(':full_name' => $condition['value']));
- }
- }
- // If the column is a subfield.
- if ($condition['column'] == $species_term) {
- $query->condition("$alias.species", $condition['value'], $operator);
- }
- if ($condition['column'] == $genus_term) {
- $query->condition("$alias.genus", $condition['value'], $operator);
- }
- if ($condition['column'] == $infraspecific_name_term) {
- $query->condition("$alias.infraspecific_name", $condition['value'], $operator);
- }
- if ($condition['column'] == $infraspecific_type_term) {
- $this->queryJoinOnce($query, 'cvterm', 'CVT', "base.type_id = CVT.cvterm_id");
- $query->condition("CVT.name", $condition['value'], $operator);
- }
- }
- /**
- * @see ChadoField::queryOrder()
- */
- public function queryOrder($query, $order) {
- $alias = $this->field['field_name'];
- $field_term_id = $this->getFieldTermID();
- $genus_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'genus');
- $species_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'species');
- $infraspecific_name_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'infraspecific_name');
- $infraspecific_type_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'type_id');
- // Join to the organism table for this field.
- $this->queryJoinOnce($query, 'organism', $alias, "base.organism_id = $alias.organism_id");
- // Now perform the sort.
- if ($order['column'] == $species_term) {
- $query->orderBy("$alias.species", $order['direction']);
- }
- if ($order['column'] == $genus_term) {
- $query->orderBy("$alias.genus", $order['direction']);
- }
- if ($order['column'] == $infraspecific_name_term) {
- $query->orderBy("$alias.infraspecific_name", $order['direction']);
- }
- if ($order['column'] == $infraspecific_type_term) {
- if (!in_array('CVT', $joins)) {
- $this->queryJoinOnce($query, 'cvterm', 'CVT', "base.type_id = CVT.cvterm_id");
- }
- $query->orderBy("CVT.name", $order['direction']);
- }
- }
- }
|