tripal_chado.module 30 KB

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