local__contact.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 lable 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 access within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal with automatically change these settings for the instnace.
  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 = array(
  22. // The short name for the vocabulary (e.g. shcema, 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 = tripal_get_chado_semweb_term('contact', 'type_id');
  53. $name_term = tripal_get_chado_semweb_term('contact', 'name');
  54. $description_term = tripal_get_chado_semweb_term('contact', 'description');
  55. return array(
  56. $field_term => array(
  57. 'operations' => array('eq', 'contains', 'starts'),
  58. 'sortable' => TRUE,
  59. 'searchable' => TRUE,
  60. 'type' => 'string',
  61. 'elements' => array(
  62. $type_term => array(
  63. 'searchable' => TRUE,
  64. 'label' => 'Contact Type',
  65. 'help' => 'The type of contact',
  66. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  67. 'sortable' => TRUE,
  68. ),
  69. $name_term => array(
  70. 'searchable' => TRUE,
  71. 'label' => 'Contact Name',
  72. 'help' => 'The name of the contact.',
  73. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  74. 'sortable' => TRUE,
  75. ),
  76. $description_term => array(
  77. 'searchable' => TRUE,
  78. 'label' => 'Contact Description',
  79. 'help' => 'A descriptoin of the contact.',
  80. 'operations' => array('contains'),
  81. 'sortable' => TRUE,
  82. ),
  83. 'entity' => array(
  84. 'searchable' => FALSE,
  85. ),
  86. ),
  87. )
  88. );
  89. }
  90. /**
  91. *
  92. * @see TripalField::load()
  93. */
  94. public function load($entity) {
  95. $record = $entity->chado_record;
  96. $field_name = $this->field['field_name'];
  97. $field_type = $this->field['type'];
  98. $field_table = $this->instance['settings']['chado_table'];
  99. $field_column = $this->instance['settings']['chado_column'];
  100. $base_table = $this->instance['settings']['base_table'];
  101. $type_term = tripal_get_chado_semweb_term('contact', 'type_id');
  102. $name_term = tripal_get_chado_semweb_term('contact', 'name');
  103. $description_term = tripal_get_chado_semweb_term('contact', 'description');
  104. // Set some defaults for the empty record.
  105. $entity->{$field_name}['und'][0] = array(
  106. 'value' => array(),
  107. );
  108. // Handle the biomaterial table.
  109. if ($field_table == 'biomaterial') {
  110. if ($record) {
  111. $contact = $record->biosourceprovider_id;
  112. $entity->{$field_name}['und'][0] = array(
  113. 'value' => array(
  114. $type_term => $contact->type_id ? $contact->type_id->name : '',
  115. $name_term => $contact->name,
  116. $description_term => $contact->description,
  117. ),
  118. $entity->{$field_name}['und'][0]['chado-biomaterial__biosourceprovider_id'] = $contact->contact_id,
  119. );
  120. if (property_exists($contact, 'entity_id')) {
  121. $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $contact->entity_id;
  122. }
  123. }
  124. };
  125. // Here place other non-linker tables that have a FK to the contact table.
  126. }
  127. /**
  128. * @see ChadoField::query()
  129. */
  130. public function query($query, $condition) {
  131. $alias = $this->field['field_name'];
  132. $operator = $condition['operator'];
  133. $field_term_id = $this->getFieldTermID();
  134. $type_term = tripal_get_chado_semweb_term('contact', 'type_id');
  135. $name_term = tripal_get_chado_semweb_term('contact', 'name');
  136. $description_term = tripal_get_chado_semweb_term('contact', 'description');
  137. if ($field_table == 'biomaterial') {
  138. if ($record) {
  139. $contact = $record->biosourceprovider_id;
  140. // Join the contact table
  141. $calias = $alias . '_provider_id';
  142. $this->queryJoinOnce($query, 'contact', $calias, "base.biosourceprovider_id = $calias.contact_id");
  143. // Search by the contact name
  144. if ($condition['column'] == $field_term_id or
  145. $condition['column'] == $field_term_id . ',' . $name_term) {
  146. $query->condition("$calias.name", $condition['value'], $operator);
  147. }
  148. // Search on the contact description.
  149. if ($condition['column'] == $field_term_id . ',' . $description_term) {
  150. $query->condition("$calias.description", $condition['value'], $operator);
  151. }
  152. // Search on the contact type.
  153. if ($condition['column'] == $field_term_id . ',' . $type_term) {
  154. $talias = $alias . 'provider_contact_type';
  155. $this->queryJoinOnce($query, 'cvterm', $talias, "$calias.type_id = $talias.cvterm_id");
  156. $query->condition("$talias.name", $condition['value'], $operator);
  157. }
  158. }
  159. }
  160. }
  161. /**
  162. * @see ChadoField::queryOrder()
  163. */
  164. public function queryOrder($query, $order) {
  165. $alias = $this->field['field_name'];
  166. $field_term_id = $this->getFieldTermID();
  167. $type_term = tripal_get_chado_semweb_term('contact', 'type_id');
  168. $name_term = tripal_get_chado_semweb_term('contact', 'name');
  169. $description_term = tripal_get_chado_semweb_term('contact', 'description');
  170. if ($field_table == 'biomaterial') {
  171. if ($record) {
  172. $contact = $record->biosourceprovider_id;
  173. // Join the contact linker table and then join the contact table.
  174. $calias = $alias . '_provider_id';
  175. $this->queryJoinOnce($query, 'contact', $calias, "base.biosourceprovider_id = $calias.contact_id");
  176. // Search by the contact name
  177. if ($order['column'] == $field_term_id or
  178. $order['column'] == $field_term_id . ',' . $name_term) {
  179. $query->orderBy("$calias.name", $order['direction']);
  180. }
  181. // Search on the contact description.
  182. if ($order['column'] == $field_term_id . ',' . $description_term) {
  183. $query->orderBy("$calias.description", $order['direction']);
  184. }
  185. // Search on the contact type.
  186. if ($order['column'] == $field_term_id . ',' . $type_term) {
  187. $talias = $alias . 'provider_contact_type';
  188. $this->queryJoinOnce($query, 'cvterm', $talias, "$calias.type_id = $talias.cvterm_id", "LEFT OUTER");
  189. $query->orderBy("$talias.name", $order['direction']);
  190. }
  191. }
  192. }
  193. }
  194. }