local__contact.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. class local__contact extends ChadoField {
  3. // --------------------------------------------------------------------------
  4. // EDITABLE STATIC CONSTANTS
  5. //
  6. // The following constants SHOULD be set for each descendent class. They are
  7. // used by the static functions to provide information to Drupal about
  8. // the field and it's default widget and formatter.
  9. // --------------------------------------------------------------------------
  10. // The default label for this field.
  11. public static $default_label = 'Contact';
  12. // The default description for this field.
  13. public static $description = 'An indviddual or organization that serves as a contact for this record.';
  14. // Provide a list of instance specific settings. These can be accessed within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal will automatically change these settings for the instance.
  17. // It is recommended to put settings at the instance level whenever possible.
  18. // If you override this variable in a child class be sure to replicate the
  19. // term_name, term_vocab, term_accession and term_fixed keys as these are
  20. // required for all TripalFields.
  21. public static $default_instance_settings = [
  22. // The short name for the vocabulary (e.g. schema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'local',
  24. // The name of the term.
  25. 'term_name' => 'contact',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => 'contact',
  28. // Set to TRUE if the site admin is allowed to change the term
  29. // type. This will create form elements when editing the field instance
  30. // to allow the site admin to change the term settings above.
  31. 'term_fixed' => FALSE,
  32. ];
  33. // The default widget for this field.
  34. public static $default_widget = 'local__contact_widget';
  35. // The default formatter for this field.
  36. public static $default_formatter = 'local__contact_formatter';
  37. // --------------------------------------------------------------------------
  38. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  39. // --------------------------------------------------------------------------
  40. // An array containing details about the field. The format of this array
  41. // is the same as that returned by field_info_fields()
  42. protected $field;
  43. // An array containing details about an instance of the field. A field does
  44. // not have to have an instance. But if dealing with an instance (such as
  45. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  46. protected $instance;
  47. /**
  48. * @see TripalField::elements()
  49. */
  50. public function elementInfo() {
  51. $field_term = $this->getFieldTermID();
  52. $type_term = chado_get_semweb_term('contact', 'type_id');
  53. $name_term = chado_get_semweb_term('contact', 'name');
  54. $description_term = chado_get_semweb_term('contact', 'description');
  55. return [
  56. $field_term => [
  57. 'operations' => ['eq', 'contains', 'starts'],
  58. 'sortable' => TRUE,
  59. 'searchable' => TRUE,
  60. 'type' => 'xs:complexType',
  61. 'readonly' => TRUE,
  62. 'elements' => [
  63. $type_term => [
  64. 'searchable' => TRUE,
  65. 'label' => 'Contact Type',
  66. 'help' => 'The type of contact',
  67. 'operations' => ['eq', 'ne', 'contains', 'starts'],
  68. 'sortable' => TRUE,
  69. 'type' => 'xs:string',
  70. 'readonly' => FALSE,
  71. 'required' => TRUE,
  72. ],
  73. $name_term => [
  74. 'searchable' => TRUE,
  75. 'label' => 'Contact Name',
  76. 'help' => 'The name of the contact.',
  77. 'operations' => ['eq', 'ne', 'contains', 'starts'],
  78. 'sortable' => TRUE,
  79. 'type' => 'xs:string',
  80. 'readonly' => FALSE,
  81. 'required' => TRUE,
  82. ],
  83. $description_term => [
  84. 'searchable' => TRUE,
  85. 'label' => 'Contact Description',
  86. 'help' => 'A descriptoin of the contact.',
  87. 'operations' => ['contains'],
  88. 'sortable' => TRUE,
  89. 'type' => 'xs:string',
  90. 'readonly' => FALSE,
  91. 'required' => FALSE,
  92. ],
  93. 'entity' => [
  94. 'searchable' => FALSE,
  95. ],
  96. ],
  97. ],
  98. ];
  99. }
  100. /**
  101. *
  102. * @see TripalField::load()
  103. */
  104. public function load($entity) {
  105. $record = $entity->chado_record;
  106. $field_name = $this->field['field_name'];
  107. $field_type = $this->field['type'];
  108. $field_table = $this->instance['settings']['chado_table'];
  109. $field_column = $this->instance['settings']['chado_column'];
  110. $base_table = $this->instance['settings']['base_table'];
  111. $type_term = chado_get_semweb_term('contact', 'type_id');
  112. $name_term = chado_get_semweb_term('contact', 'name');
  113. $description_term = chado_get_semweb_term('contact', 'description');
  114. // Set some defaults for the empty record.
  115. $entity->{$field_name}['und'][0] = [
  116. 'value' => [],
  117. ];
  118. // Handle the biomaterial table.
  119. if (!$record) {
  120. return;
  121. }
  122. $linker_field = 'chado-' . $field_table . '__' . $field_column;
  123. $contact = $record->{$field_column};
  124. if ($contact) {
  125. $entity->{$field_name}['und'][0] = [
  126. 'value' => [
  127. $type_term => $contact->type_id ? $contact->type_id->name : '',
  128. $name_term => $contact->name,
  129. $description_term => $contact->description,
  130. ],
  131. $entity->{$field_name}['und'][0][$linker_field] = $contact->contact_id,
  132. ];
  133. if (property_exists($contact, 'entity_id')) {
  134. $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $contact->entity_id;
  135. }
  136. }
  137. }
  138. /**
  139. * @see ChadoField::query()
  140. */
  141. public function query($query, $condition) {
  142. $alias = $this->field['field_name'];
  143. $operator = $condition['operator'];
  144. $field_term_id = $this->getFieldTermID();
  145. $type_term = chado_get_semweb_term('contact', 'type_id');
  146. $name_term = chado_get_semweb_term('contact', 'name');
  147. $description_term = chado_get_semweb_term('contact', 'description');
  148. if ($field_table == 'biomaterial') {
  149. if ($record) {
  150. $contact = $record->biosourceprovider_id;
  151. // Join the contact table
  152. $calias = $alias . '_provider_id';
  153. $this->queryJoinOnce($query, 'contact', $calias, "base.biosourceprovider_id = $calias.contact_id");
  154. // Search by the contact name
  155. if ($condition['column'] == $field_term_id or
  156. $condition['column'] == $field_term_id . ',' . $name_term) {
  157. $query->condition("$calias.name", $condition['value'], $operator);
  158. }
  159. // Search on the contact description.
  160. if ($condition['column'] == $field_term_id . ',' . $description_term) {
  161. $query->condition("$calias.description", $condition['value'], $operator);
  162. }
  163. // Search on the contact type.
  164. if ($condition['column'] == $field_term_id . ',' . $type_term) {
  165. $talias = $alias . 'provider_contact_type';
  166. $this->queryJoinOnce($query, 'cvterm', $talias, "$calias.type_id = $talias.cvterm_id");
  167. $query->condition("$talias.name", $condition['value'], $operator);
  168. }
  169. }
  170. }
  171. }
  172. /**
  173. * @see ChadoField::queryOrder()
  174. */
  175. public function queryOrder($query, $order) {
  176. $alias = $this->field['field_name'];
  177. $field_term_id = $this->getFieldTermID();
  178. $type_term = chado_get_semweb_term('contact', 'type_id');
  179. $name_term = chado_get_semweb_term('contact', 'name');
  180. $description_term = chado_get_semweb_term('contact', 'description');
  181. if ($field_table == 'biomaterial') {
  182. if ($record) {
  183. $contact = $record->biosourceprovider_id;
  184. // Join the contact linker table and then join the contact table.
  185. $calias = $alias . '_provider_id';
  186. $this->queryJoinOnce($query, 'contact', $calias, "base.biosourceprovider_id = $calias.contact_id");
  187. // Search by the contact name
  188. if ($order['column'] == $field_term_id or
  189. $order['column'] == $field_term_id . ',' . $name_term) {
  190. $query->orderBy("$calias.name", $order['direction']);
  191. }
  192. // Search on the contact description.
  193. if ($order['column'] == $field_term_id . ',' . $description_term) {
  194. $query->orderBy("$calias.description", $order['direction']);
  195. }
  196. // Search on the contact type.
  197. if ($order['column'] == $field_term_id . ',' . $type_term) {
  198. $talias = $alias . 'provider_contact_type';
  199. $this->queryJoinOnce($query, 'cvterm', $talias, "$calias.type_id = $talias.cvterm_id", "LEFT OUTER");
  200. $query->orderBy("$talias.name", $order['direction']);
  201. }
  202. }
  203. }
  204. }
  205. }