123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?php
- class obi__organism extends ChadoField {
-
- public static $default_label = 'Organism';
-
- public static $description = 'The organism to which this resource is associated.';
-
-
-
-
-
-
-
- public static $default_instance_settings = [
-
- 'term_vocabulary' => 'OBI',
-
- 'term_name' => 'organism',
-
- 'term_accession' => '0100026',
-
-
-
- 'term_fixed' => FALSE,
-
- 'field_display_string' => '<i>[organism.genus] [organism.species]</i>',
- ];
-
- public static $default_widget = 'obi__organism_widget';
-
- public static $default_formatter = 'obi__organism_formatter';
-
- public function validate($entity_type, $entity, $langcode, $items, &$errors) {
-
-
-
-
-
- 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'];
-
- if ($field_table == 'biomaterial') {
- $linker_field = 'chado-biomaterial__taxon_id';
- }
- else {
- $linker_field = 'chado-' . $field_table . '__organism_id';
- }
-
- foreach ($items as $delta => $values) {
-
- $organism_id = $values[$linker_field];
- if ((!$organism_id or $organism_id == 0) and !$field_table == 'biomaterial') {
- $errors[$field_name]['und'][0][] = [
- 'message' => t("Please specify an organism."),
- 'error' => 'obi__organism_id',
- ];
- }
- }
- }
-
- 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'];
-
- $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');
-
- if ($field_table == 'biomaterial') {
- $linker_field = 'chado-biomaterial__taxon_id';
- }
- else {
- $linker_field = 'chado-' . $field_table . '__organism_id';
- }
-
- $entity->{$field_name}['und'][0] = [
- 'value' => [],
- ];
- if ($record) {
- if ($field_table == 'biomaterial') {
- $organism = $record->taxon_id;
- }
- else {
- $organism = $record->organism_id;
- }
- if (!$organism) {
- return;
- }
- $string = $settings['field_display_string'];
- $label = chado_replace_tokens($string, $organism);
- $entity->{$field_name}['und'][0]['value'] = [
- $label_term => $label,
- $genus_term => $organism->genus,
- $species_term => $organism->species,
- ];
-
- 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;
-
- if (property_exists($record->{$field_column}, 'entity_id')) {
- $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $record->{$field_column}->entity_id;
- }
- }
- }
-
- public function instanceSettingsForm() {
- $element = parent::instanceSettingsForm();
- $settings = $this->instance['settings'];
- $element['instructions'] = [
- '#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'] = [
- '#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'] = [
- '#type' => 'fieldset',
- '#collapsed' => TRUE,
- '#collapsible' => TRUE,
- '#title' => 'Available Tokens',
- ];
- $headers = ['Token', 'Description'];
- $rows = [];
-
-
-
- $tokens = chado_get_tokens('organism');
- foreach ($tokens as $token) {
- $rows[] = [
- $token['token'],
- $token['description'],
- ];
- }
- $table_vars = [
- 'header' => $headers,
- 'rows' => $rows,
- 'attributes' => [],
- 'sticky' => FALSE,
- 'caption' => '',
- 'colgroups' => [],
- 'empty' => 'There are no tokens',
- ];
- $element['tokens']['list'] = [
- '#type' => 'item',
- '#markup' => theme_table($table_vars),
- ];
- return $element;
- }
-
- 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 [
- $field_term => [
- 'operations' => ['eq', 'contains', 'starts'],
- 'sortable' => TRUE,
- 'searchable' => TRUE,
- 'readonly' => FALSE,
- 'type' => 'xs:complexType',
- 'elements' => [
- 'rdfs:label' => [
- 'searchable' => TRUE,
- 'name' => 'scientific_name',
- 'operations' => ['eq', 'ne', 'contains', 'starts'],
- 'sortable' => FALSE,
- 'type' => 'xs:string',
- 'readonly' => TRUE,
- 'required' => FALSE,
- ],
- $genus_term => [
- 'searchable' => TRUE,
- 'name' => 'genus',
- 'operations' => ['eq', 'ne', 'contains', 'starts'],
- 'sortable' => TRUE,
- 'readonly' => FALSE,
- 'type' => 'xs:string',
- 'required' => TRUE,
- ],
- $species_term => [
- 'searchable' => TRUE,
- 'name' => 'species',
- 'operations' => ['eq', 'ne', 'contains', 'starts'],
- 'sortable' => TRUE,
- 'readonly' => FALSE,
- 'type' => 'xs:string',
- 'required' => TRUE,
- ],
- $infraspecific_name_term => [
- 'searchable' => TRUE,
- 'name' => 'infraspecies',
- 'operations' => ['eq', 'ne', 'contains', 'starts'],
- 'sortable' => TRUE,
- 'readonly' => FALSE,
- 'type' => 'xs:string',
- 'required' => FALSE,
- ],
- $infraspecific_type_term => [
- 'searchable' => TRUE,
- 'name' => 'infraspecific_type',
- 'operations' => ['eq', 'ne', 'contains', 'starts'],
- 'sortable' => TRUE,
- 'readonly' => FALSE,
- 'type' => 'xs:integer',
- 'required' => FALSE,
- ],
- 'entity' => [
- 'searchable' => FALSE,
- ],
- ],
- ],
- ];
- }
-
- 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');
-
- $this->queryJoinOnce($query, 'organism', $alias, "base.organism_id = $alias.organism_id");
-
-
- 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", [':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", [':full_name' => $condition['value']]);
- }
- }
-
- 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);
- }
- }
-
- 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');
-
- $this->queryJoinOnce($query, 'organism', $alias, "base.organism_id = $alias.organism_id");
-
- 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']);
- }
- }
- }
|