local__contact.inc

File

tripal_chado/includes/TripalFields/local__contact/local__contact.inc
View source
  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 = 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 array(
  56. $field_term => array(
  57. 'operations' => array('eq', 'contains', 'starts'),
  58. 'sortable' => TRUE,
  59. 'searchable' => TRUE,
  60. 'type' => 'xs:complexType',
  61. 'readonly' => TRUE,
  62. 'elements' => array(
  63. $type_term => array(
  64. 'searchable' => TRUE,
  65. 'label' => 'Contact Type',
  66. 'help' => 'The type of contact',
  67. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  68. 'sortable' => TRUE,
  69. 'type' => 'xs:string',
  70. 'readonly' => FALSE,
  71. 'required' => TRUE,
  72. ),
  73. $name_term => array(
  74. 'searchable' => TRUE,
  75. 'label' => 'Contact Name',
  76. 'help' => 'The name of the contact.',
  77. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  78. 'sortable' => TRUE,
  79. 'type' => 'xs:string',
  80. 'readonly' => FALSE,
  81. 'required' => TRUE,
  82. ),
  83. $description_term => array(
  84. 'searchable' => TRUE,
  85. 'label' => 'Contact Description',
  86. 'help' => 'A descriptoin of the contact.',
  87. 'operations' => array('contains'),
  88. 'sortable' => TRUE,
  89. 'type' => 'xs:string',
  90. 'readonly' => FALSE,
  91. 'required' => FALSE,
  92. ),
  93. 'entity' => array(
  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] = array(
  116. 'value' => array(),
  117. );
  118. // Handle the biomaterial table.
  119. if ($field_table == 'biomaterial') {
  120. if ($record) {
  121. $contact = $record->biosourceprovider_id;
  122. if ($contact) {
  123. $entity->{$field_name}['und'][0] = array(
  124. 'value' => array(
  125. $type_term => $contact->type_id ? $contact->type_id->name : '',
  126. $name_term => $contact->name,
  127. $description_term => $contact->description,
  128. ),
  129. $entity->{$field_name}['und'][0]['chado-biomaterial__biosourceprovider_id'] = $contact->contact_id,
  130. );
  131. if (property_exists($contact, 'entity_id')) {
  132. $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $contact->entity_id;
  133. }
  134. }
  135. }
  136. };
  137. // Here place other non-linker tables that have a FK to the contact table.
  138. }
  139. /**
  140. * @see ChadoField::query()
  141. */
  142. public function query($query, $condition) {
  143. $alias = $this->field['field_name'];
  144. $operator = $condition['operator'];
  145. $field_term_id = $this->getFieldTermID();
  146. $type_term = chado_get_semweb_term('contact', 'type_id');
  147. $name_term = chado_get_semweb_term('contact', 'name');
  148. $description_term = chado_get_semweb_term('contact', 'description');
  149. if ($field_table == 'biomaterial') {
  150. if ($record) {
  151. $contact = $record->biosourceprovider_id;
  152. // Join the contact table
  153. $calias = $alias . '_provider_id';
  154. $this->queryJoinOnce($query, 'contact', $calias, "base.biosourceprovider_id = $calias.contact_id");
  155. // Search by the contact name
  156. if ($condition['column'] == $field_term_id or
  157. $condition['column'] == $field_term_id . ',' . $name_term) {
  158. $query->condition("$calias.name", $condition['value'], $operator);
  159. }
  160. // Search on the contact description.
  161. if ($condition['column'] == $field_term_id . ',' . $description_term) {
  162. $query->condition("$calias.description", $condition['value'], $operator);
  163. }
  164. // Search on the contact type.
  165. if ($condition['column'] == $field_term_id . ',' . $type_term) {
  166. $talias = $alias . 'provider_contact_type';
  167. $this->queryJoinOnce($query, 'cvterm', $talias, "$calias.type_id = $talias.cvterm_id");
  168. $query->condition("$talias.name", $condition['value'], $operator);
  169. }
  170. }
  171. }
  172. }
  173. /**
  174. * @see ChadoField::queryOrder()
  175. */
  176. public function queryOrder($query, $order) {
  177. $alias = $this->field['field_name'];
  178. $field_term_id = $this->getFieldTermID();
  179. $type_term = chado_get_semweb_term('contact', 'type_id');
  180. $name_term = chado_get_semweb_term('contact', 'name');
  181. $description_term = chado_get_semweb_term('contact', 'description');
  182. if ($field_table == 'biomaterial') {
  183. if ($record) {
  184. $contact = $record->biosourceprovider_id;
  185. // Join the contact linker table and then join the contact table.
  186. $calias = $alias . '_provider_id';
  187. $this->queryJoinOnce($query, 'contact', $calias, "base.biosourceprovider_id = $calias.contact_id");
  188. // Search by the contact name
  189. if ($order['column'] == $field_term_id or
  190. $order['column'] == $field_term_id . ',' . $name_term) {
  191. $query->orderBy("$calias.name", $order['direction']);
  192. }
  193. // Search on the contact description.
  194. if ($order['column'] == $field_term_id . ',' . $description_term) {
  195. $query->orderBy("$calias.description", $order['direction']);
  196. }
  197. // Search on the contact type.
  198. if ($order['column'] == $field_term_id . ',' . $type_term) {
  199. $talias = $alias . 'provider_contact_type';
  200. $this->queryJoinOnce($query, 'cvterm', $talias, "$calias.type_id = $talias.cvterm_id", "LEFT OUTER");
  201. $query->orderBy("$talias.name", $order['direction']);
  202. }
  203. }
  204. }
  205. }
  206. }