tripal_chado.module 27 KB

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