sep__protocol.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * @class
  4. * Purpose: Provide a field for Protocol (typically the protocol_id column of a
  5. * Chado table).
  6. *
  7. * Data:
  8. * Assumptions:
  9. */
  10. class sep__protocol extends ChadoField {
  11. // The default label for this field.
  12. public static $default_label = 'Protocol';
  13. // The default description for this field.
  14. public static $default_description = 'The protocol followed to generate this resource.';
  15. // The default widget for this field.
  16. public static $default_widget = 'sep__protocol_widget';
  17. // The default formatter for this field.
  18. public static $default_formatter = 'sep__protocol_formatter';
  19. // The module that manages this field.
  20. public static $module = 'tripal_chado';
  21. // A list of global settings. These can be accessed within the
  22. // globalSettingsForm. When the globalSettingsForm is submitted then
  23. // Drupal will automatically change these settings for all fields.
  24. // Once instances exist for a field type then these settings cannot be
  25. // changed.
  26. public static $default_settings = [
  27. 'storage' => 'field_chado_storage',
  28. // It is expected that all fields set a 'value' in the load() function.
  29. // In many cases, the value may be an associative array of key/value pairs.
  30. // In order for Tripal to provide context for all data, the keys should
  31. // be a controlled vocabulary term (e.g. rdfs:type). Keys in the load()
  32. // function that are supported by the query() function should be
  33. // listed here.
  34. 'searchable_keys' => [],
  35. ];
  36. // Provide a list of instance specific settings. These can be access within
  37. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  38. // then Drupal with automatically change these settings for the instance.
  39. // It is recommended to put settings at the instance level whenever possible.
  40. // If you override this variable in a child class be sure to replicate the
  41. // term_name, term_vocab, term_accession and term_fixed keys as these are
  42. // required for all TripalFields.
  43. public static $default_instance_settings = [
  44. // The DATABASE name, as it appears in chado.db. This also builds the link-out url. In most cases this will simply be the CV name. In some cases (EDAM) this will be the SUBONTOLOGY.
  45. 'term_vocabulary' => 'sep',
  46. // The name of the term.
  47. 'term_name' => 'protocol',
  48. // The unique ID (i.e. accession) of the term.
  49. 'term_accession' => '00101',
  50. // Set to TRUE if the site admin is not allowed to change the term
  51. // type, otherwise the admin can change the term mapped to a field.
  52. 'term_fixed' => FALSE,
  53. // Indicates if this field should be automatically attached to display
  54. // or web services or if this field should be loaded separately. This
  55. // is convenient for speed. Fields that are slow should for loading
  56. // should have auto_attach set to FALSE so tha their values can be
  57. // attached asynchronously.
  58. 'auto_attach' => FALSE,
  59. ];
  60. // A boolean specifying that users should not be allowed to create
  61. // fields and instances of this field type through the UI. Such
  62. // fields can only be created programmatically with field_create_field()
  63. // and field_create_instance().
  64. public static $no_ui = FALSE;
  65. // A boolean specifying that the field will not contain any data. This
  66. // should exclude the field from web services or downloads. An example
  67. // could be a quick search field that appears on the page that redirects
  68. // the user but otherwise provides no data.
  69. public static $no_data = FALSE;
  70. /**
  71. * Loads the field values from the underlying data store.
  72. *
  73. * @param $entity
  74. *
  75. * @return
  76. * An array of the following format:
  77. * $entity->{$field_name}['und'][0]['value'] = $value;
  78. * where:
  79. * - $entity is the entity object to which this field is attached.
  80. * - $field_name is the name of this field
  81. * - 'und' is the language code (in this case 'und' == undefined)
  82. * - 0 is the cardinality. Increment by 1 when more than one item is
  83. * available.
  84. * - 'value' is the key indicating the value of this field. It should
  85. * always be set. The value of the 'value' key will be the contents
  86. * used for web services and for downloadable content. The value
  87. * should be of the follow format types: 1) A single value (text,
  88. * numeric, etc.) 2) An array of key value pair. 3) If multiple entries
  89. * then cardinality should incremented and format types 1 and 2 should
  90. * be used for each item.
  91. * The array may contain as many other keys at the same level as 'value'
  92. * but those keys are for internal field use and are not considered the
  93. * value of the field.
  94. *
  95. *
  96. */
  97. public function load($entity) {
  98. parent::load($entity);
  99. $record = $entity->chado_record;
  100. $settings = $this->instance['settings'];
  101. $field_name = $this->field['field_name'];
  102. $field_type = $this->field['type'];
  103. $field_table = $this->instance['settings']['chado_table'];
  104. $field_column = $this->instance['settings']['chado_column'];
  105. $base_table = $this->instance['settings']['base_table'];
  106. $linker_field = 'chado-' . $field_table . '__' . $field_column;
  107. // Set some defaults for the empty record.
  108. $entity->{$field_name}['und'][0] = [
  109. 'value' => [],
  110. ];
  111. if (!$record) {
  112. return;
  113. }
  114. $protocol = $record->{$field_column};
  115. $entity_id = $record->entity_id;
  116. // Get the Protocol controlled vocabulary terms.
  117. $protocol_name_term = chado_get_semweb_term('protocol', 'name');
  118. $protocol_type_term = chado_get_semweb_term('protocol', 'type_id');
  119. $entity->{$field_name}['und'][0]['value'] = [
  120. $protocol_name_term => $protocol->name,
  121. $protocol_type_term => $protocol->type_id->name,
  122. ];
  123. $entity->{$field_name}['und'][0][$linker_field] = $protocol->protocol_id;
  124. // Is there a published entity for this protocol?
  125. if (property_exists($record->{$field_column}, 'entity_id')) {
  126. $entity->{$field_name}['und'][0]['value']['entity_id'] = 'TripalEntity:' . $record->{$field_column}->entity_id;
  127. }
  128. }
  129. /**
  130. * @see TripalField::elementInfo()
  131. */
  132. function elementInfo() {
  133. $field_term = $this->getFieldTermID();
  134. $type_id_term = tripal_get_chado_semweb_term('protocol', 'type_id');
  135. return [
  136. $field_term => [
  137. 'operations' => ['eq', 'contains', 'starts'],
  138. 'sortable' => TRUE,
  139. 'searchable' => TRUE,
  140. 'readonly' => FALSE,
  141. 'type' => 'xs:complexType',
  142. 'elements' => [
  143. 'rdfs:label' => [
  144. 'searchable' => TRUE,
  145. 'name' => 'protocol_name',
  146. 'operations' => ['eq', 'ne', 'contains', 'starts'],
  147. 'sortable' => FALSE,
  148. 'type' => 'xs:string',
  149. 'readonly' => TRUE,
  150. 'required' => FALSE,
  151. ],
  152. 'entity' => [
  153. 'searchable' => FALSE,
  154. ],
  155. $type_id_term => [
  156. 'searchable' => TRUE,
  157. 'name' => 'protocol_type',
  158. 'operations' => ['eq', 'ne', 'contains', 'starts'],
  159. 'sortable' => TRUE,
  160. 'readonly' => FALSE,
  161. 'required' => TRUE,
  162. 'type' => 'xs:integer',
  163. ],
  164. ],
  165. ],
  166. ];
  167. }
  168. /**
  169. * Provide query support. We only make use of the name and type.
  170. * @param $query
  171. * @param $condition
  172. */
  173. function query($query, $condition) {
  174. $alias = $this->field['field_name'];
  175. $operator = $condition['operator'];
  176. $field_term_id = $this->getFieldTermID();
  177. $type_id_term = $field_term_id . ',' . tripal_get_chado_semweb_term('protocol', 'type_id');
  178. // Join to the protocol table for this field.
  179. $this->queryJoinOnce($query, 'protocol', $alias, "base.protocol_id = $alias.protocol_id");
  180. if ($condition['column'] == $field_term_id or
  181. $condition['column'] == $field_term_id . ',rdfs:label') {
  182. $query->condition("$alias.name", $condition['value'], $operator);
  183. }
  184. if ($condition['column'] == $type_id_term) {
  185. $this->queryJoinOnce($query, 'cvterm', 'CVT', "base.type_id = CVT.cvterm_id");
  186. $query->condition("CVT.name", $condition['value'], $operator);
  187. }
  188. }
  189. function queryOrder($query, $order) {
  190. $alias = $this->field['field_name'];
  191. $field_term_id = $this->getFieldTermID();
  192. $type_id_term = tripal_get_chado_semweb_term('protocol', 'type_id');
  193. // Join to the protocol table for this field.
  194. $this->queryJoinOnce($query, 'protocol', $alias, "base.protocol_id = $alias.organism_id");
  195. if ($order['column'] == $type_id_term) {
  196. if (!in_array('CVT', $joins)) {
  197. $this->queryJoinOnce($query, 'cvterm', 'CVT', "base.type_id = CVT.cvterm_id");
  198. }
  199. $query->orderBy("CVT.name", $order['direction']);
  200. }
  201. }
  202. }