tripal_entities.fields.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. );
  44. return $fields;
  45. }
  46. /**
  47. * Implements hook_field_widget_info().
  48. */
  49. function tripal_entities_field_widget_info() {
  50. return array(
  51. 'tripal_entities_organism_select_widget' => array(
  52. 'label' => t('Organism Select'),
  53. 'field types' => array('organism_id')
  54. ),
  55. 'tripal_entities_primary_dbxref_widget' => array(
  56. 'label' => t('Cross-reference'),
  57. 'field types' => array('dbxref_id'),
  58. '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.'),
  59. ),
  60. 'tripal_entities_md5checksum_checkbox_widget' => array(
  61. 'label' => t('MD5 Checksum Checkbox'),
  62. 'field types' => array('md5checksum')
  63. ),
  64. );
  65. }
  66. /**
  67. * Implements hook_field_formatter_info().
  68. */
  69. function tripal_entities_field_formatter_info() {
  70. return array(
  71. 'tripal_entities_organism_formatter' => array(
  72. 'label' => t('Organism'),
  73. 'field types' => array('organism_id')
  74. ),
  75. 'tripal_entities_primary_dbxref_formatter' => array(
  76. 'label' => t('Cross-reference'),
  77. 'field types' => array('dbxref_id')
  78. ),
  79. 'tripal_entities_md5checksum_formatter' => array(
  80. 'label' => t('MD5 checksum'),
  81. 'field types' => array('md5checksum')
  82. ),
  83. );
  84. }
  85. /**
  86. * Implements hook_field_formatter_view().
  87. *
  88. * Two formatters are implemented.
  89. * - field_example_simple_text just outputs markup indicating the color that
  90. * was entered and uses an inline style to set the text color to that value.
  91. * - field_example_color_background does the same but also changes the
  92. * background color of div.region-content.
  93. *
  94. * @see field_example_field_formatter_info()
  95. */
  96. function tripal_entities_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  97. $element = array();
  98. switch ($display['type']) {
  99. case 'tripal_entities_organism_formatter':
  100. foreach ($items as $delta => $item) {
  101. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $item['value']));
  102. // TODO: add hook here to allow other modules to change this display
  103. // if they want.
  104. $element[$delta] = array(
  105. // We create a render array to produce the desired markup,
  106. // "<p>Genus Species</p>".
  107. // See theme_html_tag().
  108. '#type' => 'markup',
  109. '#markup' => '<i>' . $organism[0]->genus .' ' . $organism[0]->species . '</i>',
  110. );
  111. }
  112. break;
  113. case 'tripal_entities_primary_dbxref_formatter':
  114. foreach ($items as $delta => $item) {
  115. $accession = '';
  116. if ($item['value']) {
  117. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $item['value']));
  118. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  119. if ($dbxref->db_id->urlprefix) {
  120. $accession = l($accession, $dbxref->db_id->urlprefix . '/' . $dbxref->accession);
  121. }
  122. }
  123. // TODO: add hook here to allow other modules to change this display
  124. // if they want.
  125. $element[$delta] = array(
  126. // We create a render array to produce the desired markup,
  127. '#type' => 'markup',
  128. '#markup' => $accession,
  129. );
  130. }
  131. break;
  132. case 'tripal_entities_md5checksum_formatter':
  133. foreach ($items as $delta => $item) {
  134. // TODO: add hook here to allow other modules to change this display
  135. // if they want.
  136. $element[$delta] = array(
  137. // We create a render array to produce the desired markup,
  138. '#type' => 'markup',
  139. '#markup' => key_exists('value', $item) ? $item['value'] : '',
  140. );
  141. }
  142. break;
  143. }
  144. return $element;
  145. }
  146. /**
  147. * Implements hook_field_widget_form().
  148. */
  149. function tripal_entities_field_widget_form(&$form, &$form_state, $field,
  150. $instance, $langcode, $items, $delta, $element) {
  151. $widget = $element;
  152. $widget['#delta'] = $delta;
  153. switch ($instance['widget']['type']) {
  154. case 'tripal_entities_organism_select_widget':
  155. $options = tripal_get_organism_select_options();
  156. $widget += array(
  157. '#type' => 'select',
  158. '#title' => $element['#title'],
  159. '#description' => $element['#description'],
  160. '#options' => $options,
  161. '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
  162. '#required' => $element['#required'],
  163. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  164. '#delta' => $delta,
  165. '#element_validate' => array('tripal_entities_organism_select_widget_validate'),
  166. );
  167. $element['value'] = $widget;
  168. break;
  169. case 'tripal_entities_primary_dbxref_widget':
  170. // Get the field defaults from the database if a record exists.
  171. $dbxref_id = '';
  172. $db_id = '';
  173. $accession = '';
  174. $version = '';
  175. $description = '';
  176. if (count($items) > 0 and $items[0]['value']) {
  177. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $items[0]['value']));
  178. $dbxref_id = $dbxref->dbxref_id;
  179. $db_id = $dbxref->db_id->db_id;
  180. $accession = $dbxref->accession;
  181. $version = $dbxref->version;
  182. $description = $dbxref->description;
  183. }
  184. $schema = chado_get_schema('dbxref');
  185. $options = tripal_get_db_select_options();
  186. $widget += array(
  187. '#element_validate' => array('tripal_entities_primary_dbxref_widget_validate'),
  188. '#type' => 'fieldset',
  189. '#title' => $element['#title'],
  190. '#description' => $element['#description'],
  191. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  192. '#delta' => $delta,
  193. '#theme' => 'tripal_entities_primary_dbxref_widget',
  194. array(
  195. $element['#field_name'] => array(
  196. '#type' => 'hidden',
  197. '#default_value' => $dbxref_id,
  198. ),
  199. 'dbxref__db_id' => array(
  200. '#type' => 'select',
  201. '#title' => t('Database'),
  202. '#options' => $options,
  203. '#required' => $element['#required'],
  204. '#default_value' => $db_id,
  205. ),
  206. 'dbxref__accession' => array(
  207. '#type' => 'textfield',
  208. '#title' => t('Accession'),
  209. '#default_value' => $accession,
  210. '#required' => $element['#required'],
  211. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  212. '#size' => 15,
  213. ),
  214. 'dbxref__version' => array(
  215. '#type' => 'textfield',
  216. '#title' => t('Version'),
  217. '#default_value' => $version,
  218. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  219. '#size' => 5,
  220. ),
  221. 'dbxref__description' => array(
  222. '#type' => 'textfield',
  223. '#title' => t('Description'),
  224. '#default_value' => $description,
  225. '#size' => 20,
  226. ),
  227. ),
  228. );
  229. $element['value'] = $widget;
  230. break;
  231. case 'tripal_entities_md5checksum_checkbox_widget':
  232. $widget += array(
  233. '#type' => 'checkbox',
  234. '#title' => $element['#title'],
  235. '#description' => $element['#description'],
  236. '#options' => array(0, 1),
  237. '#default_value' => 1,
  238. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  239. '#delta' => $delta,
  240. '#element_validate' => array('tripal_entities_md5checksum_checkbox_widget_validate'),
  241. );
  242. $element['value'] = $widget;
  243. break;
  244. }
  245. return $element;
  246. }
  247. /**
  248. * Implements hook_field_is_empty().
  249. */
  250. function tripal_entities_field_is_empty($item, $field) {
  251. if (empty($item['value']) && (string) $item['value'] !== '0') {
  252. return TRUE;
  253. }
  254. return FALSE;
  255. }
  256. /**
  257. * Callback function for validating the tripal_entities_organism_select_widget.
  258. */
  259. function tripal_entities_organism_select_widget_validate($element, &$form_state) {
  260. $field_name = $element['#field_name'];
  261. // If the form ID is field_ui_field_edit_form, then the user is editing the
  262. // field's values in the manage fields form of Drupal. We don't want
  263. // to validate it as if it were being used in a data entry form.
  264. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  265. return;
  266. }
  267. $organism_id = tripal_entities_get_field_form_values($field_name, $form_state);
  268. if (count($organism_id) == 0) {
  269. form_error($element, t("Please specify an organism that already exists in the database."));
  270. }
  271. }
  272. /**
  273. * Callback function for validating the tripal_entities_organism_select_widget.
  274. */
  275. function tripal_entities_primary_dbxref_widget_validate($element, &$form_state) {
  276. $field_name = $element['#field_name'];
  277. // If the form ID is field_ui_field_edit_form, then the user is editing the
  278. // field's values in the manage fields form of Drupal. We don't want
  279. // to validate it as if it were being used in a data entry form.
  280. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  281. return;
  282. }
  283. // Get the field values.
  284. $db_id = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__db_id");
  285. $accession = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__accession");
  286. $version = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__version");
  287. $description = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__description");
  288. // Make sure that if a database ID is provided that an accession is also
  289. // provided. Here we use the form_set_error function rather than the
  290. // form_error function because the form_error will add a red_highlight
  291. // around all of the fields in the fieldset which is confusing as it's not
  292. // clear to the user what field is required and which isn't. Therefore,
  293. // we borrow the code from the 'form_error' function and append the field
  294. // so that the proper field is highlighted on error.
  295. if (count($db_id) == 0 and count($accession) > 0) {
  296. 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."));
  297. }
  298. if (count($db_id) > 0 and count($accession) == 0) {
  299. form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  300. }
  301. // If user did not select a database, we want to remove dbxref_id from the
  302. // field.
  303. if (count($db_id) == 0) {
  304. tripal_entities_set_field_form_values($field_name, $form_state, '__NULL__', $field_name);
  305. }
  306. }
  307. /**
  308. * Callback function for validating the tripal_entities_md5checksum_checkbox_widget.
  309. */
  310. function tripal_entities_md5checksum_checkbox_widget_validate($element, &$form_state) {
  311. // Calculate the md5 checksum for the sequence per user's request
  312. if (key_exists('feature__md5checksum', $field_vals) && key_exists('feature__residues', $field_vals)) {
  313. $residues = $field_vals['feature__residues'];
  314. $field_vals['feature__md5checksum'] = $field_vals['feature__md5checksum'] ? md5($residues) : NULL;
  315. }
  316. }
  317. /**
  318. * Theme function for the primary_dbxref_widget.
  319. *
  320. * @param $variables
  321. */
  322. function theme_tripal_entities_primary_dbxref_widget($variables) {
  323. $element = $variables['element'];
  324. $layout = "
  325. <div class=\"primary-dbxref-widget\">
  326. <div class=\"primary-dbxref-widget-item\">" .
  327. drupal_render($element[0]['dbxref__db_id']) . "
  328. </div>
  329. <div class=\"primary-dbxref-widget-item\">" .
  330. drupal_render($element[0]['dbxref__accession']) . "
  331. </div>
  332. <div class=\"primary-dbxref-widget-item\">" .
  333. drupal_render($element[0]['dbxref__version']) . "
  334. </div>
  335. <div class=\"primary-dbxref-widget-item\">" .
  336. drupal_render($element[0]['dbxref__description']) . "
  337. </div>
  338. </div>
  339. ";
  340. return $layout;
  341. }
  342. /**
  343. * Returns the values of the field from the $form_state.
  344. */
  345. function tripal_entities_get_field_form_values($field_name, $form_state, $child = NULL) {
  346. $values = array();
  347. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  348. foreach ($items as $delta => $value) {
  349. if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
  350. $values[] = $value['value'][0][$child];
  351. }
  352. else if (!$child and $value['value']) {
  353. $values[] = $value['value'];
  354. }
  355. }
  356. }
  357. return $values;
  358. }
  359. /**
  360. * Returns the values of the field from the $form_state.
  361. */
  362. function tripal_entities_set_field_form_values($field_name, &$form_state, $newvalue, $child = NULL) {
  363. $values = array();
  364. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  365. foreach ($items as $delta => $value) {
  366. if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
  367. $form_state['values'][$field_name][$langcode][$delta]['value'][0][$child] = $newvalue;
  368. }
  369. else if (!$child and $value['value']) {
  370. $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
  371. }
  372. }
  373. }
  374. return $values;
  375. }