chado_linker__contact.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. class chado_linker__contact extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Contacts';
  5. // The default description for this field.
  6. public static $default_description = 'Associates an indviddual or organization with
  7. this record';
  8. // Add any default settings elements. If you override the globalSettingsForm()
  9. // or the instanceSettingsForm() functions then you need to be sure that
  10. // any settings you want those functions to manage are listed in this
  11. // array.
  12. public static $default_settings = array(
  13. 'chado_table' => '',
  14. 'chado_column' => '',
  15. 'base_table' => '',
  16. 'semantic_web' => '',
  17. );
  18. // Set this to the name of the storage backend that by default will support
  19. // this field.
  20. public static $default_storage = 'field_chado_storage';
  21. /**
  22. * @see TripalField::formatterView()
  23. */
  24. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  25. // Get the settings
  26. $settings = $display['settings'];
  27. $headers = array('Name', 'Description', 'Type');
  28. $rows = array();
  29. foreach ($items as $delta => $item) {
  30. $contact = $item['value'];
  31. if (!$contact) {
  32. continue;
  33. }
  34. // Get the field values
  35. $contact_name = $contact['name'];
  36. $description = $contact['description'];
  37. $type = $contact['type'];
  38. // Add a link i there is an entity.
  39. if (array_key_exists('entity', $item['value']) and $item['value']['entity']) {
  40. list($entity_type, $entity_id) = explode(':', $item['value']['entity']);
  41. $contact_name = l($contact_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  42. }
  43. $rows[] = array($contact_name, $description, $type);
  44. }
  45. $table = array(
  46. 'header' => $headers,
  47. 'rows' => $rows,
  48. 'attributes' => array(
  49. 'id' => 'tripal_linker-table-contact-object',
  50. 'class' => 'tripal-data-table'
  51. ),
  52. 'sticky' => FALSE,
  53. 'caption' => "",
  54. 'colgroups' => array(),
  55. 'empty' => 'No contacts available',
  56. );
  57. $content = theme_table($table);
  58. if (count($items) > 0) {
  59. // once we have our table array structure defined, we call Drupal's theme_table()
  60. // function to generate the table.
  61. $element[0] = array(
  62. '#type' => 'markup',
  63. '#markup' => $content,
  64. );
  65. }
  66. }
  67. /**
  68. * @see TripalField::widgetForm()
  69. */
  70. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  71. parent::widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
  72. $entity = $form['#entity'];
  73. $field_name = $this->field['field_name'];
  74. // Get the FK column that links to the base table.
  75. $table_name = $this->field['settings']['chado_table'];
  76. $base_table = $this->field['settings']['base_table'];
  77. $schema = chado_get_schema($table_name);
  78. $pkey = $schema['primary key'][0];
  79. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  80. $fkey = $fkeys[0];
  81. // Get the field defaults.
  82. $record_id = '';
  83. $fkey_value = $element['#entity']->chado_record_id;
  84. $contact_id = '';
  85. $name = '';
  86. // If the field already has a value then it will come through the $items
  87. // array. This happens when editing an existing record.
  88. if (count($items) > 0 and array_key_exists($delta, $items)) {
  89. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__' . $pkey, $record_id);
  90. $contact_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__contact_id', $contact_id);
  91. $name = tripal_get_field_item_keyval($items, $delta, 'name', $name);
  92. }
  93. $schema = chado_get_schema('contact');
  94. $widget['#table_name'] = $table_name;
  95. $widget['#fkey_field'] = $fkey;
  96. $widget['#theme'] = 'chado_linker__contact_widget';
  97. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  98. $widget['#suffix'] = "</span>";
  99. $widget['value'] = array(
  100. '#type' => 'value',
  101. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  102. );
  103. $widget['chado-' . $table_name . '__' . $pkey] = array(
  104. '#type' => 'value',
  105. '#default_value' => $record_id,
  106. );
  107. $widget['chado-' . $table_name . '__' . $fkey] = array(
  108. '#type' => 'value',
  109. '#default_value' => $fkey_value,
  110. );
  111. $widget['chado-' . $table_name . '__contact_id'] = array(
  112. '#type' => 'value',
  113. '#default_value' => $contact_id,
  114. );
  115. $widget['name'] = array(
  116. '#type' => 'textfield',
  117. '#title' => t('Contact'),
  118. '#default_value' => $name,
  119. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/contact',
  120. '#ajax' => array(
  121. 'callback' => "chado_linker__contact_widget_form_ajax_callback",
  122. 'wrapper' => "$table_name-$delta",
  123. 'effect' => 'fade',
  124. 'method' => 'replace'
  125. ),
  126. '#maxlength' => 100000,
  127. );
  128. }
  129. /**
  130. * @see TripalField::widgetFormSubmit()
  131. */
  132. public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  133. // Get the FK column that links to the base table.
  134. $table_name = $this->field['settings']['chado_table'];
  135. $base_table = $this->field['settings']['base_table'];
  136. $schema = chado_get_schema($table_name);
  137. $pkey = $schema['primary key'][0];
  138. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  139. $fkey = $fkeys[0];
  140. $field_name = $this->field['field_name'];
  141. // Get the field values.
  142. $fkey_value = $form_state['values'][$field_name][$langcode][$delta]['value'];
  143. $contact_id = $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'];
  144. $name = $form_state['values'][$field_name][$langcode][$delta]['name'];
  145. // If the user provided a name then we want to set the foreign key
  146. // value to be the chado_record_id
  147. if ($name and !$contact_id) {
  148. $contact = chado_generate_var('contact', array('name' => $name));
  149. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] = $contact->contact_id;
  150. }
  151. // In the widgetForm function we automatically add the foreign key
  152. // record. But if the user did not provide a contact we want to take
  153. // it out so that the Chado field_storage infrastructure won't try to
  154. // write a record.
  155. if (!$name and !$contact_id) {
  156. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  157. }
  158. // If the user removed the contact from the contact_name field
  159. // then we want to clear out the rest of the hidden values.
  160. // Leave the primary key so the record can be deleted.
  161. if (!$name and $contact_id) {
  162. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  163. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] = '';
  164. };
  165. }
  166. /**
  167. * @see TripalField::load()
  168. */
  169. public function load($entity, $details = array()) {
  170. $record = $details['record'];
  171. $field_name = $this->field['field_name'];
  172. $field_type = $this->field['type'];
  173. $field_table = $this->field['settings']['chado_table'];
  174. $field_column = $this->field['settings']['chado_column'];
  175. // Get the FK that links to the base record.
  176. $schema = chado_get_schema($field_table);
  177. $base_table = $details['record']->tablename;
  178. $pkey = $schema['primary key'][0];
  179. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  180. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  181. // Set some defaults for the empty record.
  182. $entity->{$field_name}['und'][0] = array(
  183. 'value' => array(),
  184. 'chado-' . $field_table . '__' . $pkey => '',
  185. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  186. 'chado-' . $field_table . '__' . 'contact_id' => '',
  187. // Ignore the synonym_sgml column for now.
  188. );
  189. $linker_table = $base_table . '_contact';
  190. $options = array(
  191. 'return_array' => 1,
  192. 'include_fk' => array(
  193. 'contact_id' => array(
  194. 'type_id' => array(
  195. 'dbxref_id' => array(
  196. 'db_id' => TRUE,
  197. ),
  198. ),
  199. ),
  200. $fkey_lcolumn => TRUE,
  201. ),
  202. );
  203. $record = chado_expand_var($record, 'table', $linker_table, $options);
  204. $contact_linkers = $record->$linker_table;
  205. if ($contact_linkers) {
  206. foreach ($contact_linkers as $i => $contact_linker) {
  207. $contact = $contact_linker->contact_id;
  208. $entity->{$field_name}['und'][$i] = array(
  209. 'value' => array(
  210. 'type' => $contact->type_id->name,
  211. 'name' => $contact->name,
  212. 'description' => $contact->description,
  213. ),
  214. // Add in the semantic web settings. This array is expected by
  215. // other Tripal modules that handle semantic web for fields.
  216. 'semantic_web' => array(
  217. 'type' => $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession,
  218. 'name' => tripal_get_chado_semweb_term('contact', 'name'),
  219. 'description' => tripal_get_chado_semweb_term('contact', 'description'),
  220. ),
  221. // Add in subfield mapping to Chado tables. This is used by the
  222. // chado_field_storage for performing queries on sub element values.
  223. // It should be a comma-separated list (no spacing) of the field names
  224. // as foreign keys are followed starting from the Chado table to which
  225. // this field maps.
  226. 'chado_mapping' => array(
  227. 'type' => 'type_id,name',
  228. 'name' => 'contact_id,name',
  229. 'description' => 'contact_id,name'
  230. ),
  231. 'chado-' . $field_table . '__' . $pkey => $contact_linker->$pkey,
  232. 'chado-' . $field_table . '__' . $fkey_lcolumn => $contact_linker->$fkey_lcolumn->$fkey_lcolumn,
  233. 'chado-' . $field_table . '__' . 'contact_id' => $contact->contact_id
  234. );
  235. if (property_exists($contact, 'entity_id')) {
  236. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $contact->entity_id;
  237. }
  238. }
  239. }
  240. }
  241. }
  242. /**
  243. * An Ajax callback for the pub widget.
  244. */
  245. function chado_linker__contact_widget_form_ajax_callback($form, $form_state) {
  246. $field_name = $form_state['triggering_element']['#parents'][0];
  247. $delta = $form_state['triggering_element']['#parents'][2];
  248. return $form[$field_name]['und'][$delta];
  249. }
  250. /**
  251. * Theme function for the pub widget.
  252. *
  253. * @param $variables
  254. */
  255. function theme_chado_linker__contact_widget($variables) {
  256. $element = $variables['element'];
  257. // These two fields were added to the widget to help identify the fields
  258. // for layout.
  259. $table_name = $element['#table_name'];
  260. $fkey = $element['#fkey_field'];
  261. $layout = "
  262. <div class=\"pub-widget\">
  263. <div class=\"pub-widget-item\">" .
  264. drupal_render($element['name']) . "
  265. </div>
  266. </div>
  267. ";
  268. return $layout;
  269. }