tripal_chado.fields.inc 48 KB

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