tripal_entities.fields.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /**
  3. * Implements hook_field_info().
  4. */
  5. function tripal_entities_field_info() {
  6. $fields = array(
  7. 'organism_id' => array(
  8. 'label' => t('Organism'),
  9. 'description' => t('A field for specifying an organism.'),
  10. 'default_widget' => 'tripal_entities_organism_select_widget',
  11. 'default_formatter' => 'tripal_entities_organism_formatter',
  12. 'settings' => array(),
  13. 'storage' => array(
  14. 'type' => 'field_chado_storage',
  15. 'module' => 'tripal_entities',
  16. 'active' => TRUE
  17. ),
  18. ),
  19. 'dbxref_id' => array(
  20. 'label' => t('Cross-reference'),
  21. 'description' => t('This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference. At a minimum, the database and accession must be provided.'),
  22. 'default_widget' => 'tripal_entities_primary_dbxref_widget',
  23. 'default_formatter' => 'tripal_entities_primary_dbxref_formatter',
  24. 'settings' => array(),
  25. 'storage' => array(
  26. 'type' => 'field_chado_storage',
  27. 'module' => 'tripal_entities',
  28. 'active' => TRUE
  29. ),
  30. ),
  31. 'md5checksum' => array(
  32. 'label' => t('MD5 checksum'),
  33. 'description' => t('A field for generating MD5 checksum for a sequence.'),
  34. 'default_widget' => 'tripal_entities_md5checksum_checkbox_widget',
  35. 'default_formatter' => 'tripal_entities_md5checksum_formatter',
  36. 'settings' => array(),
  37. 'storage' => array(
  38. 'type' => 'field_chado_storage',
  39. 'module' => 'tripal_entities',
  40. 'active' => TRUE
  41. ),
  42. ),
  43. 'seqlen' => array(
  44. 'label' => t('Sequence length'),
  45. 'description' => t('A field for calculating the length of a sequence.'),
  46. 'default_widget' => 'tripal_entities_seqlen_hidden_widget',
  47. 'default_formatter' => 'tripal_entities_seqlen_formatter',
  48. 'settings' => array(),
  49. 'storage' => array(
  50. 'type' => 'field_chado_storage',
  51. 'module' => 'tripal_entities',
  52. 'active' => TRUE
  53. ),
  54. ),
  55. );
  56. return $fields;
  57. }
  58. /**
  59. * Implements hook_field_widget_info().
  60. */
  61. function tripal_entities_field_widget_info() {
  62. return array(
  63. 'tripal_entities_organism_select_widget' => array(
  64. 'label' => t('Organism Select'),
  65. 'field types' => array('organism_id')
  66. ),
  67. 'tripal_entities_primary_dbxref_widget' => array(
  68. 'label' => t('Cross-reference'),
  69. 'field types' => array('dbxref_id'),
  70. 'description' => t('This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference. At a minimum, the database and accession must be provided.'),
  71. ),
  72. 'tripal_entities_md5checksum_checkbox_widget' => array(
  73. 'label' => t('MD5 Checksum Checkbox'),
  74. 'field types' => array('md5checksum')
  75. ),
  76. 'tripal_entities_seqlen_hidden_widget' => array(
  77. 'field types' => array('seqlen')
  78. ),
  79. );
  80. }
  81. /**
  82. * Implements hook_field_formatter_info().
  83. */
  84. function tripal_entities_field_formatter_info() {
  85. return array(
  86. 'tripal_entities_organism_formatter' => array(
  87. 'label' => t('Organism'),
  88. 'field types' => array('organism_id')
  89. ),
  90. 'tripal_entities_primary_dbxref_formatter' => array(
  91. 'label' => t('Cross-reference'),
  92. 'field types' => array('dbxref_id')
  93. ),
  94. 'tripal_entities_md5checksum_formatter' => array(
  95. 'label' => t('MD5 checksum'),
  96. 'field types' => array('md5checksum')
  97. ),
  98. 'tripal_entities_seqlen_formatter' => array(
  99. 'label' => t('Sequence length'),
  100. 'field types' => array('seqlen')
  101. ),
  102. );
  103. }
  104. /**
  105. * Implements hook_field_formatter_view().
  106. *
  107. * Two formatters are implemented.
  108. * - field_example_simple_text just outputs markup indicating the color that
  109. * was entered and uses an inline style to set the text color to that value.
  110. * - field_example_color_background does the same but also changes the
  111. * background color of div.region-content.
  112. *
  113. * @see field_example_field_formatter_info()
  114. */
  115. function tripal_entities_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  116. $element = array();
  117. switch ($display['type']) {
  118. case 'tripal_entities_organism_formatter':
  119. foreach ($items as $delta => $item) {
  120. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $item['value']));
  121. // TODO: add hook here to allow other modules to change this display
  122. // if they want.
  123. $element[$delta] = array(
  124. // We create a render array to produce the desired markup,
  125. // "<p>Genus Species</p>".
  126. // See theme_html_tag().
  127. '#type' => 'markup',
  128. '#markup' => '<i>' . $organism[0]->genus .' ' . $organism[0]->species . '</i>',
  129. );
  130. }
  131. break;
  132. case 'tripal_entities_primary_dbxref_formatter':
  133. foreach ($items as $delta => $item) {
  134. $accession = '';
  135. if ($item['value']) {
  136. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $item['value']));
  137. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  138. if ($dbxref->db_id->urlprefix) {
  139. $accession = l($accession, $dbxref->db_id->urlprefix . '/' . $dbxref->accession);
  140. }
  141. }
  142. // TODO: add hook here to allow other modules to change this display
  143. // if they want.
  144. $element[$delta] = array(
  145. // We create a render array to produce the desired markup,
  146. '#type' => 'markup',
  147. '#markup' => $accession,
  148. );
  149. }
  150. break;
  151. case 'tripal_entities_md5checksum_formatter':
  152. foreach ($items as $delta => $item) {
  153. // TODO: add hook here to allow other modules to change this display
  154. // if they want.
  155. $element[$delta] = array(
  156. // We create a render array to produce the desired markup,
  157. '#type' => 'markup',
  158. '#markup' => key_exists('value', $item) ? $item['value'] : '',
  159. );
  160. }
  161. break;
  162. case 'tripal_entities_seqlen_formatter':
  163. foreach ($items as $delta => $item) {
  164. // TODO: add hook here to allow other modules to change this display
  165. // if they want.
  166. $element[$delta] = array(
  167. // We create a render array to produce the desired markup,
  168. '#type' => 'markup',
  169. '#markup' => key_exists('value', $item) ? $item['value'] : '',
  170. );
  171. }
  172. break;
  173. }
  174. return $element;
  175. }
  176. /**
  177. * Implements hook_field_widget_form().
  178. */
  179. function tripal_entities_field_widget_form(&$form, &$form_state, $field,
  180. $instance, $langcode, $items, $delta, $element) {
  181. $widget = $element;
  182. $widget['#delta'] = $delta;
  183. $field_name = $field['field_name'];
  184. switch ($instance['widget']['type']) {
  185. case 'tripal_entities_organism_select_widget':
  186. $options = tripal_get_organism_select_options();
  187. $widget += array(
  188. '#type' => 'select',
  189. '#title' => $element['#title'],
  190. '#description' => $element['#description'],
  191. '#options' => $options,
  192. '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
  193. '#required' => $element['#required'],
  194. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  195. '#delta' => $delta,
  196. '#element_validate' => array('tripal_entities_organism_select_widget_validate'),
  197. );
  198. $element['value'] = $widget;
  199. break;
  200. case 'tripal_entities_primary_dbxref_widget':
  201. // Get the field defaults from the database if a record exists.
  202. $dbxref_id = '';
  203. $db_id = '';
  204. $accession = '';
  205. $version = '';
  206. $description = '';
  207. if (count($items) > 0 and $items[0]['value']) {
  208. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $items[0]['value']));
  209. $dbxref_id = $dbxref->dbxref_id;
  210. $db_id = $dbxref->db_id->db_id;
  211. $accession = $dbxref->accession;
  212. $version = $dbxref->version;
  213. $description = $dbxref->description;
  214. }
  215. $temp = tripal_entities_get_field_form_values($field_name, $form_state, 'dbxref__db_id');
  216. if (count($temp) > 0) {
  217. $db_id = $temp[0];
  218. }
  219. drupal_debug(array($field_name, $temp, $db_id));
  220. $schema = chado_get_schema('dbxref');
  221. $options = tripal_get_db_select_options();
  222. $widget += array(
  223. '#element_validate' => array('tripal_entities_primary_dbxref_widget_validate'),
  224. '#type' => 'fieldset',
  225. '#title' => $element['#title'],
  226. '#description' => $element['#description'],
  227. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  228. '#delta' => $delta,
  229. '#theme' => 'tripal_entities_primary_dbxref_widget',
  230. array(
  231. $element['#field_name'] => array(
  232. '#type' => 'hidden',
  233. '#default_value' => $dbxref_id,
  234. ),
  235. 'dbxref__db_id' => array(
  236. '#type' => 'select',
  237. '#title' => t('Database'),
  238. '#options' => $options,
  239. '#required' => $element['#required'],
  240. '#default_value' => $db_id,
  241. '#ajax' => array(
  242. 'callback' => "tripal_entities_primary_dbxref_widget_form_ajax_callback",
  243. 'wrapper' => "$field_name-dbxref--db-id",
  244. 'effect' => 'fade',
  245. 'method' => 'replace'
  246. )
  247. ),
  248. 'dbxref__accession' => array(
  249. '#type' => 'textfield',
  250. '#title' => t('Accession'),
  251. '#default_value' => $accession,
  252. '#required' => $element['#required'],
  253. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  254. '#size' => 15,
  255. '#autocomplete_path' => "admin/tripal/chado/tripal_db/dbxref/auto_name/$db_id",
  256. ),
  257. 'dbxref__version' => array(
  258. '#type' => 'textfield',
  259. '#title' => t('Version'),
  260. '#default_value' => $version,
  261. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  262. '#size' => 5,
  263. ),
  264. 'dbxref__description' => array(
  265. '#type' => 'textfield',
  266. '#title' => t('Description'),
  267. '#default_value' => $description,
  268. '#size' => 20,
  269. ),
  270. ),
  271. '#prefix' => "<span id='$field_name-dbxref--db-id'>",
  272. '#suffix' => "</span>"
  273. );
  274. $element['value'] = $widget;
  275. break;
  276. case 'tripal_entities_md5checksum_checkbox_widget':
  277. $widget += array(
  278. '#type' => 'checkbox',
  279. '#title' => $element['#title'],
  280. '#description' => $element['#description'],
  281. '#options' => array(0, 1),
  282. '#default_value' => 1,
  283. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  284. '#delta' => $delta,
  285. '#element_validate' => array('tripal_entities_md5checksum_checkbox_widget_validate'),
  286. );
  287. $element['value'] = $widget;
  288. break;
  289. case 'tripal_entities_seqlen_hidden_widget':
  290. $widget += array(
  291. '#type' => 'hidden',
  292. '#title' => $element['#title'],
  293. '#description' => $element['#description'],
  294. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  295. '#delta' => $delta,
  296. '#element_validate' => array('tripal_entities_seqlen_hidden_widget_validate'),
  297. );
  298. $element['value'] = $widget;
  299. break;
  300. }
  301. return $element;
  302. }
  303. /**
  304. * Implements hook_field_is_empty().
  305. */
  306. function tripal_entities_field_is_empty($item, $field) {
  307. if (empty($item['value']) && (string) $item['value'] !== '0') {
  308. return TRUE;
  309. }
  310. return FALSE;
  311. }
  312. /**
  313. * Callback function for validating the tripal_entities_organism_select_widget.
  314. */
  315. function tripal_entities_organism_select_widget_validate($element, &$form_state) {
  316. $field_name = $element['#field_name'];
  317. // If the form ID is field_ui_field_edit_form, then the user is editing the
  318. // field's values in the manage fields form of Drupal. We don't want
  319. // to validate it as if it were being used in a data entry form.
  320. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  321. return;
  322. }
  323. $organism_id = tripal_entities_get_field_form_values($field_name, $form_state);
  324. if (count($organism_id) == 0) {
  325. form_error($element, t("Please specify an organism that already exists in the database."));
  326. }
  327. }
  328. /**
  329. * Callback function for validating the tripal_entities_organism_select_widget.
  330. */
  331. function tripal_entities_primary_dbxref_widget_validate($element, &$form_state) {
  332. $field_name = $element['#field_name'];
  333. // If the form ID is field_ui_field_edit_form, then the user is editing the
  334. // field's values in the manage fields form of Drupal. We don't want
  335. // to validate it as if it were being used in a data entry form.
  336. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  337. return;
  338. }
  339. // Get the field values.
  340. $db_id = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__db_id");
  341. $accession = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__accession");
  342. $version = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__version");
  343. $description = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__description");
  344. // Make sure that if a database ID is provided that an accession is also
  345. // provided. Here we use the form_set_error function rather than the
  346. // form_error function because the form_error will add a red_highlight
  347. // around all of the fields in the fieldset which is confusing as it's not
  348. // clear to the user what field is required and which isn't. Therefore,
  349. // we borrow the code from the 'form_error' function and append the field
  350. // so that the proper field is highlighted on error.
  351. if (count($db_id) == 0 and count($accession) > 0) {
  352. form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  353. }
  354. if (count($db_id) > 0 and count($accession) == 0) {
  355. form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  356. }
  357. // If user did not select a database, we want to remove dbxref_id from the
  358. // field.
  359. if (count($db_id) == 0) {
  360. tripal_entities_set_field_form_values($field_name, $form_state, '__NULL__', $field_name);
  361. }
  362. }
  363. /**
  364. * Callback function for validating the tripal_entities_md5checksum_checkbox_widget.
  365. */
  366. function tripal_entities_md5checksum_checkbox_widget_validate($element, &$form_state) {
  367. $field_name = $element['#field_name'];
  368. // Calculate the md5 checksum for the sequence only if md5 box is checked and the residues exist
  369. $residues = tripal_entities_get_field_form_values('feature__residues', $form_state);
  370. if (count($residues) > 0 && trim($residues[0]) != '') {
  371. tripal_entities_set_field_form_values ($field_name, $form_state, md5($residues[0]));
  372. }
  373. else {
  374. // Otherwise, remove the md5 value
  375. tripal_entities_set_field_form_values ($field_name, $form_state, '__NULL__');
  376. }
  377. }
  378. /**
  379. * Callback function for validating the tripal_entities_seqlen_hidden_widget.
  380. */
  381. function tripal_entities_seqlen_hidden_widget_validate($element, &$form_state) {
  382. $field_name = $element['#field_name'];
  383. // Calculate the md5 checksum for the sequence only if md5 box is checked and the residues exist
  384. $residues = tripal_entities_get_field_form_values('feature__residues', $form_state);
  385. if (count($residues) > 0) {
  386. tripal_entities_set_field_form_values ($field_name, $form_state, strlen($residues[0]));
  387. }
  388. else {
  389. // Otherwise, remove the md5 value
  390. tripal_entities_set_field_form_values ($field_name, $form_state, '__NULL__');
  391. }
  392. }
  393. /**
  394. * Theme function for the primary_dbxref_widget.
  395. *
  396. * @param $variables
  397. */
  398. function theme_tripal_entities_primary_dbxref_widget($variables) {
  399. $element = $variables['element'];
  400. $layout = "
  401. <div class=\"primary-dbxref-widget\">
  402. <div class=\"primary-dbxref-widget-item\">" .
  403. drupal_render($element[0]['dbxref__db_id']) . "
  404. </div>
  405. <div class=\"primary-dbxref-widget-item\">" .
  406. drupal_render($element[0]['dbxref__accession']) . "
  407. </div>
  408. <div class=\"primary-dbxref-widget-item\">" .
  409. drupal_render($element[0]['dbxref__version']) . "
  410. </div>
  411. <div class=\"primary-dbxref-widget-item\">" .
  412. drupal_render($element[0]['dbxref__description']) . "
  413. </div>
  414. </div>
  415. ";
  416. return $layout;
  417. }
  418. /**
  419. * Returns the values of the field from the $form_state.
  420. */
  421. function tripal_entities_get_field_form_values($field_name, $form_state, $child = NULL) {
  422. $values = array();
  423. if (array_key_exists($field_name, $form_state['values'])) {
  424. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  425. foreach ($items as $delta => $value) {
  426. if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
  427. $values[] = $value['value'][0][$child];
  428. }
  429. else if (!$child and $value['value']) {
  430. $values[] = $value['value'];
  431. }
  432. }
  433. }
  434. }
  435. return $values;
  436. }
  437. /**
  438. * Returns the values of the field from the $form_state.
  439. */
  440. function tripal_entities_set_field_form_values($field_name, &$form_state, $newvalue, $child = NULL) {
  441. $values = array();
  442. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  443. foreach ($items as $delta => $value) {
  444. if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
  445. $form_state['values'][$field_name][$langcode][$delta]['value'][0][$child] = $newvalue;
  446. }
  447. else if (!$child) {
  448. $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
  449. }
  450. }
  451. }
  452. return $values;
  453. }
  454. /**
  455. * An Ajax callback for the tripal_entities_admin_publish_form..
  456. */
  457. function tripal_entities_primary_dbxref_widget_form_ajax_callback($form, $form_state) {
  458. // return the form so Drupal can update the content on the page
  459. // return $form['][', $element ['#parents']) . '][0][dbxref__accession',
  460. drupal_debug($form_state['triggering_element']);
  461. return $form[$form_state['triggering_element']['#parents'][0]];
  462. }