chado_linker__pub.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /**
  3. * Implements hook_info() for fields.
  4. *
  5. * This is a hook provided by the tripal_chado module for offloading the
  6. * hook_field_info() hook for each field to specify.
  7. */
  8. function chado_linker__pub_info() {
  9. return array(
  10. 'label' => t('Publications'),
  11. 'description' => t('Associates a publication (e.g. journal article,
  12. conference proceedings, book chapter, etc.) with this record.'),
  13. 'default_widget' => 'chado_linker__pub_widget',
  14. 'default_formatter' => 'chado_linker__pub_formatter',
  15. 'settings' => array(),
  16. 'storage' => array(
  17. 'type' => 'field_chado_storage',
  18. 'module' => 'tripal_chado',
  19. 'active' => TRUE
  20. ),
  21. );
  22. }
  23. /**
  24. * Implements hook_attach_info().
  25. *
  26. * This is a hook provided by the tripal_Chado module. It allows the field
  27. * to specify which bundles it will attach to and to specify thee settings.
  28. *
  29. * @param $entity_type
  30. * @param $entity
  31. * @param $term
  32. *
  33. * @return
  34. * A field array
  35. */
  36. function chado_linker__pub_attach_info($entity_type, $bundle, $target) {
  37. $field_info = array();
  38. $table_name = $target['data_table'];
  39. $type_table = $target['type_table'];
  40. $type_field = $target['field'];
  41. $cv_id = $target['cv_id'];
  42. $cvterm_id = $target['cvterm_id'];
  43. // If the linker table does not exists then we don't want to add attach.
  44. $pub_table = $table_name . '_pub';
  45. if (!chado_table_exists($pub_table)) {
  46. return $field_info;
  47. }
  48. $schema = chado_get_schema($pub_table);
  49. $pkey = $schema['primary key'][0];
  50. // Initialize the field array.
  51. $field_info = array(
  52. 'field_name' => $table_name . '__pub',
  53. 'field_type' => 'chado_linker__pub',
  54. 'widget_type' => 'chado_linker__pub_widget',
  55. 'widget_settings' => array('display_label' => 1),
  56. 'description' => '',
  57. 'label' => 'Publications',
  58. 'is_required' => 0,
  59. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  60. 'storage' => 'field_chado_storage',
  61. 'field_settings' => array(
  62. 'chado_table' => $pub_table,
  63. 'chado_column' => $pkey,
  64. 'base_table' => $table_name,
  65. 'semantic_web' => array(
  66. 'name' => 'publication',
  67. 'accession' => 'publication',
  68. 'ns' => 'schema',
  69. 'nsurl' => 'https://schema.org/',
  70. ),
  71. ),
  72. );
  73. return $field_info;
  74. }
  75. /**
  76. * Implements hook_widget_info.
  77. *
  78. * This is a hook provided by the tripal_chado module for offloading
  79. * the hook_field_widget_info() hook for each field to specify.
  80. */
  81. function chado_linker__pub_widget_info() {
  82. return array(
  83. 'label' => t('Publications'),
  84. 'field types' => array('chado_linker__pub'),
  85. );
  86. }
  87. /**
  88. * Implements hook_formatter_info.
  89. *
  90. * This is a hook provided by the tripal_chado module for
  91. * offloading the hook_field_formatter_info() for each field
  92. * to specify.
  93. *
  94. */
  95. function chado_linker__pub_formatter_info() {
  96. return array(
  97. 'label' => t('Publications'),
  98. 'field types' => array('chado_linker__pub'),
  99. 'settings' => array(
  100. ),
  101. );
  102. }
  103. /**
  104. *
  105. * @param unknown $entity_type
  106. * @param unknown $entity
  107. * @param unknown $field
  108. * @param unknown $instance
  109. * @param unknown $langcode
  110. * @param unknown $items
  111. * @param unknown $display
  112. */
  113. function chado_linker__pub_formatter(&$element, $entity_type, $entity, $field,
  114. $instance, $langcode, $items, $display) {
  115. $list_items = array();
  116. $chado_table = $field['settings']['chado_table'];
  117. foreach ($items as $delta => $item) {
  118. if ($item[$chado_table . '__pub_id']) {
  119. $pub = chado_generate_var('pub', array('pub_id' => $item[$chado_table . '__pub_id']));
  120. $list_items[$pub->pyear] = $pub->uniquename;
  121. }
  122. }
  123. krsort($list_items, SORT_NUMERIC);
  124. $list = array(
  125. 'title' => '',
  126. 'items' => $list_items,
  127. 'type' => 'ol',
  128. 'attributes' => array(),
  129. );
  130. $element[0] = array(
  131. '#type' => 'markup',
  132. '#markup' => theme_item_list($list),
  133. );
  134. }
  135. /**
  136. *
  137. * @param unknown $field_name
  138. * @param unknown $widget
  139. * @param unknown $form
  140. * @param unknown $form_state
  141. * @param unknown $field
  142. * @param unknown $instance
  143. * @param unknown $langcode
  144. * @param unknown $items
  145. * @param unknown $delta
  146. * @param unknown $element
  147. */
  148. function chado_linker__pub_widget(&$widget, $form, $form_state, $field,
  149. $instance, $langcode, $items, $delta, $element) {
  150. $entity = $form['#entity'];
  151. $field_name = $field['field_name'];
  152. // Get the FK column that links to the base table.
  153. $table_name = $field['settings']['chado_table'];
  154. $base_table = $field['settings']['base_table'];
  155. $schema = chado_get_schema($table_name);
  156. $pkey = $schema['primary key'][0];
  157. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  158. $fkey = $fkeys[0];
  159. // Get the field defaults.
  160. $record_id = '';
  161. $fkey_value = $element['#entity']->chado_record_id;
  162. $pub_id = '';
  163. $title = '';
  164. // If the field already has a value then it will come through the $items
  165. // array. This happens when editing an existing record.
  166. if (array_key_exists($delta, $items)) {
  167. $record_id = $items[$delta]['value'];
  168. $fkey_value = $items[$delta][$table_name . '__' . $fkey];
  169. $pub_id = $items[$delta][$table_name . '__pub_id'];
  170. $title = $items[$delta][$table_name . '--pub__uniquename'];
  171. }
  172. // Check $form_state['values'] to see if an AJAX call set the values.
  173. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  174. $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name);
  175. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  176. $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  177. $title = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__title');
  178. }
  179. $schema = chado_get_schema('pub');
  180. $widget['#table_name'] = $table_name;
  181. $widget['#fkey_field'] = $fkey;
  182. $widget['#element_validate'] = array('chado_linker__pub_widget_validate');
  183. $widget['#theme'] = 'chado_linker__pub_widget';
  184. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  185. $widget['#suffix'] = "</span>";
  186. $widget['value'] = array(
  187. '#type' => 'value',
  188. '#default_value' => $record_id,
  189. );
  190. $widget[$table_name . '__' . $fkey] = array(
  191. '#type' => 'value',
  192. '#default_value' => $fkey_value,
  193. );
  194. $widget[$table_name . '__pub_id'] = array(
  195. '#type' => 'value',
  196. '#default_value' => $pub_id,
  197. );
  198. $widget[$table_name . '--pub__uniquename'] = array(
  199. '#type' => 'textfield',
  200. '#title' => t('Publication ID'),
  201. '#default_value' => $title,
  202. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  203. '#ajax' => array(
  204. 'callback' => "chado_linker__pub_widget_form_ajax_callback",
  205. 'wrapper' => "$table_name-$delta",
  206. 'effect' => 'fade',
  207. 'method' => 'replace'
  208. ),
  209. '#maxlength' => 100000,
  210. );
  211. }
  212. /**
  213. * An Ajax callback for the pub widget.
  214. */
  215. function chado_linker__pub_widget_form_ajax_callback($form, $form_state) {
  216. $field_name = $form_state['triggering_element']['#parents'][0];
  217. $delta = $form_state['triggering_element']['#parents'][2];
  218. return $form[$field_name]['und'][$delta];
  219. }
  220. /**
  221. * Callback function for validating the chado_linker__pub_widget.
  222. */
  223. function chado_linker__pub_widget_validate($element, &$form_state) {
  224. $field_name = $element['#field_name'];
  225. $delta = $element['#delta'];
  226. $table_name = $element['#table_name'];
  227. $fkey = $element['#fkey_field'];
  228. // If the form ID is field_ui_field_edit_form, then the user is editing the
  229. // field's values in the manage fields form of Drupal. We don't want
  230. // to validate it as if it were being used in a data entry form.
  231. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  232. return;
  233. }
  234. // Get the field values.
  235. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  236. $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  237. $uname = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--pub__uniquename');
  238. // If the user provided a uniquename then we want to set the
  239. // foreign key value to be the chado_record_idd
  240. if ($uname) {
  241. // Get the pub. If one with the same name and type is already present
  242. // then use that. Otherwise, insert a new one.
  243. if (!$pub_id) {
  244. $pub = chado_generate_var('pub', array('uniquename' => $uname));
  245. // Set the pub_id and FK value
  246. tripal_chado_set_field_form_values($field_name, $form_state, $pub->pub_id, $delta, $table_name . '__pub_id');
  247. $fkey_value = $element['#entity']->chado_record_id;
  248. tripal_chado_set_field_form_values($field_name, $form_state, $fkey_value, $delta, $table_name . '__' . $fkey);
  249. }
  250. }
  251. else {
  252. // If the $syn_name is not set, then remove the linker FK value to the base table.
  253. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  254. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__pub_id');
  255. }
  256. }
  257. /**
  258. * Theme function for the pub widget.
  259. *
  260. * @param $variables
  261. */
  262. function theme_chado_linker__pub_widget($variables) {
  263. $element = $variables['element'];
  264. // These two fields were added to the widget to help identify the fields
  265. // for layout.
  266. $table_name = $element['#table_name'];
  267. $fkey = $element['#fkey_field'];
  268. $layout = "
  269. <div class=\"pub-widget\">
  270. <div class=\"pub-widget-item\">" .
  271. drupal_render($element[$table_name . '--pub__uniquename']) . "
  272. </div>
  273. </div>
  274. ";
  275. return $layout;
  276. }
  277. /**
  278. * Loads the field values with appropriate data.
  279. *
  280. * This function is called by the tripal_chado_field_storage_load() for
  281. * each property managed by the field_chado_storage storage type. This is
  282. * an optional hook function that is only needed if the field has
  283. * multiple form elements.
  284. *
  285. * @param $field
  286. * @param $entity
  287. * @param $base_table
  288. * @param $record
  289. */
  290. function chado_linker__pub_load($field, $entity, $base_table, $record) {
  291. $field_name = $field['field_name'];
  292. $field_type = $field['type'];
  293. $field_table = $field['settings']['chado_table'];
  294. $field_column = $field['settings']['chado_column'];
  295. // Get the FK that links to the base record.
  296. $schema = chado_get_schema($field_table);
  297. $pkey = $schema['primary key'][0];
  298. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  299. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  300. // Set some defaults for the empty record.
  301. $entity->{$field_name}['und'][0] = array(
  302. 'value' => '',
  303. $field_table . '__' . $fkey_lcolumn => '',
  304. $field_table . '__' . 'pub_id' => '',
  305. $field_table . '--' . 'pub__uniquename' => '',
  306. );
  307. $linker_table = $base_table . '_pub';
  308. $options = array(
  309. 'return_array' => 1,
  310. );
  311. $record = chado_expand_var($record, 'table', $linker_table, $options);
  312. if (count($record->$linker_table) > 0) {
  313. $i = 0;
  314. foreach ($record->$linker_table as $index => $linker) {
  315. $pub = $linker->pub_id;
  316. $entity->{$field_name}['und'][$i] = array(
  317. 'value' => $linker->$pkey,
  318. $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn->$fkey_lcolumn,
  319. $field_table . '__' . 'pub_id' => $pub->pub_id,
  320. $field_table . '--' . 'pub__uniquename' => $pub->uniquename,
  321. );
  322. $i++;
  323. }
  324. }
  325. }