schema__publication.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. class schema__publication 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 = 'Publication';
  12. // The default description for this field.
  13. public static $description = 'Associates a publication (e.g. journal article,
  14. conference proceedings, book chapter, etc.) with this record.';
  15. // Provide a list of instance specific settings. These can be access within
  16. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  17. // then Drupal with automatically change these settings for the instnace.
  18. // It is recommended to put settings at the instance level whenever possible.
  19. // If you override this variable in a child class be sure to replicate the
  20. // term_name, term_vocab, term_accession and term_fixed keys as these are
  21. // required for all TripalFields.
  22. public static $default_instance_settings = array(
  23. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  24. 'term_vocabulary' => 'schema',
  25. // The name of the term.
  26. 'term_name' => 'publication',
  27. // The unique ID (i.e. accession) of the term.
  28. 'term_accession' => 'publication',
  29. // Set to TRUE if the site admin is allowed to change the term
  30. // type. This will create form elements when editing the field instance
  31. // to allow the site admin to change the term settings above.
  32. 'term_fixed' => FALSE,
  33. );
  34. // The default widget for this field.
  35. public static $default_widget = 'schema__publication_widget';
  36. // The default formatter for this field.
  37. public static $default_formatter = 'schema__publication_formatter';
  38. // --------------------------------------------------------------------------
  39. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  40. // --------------------------------------------------------------------------
  41. // An array containing details about the field. The format of this array
  42. // is the same as that returned by field_info_fields()
  43. protected $field;
  44. // An array containing details about an instance of the field. A field does
  45. // not have to have an instance. But if dealing with an instance (such as
  46. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  47. protected $instance;
  48. /**
  49. * @see TripalField::elementInfo()
  50. */
  51. public function elementInfo() {
  52. $field_term = $this->getFieldTermID();
  53. return array(
  54. $field_term => array(
  55. 'operations' => array(),
  56. 'sortable' => FALSE,
  57. 'searchable' => FALSE,
  58. 'type' => 'xs:string',
  59. 'readonly' => TRUE,
  60. ),
  61. );
  62. }
  63. /**
  64. *
  65. * @see TripalField::load()
  66. */
  67. public function load($entity) {
  68. $record = $entity->chado_record;
  69. $field_name = $this->field['field_name'];
  70. $field_type = $this->field['type'];
  71. $field_table = $this->instance['settings']['chado_table'];
  72. $field_column = $this->instance['settings']['chado_column'];
  73. $base_table = $this->instance['settings']['base_table'];
  74. // These fields are used when the publications come through a linker table.
  75. $pkey = '';
  76. $fkey_lcolumn = '';
  77. $fkey_rcolumn = '';
  78. $linker_table = '';
  79. // If we don't have a chado record return before creating a stub for this field!
  80. if (!$record) {
  81. return;
  82. }
  83. // If the field table and the base table are not the same thing then
  84. // we are going through a linker table.
  85. if ($field_table != $base_table) {
  86. // Get the FK that links to the base record.
  87. $schema = chado_get_schema($field_table);
  88. $pkey = $schema['primary key'][0];
  89. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  90. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  91. $linker_table = $base_table . '_pub';
  92. // Set some defaults for the empty record.
  93. $entity->{$field_name}['und'][0] = array(
  94. 'value' => array(),
  95. 'chado-' . $field_table . '__' . $pkey => '',
  96. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  97. 'chado-' . $field_table . '__' . 'pub_id' => '',
  98. );
  99. }
  100. // Otherwise, the base table has a pub_id field.
  101. else {
  102. $entity->{$field_name}['und'][0] = array(
  103. 'value' => array(),
  104. 'chado-' . $field_table . '__' . $field_column => '',
  105. );
  106. }
  107. // Get the list of publications
  108. $pubs = array();
  109. if ($linker_table) {
  110. $options = array(
  111. 'return_array' => 1,
  112. );
  113. $record = chado_expand_var($record, 'table', $linker_table, $options);
  114. if (property_exists($record, $linker_table) and is_array($record->$linker_table) and count($record->$linker_table) > 0) {
  115. $i = 0;
  116. foreach ($record->$linker_table as $index => $linker) {
  117. $pub = $linker->pub_id;
  118. $pubs[$pub->pub_id] = $pub;
  119. }
  120. }
  121. }
  122. else {
  123. $pub = $record->{$field_column};
  124. if ($pub) {
  125. $pubs[$pub->pub_id] = $pub;
  126. }
  127. }
  128. // Ensure we don't have a value if there are no publications.
  129. // This is needed due to stubbing out the field above.
  130. if (sizeof($pubs) == 0) {
  131. unset($entity->{$field_name});
  132. }
  133. $i = 0;
  134. foreach ($pubs as $pub_id => $pub) {
  135. $pub_details = chado_get_minimal_pub_info($pub);
  136. $entity->{$field_name}['und'][$i]['value'] = $pub_details;
  137. if ($linker_table) {
  138. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $pkey] = $linker->$pkey;
  139. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $fkey_lcolumn] = $linker->$fkey_lcolumn->$fkey_lcolumn;
  140. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . 'pub_id'] = $pub_id;
  141. }
  142. else {
  143. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $field_column] = $pub_id;
  144. }
  145. if (property_exists($pub, 'entity_id')) {
  146. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $pub->entity_id;
  147. }
  148. $i++;
  149. }
  150. }
  151. }