tripal_entities.fields.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. switch ($instance['widget']['type']) {
  184. case 'tripal_entities_organism_select_widget':
  185. $options = tripal_get_organism_select_options();
  186. $widget += array(
  187. '#type' => 'select',
  188. '#title' => $element['#title'],
  189. '#description' => $element['#description'],
  190. '#options' => $options,
  191. '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
  192. '#required' => $element['#required'],
  193. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  194. '#delta' => $delta,
  195. '#element_validate' => array('tripal_entities_organism_select_widget_validate'),
  196. );
  197. $element['value'] = $widget;
  198. break;
  199. case 'tripal_entities_primary_dbxref_widget':
  200. // Get the field defaults from the database if a record exists.
  201. $dbxref_id = '';
  202. $db_id = '';
  203. $accession = '';
  204. $version = '';
  205. $description = '';
  206. if (count($items) > 0 and $items[0]['value']) {
  207. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $items[0]['value']));
  208. $dbxref_id = $dbxref->dbxref_id;
  209. $db_id = $dbxref->db_id->db_id;
  210. $accession = $dbxref->accession;
  211. $version = $dbxref->version;
  212. $description = $dbxref->description;
  213. }
  214. $schema = chado_get_schema('dbxref');
  215. $options = tripal_get_db_select_options();
  216. $widget += array(
  217. '#element_validate' => array('tripal_entities_primary_dbxref_widget_validate'),
  218. '#type' => 'fieldset',
  219. '#title' => $element['#title'],
  220. '#description' => $element['#description'],
  221. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  222. '#delta' => $delta,
  223. '#theme' => 'tripal_entities_primary_dbxref_widget',
  224. array(
  225. $element['#field_name'] => array(
  226. '#type' => 'hidden',
  227. '#default_value' => $dbxref_id,
  228. ),
  229. 'dbxref__db_id' => array(
  230. '#type' => 'select',
  231. '#title' => t('Database'),
  232. '#options' => $options,
  233. '#required' => $element['#required'],
  234. '#default_value' => $db_id,
  235. ),
  236. 'dbxref__accession' => array(
  237. '#type' => 'textfield',
  238. '#title' => t('Accession'),
  239. '#default_value' => $accession,
  240. '#required' => $element['#required'],
  241. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  242. '#size' => 15,
  243. ),
  244. 'dbxref__version' => array(
  245. '#type' => 'textfield',
  246. '#title' => t('Version'),
  247. '#default_value' => $version,
  248. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  249. '#size' => 5,
  250. ),
  251. 'dbxref__description' => array(
  252. '#type' => 'textfield',
  253. '#title' => t('Description'),
  254. '#default_value' => $description,
  255. '#size' => 20,
  256. ),
  257. ),
  258. );
  259. $element['value'] = $widget;
  260. break;
  261. case 'tripal_entities_md5checksum_checkbox_widget':
  262. $widget += array(
  263. '#type' => 'checkbox',
  264. '#title' => $element['#title'],
  265. '#description' => $element['#description'],
  266. '#options' => array(0, 1),
  267. '#default_value' => 1,
  268. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  269. '#delta' => $delta,
  270. '#element_validate' => array('tripal_entities_md5checksum_checkbox_widget_validate'),
  271. );
  272. $element['value'] = $widget;
  273. break;
  274. case 'tripal_entities_seqlen_hidden_widget':
  275. $widget += array(
  276. '#type' => 'hidden',
  277. '#title' => $element['#title'],
  278. '#description' => $element['#description'],
  279. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  280. '#delta' => $delta,
  281. '#element_validate' => array('tripal_entities_seqlen_hidden_widget_validate'),
  282. );
  283. $element['value'] = $widget;
  284. break;
  285. }
  286. return $element;
  287. }
  288. /**
  289. * Implements hook_field_is_empty().
  290. */
  291. function tripal_entities_field_is_empty($item, $field) {
  292. if (empty($item['value']) && (string) $item['value'] !== '0') {
  293. return TRUE;
  294. }
  295. return FALSE;
  296. }
  297. /**
  298. * Callback function for validating the tripal_entities_organism_select_widget.
  299. */
  300. function tripal_entities_organism_select_widget_validate($element, &$form_state) {
  301. $field_name = $element['#field_name'];
  302. // If the form ID is field_ui_field_edit_form, then the user is editing the
  303. // field's values in the manage fields form of Drupal. We don't want
  304. // to validate it as if it were being used in a data entry form.
  305. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  306. return;
  307. }
  308. $organism_id = tripal_entities_get_field_form_values($field_name, $form_state);
  309. if (count($organism_id) == 0) {
  310. form_error($element, t("Please specify an organism that already exists in the database."));
  311. }
  312. }
  313. /**
  314. * Callback function for validating the tripal_entities_organism_select_widget.
  315. */
  316. function tripal_entities_primary_dbxref_widget_validate($element, &$form_state) {
  317. $field_name = $element['#field_name'];
  318. // If the form ID is field_ui_field_edit_form, then the user is editing the
  319. // field's values in the manage fields form of Drupal. We don't want
  320. // to validate it as if it were being used in a data entry form.
  321. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  322. return;
  323. }
  324. // Get the field values.
  325. $db_id = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__db_id");
  326. $accession = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__accession");
  327. $version = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__version");
  328. $description = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__description");
  329. // Make sure that if a database ID is provided that an accession is also
  330. // provided. Here we use the form_set_error function rather than the
  331. // form_error function because the form_error will add a red_highlight
  332. // around all of the fields in the fieldset which is confusing as it's not
  333. // clear to the user what field is required and which isn't. Therefore,
  334. // we borrow the code from the 'form_error' function and append the field
  335. // so that the proper field is highlighted on error.
  336. if (count($db_id) == 0 and count($accession) > 0) {
  337. 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."));
  338. }
  339. if (count($db_id) > 0 and count($accession) == 0) {
  340. form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  341. }
  342. // If user did not select a database, we want to remove dbxref_id from the
  343. // field.
  344. if (count($db_id) == 0) {
  345. tripal_entities_set_field_form_values($field_name, $form_state, '__NULL__', $field_name);
  346. }
  347. }
  348. /**
  349. * Callback function for validating the tripal_entities_md5checksum_checkbox_widget.
  350. */
  351. function tripal_entities_md5checksum_checkbox_widget_validate($element, &$form_state) {
  352. $field_name = $element['#field_name'];
  353. // Calculate the md5 checksum for the sequence only if md5 box is checked and the residues exist
  354. $residues = tripal_entities_get_field_form_values('feature__residues', $form_state);
  355. if (count($residues) > 0 && trim($residues[0]) != '') {
  356. tripal_entities_set_field_form_values ($field_name, $form_state, md5($residues[0]));
  357. }
  358. else {
  359. // Otherwise, remove the md5 value
  360. tripal_entities_set_field_form_values ($field_name, $form_state, '__NULL__');
  361. }
  362. }
  363. /**
  364. * Callback function for validating the tripal_entities_seqlen_hidden_widget.
  365. */
  366. function tripal_entities_seqlen_hidden_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) {
  371. tripal_entities_set_field_form_values ($field_name, $form_state, strlen($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. * Theme function for the primary_dbxref_widget.
  380. *
  381. * @param $variables
  382. */
  383. function theme_tripal_entities_primary_dbxref_widget($variables) {
  384. $element = $variables['element'];
  385. $layout = "
  386. <div class=\"primary-dbxref-widget\">
  387. <div class=\"primary-dbxref-widget-item\">" .
  388. drupal_render($element[0]['dbxref__db_id']) . "
  389. </div>
  390. <div class=\"primary-dbxref-widget-item\">" .
  391. drupal_render($element[0]['dbxref__accession']) . "
  392. </div>
  393. <div class=\"primary-dbxref-widget-item\">" .
  394. drupal_render($element[0]['dbxref__version']) . "
  395. </div>
  396. <div class=\"primary-dbxref-widget-item\">" .
  397. drupal_render($element[0]['dbxref__description']) . "
  398. </div>
  399. </div>
  400. ";
  401. return $layout;
  402. }
  403. /**
  404. * Returns the values of the field from the $form_state.
  405. */
  406. function tripal_entities_get_field_form_values($field_name, $form_state, $child = NULL) {
  407. $values = array();
  408. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  409. foreach ($items as $delta => $value) {
  410. if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
  411. $values[] = $value['value'][0][$child];
  412. }
  413. else if (!$child and $value['value']) {
  414. $values[] = $value['value'];
  415. }
  416. }
  417. }
  418. return $values;
  419. }
  420. /**
  421. * Returns the values of the field from the $form_state.
  422. */
  423. function tripal_entities_set_field_form_values($field_name, &$form_state, $newvalue, $child = NULL) {
  424. $values = array();
  425. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  426. foreach ($items as $delta => $value) {
  427. if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
  428. $form_state['values'][$field_name][$langcode][$delta]['value'][0][$child] = $newvalue;
  429. }
  430. else if (!$child) {
  431. $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
  432. }
  433. }
  434. }
  435. return $values;
  436. }