efo__array_design.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. class efo__array_design extends ChadoField {
  3. // The default lable for this field.
  4. public static $default_label = 'Array Design';
  5. // The default description for this field.
  6. public static $description = 'An instrument design which describes the design of the array.';
  7. // Provide a list of instance specific settings. These can be access within
  8. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  9. // then Drupal with automatically change these settings for the instnace.
  10. // It is recommended to put settings at the instance level whenever possible.
  11. // If you override this variable in a child class be sure to replicate the
  12. // term_name, term_vocab, term_accession and term_fixed keys as these are
  13. // required for all TripalFields.
  14. public static $default_instance_settings = array(
  15. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  16. 'term_vocabulary' => 'EFO',
  17. // The name of the term.
  18. 'term_name' => 'array design',
  19. // The unique ID (i.e. accession) of the term.
  20. 'term_accession' => '0000269',
  21. // Set to TRUE if the site admin is allowed to change the term
  22. // type. This will create form elements when editing the field instance
  23. // to allow the site admin to change the term settings above.
  24. 'term_fixed' => FALSE,
  25. );
  26. // The default widget for this field.
  27. public static $default_widget = 'efo__array_design_widget';
  28. // The default formatter for this field.
  29. public static $default_formatter = 'efo__array_design_formatter';
  30. /**
  31. * @see TripalField::validate()
  32. */
  33. public function validate($entity_type, $entity, $langcode, $items, &$errors) {
  34. // If we don't have an entity then we don't want to validate. The case
  35. // where this could happen is when a user is editing the field settings
  36. // and trying to set a default value. In that case there's no entity and
  37. // we don't want to validate. There will always be an entity for creation
  38. // and update operations of a content type.
  39. if (!$entity) {
  40. return;
  41. }
  42. $settings = $this->field['settings'];
  43. $field_name = $this->field['field_name'];
  44. $field_type = $this->field['type'];
  45. $field_table = $this->instance['settings']['chado_table'];
  46. $field_column = $this->instance['settings']['chado_column'];
  47. $linker_field = 'chado-' . $field_table . '__' . $field_column;
  48. // Get the field values.
  49. foreach ($items as $delta => $values) {
  50. // Get the field values.
  51. $arraydesign_id = $values[$linker_field];
  52. if (!$arraydesign_id or $arraydesign_id == 0) {
  53. $errors[$field_name]['und'][0][] = array(
  54. 'message' => t("Please specify an array design."),
  55. 'error' => 'efo__array_design'
  56. );
  57. }
  58. }
  59. }
  60. /**
  61. * @see TripalField::load()
  62. */
  63. public function load($entity) {
  64. $record = $entity->chado_record;
  65. $settings = $this->instance['settings'];
  66. $field_name = $this->field['field_name'];
  67. $field_type = $this->field['type'];
  68. $field_table = $this->instance['settings']['chado_table'];
  69. $field_column = $this->instance['settings']['chado_column'];
  70. // Get the terms for each of the keys for the 'values' property.
  71. $name_term = chado_get_semweb_term('arraydesign', 'name');
  72. $version_term = chado_get_semweb_term('arraydesign', 'version');
  73. // Set some defaults for the empty record.
  74. $entity->{$field_name}['und'][0] = array(
  75. 'value' => array(),
  76. );
  77. if (!$record) {
  78. return;
  79. }
  80. $linker_field = 'chado-' . $field_table . '__' . $field_column;
  81. $entity->{$field_name}['und'][0]['value'] = array(
  82. $name_term => $record->{$field_column}->name,
  83. $version_term => $record->{$field_column}->version,
  84. );
  85. $entity->{$field_name}['und'][0][$linker_field] = $record->{$field_column}->arraydesign_id;
  86. // Is there a published entity for this arraydesign?
  87. if (property_exists($record->{$field_column}, 'entity_id')) {
  88. $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $record->{$field_column}->entity_id;
  89. }
  90. }
  91. /**
  92. * @see TripalField::elementInfo()
  93. */
  94. public function elementInfo() {
  95. $field_term = $this->getFieldTermID();
  96. $name_term = chado_get_semweb_term('arraydesign', 'name');
  97. $version_term = chado_get_semweb_term('arraydesign', 'version');
  98. return array(
  99. $field_term => array(
  100. 'operations' => array('eq', 'contains', 'starts'),
  101. 'sortable' => TRUE,
  102. 'searchable' => TRUE,
  103. 'readonly' => FALSE,
  104. 'type' => 'xs:complexType',
  105. 'elements' => array(
  106. $name_term => array(
  107. 'searchable' => TRUE,
  108. 'name' => 'name',
  109. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  110. 'sortable' => FALSE,
  111. 'type' => 'xs:string',
  112. 'readonly' => TRUE,
  113. 'required' => FALSE,
  114. ),
  115. $version_term => array(
  116. 'searchable' => TRUE,
  117. 'name' => 'version',
  118. 'operations' => array('eq', 'ne'),
  119. 'sortable' => TRUE,
  120. 'readonly' => FALSE,
  121. 'type' => 'xs:string',
  122. 'required' => TRUE,
  123. ),
  124. 'entity' => array(
  125. 'searchable' => FALSE,
  126. ),
  127. ),
  128. ),
  129. );
  130. }
  131. /**
  132. * @see ChadoField::query()
  133. */
  134. public function query($query, $condition) {
  135. $alias = $this->field['field_name'];
  136. $operator = $condition['operator'];
  137. $field_term_id = $this->getFieldTermID();
  138. $name_term = $field_term_id . ',' . chado_get_semweb_term('arraydesign', 'name');
  139. $version_term = $field_term_id . ',' . chado_get_semweb_term('arraydesign', 'version');
  140. // Join to the organism table for this field.
  141. $this->queryJoinOnce($query, 'arraydesign', $alias, "base.arraydesign_id = $alias.arraydesign_id");
  142. // If the column is the field name then we're during a search on the full
  143. // scientific name.
  144. if ($condition['column'] == $field_term_id or
  145. $condition['column'] == $name_term) {
  146. $query->condition("$alias.name", $condition['value'], $operator);
  147. }
  148. // If the column is a subfield.
  149. if ($condition['column'] == $version_term) {
  150. $query->condition("$alias.version", $condition['value'], $operator);
  151. }
  152. }
  153. /**
  154. * @see ChadoField::queryOrder()
  155. */
  156. public function queryOrder($query, $order) {
  157. $alias = $this->field['field_name'];
  158. $field_term_id = $this->getFieldTermID();
  159. $name_term = $field_term_id . ',' . chado_get_semweb_term('arraydesign', 'name');
  160. $version_term = $field_term_id . ',' . chado_get_semweb_term('arraydesign', 'version');
  161. // Join to the organism table for this field.
  162. $this->queryJoinOnce($query, 'arraydesign', $alias, "base.arraydesign_id = $alias.arraydesign_id");
  163. // Now perform the sort.
  164. if ($order['column'] == $name_term) {
  165. $query->orderBy("$alias.name", $order['direction']);
  166. }
  167. if ($order['column'] == $version_term) {
  168. $query->orderBy("$alias.version", $order['direction']);
  169. }
  170. }
  171. }