chado_linker__contact.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. class chado_linker__contact extends TripalField {
  3. /**
  4. * @see TripalField::fieldInfo()
  5. */
  6. public static function fieldInfo() {
  7. return array(
  8. 'label' => t('Contacts'),
  9. 'description' => t('Associates an indviddual or organization with
  10. this record.'),
  11. 'default_widget' => 'chado_linker__contact_widget',
  12. 'default_formatter' => 'chado_linker__contact_formatter',
  13. 'settings' => array(),
  14. 'storage' => array(
  15. 'type' => 'field_chado_storage',
  16. 'module' => 'tripal_chado',
  17. 'active' => TRUE
  18. ),
  19. );
  20. }
  21. /**
  22. * @see TripalField::can_attach()
  23. */
  24. protected function setCanAttach() {
  25. $table_name = $this->details['chado_table'];
  26. $type_table = $this->details['chado_type_table'];
  27. $type_field = $this->details['chado_type_column'];
  28. $cv_id = $this->details['chado_cv_id'];
  29. $cvterm_id = $this->details['chado_cvterm_id'];;
  30. // If the linker table does not exists then we don't want to add attach.
  31. $contact_table = $table_name . '_contact';
  32. if (chado_table_exists($contact_table)) {
  33. $this->can_attach = TRUE;
  34. return;
  35. }
  36. $this->can_attach = FALSE;
  37. }
  38. /**
  39. * @see TripalField::create_info()
  40. */
  41. function createInfo() {
  42. if (!$this->can_attach) {
  43. return;
  44. }
  45. $table_name = $this->details['chado_table'];
  46. $type_table = $this->details['chado_type_table'];
  47. $type_field = $this->details['chado_type_column'];
  48. $cv_id = $this->details['chado_cv_id'];
  49. $cvterm_id = $this->details['chado_cvterm_id'];
  50. $contact_table = $table_name . '_contact';
  51. $schema = chado_get_schema($contact_table);
  52. $pkey = $schema['primary key'][0];
  53. return array(
  54. 'field_name' => $contact_table,
  55. 'type' => 'chado_linker__contact',
  56. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  57. 'locked' => FALSE,
  58. 'storage' => array(
  59. 'type' => 'field_chado_storage',
  60. ),
  61. 'settings' => array(
  62. 'chado_table' => $contact_table,
  63. 'chado_column' => $pkey,
  64. 'base_table' => $table_name,
  65. 'semantic_web' => 'local:contact'
  66. ),
  67. );
  68. }
  69. /**
  70. * @see TripalField::createInstanceInfo()
  71. */
  72. function createInstanceInfo() {
  73. if (!$this->can_attach) {
  74. return;
  75. }
  76. $table_name = $this->details['chado_table'];
  77. $type_table = $this->details['chado_type_table'];
  78. $type_field = $this->details['chado_type_column'];
  79. $cv_id = $this->details['chado_cv_id'];
  80. $cvterm_id = $this->details['chado_cvterm_id'];;
  81. $contact_table = $table_name . '_contact';
  82. return array(
  83. 'field_name' => $contact_table,
  84. 'entity_type' => $this->entity_type,
  85. 'bundle' => $this->bundle->name,
  86. 'label' => 'Contacts',
  87. 'description' => 'An individual, organization or entity that has had
  88. some responsibility for the creation, delivery or maintenance of
  89. the associated data.',
  90. 'required' => FALSE,
  91. 'settings' => array(
  92. 'auto_attach' => FALSE,
  93. ),
  94. 'widget' => array(
  95. 'type' => 'chado_linker__contact_widget',
  96. 'settings' => array(
  97. 'display_label' => 1,
  98. ),
  99. ),
  100. 'display' => array(
  101. 'deafult' => array(
  102. 'label' => 'above',
  103. 'type' => 'chado_linker__contact_formatter',
  104. 'settings' => array(),
  105. ),
  106. ),
  107. );
  108. }
  109. /**
  110. * @see TripalField::widgetInfo()
  111. */
  112. public static function widgetInfo() {
  113. return array(
  114. 'chado_linker__contact_widget' => array(
  115. 'label' => t('Contacts'),
  116. 'field types' => array('chado_linker__contact'),
  117. ),
  118. );
  119. }
  120. /**
  121. * @see TripalField::formatterInfo()
  122. */
  123. public static function formatterInfo() {
  124. return array(
  125. 'chado_linker__contact_formatter' => array(
  126. 'label' => t('Contacts'),
  127. 'field types' => array('chado_linker__contact'),
  128. 'settings' => array(
  129. ),
  130. ),
  131. );
  132. }
  133. /**
  134. * @see TripalField::formatterView()
  135. */
  136. public static function formatterView(&$element, $entity_type, $entity,
  137. $field, $instance, $langcode, $items, $display) {
  138. // Get the settings
  139. $settings = $display['settings'];
  140. $record = $entity->chado_record;
  141. $headers = array('Name', 'Description', 'Type');
  142. $rows = array();
  143. foreach ($items as $delta => $item) {
  144. $contact = $item['value'];
  145. if (!$contact) {
  146. continue;
  147. }
  148. // Get the field values
  149. $contact_name = $contact['name'];
  150. $description = $contact['description'];
  151. $type = $contact['type'];
  152. // Add a link i there is an entity.
  153. if (array_key_exists('entity_id', $item) and $item['entity_id']) {
  154. $entity_id = $item['entity_id'];
  155. $contact_name = l($contact_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  156. }
  157. $rows[] = array($contact_name, $description, $type);
  158. }
  159. $table = array(
  160. 'header' => $headers,
  161. 'rows' => $rows,
  162. 'attributes' => array(
  163. 'id' => 'tripal_linker-table-contact-object',
  164. 'class' => 'tripal-data-table'
  165. ),
  166. 'sticky' => FALSE,
  167. 'caption' => "",
  168. 'colgroups' => array(),
  169. 'empty' => 'No contacts available',
  170. );
  171. $content = theme_table($table);
  172. if (count($items) > 0) {
  173. // once we have our table array structure defined, we call Drupal's theme_table()
  174. // function to generate the table.
  175. $element[0] = array(
  176. '#type' => 'markup',
  177. '#markup' => $content,
  178. );
  179. }
  180. }
  181. /**
  182. * @see TripalField::widgetForm()
  183. */
  184. public static function widgetForm(&$widget, &$form, &$form_state, $field, $instance,
  185. $langcode, $items, $delta, $element) {
  186. }
  187. /**
  188. * @see TripalField::load()
  189. */
  190. public function load($field, $entity, $details) {
  191. $record = $details['record'];
  192. $field_name = $field['field_name'];
  193. $field_type = $field['type'];
  194. $field_table = $field['settings']['chado_table'];
  195. $field_column = $field['settings']['chado_column'];
  196. // Get the FK that links to the base record.
  197. $schema = chado_get_schema($field_table);
  198. $base_table = $details['record']->tablename;
  199. $pkey = $schema['primary key'][0];
  200. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  201. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  202. // Set some defaults for the empty record.
  203. $entity->{$field_name}['und'][0] = array(
  204. 'value' => array(),
  205. $field_table . '__' . $pkey => '',
  206. $field_table . '__' . $fkey_lcolumn => '',
  207. $field_table . '__' . 'contact_id' => '',
  208. // Ignore the synonym_sgml column for now.
  209. );
  210. $linker_table = $base_table . '_contact';
  211. $options = array(
  212. 'return_array' => 1,
  213. 'include_fk' => array(
  214. 'contact_id' => array(
  215. 'type_id' => array(
  216. 'dbxref_id' => array(
  217. 'db_id' => TRUE,
  218. ),
  219. ),
  220. ),
  221. $fkey_lcolumn => TRUE,
  222. ),
  223. );
  224. $record = chado_expand_var($record, 'table', $linker_table, $options);
  225. $contact_linkers = $record->$linker_table;
  226. if ($contact_linkers) {
  227. foreach ($contact_linkers as $i => $contact_linker) {
  228. $contact = $contact_linker->contact_id;
  229. $entity->{$field_name}['und'][$i] = array(
  230. 'value' => array(
  231. '@type' => $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession,
  232. 'type' => $contact->type_id->name,
  233. 'name' => $contact->name,
  234. 'description' => $contact->description,
  235. ),
  236. $field_table . '__' . $pkey => $contact_linker->$pkey,
  237. $field_table . '__' . $fkey_lcolumn => $contact_linker->$fkey_lcolumn->$fkey_lcolumn,
  238. $field_table . '__' . 'contact_id' => $contact->contact_id
  239. );
  240. if (property_exists($contact, 'entity_id')) {
  241. $entity->{$field_name}['und'][$i]['entity_id'] = $contact->entity_id;
  242. $entity->{$field_name}['und'][$i]['entity_type'] = 'TripalEntity';
  243. }
  244. }
  245. }
  246. }
  247. }