chado_linker__contact.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. if ($contact_id) {
  92. $contact = chado_generate_var('contact', array('contact_id' => $contact_id));
  93. $name = $contact->name;
  94. }
  95. }
  96. $schema = chado_get_schema('contact');
  97. $widget['#table_name'] = $table_name;
  98. $widget['#fkey_field'] = $fkey;
  99. $widget['#theme'] = 'chado_linker__contact_widget';
  100. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  101. $widget['#suffix'] = "</span>";
  102. $widget['value'] = array(
  103. '#type' => 'value',
  104. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  105. );
  106. $widget['chado-' . $table_name . '__' . $pkey] = array(
  107. '#type' => 'value',
  108. '#default_value' => $record_id,
  109. );
  110. $widget['chado-' . $table_name . '__' . $fkey] = array(
  111. '#type' => 'value',
  112. '#default_value' => $fkey_value,
  113. );
  114. $widget['chado-' . $table_name . '__contact_id'] = array(
  115. '#type' => 'value',
  116. '#default_value' => $contact_id,
  117. );
  118. $widget['name'] = array(
  119. '#type' => 'textfield',
  120. '#title' => t('Contact'),
  121. '#default_value' => $name,
  122. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/contact',
  123. '#ajax' => array(
  124. 'callback' => "chado_linker__contact_widget_form_ajax_callback",
  125. 'wrapper' => "$table_name-$delta",
  126. 'effect' => 'fade',
  127. 'method' => 'replace'
  128. ),
  129. '#maxlength' => 100000,
  130. );
  131. }
  132. /**
  133. * @see TripalField::widgetFormSubmit()
  134. */
  135. public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  136. // Get the FK column that links to the base table.
  137. $table_name = $this->field['settings']['chado_table'];
  138. $base_table = $this->field['settings']['base_table'];
  139. $schema = chado_get_schema($table_name);
  140. $pkey = $schema['primary key'][0];
  141. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  142. $fkey = $fkeys[0];
  143. $field_name = $this->field['field_name'];
  144. // Get the field values.
  145. $fkey_value = isset($form_state['values'][$field_name][$langcode][$delta]['value']) ? $form_state['values'][$field_name][$langcode][$delta]['value'] : '';
  146. $contact_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] : '';
  147. $name = isset($form_state['values'][$field_name][$langcode][$delta]['name']) ? $form_state['values'][$field_name][$langcode][$delta]['name'] : '';
  148. // If the user provided a name then we want to set the foreign key
  149. // value to be the chado_record_id
  150. if ($name and !$contact_id) {
  151. $contact = chado_generate_var('contact', array('name' => $name));
  152. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] = $contact->contact_id;
  153. }
  154. // In the widgetForm function we automatically add the foreign key
  155. // record. But if the user did not provide a contact we want to take
  156. // it out so that the Chado field_storage infrastructure won't try to
  157. // write a record.
  158. if (!$name and !$contact_id) {
  159. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  160. }
  161. // If the user removed the contact from the contact_name field
  162. // then we want to clear out the rest of the hidden values.
  163. // Leave the primary key so the record can be deleted.
  164. if (!$name and $contact_id) {
  165. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  166. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] = '';
  167. }
  168. }
  169. /**
  170. * @see TripalField::load()
  171. */
  172. public function load($entity, $details = array()) {
  173. $record = $details['record'];
  174. $field_name = $this->field['field_name'];
  175. $field_type = $this->field['type'];
  176. $field_table = $this->field['settings']['chado_table'];
  177. $field_column = $this->field['settings']['chado_column'];
  178. // Get the FK that links to the base record.
  179. $schema = chado_get_schema($field_table);
  180. $base_table = $details['record']->tablename;
  181. $pkey = $schema['primary key'][0];
  182. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  183. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  184. // Set some defaults for the empty record.
  185. $entity->{$field_name}['und'][0] = array(
  186. 'value' => array(),
  187. 'chado-' . $field_table . '__' . $pkey => '',
  188. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  189. 'chado-' . $field_table . '__' . 'contact_id' => '',
  190. // Ignore the synonym_sgml column for now.
  191. );
  192. $linker_table = $base_table . '_contact';
  193. $options = array(
  194. 'return_array' => 1,
  195. 'include_fk' => array(
  196. 'contact_id' => array(
  197. 'type_id' => array(
  198. 'dbxref_id' => array(
  199. 'db_id' => TRUE,
  200. ),
  201. ),
  202. ),
  203. $fkey_lcolumn => TRUE,
  204. ),
  205. );
  206. $record = chado_expand_var($record, 'table', $linker_table, $options);
  207. $contact_linkers = $record->$linker_table;
  208. if ($contact_linkers) {
  209. foreach ($contact_linkers as $i => $contact_linker) {
  210. $contact = $contact_linker->contact_id;
  211. $entity->{$field_name}['und'][$i] = array(
  212. 'value' => array(
  213. 'type' => $contact->type_id ? $contact->type_id->name : '',
  214. 'name' => $contact->name,
  215. 'description' => $contact->description,
  216. ),
  217. // Add in the semantic web settings. This array is expected by
  218. // other Tripal modules that handle semantic web for fields.
  219. 'semantic_web' => array(
  220. 'type' => $contact->type_id ? $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession : '',
  221. 'name' => tripal_get_chado_semweb_term('contact', 'name'),
  222. 'description' => tripal_get_chado_semweb_term('contact', 'description'),
  223. ),
  224. // Add in subfield mapping to Chado tables. This is used by the
  225. // chado_field_storage for performing queries on sub element values.
  226. // It should be a comma-separated list (no spacing) of the field names
  227. // as foreign keys are followed starting from the Chado table to which
  228. // this field maps.
  229. 'chado_mapping' => array(
  230. 'type' => 'type_id,name',
  231. 'name' => 'contact_id,name',
  232. 'description' => 'contact_id,name'
  233. ),
  234. 'chado-' . $field_table . '__' . $pkey => $contact_linker->$pkey,
  235. 'chado-' . $field_table . '__' . $fkey_lcolumn => $contact_linker->$fkey_lcolumn->$fkey_lcolumn,
  236. 'chado-' . $field_table . '__' . 'contact_id' => $contact->contact_id
  237. );
  238. if (property_exists($contact, 'entity_id')) {
  239. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $contact->entity_id;
  240. }
  241. }
  242. }
  243. }
  244. }
  245. /**
  246. * An Ajax callback for the pub widget.
  247. */
  248. function chado_linker__contact_widget_form_ajax_callback($form, $form_state) {
  249. $field_name = $form_state['triggering_element']['#parents'][0];
  250. $delta = $form_state['triggering_element']['#parents'][2];
  251. return $form[$field_name]['und'][$delta];
  252. }
  253. /**
  254. * Theme function for the pub widget.
  255. *
  256. * @param $variables
  257. */
  258. function theme_chado_linker__contact_widget($variables) {
  259. $element = $variables['element'];
  260. // These two fields were added to the widget to help identify the fields
  261. // for layout.
  262. $table_name = $element['#table_name'];
  263. $fkey = $element['#fkey_field'];
  264. $layout = "
  265. <div class=\"pub-widget\">
  266. <div class=\"pub-widget-item\">" .
  267. drupal_render($element['name']) . "
  268. </div>
  269. </div>
  270. ";
  271. return $layout;
  272. }