chado_linker__pub.inc 11 KB

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