sio__vocabulary.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. class sio__vocabulary extends ChadoField {
  3. // The default lable for this field.
  4. public static $default_label = 'Vocabulary';
  5. // The default description for this field.
  6. public static $description = 'The vocabulary to which this resource is associated.';
  7. // Provide a list of instance specific settings. These can be access within
  8. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  9. // then Drupal with automatically change these settings for the instnace.
  10. // It is recommended to put settings at the instance level whenever possible.
  11. // If you override this variable in a child class be sure to replicate the
  12. // term_name, term_vocab, term_accession and term_fixed keys as these are
  13. // required for all TripalFields.
  14. public static $default_instance_settings = array(
  15. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  16. 'term_vocabulary' => 'SIO',
  17. // The name of the term.
  18. 'term_name' => 'vocabulary',
  19. // The unique ID (i.e. accession) of the term.
  20. 'term_accession' => '001080',
  21. // Set to TRUE if the site admin is allowed to change the term
  22. // type. This will create form elements when editing the field instance
  23. // to allow the site admin to change the term settings above.
  24. 'term_fixed' => FALSE,
  25. );
  26. // The default widget for this field.
  27. public static $default_widget = 'sio__vocabulary_widget';
  28. // The default formatter for this field.
  29. public static $default_formatter = 'sio__vocabulary_formatter';
  30. /**
  31. * @see TripalField::validate()
  32. */
  33. public function validate($entity_type, $entity, $field, $items, &$errors) {
  34. $settings = $this->field['settings'];
  35. $field_name = $this->field['field_name'];
  36. $field_type = $this->field['type'];
  37. $field_table = $this->instance['settings']['chado_table'];
  38. $field_column = $this->instance['settings']['chado_column'];
  39. // Get the field values.
  40. foreach ($items as $delta => $values) {
  41. // Get the field values.
  42. $cv_id = $values['chado-' . $field_table . '__cv_id'];
  43. if (!$cv_id or $cv_id == 0) {
  44. $errors[$field_name]['und'][0][] = array(
  45. 'message' => t("Please specify a vocabulary."),
  46. 'error' => 'chado-' . $field_table . '__cv_id'
  47. );
  48. }
  49. }
  50. }
  51. /**
  52. * @see TripalField::load()
  53. */
  54. public function load($entity) {
  55. $record = $entity->chado_record;
  56. $settings = $this->instance['settings'];
  57. $field_name = $this->field['field_name'];
  58. $field_type = $this->field['type'];
  59. $field_table = $this->instance['settings']['chado_table'];
  60. $field_column = $this->instance['settings']['chado_column'];
  61. // Set some defaults for the empty record.
  62. $entity->{$field_name}['und'][0] = array(
  63. 'value' => array(),
  64. );
  65. if ($record) {
  66. $cv = $record->cv_id;
  67. $entity->{$field_name}['und'][0]['value'] = $cv->name;
  68. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__cv_id'] = $cv->cv_id;
  69. }
  70. }
  71. /**
  72. * @see ChadoField::query()
  73. */
  74. public function query($query, $condition) {
  75. $alias = $this->field['field_name'];
  76. $operator = $condition['operator'];
  77. $query->join('cv', $alias, "base.cv_id = $alias.cv_id");
  78. $query->condition("$alias.name", $condition['value'], $operator);
  79. }
  80. /**
  81. * @see ChadoField::queryOrder()
  82. */
  83. public function queryOrder($query, $order) {
  84. // If the table hasn't yet been joined then add it.
  85. $joins = $query->getTables();
  86. if (!in_array($this->field['field_name'], $joins)) {
  87. $alias = $this->field['field_name'];
  88. $query->join('cv', $alias, "base.cv_id = $alias.cv_id");
  89. $query->orderBy("$alias.name", $order['direction']);
  90. }
  91. }
  92. }