sep__protocol.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. if (!$record) {
  108. return;
  109. }
  110. $protocol = $record->{$field_column};
  111. // If this record does not have a protcol then just return.
  112. if (!$protocol) {
  113. return;
  114. }
  115. // Set some defaults for the empty record.
  116. $entity->{$field_name}['und'][0] = [
  117. 'value' => [],
  118. ];
  119. $entity_id = $record->entity_id;
  120. // Get the Protocol controlled vocabulary terms.
  121. $protocol_name_term = chado_get_semweb_term('protocol', 'name');
  122. $protocol_type_term = chado_get_semweb_term('protocol', 'type_id');
  123. $entity->{$field_name}['und'][0]['value'] = [
  124. $protocol_name_term => $protocol->name,
  125. $protocol_type_term => $protocol->type_id->name,
  126. ];
  127. $entity->{$field_name}['und'][0][$linker_field] = $protocol->protocol_id;
  128. // Is there a published entity for this protocol?
  129. if (property_exists($record->{$field_column}, 'entity_id')) {
  130. $entity->{$field_name}['und'][0]['value']['entity_id'] = 'TripalEntity:' . $record->{$field_column}->entity_id;
  131. }
  132. }
  133. /**
  134. * @see TripalField::elementInfo()
  135. */
  136. function elementInfo() {
  137. $field_term = $this->getFieldTermID();
  138. $type_id_term = tripal_get_chado_semweb_term('protocol', 'type_id');
  139. return [
  140. $field_term => [
  141. 'operations' => ['eq', 'contains', 'starts'],
  142. 'sortable' => TRUE,
  143. 'searchable' => TRUE,
  144. 'readonly' => FALSE,
  145. 'type' => 'xs:complexType',
  146. 'elements' => [
  147. 'rdfs:label' => [
  148. 'searchable' => TRUE,
  149. 'name' => 'protocol_name',
  150. 'operations' => ['eq', 'ne', 'contains', 'starts'],
  151. 'sortable' => FALSE,
  152. 'type' => 'xs:string',
  153. 'readonly' => TRUE,
  154. 'required' => FALSE,
  155. ],
  156. 'entity' => [
  157. 'searchable' => FALSE,
  158. ],
  159. $type_id_term => [
  160. 'searchable' => TRUE,
  161. 'name' => 'protocol_type',
  162. 'operations' => ['eq', 'ne', 'contains', 'starts'],
  163. 'sortable' => TRUE,
  164. 'readonly' => FALSE,
  165. 'required' => TRUE,
  166. 'type' => 'xs:integer',
  167. ],
  168. ],
  169. ],
  170. ];
  171. }
  172. /**
  173. * Provide query support. We only make use of the name and type.
  174. * @param $query
  175. * @param $condition
  176. */
  177. function query($query, $condition) {
  178. $alias = $this->field['field_name'];
  179. $operator = $condition['operator'];
  180. $field_term_id = $this->getFieldTermID();
  181. $type_id_term = $field_term_id . ',' . tripal_get_chado_semweb_term('protocol', 'type_id');
  182. // Join to the protocol table for this field.
  183. $this->queryJoinOnce($query, 'protocol', $alias, "base.protocol_id = $alias.protocol_id");
  184. if ($condition['column'] == $field_term_id or
  185. $condition['column'] == $field_term_id . ',rdfs:label') {
  186. $query->condition("$alias.name", $condition['value'], $operator);
  187. }
  188. if ($condition['column'] == $type_id_term) {
  189. $this->queryJoinOnce($query, 'cvterm', 'CVT', "base.type_id = CVT.cvterm_id");
  190. $query->condition("CVT.name", $condition['value'], $operator);
  191. }
  192. }
  193. function queryOrder($query, $order) {
  194. $alias = $this->field['field_name'];
  195. $field_term_id = $this->getFieldTermID();
  196. $type_id_term = tripal_get_chado_semweb_term('protocol', 'type_id');
  197. // Join to the protocol table for this field.
  198. $this->queryJoinOnce($query, 'protocol', $alias, "base.protocol_id = $alias.organism_id");
  199. if ($order['column'] == $type_id_term) {
  200. if (!in_array('CVT', $joins)) {
  201. $this->queryJoinOnce($query, 'cvterm', 'CVT', "base.type_id = CVT.cvterm_id");
  202. }
  203. $query->orderBy("CVT.name", $order['direction']);
  204. }
  205. }
  206. }