tripal_chado.module 29 KB

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