tripal_chado.fields.inc 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. <?php
  2. /**
  3. * Implements hook_field_info().
  4. */
  5. function tripal_chado_field_info() {
  6. $fields = array(
  7. /*
  8. * Generic fields that support multiple base tables.
  9. */
  10. 'chado_base__organism_id' => array(
  11. 'label' => t('Organism'),
  12. 'description' => t('A field for specifying an organism.'),
  13. 'default_widget' => 'tripal_chado_organism_select_widget',
  14. 'default_formatter' => 'tripal_chado_organism_formatter',
  15. 'settings' => array(),
  16. 'storage' => array(
  17. 'type' => 'field_chado_storage',
  18. 'module' => 'tripal_chado',
  19. 'active' => TRUE
  20. ),
  21. ),
  22. 'chado_base__dbxref_id' => array(
  23. 'label' => t('Cross reference'),
  24. 'description' => t('This record can be cross referenced with a record in
  25. another online database. This field is intended for the most prominent
  26. reference. At a minimum, the database and accession must be provided.'),
  27. 'default_widget' => 'tripal_chado_dbxref_id_widget',
  28. 'default_formatter' => 'tripal_chado_dbxref_id_formatter',
  29. 'settings' => array(),
  30. 'storage' => array(
  31. 'type' => 'field_chado_storage',
  32. 'module' => 'tripal_chado',
  33. 'active' => TRUE
  34. ),
  35. ),
  36. 'chado_base_pub_id' => array(
  37. 'label' => t('Publications'),
  38. 'description' => t('Associates a publication (e.g. journal article,
  39. conference proceedings, book chapter, etc.) with this record.'),
  40. 'default_widget' => 'tripal_chado_pub_widget',
  41. 'default_formatter' => 'tripal_chado_pub_formatter',
  42. 'settings' => array(),
  43. 'storage' => array(
  44. 'type' => 'field_chado_storage',
  45. 'module' => 'tripal_chado',
  46. 'active' => TRUE
  47. ),
  48. ),
  49. /*
  50. * Fields that support linker tables.
  51. */
  52. 'chado_linker_cvterm' => array(
  53. 'label' => t('Annotations'),
  54. 'description' => t('This record can be annotated with terms
  55. from other vocabularies.'),
  56. 'default_widget' => 'tripal_chado_cvterm_widget',
  57. 'default_formatter' => 'tripal_chado_cvterm_formatter',
  58. 'settings' => array(),
  59. 'storage' => array(
  60. 'type' => 'field_chado_storage',
  61. 'module' => 'tripal_chado',
  62. 'active' => TRUE
  63. ),
  64. ),
  65. 'chado_linker_synonym' => array(
  66. 'label' => t('Synonyms'),
  67. 'description' => t('Adds an alternative name (synonym or alias) to this record.'),
  68. 'default_widget' => 'tripal_chado_synonym_widget',
  69. 'default_formatter' => 'tripal_chado_synonym_formatter',
  70. 'settings' => array(),
  71. 'storage' => array(
  72. 'type' => 'field_chado_storage',
  73. 'module' => 'tripal_chado',
  74. 'active' => TRUE
  75. ),
  76. ),
  77. 'chado_linker_prop' => array(
  78. 'label' => t('Add a Property'),
  79. 'description' => t('Add details about this property.'),
  80. 'default_widget' => 'tripal_chado_kvproperty_widget',
  81. 'default_formatter' => 'tripal_chado_kvproperty_formatter',
  82. 'settings' => array(),
  83. 'storage' => array(
  84. 'type' => 'field_chado_storage',
  85. 'module' => 'tripal_chado',
  86. 'active' => TRUE
  87. ),
  88. ),
  89. 'chado_linker_dbxref' => array(
  90. 'label' => t('Cross references'),
  91. 'description' => t('This record can be cross referenced with a record in
  92. another online database. This field is intended for one or more
  93. references. At a minimum, the database and accession must be provided.'),
  94. 'default_widget' => 'tripal_chado_dbxref_widget',
  95. 'default_formatter' => 'tripal_chado_dbxref_formatter',
  96. 'settings' => array(),
  97. 'storage' => array(
  98. 'type' => 'field_chado_storage',
  99. 'module' => 'tripal_chado',
  100. 'active' => TRUE
  101. ),
  102. ),
  103. /*
  104. * Fields that add new fields. These fields are not shown on
  105. * pages. They are avaiable to site curators when adding/updating
  106. * a record and allow the user to add new linker table fields.
  107. */
  108. 'chado_linker_prop_adder' => array(
  109. 'label' => t('Add a Property Type'),
  110. 'description' => t('This record may have any number of properties. Use
  111. this field to first add the type.'),
  112. 'default_widget' => 'tripal_chado_kvproperty_adder_widget',
  113. 'default_formatter' => 'hidden',
  114. 'settings' => array(),
  115. 'storage' => array(
  116. 'type' => 'field_chado_storage',
  117. 'module' => 'tripal_chado',
  118. 'active' => TRUE
  119. ),
  120. ),
  121. // The field provides a widget for adding new vocabularies for cvterm
  122. // linker tables. This will allow cvterms to be grouped by vocabulary
  123. // ('category').
  124. 'chado_linker_cvterm_adder' => array(
  125. 'label' => t('Add an Annotation Type'),
  126. 'description' => t('This record may have any number of types of
  127. annotations. Use this field to first add the type.'),
  128. 'default_widget' => 'tripal_chado_cvterm_class_adder_widget',
  129. 'default_formatter' => 'hidden',
  130. 'settings' => array(),
  131. 'storage' => array(
  132. 'type' => 'field_chado_storage',
  133. 'module' => 'tripal_chado',
  134. 'active' => TRUE
  135. ),
  136. ),
  137. /*
  138. * Field specific to the feature table of Chado.
  139. */
  140. 'chado_feature__residues' => array(
  141. 'label' => t('Residues'),
  142. 'description' => t('A field for managing nucleotide and protein residues.'),
  143. 'default_widget' => 'tripal_chado_residue_textarea_widget',
  144. 'default_formatter' => 'tripal_chado_residues_formatter',
  145. 'settings' => array(),
  146. 'storage' => array(
  147. 'type' => 'field_chado_storage',
  148. 'module' => 'tripal_chado',
  149. 'active' => TRUE
  150. ),
  151. ),
  152. 'chado_feature__md5checksum' => array(
  153. 'label' => t('MD5 checksum'),
  154. 'description' => t('A field for generating MD5 checksum for a sequence.'),
  155. 'default_widget' => 'tripal_chado_md5checksum_checkbox_widget',
  156. 'default_formatter' => 'tripal_chado_md5checksum_formatter',
  157. 'settings' => array(),
  158. 'storage' => array(
  159. 'type' => 'field_chado_storage',
  160. 'module' => 'tripal_chado',
  161. 'active' => TRUE
  162. ),
  163. ),
  164. 'chado_feature__seqlen' => array(
  165. 'label' => t('Sequence length'),
  166. 'description' => t('A field for calculating the length of a sequence.'),
  167. 'default_widget' => 'tripal_chado_seqlen_hidden_widget',
  168. 'default_formatter' => 'tripal_chado_seqlen_formatter',
  169. 'settings' => array(),
  170. 'storage' => array(
  171. 'type' => 'field_chado_storage',
  172. 'module' => 'tripal_chado',
  173. 'active' => TRUE
  174. ),
  175. ),
  176. );
  177. return $fields;
  178. }
  179. /**
  180. * Implements hook_field_formatter_settings_summary.
  181. */
  182. function tripal_chado_field_formatter_settings_summary($field, $instance, $view_mode) {
  183. $summary = '';
  184. switch ($field['type']) {
  185. case 'organism_id':
  186. $summary = 'Organisms can be displayed in vaious ways.';
  187. break;
  188. default:
  189. $summary = '';
  190. }
  191. return $summary;
  192. }
  193. /**
  194. * Implements hook_field_formatter_settings_form.
  195. */
  196. function tripal_chado_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  197. $element = array();
  198. switch ($field['type']) {
  199. case 'chado_base__organism_id':
  200. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/organism_id');
  201. module_load_include('inc', 'tripal_chado', 'includes/fields/organism_id');
  202. $element = tripal_chado_organism_select_formatter_form($field, $instance, $view_mode, $form, $form_state);
  203. break;
  204. }
  205. return $element;
  206. }
  207. /**
  208. * Implements hook_field_formatter_view().
  209. */
  210. function tripal_chado_field_formatter_view($entity_type, $entity, $field,
  211. $instance, $langcode, $items, $display) {
  212. $element = array();
  213. switch ($display['type']) {
  214. case 'chado_base__organism_id_formatter':
  215. module_load_include('inc', 'tripal_chado', 'includes/fields/organism_id');
  216. tripal_chado_organism_select_formatter($element, $entity_type, $entity, $field,
  217. $instance, $langcode, $items, $display);
  218. break;
  219. case 'chado_base__dbxref_id_formatter':
  220. module_load_include('inc', 'tripal_chado', 'includes/fields/dbxref_id');
  221. tripal_chado_dbxref_id_formatter($element, $entity_type, $entity, $field,
  222. $instance, $langcode, $items, $display);
  223. break;
  224. case 'chado_base__pub_id_formatter':
  225. module_load_include('inc', 'tripal_chado', 'includes/fields/pub');
  226. tripal_chado_pub_formatter($element, $entity_type, $entity, $field,
  227. $instance, $langcode, $items, $display);
  228. break;
  229. case 'chado_linker_dbxref_formatter':
  230. module_load_include('inc', 'tripal_chado', 'includes/fields/dbxref');
  231. tripal_chado_dbxref_formatter($element, $entity_type, $entity, $field,
  232. $instance, $langcode, $items, $display);
  233. break;
  234. case 'chado_linker_cvterm_formatter':
  235. module_load_include('inc', 'tripal_chado', 'includes/fields/cvterm');
  236. tripal_chado_cvterm_formatter($element, $entity_type, $entity, $field,
  237. $instance, $langcode, $items, $display);
  238. break;
  239. case 'chado_linker_prop_formatter':
  240. module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty');
  241. tripal_chado_kvproperty_formatter($element, $entity_type, $entity, $field,
  242. $instance, $langcode, $items, $display);
  243. break;
  244. case 'chado_linker_synonym_formatter':
  245. module_load_include('inc', 'tripal_chado', 'includes/fields/synonym');
  246. tripal_chado_synonym_formatter($element, $entity_type, $entity, $field,
  247. $instance, $langcode, $items, $display);
  248. break;
  249. case 'chado_linker_prop_adder_formatter':
  250. module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty_adder');
  251. tripal_chado_kvproperty_adder_formatter($element, $entity_type, $entity, $field,
  252. $instance, $langcode, $items, $display);
  253. break;
  254. case 'chado_linker_cvterm_adder_formatter':
  255. module_load_include('inc', 'tripal_chado', 'includes/fields/cvterm_class_adder');
  256. tripal_chado_cvterm_class_adder_formatter($element, $entity_type, $entity, $field,
  257. $instance, $langcode, $items, $display);
  258. break;
  259. case 'chado_feature__md5checksum_formatter':
  260. module_load_include('inc', 'tripal_chado', 'includes/fields/md5checksum');
  261. tripal_chado_md5checksum_checkbox_formatter($element, $entity_type, $entity, $field,
  262. $instance, $langcode, $items, $display);
  263. break;
  264. case 'chado_feature__residues_formatter':
  265. module_load_include('inc', 'tripal_chado', 'includes/fields/residues');
  266. tripal_chado_residues_textarea_formatter($element, $entity_type, $entity, $field,
  267. $instance, $langcode, $items, $display);
  268. break;
  269. case 'chado_feature__seqlen_formatter':
  270. module_load_include('inc', 'tripal_chado', 'includes/fields/seqlen');
  271. tripal_chado_seqlen_hidden_formatter($element, $entity_type, $entity, $field,
  272. $instance, $langcode, $items, $display);
  273. break;
  274. }
  275. return $element;
  276. }
  277. /**
  278. * Implements hook_field_widget_form().
  279. */
  280. function tripal_chado_field_widget_form(&$form, &$form_state, $field,
  281. $instance, $langcode, $items, $delta, $element) {
  282. $widget = $element;
  283. switch ($instance['widget']['type']) {
  284. case 'chado_organism_select_widget':
  285. // Make sure the include files get parsed now and for the form submits.
  286. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/organism_id');
  287. module_load_include('inc', 'tripal_chado', 'includes/fields/organism_id');
  288. // Update the widget with the new field.
  289. tripal_chado_organism_select_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  290. break;
  291. case 'chado_dbxref_id_widget':
  292. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/dbxref_id');
  293. module_load_include('inc', 'tripal_chado', 'includes/fields/dbxref_id');
  294. tripal_chado_dbxref_id_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  295. break;
  296. case 'chado_dbxref_widget':
  297. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/dbxref');
  298. module_load_include('inc', 'tripal_chado', 'includes/fields/dbxref');
  299. tripal_chado_dbxref_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  300. break;
  301. case 'chado_cvterm_class_adder_widget':
  302. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/cvterm_class_adder');
  303. module_load_include('inc', 'tripal_chado', 'includes/fields/cvterm_class_adder');
  304. tripal_chado_cvterm_class_adder_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  305. break;
  306. case 'chado_cvterm_widget':
  307. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/cvterm');
  308. module_load_include('inc', 'tripal_chado', 'includes/fields/cvterm');
  309. tripal_chado_cvterm_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  310. break;
  311. case 'chado_md5checksum_checkbox_widget':
  312. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/md5checksum');
  313. module_load_include('inc', 'tripal_chado', 'includes/fields/md5checksum');
  314. tripal_chado_md5checksum_checkbox_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  315. break;
  316. case 'chado_residues_textarea_widget':
  317. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/residues');
  318. module_load_include('inc', 'tripal_chado', 'includes/fields/residues');
  319. tripal_chado_residues_textarea_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  320. break;
  321. case 'chado_seqlen_hidden_widget':
  322. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/seqlen');
  323. module_load_include('inc', 'tripal_chado', 'includes/fields/seqlen');
  324. tripal_chado_seqlen_hidden_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  325. break;
  326. case 'chado_kvproperty_adder_widget':
  327. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/kvproperty_adder');
  328. module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty_adder');
  329. tripal_chado_kvproperty_adder_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  330. break;
  331. case 'chado_kvproperty_widget':
  332. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/kvproperty');
  333. module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty');
  334. tripal_chado_kvproperty_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  335. break;
  336. case 'chado_synonym_widget':
  337. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/synonym');
  338. module_load_include('inc', 'tripal_chado', 'includes/fields/synonym');
  339. tripal_chado_synonym_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  340. break;
  341. case 'chado_pub_widget':
  342. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/pub');
  343. module_load_include('inc', 'tripal_chado', 'includes/fields/pub');
  344. tripal_chado_pub_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  345. break;
  346. }
  347. return $widget;
  348. }
  349. /**
  350. * Implements hook_field_widget_info().
  351. */
  352. function tripal_chado_field_widget_info() {
  353. return array(
  354. /*
  355. * Generic fields that support multiple base tables.
  356. */
  357. 'chado_base__organism_id_widget' => array(
  358. 'label' => t('Organism Select'),
  359. 'field types' => array('organism_id')
  360. ),
  361. 'chado_base__dbxref_id_widget' => array(
  362. 'label' => t('Cross reference'),
  363. 'field types' => array('dbxref_id'),
  364. 'description' => t('This record can be cross referenced with a record in
  365. another online database. This field is intended for the most
  366. prominent reference. At a minimum, the database and accession
  367. must be provided.'),
  368. ),
  369. 'chado_base__pub_id_widget' => array(
  370. 'label' => t('Publications'),
  371. 'field types' => array('pub'),
  372. ),
  373. /*
  374. * Fields that support linker tables.
  375. */
  376. 'chado_linker_dbxref_widget' => array(
  377. 'label' => t('Cross references'),
  378. 'field types' => array('dbxref'),
  379. 'description' => t('This record can be cross referenced with a record
  380. in another online database. This field is intended for the most
  381. prominent reference. At a minimum, the database and accession
  382. must be provided.'),
  383. ),
  384. 'chado_linker_cvterm_widget' => array(
  385. 'label' => t('Annotations'),
  386. 'field types' => array('cvterm'),
  387. 'description' => t('This record can be annotated with terms
  388. from other vocabularies.'),
  389. ),
  390. 'chado_linker_prop_widget' => array(
  391. 'label' => t('Property'),
  392. 'field types' => array('kvproperty'),
  393. ),
  394. 'chado_linker_synonym_widget' => array(
  395. 'label' => t('Synonyms'),
  396. 'field types' => array('synonym'),
  397. ),
  398. /*
  399. * Fields that add new fields. These fields are not shown on
  400. * pages. They are avaiable to site curators when adding/updating
  401. * a record and allow the user to add new linker table fields.
  402. */
  403. 'chado_linker_prop_adder_widget' => array(
  404. 'label' => t('Add a Property'),
  405. 'field types' => array('kvproperty_adder'),
  406. ),
  407. 'chado_linker_cvterm_adder_widget' => array(
  408. 'label' => t('Add an Annotation'),
  409. 'field types' => array('cvterm_class_adder'),
  410. ),
  411. /*
  412. * Field specific to the feature table of Chado.
  413. */
  414. 'chado_feature__md5checksum_widget' => array(
  415. 'label' => t('MD5 Checksum Checkbox'),
  416. 'field types' => array('md5checksum'),
  417. ),
  418. 'chado_feature__residues_widget' => array(
  419. 'label' => t('Residues'),
  420. 'field types' => array('residues'),
  421. ),
  422. 'chado_feature__seqlen_widget' => array(
  423. 'label' => t('Sequence Length'),
  424. 'field types' => array('seqlen'),
  425. ),
  426. );
  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. if (!array_key_exists($delta, $items)) {
  444. continue;
  445. }
  446. $item = $items[$delta];
  447. if ($child){
  448. if(array_key_exists($child, $item) and $item[$child] != '') {
  449. $value = $item[$child];
  450. }
  451. }
  452. else {
  453. $value = $item['value'];
  454. }
  455. }
  456. return $value;
  457. }
  458. /**
  459. * Sets the values of the field from the $form_state.
  460. */
  461. function tripal_chado_set_field_form_values($field_name, &$form_state, $newvalue, $delta = 0, $child = NULL) {
  462. // The form_state must have the 'values' key. If not then just return.
  463. if (!array_key_exists('values', $form_state)) {
  464. return FALSE;
  465. }
  466. // If the field name is not in the form_state['values'] then reutrn.
  467. if (!array_key_exists($field_name, $form_state['values'])) {
  468. return FALSE;
  469. }
  470. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  471. if ($child) {
  472. $form_state['values'][$field_name][$langcode][$delta][$child] = $newvalue;
  473. }
  474. else {
  475. $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
  476. }
  477. }
  478. return TRUE;
  479. }
  480. /**
  481. * Implements hook_field_widget_form_alter().
  482. */
  483. function tripal_chado_field_widget_form_alter(&$element, &$form_state, $context) {
  484. if (array_key_exists('#field_name', $element)) {
  485. $field_name = $element['#field_name'];
  486. $matches = array();
  487. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  488. $tablename = $matches[1];
  489. $colname = $matches[2];
  490. $schema = chado_get_schema($tablename);
  491. // The timelastmodified field exists in many Chado tables. We want
  492. // the form element to update to the most recent time rather than the time
  493. // in the database.
  494. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  495. // We want the default value for the field to be the current time.
  496. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  497. $element['#date_items']['value'] = $element['#default_value']['value'];
  498. }
  499. // We want the date combo fieldset to be collaspible so we will
  500. // add our own theme_wrapper to replace the one added by the date
  501. // module.
  502. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  503. $element['#theme_wrappers'] = array('tripal_chado_date_combo');
  504. }
  505. }
  506. }
  507. }
  508. /**
  509. * Implements hook_field_formatter_info().
  510. */
  511. function tripal_chado_field_formatter_info() {
  512. return array(
  513. 'chado_base__organism_id_formatter' => array(
  514. 'label' => t('Organism'),
  515. 'field types' => array('organism_id')
  516. ),
  517. 'chado_base__dbxref_id_formatter' => array(
  518. 'label' => t('Cross reference'),
  519. 'field types' => array('dbxref_id')
  520. ),
  521. 'chado_base__pub_id_formatter' => array(
  522. 'label' => t('Publications'),
  523. 'field types' => array('pub')
  524. ),
  525. 'chado_linker_dbxref_formatter' => array(
  526. 'label' => t('Cross references'),
  527. 'field types' => array('dbxref')
  528. ),
  529. 'chado_linker_cvterm_formatter' => array(
  530. 'label' => t('Annotations'),
  531. 'field types' => array('cvterm')
  532. ),
  533. 'chado_linker_prop_formatter' => array(
  534. 'label' => t('Property'),
  535. 'field types' => array('kvproperty')
  536. ),
  537. 'chado_linker_synonym_formatter' => array(
  538. 'label' => t('Synonyms'),
  539. 'field types' => array('synonym')
  540. ),
  541. 'chado_linker_cvterm_adder_formatter' => array(
  542. 'label' => t('Add an Annotation'),
  543. 'field types' => array('cvterm_class_adder')
  544. ),
  545. 'chado_linker_prop_adder_formatter' => array(
  546. 'label' => t('Add a Property'),
  547. 'field types' => array('kvproperty_adder')
  548. ),
  549. 'chado_feature__md5checksum_formatter' => array(
  550. 'label' => t('MD5 checksum'),
  551. 'field types' => array('md5checksum')
  552. ),
  553. 'chado_feature__residues_formatter' => array(
  554. 'label' => t('Residues'),
  555. 'field types' => array('residues')
  556. ),
  557. 'chado_feature__seqlen_formatter' => array(
  558. 'label' => t('Sequence length'),
  559. 'field types' => array('seqlen')
  560. ),
  561. );
  562. }
  563. /**
  564. * Returns a $field_info array for a field based on a database column.
  565. *
  566. */
  567. function tripal_chado_add_bundle_fields_base_fields_defaults($table_name, $schema, $column_name) {
  568. $details = $schema['fields'][$column_name];
  569. // Create an array with information about this field.
  570. $field = array(
  571. 'field_type' => '',
  572. 'widget_type' => '',
  573. 'description' => '',
  574. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  575. 'is_required' => 0,
  576. 'storage' => 'field_chado_storage',
  577. 'widget_settings' => array(
  578. 'display_label' => 1
  579. ),
  580. 'field_settings' => array(
  581. // The table in Chado where this field maps to.
  582. 'chado_table' => $table_name,
  583. // The column in the Chado table that this field maps to.
  584. 'chado_column' => $column_name,
  585. 'semantic_web' => array(
  586. // The type is the term from a vocabulary that desribes this field..
  587. 'type' => '',
  588. // The namepsace for the vocabulary (e.g. 'foaf').
  589. 'ns' => '',
  590. // The URL for the namespace. It must be that the type can be
  591. // appended to the URL.
  592. 'nsurl' => '',
  593. ),
  594. ),
  595. );
  596. // Alter the field info array depending on the column details.
  597. switch($details['type']) {
  598. case 'char':
  599. $field['field_type'] = 'text';
  600. $field['widget_type'] = 'text_textfield';
  601. $field['field_settings']['max_length'] = $details['length'];
  602. break;
  603. case 'varchar':
  604. $field['field_type'] = 'text';
  605. $field['widget_type'] = 'text_textfield';
  606. $field['field_settings']['max_length'] = $details['length'];
  607. break;
  608. case 'text':
  609. $field['field_type'] = 'text';
  610. $field['widget_type'] = 'text_textarea';
  611. $field['field_settings']['max_length'] = 17179869184;
  612. $field['field_settings']['text_processing'] = 1;
  613. $field['format'] = filter_default_format();
  614. break;
  615. case 'blob':
  616. // not sure how to support a blob field.
  617. continue;
  618. break;
  619. case 'int':
  620. $field['field_type'] = 'number_integer';
  621. $field['widget_type'] = 'number';
  622. break;
  623. case 'float':
  624. $field['field_type'] = 'number_float';
  625. $field['widget_type'] = 'number';
  626. $field['field_settings']['precision'] = 10;
  627. $field['field_settings']['scale'] = 2;
  628. $field['field_settings']['decimal_separator'] = '.';
  629. break;
  630. case 'numeric':
  631. $field['field_type'] = 'number_decimal';
  632. $field['widget_type'] = 'number';
  633. break;
  634. case 'serial':
  635. // Serial fields are most likely not needed as a field.
  636. break;
  637. case 'boolean':
  638. $field['field_type'] = 'list_boolean';
  639. $field['widget_type'] = 'options_onoff';
  640. $field['field_settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  641. break;
  642. case 'datetime':
  643. // Use the Drupal Date and Date API to create the field/widget
  644. $field['field_type'] = 'datetime';
  645. $field['widget_type'] = 'date_select';
  646. $field['widget_settings']['increment'] = 1;
  647. $field['widget_settings']['tz_handling'] = 'none';
  648. $field['widget_settings']['collapsible'] = TRUE;
  649. // TODO: Add settings so that the minutes increment by 1.
  650. // And turn off the timezone, as the Chado field doesn't support it.
  651. break;
  652. }
  653. // Set some default semantic web information
  654. if ($column_name == 'name') {
  655. $field['field_settings']['semantic_web']['type'] = 'name';
  656. $field['field_settings']['semantic_web']['ns'] = 'foaf';
  657. $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
  658. }
  659. if ($column_name == 'description' or $column_name == 'definition' or
  660. $column_name == 'comment') {
  661. $field['field_settings']['semantic_web']['type'] = 'description';
  662. $field['field_settings']['semantic_web']['ns'] = 'hydra';
  663. $field['field_settings']['semantic_web']['nsurl'] = 'http://www.w3.org/ns/hydra/core#';
  664. }
  665. //
  666. // GENERIC COLUMNS
  667. //
  668. if ($field['field_settings']['chado_column'] == 'organism_id') {
  669. $field['field_type'] = 'chado_base__organism_id';
  670. $field['widget_type'] = 'tripal_chado_organism_select_widget';
  671. $field['label'] = 'Organism';
  672. $field['description'] = 'Select an organism.';
  673. $field['field_settings']['semantic_web']['type'] = 'organism';
  674. $field['field_settings']['semantic_web']['ns'] = 'local';
  675. $field['field_settings']['semantic_web']['nsurl'] = '';
  676. }
  677. elseif ($field['field_settings']['chado_column'] =='dbxref_id') {
  678. $field['field_type'] = 'chado_base__dbxref_id';
  679. $field['widget_type'] = 'tripal_chado_primary_dbxref_widget';
  680. $field['label'] = 'Cross Reference';
  681. $field['description'] = 'This record can be cross referenced with a ' .
  682. 'record in another online database. The primary reference is for the ' .
  683. 'most prominent reference. At a minimum, the database and accession ' .
  684. 'must be provided. To remove a set reference, change the database ' .
  685. 'field to "Select a Database".';
  686. }
  687. elseif ($field['label'] == 'Timeaccessioned') {
  688. $field['label'] = 'Time Accessioned';
  689. $field['description'] = 'Please enter the time that this record was first added to the database.';
  690. }
  691. elseif ($field['label'] == 'Timelastmodified') {
  692. $field['label'] = 'Time Last Modified';
  693. $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  694. }
  695. //
  696. // ORGANISM TABLE
  697. //
  698. elseif ($field['field_settings']['chado_table'] == 'organism' and $field['field_settings']['chado_column'] == 'comment') {
  699. $field['label'] = 'Description';
  700. }
  701. //
  702. // FEATURE TABLE
  703. //
  704. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'uniquename') {
  705. $field['field_type'] = 'text';
  706. $field['widget_type'] = 'text_textfield';
  707. $field['field_settings']['text_processing'] = 0;
  708. $field['field_settings']['semantic_web']['type'] = 'name';
  709. $field['field_settings']['semantic_web']['ns'] = 'foaf';
  710. $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
  711. }
  712. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'md5checksum') {
  713. $field['field_type'] = 'chado_feature__md5checksum';
  714. $field['widget_type'] = 'chado_feature__md5checksum_widget';
  715. $field['label'] = 'MD5 Checksum';
  716. $field['description'] = 'Generate an MD5 checksum for the sequence.';
  717. }
  718. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'seqlen') {
  719. $field['field_type'] = 'chado_feature__seqlen';
  720. $field['widget_type'] = 'chado_feature__seqlen_widget';
  721. $field['label'] = 'Seqlen';
  722. $field['description'] = 'The length of the residues.';
  723. }
  724. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'residues') {
  725. $field['field_type'] = 'chado_feature__residues';
  726. $field['widget_type'] = 'chado_feature__residues_widget';
  727. $field['label'] = 'Residues';
  728. $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
  729. }
  730. //
  731. // ANALYSIS TABLE
  732. //
  733. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'program') {
  734. $field['field_settings']['semantic_web']['type'] = 'SoftwareApplication';
  735. $field['field_settings']['semantic_web']['ns'] = 'schema';
  736. $field['field_settings']['semantic_web']['nsurl'] = 'https://schema.org/';
  737. $field['description'] = 'The program name (e.g. blastx, blastp, sim4, genscan. If the analysis was not derived from a software package then provide a very brief description of the pipeline, workflow or method.';
  738. $field['label'] = 'Program, Pipeline, Workflow or Method Name.';
  739. }
  740. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceuri') {
  741. $field['field_type'] = 'text';
  742. $field['widget_type'] = 'text_textfield';
  743. $field['field_settings']['text_processing'] = 0;
  744. $field['label'] = 'Source URL';
  745. $field['description'] = 'The URL where the original source data was derived. Ideally, this should link to the page where more information about the source data can be found.';
  746. }
  747. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourcename') {
  748. $field['label'] = 'Source Name';
  749. $field['description'] = 'The name of the source data. This could be a file name, data set or a small description for how the data was collected. For long descriptions use the larger description field.';
  750. }
  751. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceversion') {
  752. $field['label'] = 'Source Version';
  753. $field['description'] = 'If hte source data set has a version include it here.';
  754. }
  755. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'algorithm') {
  756. $field['label'] = 'Source Version';
  757. $field['description'] = 'The name of the algorithm used to produce the dataset if different from the program.';
  758. }
  759. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'programversion') {
  760. $field['label'] = 'Program Version';
  761. $field['description'] = 'The version of the program used to perform this analysis. (e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter "n/a" if no version is available or applicable.';
  762. }
  763. //
  764. // PROJECT TABLE
  765. //
  766. elseif ($field['field_settings']['chado_table'] == 'project' and $field['field_settings']['chado_column'] == 'description') {
  767. $field['label'] = 'Short Description';
  768. }
  769. return $field;
  770. }
  771. /**
  772. * Implements hook_form_FORM_ID_alter().
  773. *
  774. * The field_ui_display_overview_form is used for formatting the display
  775. * or layout of fields attached to an entity and shown on the entity view page.
  776. *
  777. * This function removes the cvterm class and property adder field as those are
  778. * really not meant for users to show or manage.
  779. */
  780. function tripal_chado_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  781. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  782. // on the add/edit form of an entity for adding new property fields.
  783. $fields_names = element_children($form['fields']);
  784. foreach ($fields_names as $field_name) {
  785. $field_info = field_info_field($field_name);
  786. if ($field_info['type'] == 'kvproperty_adder') {
  787. unset($form['fields'][$field_name]);
  788. }
  789. if ($field_info['type'] == 'cvterm_class_adder') {
  790. unset($form['fields'][$field_name]);
  791. }
  792. }
  793. }
  794. /**
  795. * Implements hook_form_FORM_ID_alter().
  796. *
  797. * The field_ui_field_overview_form is used for ordering and configuring the
  798. * fields attached to an entity.
  799. *
  800. * This function removes the property adder field as that is really not meant
  801. * for users to show or manage.
  802. */
  803. function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  804. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  805. // on the add/edit form of an entity for adding new property fields.
  806. $fields_names = element_children($form['fields']);
  807. foreach ($fields_names as $field_name) {
  808. $field_info = field_info_field($field_name);
  809. if ($field_info['type'] == 'kvproperty_adder') {
  810. unset($form['fields'][$field_name]);
  811. }
  812. if ($field_info['type'] == 'cvterm_class_adder') {
  813. unset($form['fields'][$field_name]);
  814. }
  815. }
  816. }
  817. /**
  818. * Implements hook_field_is_empty().
  819. */
  820. function tripal_chado_field_is_empty($item, $field) {
  821. // If there is no value field then the field is empty.
  822. if (!array_key_exists('value', $item)) {
  823. return TRUE;
  824. }
  825. // Iterate through all of the fields and if at least one has a value
  826. // the field is not empty.
  827. foreach ($item as $form_field_name => $value) {
  828. if (isset($value) and $value != NULL and $value != '') {
  829. return FALSE;
  830. }
  831. }
  832. // Otherwise, the field is empty.
  833. return TRUE;
  834. }
  835. /**
  836. * Implements hook_add_bundle_fields().
  837. */
  838. function tripal_chado_add_bundle_fields($entity_type, $bundle, $term) {
  839. $bundle_name = $bundle->name;
  840. // This array will hold details that map the bundle to tables in Chado.
  841. $bundle_data = array();
  842. // Get the cvterm that corresponds to this TripalTerm object.
  843. $vocab = entity_load('TripalVocab', array($term->vocab_id));
  844. $vocab = reset($vocab);
  845. $match = array(
  846. 'dbxref_id' => array(
  847. 'db_id' => array(
  848. 'name' => $vocab->namespace,
  849. ),
  850. 'accession' => $term->accession
  851. ),
  852. );
  853. $cvterm = chado_generate_var('cvterm', $match);
  854. // The organism table does not have a type_id so we won't ever find
  855. // a record for it in the tripal_cv_defaults table.
  856. if ($cvterm->name == 'organism') {
  857. $bundle_data = array(
  858. 'cv_id' => $cvterm->cv_id->cv_id,
  859. 'cvterm_id' => $cvterm->cvterm_id,
  860. 'data_table' => 'organism',
  861. 'type_table' => 'organism',
  862. 'field' => '',
  863. );
  864. }
  865. // The analysis table does not have a type_id so we won't ever find
  866. // a record for it in the tripalcv_defaults table.
  867. else if ($cvterm->name == 'analysis') {
  868. $bundle_data = array(
  869. 'cv_id' => $cvterm->cv_id->cv_id,
  870. 'cvterm_id' => $cvterm->cvterm_id,
  871. 'data_table' => 'analysis',
  872. 'type_table' => 'analysis',
  873. 'field' => '',
  874. );
  875. }
  876. else if ($cvterm->name == 'project') {
  877. $bundle_data = array(
  878. 'cv_id' => $cvterm->cv_id->cv_id,
  879. 'cvterm_id' => $cvterm->cvterm_id,
  880. 'data_table' => 'project',
  881. 'type_table' => 'project',
  882. 'field' => '',
  883. );
  884. }
  885. else {
  886. // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
  887. // TABLES.
  888. // Look to see if this vocabulary is used as a default for any table.
  889. $default = db_select('tripal_cv_defaults', 't')
  890. ->fields('t')
  891. ->condition('cv_id', $cvterm->cv_id->cv_id)
  892. ->execute()
  893. ->fetchObject();
  894. if ($default) {
  895. $bundle_data = array(
  896. 'cv_id' => $cvterm->cv_id->cv_id,
  897. 'cvterm_id' => $cvterm->cvterm_id,
  898. 'data_table' => $default->table_name,
  899. 'type_table' => $default->table_name,
  900. 'field' => $default->field_name,
  901. );
  902. }
  903. }
  904. // Save the mapping information so that we can reuse it when we need to
  905. // look things up for later for an entity
  906. tripal_set_bundle_variable('chado_cvterm_id', $bundle->id, $bundle_data['cvterm_id']);
  907. tripal_set_bundle_variable('chado_table', $bundle->id, $bundle_data['data_table']);
  908. tripal_set_bundle_variable('chado_column', $bundle->id, $bundle_data['field']);
  909. //////////////////////////////////////////////////////////////////////////////
  910. // ADD FIELDS TO BUNDLE
  911. ////////////////////////////////////////////////////////////////////////////
  912. ////
  913. //
  914. // Base table fields.
  915. //
  916. // Adds the fields for the base table to the entity. Adds fields
  917. // for all columns including FK fields. Excludes primary key and the
  918. // type_id field (that's inherent in the bundle).
  919. tripal_chado_add_bundle_fields_base_fields($entity_type, $bundle_name, $bundle_data);
  920. ////
  921. //
  922. // Property table fields.
  923. //
  924. // Check to see if there are any property tables with FKs to this
  925. // base table. If so, add the fields for that type of table.
  926. $prop_table = $bundle_data['data_table'] . 'prop';
  927. if (chado_table_exists($prop_table)) {
  928. tripal_chado_add_bundle_fields_kvproperty_adder_field($entity_type, $bundle_name, $prop_table, $bundle_data['data_table']);
  929. }
  930. ////
  931. //
  932. // Dbxref table fields.
  933. //
  934. // Check to see if there are any dbxref tables with FKs to this
  935. // base table. If so, add the fields for that type of table.
  936. $dbxref_table = $bundle_data['data_table'] . '_dbxref';
  937. if (chado_table_exists($dbxref_table)) {
  938. tripal_chado_add_bundle_fields_dbxref_field($entity_type, $bundle_name, $dbxref_table, $bundle_data['data_table']);
  939. }
  940. ////
  941. //
  942. // Cvterm table fields.
  943. //
  944. // Check to see if there are any cvterm tables with FKs to this
  945. // base table. If so, add the fields for that type of table.
  946. $cvterm_table = $bundle_data['data_table'] . '_cvterm';
  947. if (chado_table_exists($cvterm_table)) {
  948. tripal_chado_add_bundle_fields_cvterm_class_adder_field($entity_type, $bundle_name, $cvterm_table, $bundle_data['data_table']);
  949. }
  950. ////
  951. //
  952. // Synonym table fields.
  953. //
  954. // Check to see if there are any synonym tables with FKs to this
  955. // base table. If so, add the fields for that type of table.
  956. $syn_table = $bundle_data['data_table'] . '_synonym';
  957. if (chado_table_exists($syn_table)) {
  958. tripal_chado_add_bundle_fields_synonym_field($entity_type, $bundle_name, $syn_table, $bundle_data['data_table']);
  959. }
  960. ////
  961. //
  962. // Pub table fields.
  963. //
  964. // Check to see if there are any pub tables with FKs to this
  965. // base table. If so, add the fields for that type of table.
  966. $pub_table = $bundle_data['data_table'] . '_pub';
  967. if (chado_table_exists($pub_table)) {
  968. tripal_chado_add_bundle_fields_pub_field($entity_type, $bundle_name, $pub_table, $bundle_data['data_table']);
  969. }
  970. }
  971. /**
  972. * Adds the fields for managing xrefs that are stored in a [base]_dbxref table.
  973. *
  974. * @param $entity_type
  975. * @param $bundle_name
  976. * @param $base_table
  977. * @param $dbxref_table
  978. */
  979. function tripal_chado_add_bundle_fields_dbxref_field($entity_type_name, $bundle_name, $dbxref_table, $base_table) {
  980. // We already have a dbxref_id field.
  981. $field_name = $dbxref_table;
  982. $schema = chado_get_schema($dbxref_table);
  983. $pkey = $schema['primary key'][0];
  984. // Initialize the field array.
  985. $field_info = array(
  986. 'field_type' => 'chado_linker_dbxref_adder',
  987. 'widget_type' => 'chado_linker_dbxref_adder_widget',
  988. 'widget_settings' => array('display_label' => 1),
  989. 'description' => '',
  990. 'label' => 'Cross References',
  991. 'is_required' => 0,
  992. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  993. 'storage' => 'field_chado_storage',
  994. 'field_settings' => array(
  995. // The Chado table that this field maps to.
  996. 'chado_table' => $dbxref_table,
  997. // The column in the chado table that this field maps to.
  998. 'chado_column' => $pkey,
  999. // The base table that this field is connected to.
  1000. 'base_table' => $base_table,
  1001. 'semantic_web' => array(
  1002. // The type is the term from a vocabulary that desribes this field..
  1003. 'type' => '',
  1004. // The namepsace for the vocabulary (e.g. 'foaf').
  1005. 'ns' => '',
  1006. // The URL for the namespace. It must be that the type can be
  1007. // appended to the URL.
  1008. 'nsurl' => '',
  1009. ),
  1010. ),
  1011. );
  1012. // If the base table has a 'dbxref_id' then change the label to
  1013. // indicate these are secondary cross references.
  1014. $schema = chado_get_schema($base_table);
  1015. if (array_key_exists('dbxref_id', $schema['fields'])) {
  1016. $field_info['label'] = 'Secondary Cross References';
  1017. }
  1018. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  1019. }
  1020. /**
  1021. * Adds the fields for managing xrefs that are stored in a [base]_dbxref table.
  1022. *
  1023. * @param $entity_type
  1024. * @param $bundle_name
  1025. * @param $base_table
  1026. * @param $dbxref_table
  1027. */
  1028. function tripal_chado_add_bundle_fields_synonym_field($entity_type_name, $bundle_name, $syn_table, $base_table) {
  1029. // We already have a dbxref_id field.
  1030. $field_name = $syn_table;
  1031. $schema = chado_get_schema($syn_table);
  1032. $pkey = $schema['primary key'][0];
  1033. // Initialize the field array.
  1034. $field_info = array(
  1035. 'field_type' => 'chado_linker_synonym',
  1036. 'widget_type' => 'chado_linker_synonym_widget',
  1037. 'widget_settings' => array('display_label' => 1),
  1038. 'description' => '',
  1039. 'label' => 'Synonyms',
  1040. 'is_required' => 0,
  1041. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1042. 'storage' => 'field_chado_storage',
  1043. 'field_settings' => array(
  1044. // The Chado table that this field maps to.
  1045. 'chado_table' => $syn_table,
  1046. // The column in the chado table that this field maps to.
  1047. 'chado_column' => $pkey,
  1048. // The base table that this field is connected to.
  1049. 'base_table' => $base_table,
  1050. 'semantic_web' => array(
  1051. // The type is the term from a vocabulary that desribes this field..
  1052. 'type' => '',
  1053. // The namepsace for the vocabulary (e.g. 'foaf').
  1054. 'ns' => '',
  1055. // The URL for the namespace. It must be that the type can be
  1056. // appended to the URL.
  1057. 'nsurl' => '',
  1058. ),
  1059. ),
  1060. );
  1061. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  1062. }
  1063. /**
  1064. * Adds the fields for managing xrefs that are stored in a [base]_dbxref table.
  1065. *
  1066. * @param $entity_type
  1067. * @param $bundle_name
  1068. * @param $base_table
  1069. * @param $dbxref_table
  1070. */
  1071. function tripal_chado_add_bundle_fields_pub_field($entity_type_name, $bundle_name, $pub_table, $base_table) {
  1072. // We already have a dbxref_id field.
  1073. $field_name = $pub_table;
  1074. $schema = chado_get_schema($pub_table);
  1075. $pkey = $schema['primary key'][0];
  1076. // Initialize the field array.
  1077. $field_info = array(
  1078. 'field_type' => 'chado_base__pub_id',
  1079. 'widget_type' => 'chado_base__pub_id_widget',
  1080. 'widget_settings' => array('display_label' => 1),
  1081. 'description' => '',
  1082. 'label' => 'Publications',
  1083. 'is_required' => 0,
  1084. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1085. 'storage' => 'field_chado_storage',
  1086. 'field_settings' => array(
  1087. // The Chado table that this field maps to.
  1088. 'chado_table' => $pub_table,
  1089. // The column in the chado table that this field maps to.
  1090. 'chado_column' => $pkey,
  1091. // The base table that this field is connected to.
  1092. 'base_table' => $base_table,
  1093. 'semantic_web' => array(
  1094. // The type is the term from a vocabulary that desribes this field..
  1095. 'type' => '',
  1096. // The namepsace for the vocabulary (e.g. 'foaf').
  1097. 'ns' => '',
  1098. // The URL for the namespace. It must be that the type can be
  1099. // appended to the URL.
  1100. 'nsurl' => '',
  1101. ),
  1102. ),
  1103. );
  1104. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  1105. }
  1106. /**
  1107. * Adds the fields for managing properties that are stored in a prop table.
  1108. *
  1109. * @param $entity_type_name
  1110. * @param $bundle_name
  1111. * @param $kv_table
  1112. */
  1113. function tripal_chado_add_bundle_fields_kvproperty_adder_field($entity_type_name, $bundle_name, $kv_table, $base_table) {
  1114. $field_name = $kv_table;
  1115. // Initialize the field array.
  1116. $field_info = array(
  1117. 'field_type' => 'chado_linker_prop_adder',
  1118. 'widget_type' => 'chado_linker_prop_adder_widget',
  1119. 'field_settings' => array(
  1120. 'base_table' => $base_table,
  1121. ),
  1122. 'storage' => 'field_chado_storage',
  1123. 'widget_settings' => array('display_label' => 1),
  1124. 'description' => '',
  1125. 'label' => 'Additional Properties',
  1126. 'is_required' => 0,
  1127. );
  1128. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  1129. }
  1130. /**
  1131. * Adds the fields for managing properties that are stored in a prop table.
  1132. *
  1133. * @param $entity_type_name
  1134. * @param $bundle_name
  1135. * @param $kv_table
  1136. */
  1137. function tripal_chado_add_bundle_fields_cvterm_class_adder_field($entity_type_name, $bundle_name, $cvterm_table, $base_table) {
  1138. // First add a generic property field so that users can add new property types.
  1139. $field_name = $cvterm_table;
  1140. // Initialize the field array.
  1141. $field_info = array(
  1142. 'field_type' => 'chado_linker_cvterm_adder',
  1143. 'widget_type' => 'chado_linker_cvterm_adder_widget',
  1144. 'field_settings' => array(
  1145. 'base_table' => $base_table,
  1146. ),
  1147. 'storage' => 'field_chado_storage',
  1148. 'widget_settings' => array('display_label' => 1),
  1149. 'description' => '',
  1150. 'label' => 'Additional Annotation Types',
  1151. 'is_required' => 0,
  1152. );
  1153. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  1154. }
  1155. /**
  1156. * Adds the fields for the base table to the entity.
  1157. */
  1158. function tripal_chado_add_bundle_fields_base_fields($entity_type_name, $bundle_name, $bundle_data) {
  1159. $table_name = $bundle_data['data_table'];
  1160. $type_table = $bundle_data['type_table'];
  1161. $type_field = $bundle_data['field'];
  1162. // Iterate through the columns of the table and see if fields have been
  1163. // created for each one. If not, then create them.
  1164. $schema = chado_get_schema($table_name);
  1165. $columns = $schema['fields'];
  1166. foreach ($columns as $column_name => $details) {
  1167. $field_name = $table_name . '__' . $column_name;
  1168. // Skip the primary key field.
  1169. if ($column_name == $schema['primary key'][0]) {
  1170. continue;
  1171. }
  1172. // Skip the type field.
  1173. if ($table_name == $type_table and $column_name == $type_field) {
  1174. continue;
  1175. }
  1176. // Get the field defaults for this column.
  1177. $field_info = tripal_chado_add_bundle_fields_base_fields_defaults($table_name, $schema, $column_name);
  1178. // TODO: add in a call to drupal_alter to allow other modules to change
  1179. // the field settings.
  1180. // Determine if the field is required.
  1181. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  1182. $field_info['is_required'] = array_key_exists('default', $details) ? 0 : 1;
  1183. }
  1184. // If we don't have a field type then we don't need to create a field.
  1185. if (!$field_info['field_type']) {
  1186. // If we don't have a field type but it is required and doesn't have
  1187. // a default value then we are in trouble.
  1188. if ($field_info['is_required'] and !array_key_exists('default', $details)) {
  1189. throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
  1190. array('%table' => $table_name, '%field' => $column_name, '%type' => $details['type'])));
  1191. }
  1192. continue;
  1193. }
  1194. // If this field is a foreign key field then we will have a custom field.
  1195. $is_fk = FALSE;
  1196. if (array_key_exists('foreign keys', $schema)) {
  1197. foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
  1198. if (array_key_exists($column_name, $fk_details['columns'])) {
  1199. $is_fk = TRUE;
  1200. }
  1201. }
  1202. }
  1203. // Add the field to the bundle.
  1204. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  1205. }
  1206. }