tripal_chado.module 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. <?php
  2. require_once "api/tripal_chado.api.inc";
  3. require_once "includes/tripal_chado.term_storage.inc";
  4. require_once "includes/tripal_chado.field_storage.inc";
  5. /**
  6. * Implements hook_field_info().
  7. */
  8. function tripal_chado_field_info() {
  9. $fields = array(
  10. 'organism_id' => array(
  11. 'label' => t('Organism'),
  12. 'description' => t('A field for specifying an organism.'),
  13. 'default_widget' => 'tripal_chado_organism_select_widget',
  14. 'default_formatter' => 'tripal_chado_organism_formatter',
  15. 'settings' => array(),
  16. 'storage' => array(
  17. 'type' => 'field_chado_storage',
  18. 'module' => 'tripal_chado',
  19. 'active' => TRUE
  20. ),
  21. ),
  22. 'dbxref_id' => array(
  23. 'label' => t('Cross-reference'),
  24. 'description' => t('This record can be cross-referenced with a record in
  25. another online database. This field is intended for the most prominent
  26. reference. At a minimum, the database and accession must be provided.'),
  27. 'default_widget' => 'tripal_chado_dbxref_id_widget',
  28. 'default_formatter' => 'tripal_chado_dbxref_id_formatter',
  29. 'settings' => array(),
  30. 'storage' => array(
  31. 'type' => 'field_chado_storage',
  32. 'module' => 'tripal_chado',
  33. 'active' => TRUE
  34. ),
  35. ),
  36. 'residues' => array(
  37. 'label' => t('Residues'),
  38. 'description' => t('A field for managing nucleotide and protein residues.'),
  39. 'default_widget' => 'tripal_chado_residue_textarea_widget',
  40. 'default_formatter' => 'tripal_chado_residues_formatter',
  41. 'settings' => array(),
  42. 'storage' => array(
  43. 'type' => 'field_chado_storage',
  44. 'module' => 'tripal_chado',
  45. 'active' => TRUE
  46. ),
  47. ),
  48. 'md5checksum' => array(
  49. 'label' => t('MD5 checksum'),
  50. 'description' => t('A field for generating MD5 checksum for a sequence.'),
  51. 'default_widget' => 'tripal_chado_md5checksum_checkbox_widget',
  52. 'default_formatter' => 'tripal_chado_md5checksum_formatter',
  53. 'settings' => array(),
  54. 'storage' => array(
  55. 'type' => 'field_chado_storage',
  56. 'module' => 'tripal_chado',
  57. 'active' => TRUE
  58. ),
  59. ),
  60. 'seqlen' => array(
  61. 'label' => t('Sequence length'),
  62. 'description' => t('A field for calculating the length of a sequence.'),
  63. 'default_widget' => 'tripal_chado_seqlen_hidden_widget',
  64. 'default_formatter' => 'tripal_chado_seqlen_formatter',
  65. 'settings' => array(),
  66. 'storage' => array(
  67. 'type' => 'field_chado_storage',
  68. 'module' => 'tripal_chado',
  69. 'active' => TRUE
  70. ),
  71. ),
  72. // The field provides a widget for adding new properties
  73. // to an entity that is connected to a base table that has a prop table
  74. // in Chado.
  75. 'kvproperty_adder' => array(
  76. 'label' => t('Add a Property Type'),
  77. 'description' => t('This record may have any number of properties. Use
  78. this field to first add the type.'),
  79. 'default_widget' => 'tripal_chado_kvproperty_adder_widget',
  80. 'default_formatter' => 'tripal_chado_kvproperty_adder_formatter',
  81. 'settings' => array(),
  82. 'storage' => array(
  83. 'type' => 'field_chado_storage',
  84. 'module' => 'tripal_chado',
  85. 'active' => TRUE
  86. ),
  87. ),
  88. // The field provides form elements for adding a property to an entity
  89. // that in turn gets stored in a prop table of Chado (e.g. featureprop,
  90. // stockprop, etc).
  91. 'kvproperty' => array(
  92. 'label' => t('Add a Property'),
  93. 'description' => t('Add details about this property.'),
  94. 'default_widget' => 'tripal_chado_kvproperty_widget',
  95. 'default_formatter' => 'tripal_chado_kvproperty_formatter',
  96. 'settings' => array(),
  97. 'storage' => array(
  98. 'type' => 'field_chado_storage',
  99. 'module' => 'tripal_chado',
  100. 'active' => TRUE
  101. ),
  102. ),
  103. );
  104. return $fields;
  105. }
  106. /**
  107. * Implements hook_field_widget_info().
  108. */
  109. function tripal_chado_field_widget_info() {
  110. return array(
  111. 'tripal_chado_organism_select_widget' => array(
  112. 'label' => t('Organism Select'),
  113. 'field types' => array('organism_id')
  114. ),
  115. 'tripal_chado_dbxref_id_widget' => array(
  116. 'label' => t('Cross-reference'),
  117. 'field types' => array('dbxref_id'),
  118. '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.'),
  119. ),
  120. 'tripal_chado_md5checksum_checkbox_widget' => array(
  121. 'label' => t('MD5 Checksum Checkbox'),
  122. 'field types' => array('md5checksum'),
  123. ),
  124. 'tripal_chado_residues_textarea_widget' => array(
  125. 'label' => t('Residues'),
  126. 'field types' => array('residues'),
  127. ),
  128. 'tripal_chado_seqlen_hidden_widget' => array(
  129. 'label' => t('Sequence Length'),
  130. 'field types' => array('seqlen'),
  131. ),
  132. 'tripal_chado_kvproperty_adder_widget' => array(
  133. 'label' => t('Add a Property'),
  134. 'field types' => array('kvproperty_adder'),
  135. ),
  136. 'tripal_chado_kvproperty_widget' => array(
  137. 'label' => t('Property'),
  138. 'field types' => array('kvproperty'),
  139. ),
  140. );
  141. }
  142. /**
  143. * Implements hook_field_formatter_info().
  144. */
  145. function tripal_chado_field_formatter_info() {
  146. return array(
  147. 'tripal_chado_organism_formatter' => array(
  148. 'label' => t('Organism'),
  149. 'field types' => array('organism_id')
  150. ),
  151. 'tripal_chado_dbxref_id_formatter' => array(
  152. 'label' => t('Cross-reference'),
  153. 'field types' => array('dbxref_id')
  154. ),
  155. 'tripal_chado_md5checksum_formatter' => array(
  156. 'label' => t('MD5 checksum'),
  157. 'field types' => array('md5checksum')
  158. ),
  159. 'tripal_chado_residues_formatter' => array(
  160. 'label' => t('Residues'),
  161. 'field types' => array('residues')
  162. ),
  163. 'tripal_chado_seqlen_formatter' => array(
  164. 'label' => t('Sequence length'),
  165. 'field types' => array('seqlen')
  166. ),
  167. 'tripal_chado_kvproperty_adder_formatter' => array(
  168. 'label' => t('Add a Property'),
  169. 'field types' => array('kvproperty_adder')
  170. ),
  171. 'tripal_chado_kvproperty_formatter' => array(
  172. 'label' => t('Property'),
  173. 'field types' => array('kvproperty')
  174. ),
  175. );
  176. }
  177. /**
  178. * Implements hook_chado_field_alter().
  179. *
  180. * This function is used to change the default field formatter and widget
  181. * that are assigned to fields of an Entity. This hook is only used for
  182. * those fields that correspond to a column in a Chado table. An implementation
  183. * of this hook can be used to change the default formatters and widgets to
  184. * custom formatters and widgets that are created by the module creating
  185. * this hook.
  186. *
  187. * By default, Tripal will provide custom formatters and widgets for many
  188. * columns in Chado tables, therefore, this hook will most likely be of use
  189. * to extension modules that create custom table inside of Chado.
  190. *
  191. * @param $field
  192. */
  193. function hook_chado_field_alter(&$field) {
  194. // TODO: add example code for how to use this hook.
  195. }
  196. /**
  197. * Implements hook_chado_field_alter().
  198. *
  199. * This function adds the custom formatters and widgets to many of the Chado
  200. * tables. This way Tripal users get a nice set of already usable fields.
  201. */
  202. function tripal_chado_chado_field_alter(&$field) {
  203. if (!array_key_exists('field_settings', $field)) {
  204. return;
  205. }
  206. // If the field doesn't list the Chado table or column then just return.
  207. if (!array_key_exists('chado_table', $field['field_settings']) or
  208. !array_key_exists('chado_column', $field['field_settings'])) {
  209. return;
  210. }
  211. // Here we provide new field types and widgets for FK fields
  212. // and fields that need special attention.
  213. if ($field['field_settings']['chado_column'] =='organism_id') {
  214. $field['field_type'] = 'organism_id';
  215. $field['widget_type'] = 'tripal_chado_organism_select_widget';
  216. $field['label'] = 'Organism';
  217. $field['description'] = 'Select an organism.';
  218. }
  219. else if ($field['field_settings']['chado_column'] =='dbxref_id') {
  220. $field['field_type'] = 'dbxref_id';
  221. $field['widget_type'] = 'tripal_chado_primary_dbxref_widget';
  222. $field['label'] = 'Primary Cross Reference';;
  223. $field['description'] = 'This record can be cross-referenced with a
  224. record in another online database. The primary reference is for the
  225. most prominent reference. At a minimum, the database and accession
  226. must be provided. To remove a set reference, change the database
  227. field to "Select a Database".';
  228. }
  229. else if ($field['field_settings']['chado_table'] == 'feature' and
  230. $field['field_settings']['chado_column'] == 'md5checksum') {
  231. $field['field_type'] = 'md5checksum';
  232. $field['widget_type'] = 'tripal_chado_md5checksum_checkbox_widget';
  233. $field['label'] = 'MD5 Checksum';
  234. $field['description'] = 'Generating MD5 checksum for the sequence.';
  235. }
  236. else if ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'seqlen') {
  237. $field['field_type'] = 'seqlen';
  238. $field['widget_type'] = 'tripal_chado_seqlen_hidden_widget';
  239. $field['label'] = 'Seqlen';
  240. $field['description'] = 'The length of the residues.';
  241. }
  242. else if ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'residues') {
  243. $field['field_type'] = 'residues';
  244. $field['widget_type'] = 'tripal_chado_residues_textarea_widget';
  245. $field['label'] = 'Residues';
  246. $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
  247. }
  248. else if ($field['label'] == 'Timeaccessioned') {
  249. $field['label'] = 'Time Accessioned';
  250. $field['description'] = 'Please enter the time that this record was first added to the database.';
  251. }
  252. else if ($field['label'] == 'Timelastmodified') {
  253. $field['label'] = 'Time Last Modified';
  254. $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  255. }
  256. }
  257. /**
  258. * Implements hook_field_widget_form_alter().
  259. */
  260. function tripal_chado_field_widget_form_alter(&$element, &$form_state, $context) {
  261. if (array_key_exists('#field_name', $element)) {
  262. $field_name = $element['#field_name'];
  263. $matches = array();
  264. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  265. $tablename = $matches[1];
  266. $colname = $matches[2];
  267. $schema = chado_get_schema($tablename);
  268. // The timelastmodified field exists in many Chado tables. We want
  269. // the form element to update to the most recent time rather than the time
  270. // in the database.
  271. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  272. // We want the default value for the field to be the current time.
  273. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  274. $element['#date_items']['value'] = $element['#default_value']['value'];
  275. }
  276. // We want the date combo fieldset to be collaspible so we will
  277. // add our own theme_wrapper to replace the one added by the date
  278. // module.
  279. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  280. $element['#theme_wrappers'] = array('tripal_chado_date_combo');
  281. }
  282. }
  283. }
  284. }
  285. /**
  286. * Implements hook_field_formatter_view().
  287. */
  288. function tripal_chado_field_formatter_view($entity_type, $entity, $field,
  289. $instance, $langcode, $items, $display) {
  290. $element = array();
  291. switch ($display['type']) {
  292. case 'tripal_chado_organism_formatter':
  293. module_load_include('inc', 'tripal_chado', 'includes/fields/organism_id');
  294. tripal_chado_organism_select_formatter($element, $entity_type, $entity, $field,
  295. $instance, $langcode, $items, $display);
  296. break;
  297. case 'tripal_chado_dbxref_id_formatter':
  298. module_load_include('inc', 'tripal_chado', 'includes/fields/dbxref_id');
  299. tripal_chado_dbxref_id_formatter($element, $entity_type, $entity, $field,
  300. $instance, $langcode, $items, $display);
  301. break;
  302. case 'tripal_chado_md5checksum_formatter':
  303. module_load_include('inc', 'tripal_chado', 'includes/fields/md5checksum');
  304. tripal_chado_md5checksum_checkbox_formatter($element, $entity_type, $entity, $field,
  305. $instance, $langcode, $items, $display);
  306. break;
  307. case 'tripal_chado_residues_formatter':
  308. module_load_include('inc', 'tripal_chado', 'includes/fields/residues');
  309. tripal_chado_residues_textarea_formatter($element, $entity_type, $entity, $field,
  310. $instance, $langcode, $items, $display);
  311. break;
  312. case 'tripal_chado_seqlen_formatter':
  313. module_load_include('inc', 'tripal_chado', 'includes/fields/seqlen');
  314. tripal_chado_seqlen_hidden_formatter($element, $entity_type, $entity, $field,
  315. $instance, $langcode, $items, $display);
  316. break;
  317. case 'tripal_chado_kvproperty_adder_formatter':
  318. module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty_adder');
  319. tripal_chado_kvproperty_adder_formatter($element, $entity_type, $entity, $field,
  320. $instance, $langcode, $items, $display);
  321. break;
  322. case 'tripal_chado_kvproperty_formatter':
  323. module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty');
  324. tripal_chado_kvproperty_formatter($element, $entity_type, $entity, $field,
  325. $instance, $langcode, $items, $display);
  326. break;
  327. }
  328. return $element;
  329. }
  330. /**
  331. * Implements hook_field_widget_form().
  332. */
  333. function tripal_chado_field_widget_form(&$form, &$form_state, $field,
  334. $instance, $langcode, $items, $delta, $element) {
  335. $widget = $element;
  336. switch ($instance['widget']['type']) {
  337. case 'tripal_chado_organism_select_widget':
  338. // Make sure the include files get parsed now and for the form submits.
  339. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/organism_id');
  340. module_load_include('inc', 'tripal_chado', 'includes/fields/organism_id');
  341. // Update the widget with the new field.
  342. tripal_chado_organism_select_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  343. break;
  344. case 'tripal_chado_dbxref_id_widget':
  345. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/dbxref_id');
  346. module_load_include('inc', 'tripal_chado', 'includes/fields/dbxref_id');
  347. tripal_chado_dbxref_id_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  348. break;
  349. case 'tripal_chado_md5checksum_checkbox_widget':
  350. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/md5checksum');
  351. module_load_include('inc', 'tripal_chado', 'includes/fields/md5checksum');
  352. tripal_chado_md5checksum_checkbox_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  353. break;
  354. case 'tripal_chado_residues_textarea_widget':
  355. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/residues');
  356. module_load_include('inc', 'tripal_chado', 'includes/fields/residues');
  357. tripal_chado_residues_textarea_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  358. break;
  359. case 'tripal_chado_seqlen_hidden_widget':
  360. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/seqlen');
  361. module_load_include('inc', 'tripal_chado', 'includes/fields/seqlen');
  362. tripal_chado_seqlen_hidden_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  363. break;
  364. case 'tripal_chado_kvproperty_adder_widget':
  365. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/kvproperty_adder');
  366. module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty_adder');
  367. tripal_chado_kvproperty_adder_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  368. break;
  369. case 'tripal_chado_kvproperty_widget':
  370. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/kvproperty');
  371. module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty');
  372. tripal_chado_kvproperty_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  373. break;
  374. }
  375. return $widget;
  376. }
  377. /**
  378. * Implements hook_form_FORM_ID_alter().
  379. *
  380. * The field_ui_display_overview_form is used for formatting the display
  381. * or layout of fields attached to an entity and shown on the entity view page.
  382. */
  383. function tripal_chado_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  384. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  385. // on the add/edit form of an entity for adding new property fields.
  386. $fields_names = element_children($form['fields']);
  387. foreach ($fields_names as $field_name) {
  388. $field_info = field_info_field($field_name);
  389. if ($field_info['type'] == 'kvproperty_adder') {
  390. unset($form['fields'][$field_name]);
  391. }
  392. }
  393. }
  394. /**
  395. * Implements hook_form_FORM_ID_alter().
  396. *
  397. * The field_ui_field_overview_form is used for ordering and configuring the
  398. * fields attached to an entity.
  399. */
  400. function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  401. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  402. // on the add/edit form of an entity for adding new property fields.
  403. $fields_names = element_children($form['fields']);
  404. foreach ($fields_names as $field_name) {
  405. $field_info = field_info_field($field_name);
  406. if ($field_info['type'] == 'kvproperty_adder') {
  407. unset($form['fields'][$field_name]);
  408. }
  409. }
  410. }
  411. /**
  412. * Implements hook_field_is_empty().
  413. */
  414. function tripal_chado_field_is_empty($item, $field) {
  415. // If there is no value field then the field is empty.
  416. if (!array_key_exists('value', $item)) {
  417. return TRUE;
  418. }
  419. // Iterate through all of the fields and if at least one has a value
  420. // the field is not empty.
  421. foreach ($item as $form_field_name => $value) {
  422. if (isset($value) and $value != NULL and $value != '') {
  423. return FALSE;
  424. }
  425. }
  426. // Otherwise, the field is empty.
  427. return TRUE;
  428. }
  429. /**
  430. * Returns the values of the field from the $form_state.
  431. */
  432. function tripal_chado_get_field_form_values($field_name, $form_state, $delta = 0, $child = NULL) {
  433. $value = NULL;
  434. // The form_state must have the 'values' key. If not then just return.
  435. if (!array_key_exists('values', $form_state)) {
  436. return $value;
  437. }
  438. // If the field name is not in the form_state['values'] then return.
  439. if (!array_key_exists($field_name, $form_state['values'])) {
  440. return $value;
  441. }
  442. // Iterate through the values looking for the field_name provided.
  443. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  444. $item = $items[$delta];
  445. if ($child){
  446. if(array_key_exists($child, $item) and $item[$child] != '') {
  447. $value = $item[$child];
  448. }
  449. }
  450. else {
  451. $value = $item['value'];
  452. }
  453. }
  454. return $value;
  455. }
  456. /**
  457. * Sets the values of the field from the $form_state.
  458. */
  459. function tripal_chado_set_field_form_values($field_name, &$form_state, $newvalue, $delta = 0, $child = NULL) {
  460. // The form_state must have the 'values' key. If not then just return.
  461. if (!array_key_exists('values', $form_state)) {
  462. return FALSE;
  463. }
  464. // If the field name is not in the form_state['values'] then reutrn.
  465. if (!array_key_exists($field_name, $form_state['values'])) {
  466. return FALSE;
  467. }
  468. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  469. if ($child) {
  470. $form_state['values'][$field_name][$langcode][$delta][$child] = $newvalue;
  471. }
  472. else {
  473. $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
  474. }
  475. }
  476. return TRUE;
  477. }
  478. /**
  479. * Implements hook_theme().
  480. */
  481. function tripal_chado_theme($existing, $type, $theme, $path) {
  482. return array(
  483. 'tripal_chado_dbxref_id_widget' => array(
  484. 'render element' => 'element',
  485. 'file' => 'includes/fields/dbxref_id.inc',
  486. ),
  487. 'tripal_chado_kvproperty_addr_widget' => array(
  488. 'render element' => 'element',
  489. 'file' => 'includes/fields/dbxref_id.inc',
  490. ),
  491. 'tripal_chado_date_combo' => array(
  492. 'render element' => 'element',
  493. 'file' => 'includes/tripal_chado.entity.inc',
  494. ),
  495. );
  496. }
  497. /**
  498. * Implements hook_add_bundle_fields().
  499. */
  500. function tripal_chado_add_bundle_fields($entity_type, $bundle, $term) {
  501. $bundle_name = $bundle->bundle;
  502. // This array will hold details that map the bundle to tables in Chado.
  503. $bundle_data = array();
  504. // Get the cvterm that corresponds to this TripalTerm object.
  505. $vocab = entity_load('TripalVocab', array($term->vocab_id));
  506. $vocab = reset($vocab);
  507. $match = array(
  508. 'dbxref_id' => array(
  509. 'db_id' => array(
  510. 'name' => $vocab->namespace,
  511. ),
  512. 'accession' => $term->term_id
  513. ),
  514. );
  515. $cvterm = chado_generate_var('cvterm', $match);
  516. // The organism table does not have a type_id so we won't ever find
  517. // a record for it in the tripal_cv_defaults table.
  518. if ($cvterm->name == 'organism') {
  519. $bundle_data = array(
  520. 'cv_id' => $cvterm->cv_id->cv_id,
  521. 'cvterm_id' => $cvterm->cvterm_id,
  522. 'data_table' => 'organism',
  523. 'type_table' => 'organism',
  524. 'field' => '',
  525. );
  526. }
  527. // The analysis table does not have a type_id so we won't ever find
  528. // a record for it in the tripalcv_defaults table.
  529. else if ($cvterm->name == 'analysis') {
  530. $bundle_data = array(
  531. 'cv_id' => $cvterm->cv_id->cv_id,
  532. 'cvterm_id' => $cvterm->cvterm_id,
  533. 'data_table' => 'analysis',
  534. 'type_table' => 'analysis',
  535. 'field' => '',
  536. );
  537. }
  538. else if ($cvterm->name == 'project') {
  539. $bundle_data = array(
  540. 'cv_id' => $cvterm->cv_id->cv_id,
  541. 'cvterm_id' => $cvterm->cvterm_id,
  542. 'data_table' => 'project',
  543. 'type_table' => 'project',
  544. 'field' => '',
  545. );
  546. }
  547. else {
  548. // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
  549. // TABLES.
  550. // Look to see if this vocabulary is used as a default for any table.
  551. $default = db_select('tripal_cv_defaults', 't')
  552. ->fields('t')
  553. ->condition('cv_id', $cvterm->cv_id->cv_id)
  554. ->execute()
  555. ->fetchObject();
  556. if ($default) {
  557. $bundle_data = array(
  558. 'cv_id' => $cvterm->cv_id->cv_id,
  559. 'cvterm_id' => $cvterm->cvterm_id,
  560. 'data_table' => $default->table_name,
  561. 'type_table' => $default->table_name,
  562. 'field' => $default->field_name,
  563. );
  564. }
  565. }
  566. // Adds the fields for the base table to the entity.
  567. tripal_chado_add_bundle_base_fields($entity_type, $bundle_name, $bundle_data);
  568. // Save the mapping information so that we can reuse it when we need to
  569. // look things up for later for an entity
  570. tripal_set_bundle_variable('chado_cvterm_id', $bundle->id, $bundle_data['cvterm_id']);
  571. tripal_set_bundle_variable('chado_table', $bundle->id, $bundle_data['data_table']);
  572. tripal_set_bundle_variable('chado_column', $bundle->id, $bundle_data['field']);
  573. // Check to see if there are any kv-property tables associated to this
  574. // base table. If so, add the fields for that type of table.
  575. tripal_chado_add_bundle_kvproperty_adder_field($entity_type, $bundle_name, 'featureprop');
  576. }
  577. /**
  578. * Adds the fields for a kv-property table fields
  579. *
  580. * @param $entity_type_name
  581. * @param $bundle_name
  582. * @param $kv_table
  583. */
  584. function tripal_chado_add_bundle_kvproperty_adder_field($entity_type_name, $bundle_name, $kv_table) {
  585. // First add a generic property field so that users can add new proeprty types.
  586. $field_name = $kv_table;
  587. // Initialize the field array.
  588. $field_info = array(
  589. 'field_type' => 'kvproperty_adder',
  590. 'widget_type' => 'tripal_fields_kvproperty_adder_widget',
  591. 'field_settings' => array(),
  592. 'widget_settings' => array('display_label' => 1),
  593. 'description' => '',
  594. 'label' => 'Additional Properties',
  595. 'is_required' => 0,
  596. );
  597. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  598. }
  599. /**
  600. * Adds the fields for the base table to the entity.
  601. */
  602. function tripal_chado_add_bundle_base_fields($entity_type_name, $bundle_name, $bundle_data) {
  603. $table_name = $bundle_data['data_table'];
  604. $type_table = $bundle_data['type_table'];
  605. $type_field = $bundle_data['field'];
  606. // Iterate through the columns of the table and see if fields have been
  607. // created for each one. If not, then create them.
  608. $schema = chado_get_schema($table_name);
  609. $columns = $schema['fields'];
  610. foreach ($columns as $column_name => $details) {
  611. $field_name = $table_name . '__' . $column_name;
  612. // Skip the primary key field.
  613. if ($column_name == $schema['primary key'][0]) {
  614. continue;
  615. }
  616. // Skip the type field.
  617. if ($table_name == $type_table and $column_name == $type_field) {
  618. continue;
  619. }
  620. // Get the field defaults for this column.
  621. $field_info = tripal_chado_get_table_column_field_default($table_name, $schema, $column_name);
  622. // Determine if the field is required.
  623. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  624. $field_info['is_required'] = array_key_exists('default', $details) ? 0 : 1;
  625. }
  626. // If we don't have a field type then we don't need to create a field.
  627. if (!$field_info['field_type']) {
  628. // If we don't have a field type but it is required and doesn't have
  629. // a default value then we are in trouble.
  630. if ($field_info['is_required'] and !array_key_exists('default', $details)) {
  631. throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
  632. array('%table' => $table_name, '%field' => $column_name, '%type' => $details['type'])));
  633. }
  634. continue;
  635. }
  636. // If this field is a foreign key field then we will have a special custom
  637. // field provided by Tripal.
  638. $is_fk = FALSE;
  639. if (array_key_exists('foreign keys', $schema)) {
  640. foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
  641. if (array_key_exists($column_name, $fk_details['columns'])) {
  642. $is_fk = TRUE;
  643. }
  644. }
  645. }
  646. // Add the field to the bundle.
  647. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  648. }
  649. }
  650. /**
  651. * Returns a $field_info array for a field based on a database column.
  652. *
  653. */
  654. function tripal_chado_get_table_column_field_default($table_name, $schema, $column_name) {
  655. $details = $schema['fields'][$column_name];
  656. // Create an array with information about this field.
  657. $field_info = array(
  658. 'field_type' => '',
  659. 'widget_type' => '',
  660. 'field_settings' => array(
  661. 'chado_table' => $table_name,
  662. 'chado_column' => $column_name,
  663. ),
  664. 'widget_settings' => array('display_label' => 1),
  665. 'description' => '',
  666. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  667. 'is_required' => 0,
  668. );
  669. // Alter the field info array depending on the column details.
  670. switch($details['type']) {
  671. case 'char':
  672. $field_info['field_type'] = 'text';
  673. $field_info['widget_type'] = 'text_textfield';
  674. $field_info['field_settings']['max_length'] = $details['length'];
  675. break;
  676. case 'varchar':
  677. $field_info['field_type'] = 'text';
  678. $field_info['widget_type'] = 'text_textfield';
  679. $field_info['field_settings']['max_length'] = $details['length'];
  680. break;
  681. case 'text':
  682. $field_info['field_type'] = 'text';
  683. $field_info['widget_type'] = 'text_textarea';
  684. $field_info['field_settings']['max_length'] = 17179869184;
  685. break;
  686. case 'blob':
  687. // not sure how to support a blob field.
  688. continue;
  689. break;
  690. case 'int':
  691. $field_info['field_type'] = 'number_integer';
  692. $field_info['widget_type'] = 'number';
  693. break;
  694. case 'float':
  695. $field_info['field_type'] = 'number_float';
  696. $field_info['widget_type'] = 'number';
  697. $field_info['field_settings']['precision'] = 10;
  698. $field_info['field_settings']['scale'] = 2;
  699. $field_info['field_settings']['decimal_separator'] = '.';
  700. break;
  701. case 'numeric':
  702. $field_info['field_type'] = 'number_decimal';
  703. $field_info['widget_type'] = 'number';
  704. break;
  705. case 'serial':
  706. // Serial fields are most likely not needed as a field.
  707. break;
  708. case 'boolean':
  709. $field_info['field_type'] = 'list_boolean';
  710. $field_info['widget_type'] = 'options_onoff';
  711. $field_info['field_settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  712. break;
  713. case 'datetime':
  714. // Use the Drupal Date and Date API to create the field/widget
  715. $field_info['field_type'] = 'datetime';
  716. $field_info['widget_type'] = 'date_select';
  717. $field_info['widget_settings']['increment'] = 1;
  718. $field_info['widget_settings']['tz_handling'] = 'none';
  719. $field_info['widget_settings']['collapsible'] = TRUE;
  720. // TODO: Add settings so that the minutes increment by 1.
  721. // And turn off the timezone, as the Chado field doesn't support it.
  722. break;
  723. }
  724. return $field_info;
  725. }