tripal_chado.module 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. <?php
  2. // APPLICATION PROGRAMMER INTERFACE -------------
  3. // Chado API
  4. require_once "api/tripal_chado.api.inc";
  5. require_once 'api/tripal_chado.property.api.inc';
  6. require_once 'api/tripal_chado.query.api.inc';
  7. require_once 'api/tripal_chado.variables.api.inc';
  8. require_once 'api/tripal_chado.schema.api.inc';
  9. require_once 'api/tripal_chado.custom_tables.api.inc';
  10. require_once 'api/tripal_chado.mviews.api.inc';
  11. require_once 'api/tripal_chado.schema_v1.2.api.inc';
  12. require_once 'api/tripal_chado.schema_v1.11.api.inc';
  13. require_once 'api/modules/tripal_chado.analysis.api.inc';
  14. require_once 'api/modules/tripal_chado.contact.api.inc';
  15. require_once 'api/modules/tripal_chado.cv.api.inc';
  16. require_once 'api/modules/tripal_chado.db.api.inc';
  17. require_once 'api/modules/tripal_chado.feature.api.inc';
  18. require_once 'api/modules/tripal_chado.organism.api.inc';
  19. require_once 'api/modules/tripal_chado.pub.api.inc';
  20. require_once 'api/modules/tripal_chado.stock.api.inc';
  21. require_once "includes/tripal_chado.entity.inc";
  22. require_once "includes/tripal_chado.schema.inc";
  23. require_once "includes/tripal_chado.term_storage.inc";
  24. require_once "includes/tripal_chado.field_storage.inc";
  25. tripal_chado_set_globals();
  26. /**
  27. * This function is used to set the global Chado variables
  28. *
  29. * @ingroup tripal_chado
  30. */
  31. function tripal_chado_set_globals() {
  32. // these global variables are meant to be accessed by all Tripal
  33. // modules to find the chado version installed and if Chado is local.
  34. // these variables are stored as globals rather than using the drupal_set_variable
  35. // functions because the Drupal functions make databaes queries and for long
  36. // running loaders we don't want those queries repeatedly.
  37. $GLOBALS["chado_is_installed"] = chado_is_installed();
  38. if ($GLOBALS["chado_is_installed"]) {
  39. $GLOBALS["chado_is_local"] = chado_is_local();
  40. $GLOBALS["chado_version"] = chado_get_version();
  41. $GLOBALS["exact_chado_version"] = chado_get_version(TRUE);
  42. }
  43. }
  44. /**
  45. * Implements hook_init().
  46. * Used to set the search_path, create default content and set default variables.
  47. *
  48. * @ingroup tripal_chado
  49. */
  50. function tripal_chado_init() {
  51. if ($GLOBALS["chado_is_installed"]) {
  52. // Check to see if the Chado and Drupal have been prepared
  53. if (!variable_get('tripal_chado_is_prepared', FALSE)) {
  54. drupal_set_message('Chado is installed but Tripal has not yet prepared Drupal and Chado. Please ' .
  55. l('prepare both Drupal and Chado', 'admin/tripal/storage/chado/chado_prepare') .
  56. ' before continuing.', 'warning');
  57. }
  58. }
  59. else {
  60. drupal_set_message('Tripal cannot find a Chado installation. Please ' .
  61. l('install Chado', 'admin/tripal/storage/chado/chado_install') .
  62. ' before continuing.', 'warning');
  63. }
  64. }
  65. /**
  66. * Implements hook_views_api().
  67. *
  68. * Essentially this hook tells drupal that there is views support for
  69. * for this module which then includes tripal_db.views.inc where all the
  70. * views integration code is
  71. *
  72. * @ingroup tripal_feature
  73. */
  74. function tripal_chado_views_api() {
  75. return array(
  76. 'api' => 3.0,
  77. );
  78. }
  79. /**
  80. * Implements hook_menu().
  81. */
  82. function tripal_chado_menu() {
  83. $items = array();
  84. $items['admin/tripal/storage/term/%/%'] = array(
  85. 'page callback' => 'tripal_autocomplete_cvterm',
  86. 'page arguments' => array(4, 5),
  87. 'access arguments' => array('administer content'),
  88. 'type' => MENU_CALLBACK,
  89. );
  90. //////////////////////////////////////////////////////////////////////////////
  91. // Chado Storage Backend
  92. //////////////////////////////////////////////////////////////////////////////
  93. $items['admin/tripal/storage/chado'] = array(
  94. 'title' => 'Chado',
  95. 'description' => t("Integrates Chado with Tripal and includes tools to
  96. load data, and extend the chado schema through custom tables &
  97. materialized views."),
  98. 'weight' => -100,
  99. 'access arguments' => array('administer tripal'),
  100. );
  101. $items['admin/tripal/storage/chado/chado_install'] = array(
  102. 'title' => 'Install Chado',
  103. 'description' => t('Installs the Chado database tables, views, etc., inside the current Drupal database'),
  104. 'page callback' => 'drupal_get_form',
  105. 'page arguments' => array('tripal_chado_install_form'),
  106. 'type' => MENU_NORMAL_ITEM,
  107. 'access arguments' => array('install chado'),
  108. 'file' => 'includes/tripal_chado.setup.inc',
  109. 'file path' => drupal_get_path('module', 'tripal_chado'),
  110. 'weight' => -100
  111. );
  112. $items['admin/tripal/storage/chado/chado_prepare'] = array(
  113. 'title' => 'Prepare Chado',
  114. 'description' => t('Prepares Drupal to use Chado.'),
  115. 'page callback' => 'drupal_get_form',
  116. 'page arguments' => array('tripal_chado_prepare_form'),
  117. 'type' => MENU_NORMAL_ITEM,
  118. 'access arguments' => array('install chado'),
  119. 'file' => 'includes/tripal_chado.setup.inc',
  120. 'file path' => drupal_get_path('module', 'tripal_chado'),
  121. 'weight' => -99
  122. );
  123. $items['admin/tripal/storage/chado/publish'] = array(
  124. 'title' => 'Publish',
  125. 'description' => t('Publish data that is present in Chado but which does
  126. not yet have a page on this site for viewing. In Tripal v2.0 or
  127. earlier this was refered to as "syncing".'),
  128. 'page callback' => 'drupal_get_form',
  129. 'page arguments' => array('tripal_chado_publish_form'),
  130. 'type' => MENU_NORMAL_ITEM,
  131. 'access arguments' => array('administer tripal'),
  132. 'file' => 'includes/tripal_chado.publish.inc',
  133. 'file path' => drupal_get_path('module', 'tripal_chado'),
  134. 'weight' => -99
  135. );
  136. //////////////////////////////////////////////////////////////////////////////
  137. // Materialized Views
  138. //////////////////////////////////////////////////////////////////////////////
  139. $items['admin/tripal/storage/chado/mviews'] = array(
  140. 'title' => 'Materialized Views',
  141. 'description' => t('Materialized views are used to improve speed of large or complex queries.'),
  142. 'page callback' => 'tripal_mview_admin_view',
  143. 'access arguments' => array('administer tripal'),
  144. 'type' => MENU_NORMAL_ITEM,
  145. 'file' => 'includes/tripal_chado.mviews.inc',
  146. 'file path' => drupal_get_path('module', 'tripal_chado'),
  147. 'weight' => -10
  148. );
  149. $items['admin/tripal/storage/chado/mviews/help'] = array(
  150. 'title' => 'Help',
  151. 'description' => t('Help for the materialized views management system'),
  152. 'page callback' => 'theme',
  153. 'page arguments' => array('tripal_mviews_help'),
  154. 'access arguments' => array('administer tripal'),
  155. 'type' => MENU_LOCAL_TASK,
  156. 'file' => 'includes/tripal_chado.mviews.inc',
  157. 'file path' => drupal_get_path('module', 'tripal_chado'),
  158. 'weight' => 10
  159. );
  160. $items['admin/tripal/storage/chado/mviews/report/%'] = array(
  161. 'title' => 'Materialized View',
  162. 'description' => t('Materialized views are used to improve speed of large or complex queries. These are database views as compared to Drupal views.'),
  163. 'page callback' => 'tripal_mview_report',
  164. 'page arguments' => array(6),
  165. 'access arguments' => array('administer tripal'),
  166. 'type' => MENU_CALLBACK,
  167. 'file' => 'includes/tripal_chado.mviews.inc',
  168. 'file path' => drupal_get_path('module', 'tripal_chado'),
  169. );
  170. $items['admin/tripal/storage/chado/mviews/new'] = array(
  171. 'title' => 'Create Materialized View',
  172. 'description' => t('Create a new materialized view.'),
  173. 'page callback' => 'drupal_get_form',
  174. 'page arguments' => array('tripal_mviews_form'),
  175. 'access arguments' => array('administer tripal'),
  176. 'type' => MENU_CALLBACK,
  177. 'file' => 'includes/tripal_chado.mviews.inc',
  178. 'file path' => drupal_get_path('module', 'tripal_chado'),
  179. );
  180. $items['admin/tripal/storage/chado/mviews/edit/%'] = array(
  181. 'title' => 'Edit Materialized View',
  182. 'page callback' => 'drupal_get_form',
  183. 'page arguments' => array('tripal_mviews_form', 6),
  184. 'access arguments' => array('administer tripal'),
  185. 'type' => MENU_CALLBACK,
  186. 'file' => 'includes/tripal_chado.mviews.inc',
  187. 'file path' => drupal_get_path('module', 'tripal_chado'),
  188. );
  189. $items['admin/tripal/storage/chado/mviews/update/%'] = array(
  190. 'title' => 'Create Materialized View',
  191. 'description' => t('Materialized views are used to improve speed of large or complex queries.'),
  192. 'page callback' => 'tripal_mviews_add_populate_job',
  193. 'page arguments' => array(6),
  194. 'access arguments' => array('administer tripal'),
  195. 'type' => MENU_CALLBACK,
  196. 'file' => 'includes/tripal_chado.mviews.inc',
  197. 'file path' => drupal_get_path('module', 'tripal_chado'),
  198. );
  199. $items['admin/tripal/storage/chado/mviews/delete/%'] = array(
  200. 'title' => 'Create Materialized View',
  201. 'description' => t('Materialized views are used to improve speed of large or complex queries.'),
  202. 'page callback' => 'drupal_get_form',
  203. 'page arguments' => array('tripal_mviews_delete_form', 5),
  204. 'access arguments' => array('administer tripal'),
  205. 'type' => MENU_CALLBACK,
  206. 'file' => 'includes/tripal_chado.mviews.inc',
  207. 'file path' => drupal_get_path('module', 'tripal_chado'),
  208. );
  209. // TODO: complete the code for exporting and importing of MViews.
  210. // Need to address security issues of sharing SQL.
  211. $items['admin/tripal/storage/chado/mviews/import'] = array(
  212. 'title' => 'Import MView',
  213. 'description' => 'Import a materialized view from another Tripal instance.',
  214. 'page callback' => 'drupal_get_form',
  215. 'page arguments' => array('tripal_mviews_import_form'),
  216. 'access arguments' => array('administer tripal'),
  217. 'type' => MENU_CALLBACK,
  218. 'file' => 'includes/tripal_chado.mviews.inc',
  219. 'file path' => drupal_get_path('module', 'tripal_chado'),
  220. );
  221. $items['admin/tripal/storage/chado/mviews/%tblid/export'] = array(
  222. 'title' => 'Export MView',
  223. 'description' => 'Export a materialized view for use by another Tripal instance.',
  224. 'page callback' => 'drupal_get_form',
  225. 'page arguments' => array('tripal_mviews_export_form', 5),
  226. 'access arguments' => array('administer tripal'),
  227. 'type' => MENU_CALLBACK,
  228. 'file' => 'includes/tripal_chado.mviews.inc',
  229. 'file path' => drupal_get_path('module', 'tripal_chado'),
  230. );
  231. //////////////////////////////////////////////////////////////////////////////
  232. // Custom Tables
  233. //////////////////////////////////////////////////////////////////////////////
  234. $items['admin/tripal/storage/chado/custom_tables'] = array(
  235. 'title' => 'Custom Tables',
  236. 'description' => t('Creation of custom tables that are added to Chado database.'),
  237. 'page callback' => 'tripal_custom_table_admin_view',
  238. 'access arguments' => array('administer tripal'),
  239. 'type' => MENU_NORMAL_ITEM,
  240. 'file' => 'includes/tripal_chado.custom_tables.inc',
  241. 'file path' => drupal_get_path('module', 'tripal_chado'),
  242. 'weight' => -10
  243. );
  244. $items['admin/tripal/storage/chado/custom_tables/help'] = array(
  245. 'title' => 'Help',
  246. 'description' => t('Help for the tripal job management system'),
  247. 'page callback' => 'theme',
  248. 'page arguments' => array('tripal_job_help'),
  249. 'access arguments' => array('administer tripal'),
  250. 'type' => MENU_LOCAL_TASK,
  251. 'file' => 'includes/tripal_chado.custom_tables.inc',
  252. 'file path' => drupal_get_path('module', 'tripal_chado'),
  253. 'weight' => 10
  254. );
  255. $items['admin/tripal/storage/chado/custom_tables/view/%'] = array(
  256. 'title' => 'Custom Tables',
  257. 'description' => t('Custom tables are added to Chado.'),
  258. 'page callback' => 'tripal_custom_table_view',
  259. 'page arguments' => array(6),
  260. 'access arguments' => array('administer tripal'),
  261. 'file' => 'includes/tripal_chado.custom_tables.inc',
  262. 'file path' => drupal_get_path('module', 'tripal_chado'),
  263. 'type' => MENU_CALLBACK,
  264. );
  265. $items['admin/tripal/storage/chado/custom_tables/new'] = array(
  266. 'title' => 'Create Custom Table',
  267. 'description' => t('An interface for creating your own custom tables.'),
  268. 'page callback' => 'tripal_custom_table_new_page',
  269. 'access arguments' => array('administer tripal'),
  270. 'file' => 'includes/tripal_chado.custom_tables.inc',
  271. 'file path' => drupal_get_path('module', 'tripal_chado'),
  272. 'type' => MENU_CALLBACK,
  273. );
  274. $items['admin/tripal/storage/chado/custom_tables/edit/%'] = array(
  275. 'title' => 'Edit Custom Table',
  276. 'page callback' => 'drupal_get_form',
  277. 'page arguments' => array('tripal_custom_tables_form', 6),
  278. 'access arguments' => array('administer tripal'),
  279. 'file' => 'includes/tripal_chado.custom_tables.inc',
  280. 'file path' => drupal_get_path('module', 'tripal_chado'),
  281. 'type' => MENU_CALLBACK,
  282. );
  283. $items['admin/tripal/storage/chado/custom_tables/delete/%'] = array(
  284. 'title' => 'Create Custom Table',
  285. 'description' => t('Custom tables are added to Chado.'),
  286. 'page callback' => 'drupal_get_form',
  287. 'page arguments' => array('tripal_custom_tables_delete_form', 6),
  288. 'access arguments' => array('administer tripal'),
  289. 'file' => 'includes/tripal_chado.custom_tables.inc',
  290. 'file path' => drupal_get_path('module', 'tripal_chado'),
  291. 'type' => MENU_CALLBACK,
  292. );
  293. $items['admin/tripal/storage/chado/custom_tables/views/tables/enable'] = array(
  294. 'title' => 'Enable Custom Tables Administrative View',
  295. 'page callback' => 'tripal_enable_view',
  296. 'page arguments' => array('tripal_admin_custom_table', 'admin/tripal/storage/chado/custom_tables'),
  297. 'access arguments' => array('administer tripal'),
  298. 'file' => 'includes/tripal_chado.custom_tables.inc',
  299. 'file path' => drupal_get_path('module', 'tripal_chado'),
  300. 'type' => MENU_CALLBACK,
  301. );
  302. //////////////////////////////////////////////////////////////////////////////
  303. // Data Loaders
  304. //////////////////////////////////////////////////////////////////////////////
  305. $items['admin/tripal/storage/chado/loaders'] = array(
  306. 'title' => 'Data Loaders',
  307. 'description' => t('Tools facilitating data import.'),
  308. 'access arguments' => array('administer tripal'),
  309. 'type' => MENU_NORMAL_ITEM,
  310. 'weight' => 6
  311. );
  312. $items['admin/tripal/storage/chado/loaders/fasta_loader'] = array(
  313. 'title' => 'FASTA file Loader',
  314. 'description' => 'Load sequences from a multi-FASTA file into Chado',
  315. 'page callback' => 'drupal_get_form',
  316. 'page arguments' => array('tripal_feature_fasta_load_form'),
  317. 'access arguments' => array('administer tripal feature'),
  318. 'file' => 'includes/loaders/tripal_chado.fasta_loader.inc',
  319. 'file path' => drupal_get_path('module', 'tripal_chado'),
  320. 'type' => MENU_NORMAL_ITEM,
  321. );
  322. $items['admin/tripal/storage/chado/loaders/gff3_load'] = array(
  323. 'title' => 'GFF3 file Loader',
  324. 'description' => 'Import a GFF3 file into Chado',
  325. 'page callback' => 'drupal_get_form',
  326. 'page arguments' => array('tripal_feature_gff3_load_form'),
  327. 'access arguments' => array('administer tripal feature'),
  328. 'file' => 'includes/loaders/tripal_chado.gff_loader.inc',
  329. 'file path' => drupal_get_path('module', 'tripal_chado'),
  330. 'type' => MENU_NORMAL_ITEM,
  331. );
  332. return $items;
  333. }
  334. /**
  335. * Implements hook_permission().
  336. *
  337. * Set the permission types that the chado module uses. Essentially we
  338. * want permissionis that protect creation, editing and deleting of chado
  339. * data objects
  340. *
  341. * @ingroup tripal
  342. */
  343. function tripal_chado_permission() {
  344. return array(
  345. 'install chado' => array(
  346. 'title' => t('Install Chado'),
  347. 'description' => t('Allow the user to install or upgrade a Chado database in the existing Drupal database.')
  348. ),
  349. 'view chado_ids' => array(
  350. 'title' => t('View Internal IDs'),
  351. 'description' => t('On content pages Tripal will typically provide
  352. a table of information pulled from the Chado database but the
  353. primary key IDs for that data is typically not shown. The
  354. default Tripal templates can show the primary key ID inside of a
  355. blue shaded table row if this permission is enabled. This can
  356. be useful for site developers who might want these IDs when working
  357. with the underlying database.'),
  358. 'restrict access' => TRUE,
  359. )
  360. );
  361. }
  362. /**
  363. * Implements hook_field_info().
  364. */
  365. function tripal_chado_field_info() {
  366. $fields = array(
  367. 'organism_id' => array(
  368. 'label' => t('Organism'),
  369. 'description' => t('A field for specifying an organism.'),
  370. 'default_widget' => 'tripal_chado_organism_select_widget',
  371. 'default_formatter' => 'tripal_chado_organism_formatter',
  372. 'settings' => array(),
  373. 'storage' => array(
  374. 'type' => 'field_chado_storage',
  375. 'module' => 'tripal_chado',
  376. 'active' => TRUE
  377. ),
  378. ),
  379. 'dbxref_id' => array(
  380. 'label' => t('Cross-reference'),
  381. 'description' => t('This record can be cross-referenced with a record in
  382. another online database. This field is intended for the most prominent
  383. reference. At a minimum, the database and accession must be provided.'),
  384. 'default_widget' => 'tripal_chado_dbxref_id_widget',
  385. 'default_formatter' => 'tripal_chado_dbxref_id_formatter',
  386. 'settings' => array(),
  387. 'storage' => array(
  388. 'type' => 'field_chado_storage',
  389. 'module' => 'tripal_chado',
  390. 'active' => TRUE
  391. ),
  392. ),
  393. 'residues' => array(
  394. 'label' => t('Residues'),
  395. 'description' => t('A field for managing nucleotide and protein residues.'),
  396. 'default_widget' => 'tripal_chado_residue_textarea_widget',
  397. 'default_formatter' => 'tripal_chado_residues_formatter',
  398. 'settings' => array(),
  399. 'storage' => array(
  400. 'type' => 'field_chado_storage',
  401. 'module' => 'tripal_chado',
  402. 'active' => TRUE
  403. ),
  404. ),
  405. 'md5checksum' => array(
  406. 'label' => t('MD5 checksum'),
  407. 'description' => t('A field for generating MD5 checksum for a sequence.'),
  408. 'default_widget' => 'tripal_chado_md5checksum_checkbox_widget',
  409. 'default_formatter' => 'tripal_chado_md5checksum_formatter',
  410. 'settings' => array(),
  411. 'storage' => array(
  412. 'type' => 'field_chado_storage',
  413. 'module' => 'tripal_chado',
  414. 'active' => TRUE
  415. ),
  416. ),
  417. 'seqlen' => array(
  418. 'label' => t('Sequence length'),
  419. 'description' => t('A field for calculating the length of a sequence.'),
  420. 'default_widget' => 'tripal_chado_seqlen_hidden_widget',
  421. 'default_formatter' => 'tripal_chado_seqlen_formatter',
  422. 'settings' => array(),
  423. 'storage' => array(
  424. 'type' => 'field_chado_storage',
  425. 'module' => 'tripal_chado',
  426. 'active' => TRUE
  427. ),
  428. ),
  429. // The field provides a widget for adding new properties
  430. // to an entity that is connected to a base table that has a prop table
  431. // in Chado.
  432. 'kvproperty_adder' => array(
  433. 'label' => t('Add a Property Type'),
  434. 'description' => t('This record may have any number of properties. Use
  435. this field to first add the type.'),
  436. 'default_widget' => 'tripal_chado_kvproperty_adder_widget',
  437. //'default_formatter' => 'tripal_chado_kvproperty_adder_formatter',
  438. 'default_formatter' => 'hidden',
  439. 'settings' => array(),
  440. 'storage' => array(
  441. 'type' => 'field_chado_storage',
  442. 'module' => 'tripal_chado',
  443. 'active' => TRUE
  444. ),
  445. ),
  446. // The field provides form elements for adding a property to an entity
  447. // that in turn gets stored in a prop table of Chado (e.g. featureprop,
  448. // stockprop, etc).
  449. 'kvproperty' => array(
  450. 'label' => t('Add a Property'),
  451. 'description' => t('Add details about this property.'),
  452. 'default_widget' => 'tripal_chado_kvproperty_widget',
  453. 'default_formatter' => 'tripal_chado_kvproperty_formatter',
  454. 'settings' => array(),
  455. 'storage' => array(
  456. 'type' => 'field_chado_storage',
  457. 'module' => 'tripal_chado',
  458. 'active' => TRUE
  459. ),
  460. ),
  461. );
  462. return $fields;
  463. }
  464. /**
  465. * Implements hook_field_widget_info().
  466. */
  467. function tripal_chado_field_widget_info() {
  468. return array(
  469. 'tripal_chado_organism_select_widget' => array(
  470. 'label' => t('Organism Select'),
  471. 'field types' => array('organism_id')
  472. ),
  473. 'tripal_chado_dbxref_id_widget' => array(
  474. 'label' => t('Cross-reference'),
  475. 'field types' => array('dbxref_id'),
  476. 'description' => t('This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference. At a minimum, the database and accession must be provided.'),
  477. ),
  478. 'tripal_chado_md5checksum_checkbox_widget' => array(
  479. 'label' => t('MD5 Checksum Checkbox'),
  480. 'field types' => array('md5checksum'),
  481. ),
  482. 'tripal_chado_residues_textarea_widget' => array(
  483. 'label' => t('Residues'),
  484. 'field types' => array('residues'),
  485. ),
  486. 'tripal_chado_seqlen_hidden_widget' => array(
  487. 'label' => t('Sequence Length'),
  488. 'field types' => array('seqlen'),
  489. ),
  490. 'tripal_chado_kvproperty_adder_widget' => array(
  491. 'label' => t('Add a Property'),
  492. 'field types' => array('kvproperty_adder'),
  493. ),
  494. 'tripal_chado_kvproperty_widget' => array(
  495. 'label' => t('Property'),
  496. 'field types' => array('kvproperty'),
  497. ),
  498. );
  499. }
  500. /**
  501. * Implements hook_field_formatter_info().
  502. */
  503. function tripal_chado_field_formatter_info() {
  504. return array(
  505. 'tripal_chado_organism_formatter' => array(
  506. 'label' => t('Organism'),
  507. 'field types' => array('organism_id')
  508. ),
  509. 'tripal_chado_dbxref_id_formatter' => array(
  510. 'label' => t('Cross-reference'),
  511. 'field types' => array('dbxref_id')
  512. ),
  513. 'tripal_chado_md5checksum_formatter' => array(
  514. 'label' => t('MD5 checksum'),
  515. 'field types' => array('md5checksum')
  516. ),
  517. 'tripal_chado_residues_formatter' => array(
  518. 'label' => t('Residues'),
  519. 'field types' => array('residues')
  520. ),
  521. 'tripal_chado_seqlen_formatter' => array(
  522. 'label' => t('Sequence length'),
  523. 'field types' => array('seqlen')
  524. ),
  525. 'tripal_chado_kvproperty_adder_formatter' => array(
  526. 'label' => t('Add a Property'),
  527. 'field types' => array('kvproperty_adder')
  528. ),
  529. 'tripal_chado_kvproperty_formatter' => array(
  530. 'label' => t('Property'),
  531. 'field types' => array('kvproperty')
  532. ),
  533. );
  534. }
  535. /**
  536. * Implements hook_chado_field_alter().
  537. *
  538. * This function is used to change the default field formatter and widget
  539. * that are assigned to fields of an Entity. This hook is only used for
  540. * those fields that correspond to a column in a Chado table. An implementation
  541. * of this hook can be used to change the default formatters and widgets to
  542. * custom formatters and widgets that are created by the module creating
  543. * this hook.
  544. *
  545. * By default, Tripal will provide custom formatters and widgets for many
  546. * columns in Chado tables, therefore, this hook will most likely be of use
  547. * to extension modules that create custom table inside of Chado.
  548. *
  549. * @param $field
  550. */
  551. function hook_chado_field_alter(&$field) {
  552. if (!array_key_exists('field_settings', $field)) {
  553. return;
  554. }
  555. // If the field doesn't list the Chado table or column then just return.
  556. if (!array_key_exists('chado_table', $field['field_settings']) or
  557. !array_key_exists('chado_column', $field['field_settings'])) {
  558. return;
  559. }
  560. // Here we provide new field types and widgets for FK fields
  561. // and fields that need special attention.
  562. }
  563. /**
  564. * Implements hook_field_widget_form_alter().
  565. */
  566. function tripal_chado_field_widget_form_alter(&$element, &$form_state, $context) {
  567. if (array_key_exists('#field_name', $element)) {
  568. $field_name = $element['#field_name'];
  569. $matches = array();
  570. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  571. $tablename = $matches[1];
  572. $colname = $matches[2];
  573. $schema = chado_get_schema($tablename);
  574. // The timelastmodified field exists in many Chado tables. We want
  575. // the form element to update to the most recent time rather than the time
  576. // in the database.
  577. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  578. // We want the default value for the field to be the current time.
  579. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  580. $element['#date_items']['value'] = $element['#default_value']['value'];
  581. }
  582. // We want the date combo fieldset to be collaspible so we will
  583. // add our own theme_wrapper to replace the one added by the date
  584. // module.
  585. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  586. $element['#theme_wrappers'] = array('tripal_chado_date_combo');
  587. }
  588. }
  589. }
  590. }
  591. /**
  592. * Implements hook_field_formatter_view().
  593. */
  594. function tripal_chado_field_formatter_view($entity_type, $entity, $field,
  595. $instance, $langcode, $items, $display) {
  596. $element = array();
  597. switch ($display['type']) {
  598. case 'tripal_chado_organism_formatter':
  599. module_load_include('inc', 'tripal_chado', 'includes/fields/organism_id');
  600. tripal_chado_organism_select_formatter($element, $entity_type, $entity, $field,
  601. $instance, $langcode, $items, $display);
  602. break;
  603. case 'tripal_chado_dbxref_id_formatter':
  604. module_load_include('inc', 'tripal_chado', 'includes/fields/dbxref_id');
  605. tripal_chado_dbxref_id_formatter($element, $entity_type, $entity, $field,
  606. $instance, $langcode, $items, $display);
  607. break;
  608. case 'tripal_chado_md5checksum_formatter':
  609. module_load_include('inc', 'tripal_chado', 'includes/fields/md5checksum');
  610. tripal_chado_md5checksum_checkbox_formatter($element, $entity_type, $entity, $field,
  611. $instance, $langcode, $items, $display);
  612. break;
  613. case 'tripal_chado_residues_formatter':
  614. module_load_include('inc', 'tripal_chado', 'includes/fields/residues');
  615. tripal_chado_residues_textarea_formatter($element, $entity_type, $entity, $field,
  616. $instance, $langcode, $items, $display);
  617. break;
  618. case 'tripal_chado_seqlen_formatter':
  619. module_load_include('inc', 'tripal_chado', 'includes/fields/seqlen');
  620. tripal_chado_seqlen_hidden_formatter($element, $entity_type, $entity, $field,
  621. $instance, $langcode, $items, $display);
  622. break;
  623. case 'tripal_chado_kvproperty_adder_formatter':
  624. module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty_adder');
  625. tripal_chado_kvproperty_adder_formatter($element, $entity_type, $entity, $field,
  626. $instance, $langcode, $items, $display);
  627. break;
  628. case 'tripal_chado_kvproperty_formatter':
  629. module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty');
  630. tripal_chado_kvproperty_formatter($element, $entity_type, $entity, $field,
  631. $instance, $langcode, $items, $display);
  632. break;
  633. }
  634. return $element;
  635. }
  636. /**
  637. * Implements hook_field_widget_form().
  638. */
  639. function tripal_chado_field_widget_form(&$form, &$form_state, $field,
  640. $instance, $langcode, $items, $delta, $element) {
  641. $widget = $element;
  642. switch ($instance['widget']['type']) {
  643. case 'tripal_chado_organism_select_widget':
  644. // Make sure the include files get parsed now and for the form submits.
  645. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/organism_id');
  646. module_load_include('inc', 'tripal_chado', 'includes/fields/organism_id');
  647. // Update the widget with the new field.
  648. tripal_chado_organism_select_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  649. break;
  650. case 'tripal_chado_dbxref_id_widget':
  651. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/dbxref_id');
  652. module_load_include('inc', 'tripal_chado', 'includes/fields/dbxref_id');
  653. tripal_chado_dbxref_id_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  654. break;
  655. case 'tripal_chado_md5checksum_checkbox_widget':
  656. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/md5checksum');
  657. module_load_include('inc', 'tripal_chado', 'includes/fields/md5checksum');
  658. tripal_chado_md5checksum_checkbox_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  659. break;
  660. case 'tripal_chado_residues_textarea_widget':
  661. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/residues');
  662. module_load_include('inc', 'tripal_chado', 'includes/fields/residues');
  663. tripal_chado_residues_textarea_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  664. break;
  665. case 'tripal_chado_seqlen_hidden_widget':
  666. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/seqlen');
  667. module_load_include('inc', 'tripal_chado', 'includes/fields/seqlen');
  668. tripal_chado_seqlen_hidden_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  669. break;
  670. case 'tripal_chado_kvproperty_adder_widget':
  671. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/kvproperty_adder');
  672. module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty_adder');
  673. tripal_chado_kvproperty_adder_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  674. break;
  675. case 'tripal_chado_kvproperty_widget':
  676. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/kvproperty');
  677. module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty');
  678. tripal_chado_kvproperty_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  679. break;
  680. }
  681. return $widget;
  682. }
  683. /**
  684. * Implements hook_form_FORM_ID_alter().
  685. *
  686. * The field_ui_display_overview_form is used for formatting the display
  687. * or layout of fields attached to an entity and shown on the entity view page.
  688. */
  689. function tripal_chado_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  690. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  691. // on the add/edit form of an entity for adding new property fields.
  692. $fields_names = element_children($form['fields']);
  693. foreach ($fields_names as $field_name) {
  694. $field_info = field_info_field($field_name);
  695. if ($field_info['type'] == 'kvproperty_adder') {
  696. unset($form['fields'][$field_name]);
  697. }
  698. }
  699. }
  700. /**
  701. * Implements hook_form_FORM_ID_alter().
  702. *
  703. * The field_ui_field_overview_form is used for ordering and configuring the
  704. * fields attached to an entity.
  705. */
  706. function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  707. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  708. // on the add/edit form of an entity for adding new property fields.
  709. $fields_names = element_children($form['fields']);
  710. foreach ($fields_names as $field_name) {
  711. $field_info = field_info_field($field_name);
  712. if ($field_info['type'] == 'kvproperty_adder') {
  713. unset($form['fields'][$field_name]);
  714. }
  715. }
  716. }
  717. /**
  718. * Implements hook_field_is_empty().
  719. */
  720. function tripal_chado_field_is_empty($item, $field) {
  721. // If there is no value field then the field is empty.
  722. if (!array_key_exists('value', $item)) {
  723. return TRUE;
  724. }
  725. // Iterate through all of the fields and if at least one has a value
  726. // the field is not empty.
  727. foreach ($item as $form_field_name => $value) {
  728. if (isset($value) and $value != NULL and $value != '') {
  729. return FALSE;
  730. }
  731. }
  732. // Otherwise, the field is empty.
  733. return TRUE;
  734. }
  735. /**
  736. * Returns the values of the field from the $form_state.
  737. */
  738. function tripal_chado_get_field_form_values($field_name, $form_state, $delta = 0, $child = NULL) {
  739. $value = NULL;
  740. // The form_state must have the 'values' key. If not then just return.
  741. if (!array_key_exists('values', $form_state)) {
  742. return $value;
  743. }
  744. // If the field name is not in the form_state['values'] then return.
  745. if (!array_key_exists($field_name, $form_state['values'])) {
  746. return $value;
  747. }
  748. // Iterate through the values looking for the field_name provided.
  749. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  750. $item = $items[$delta];
  751. if ($child){
  752. if(array_key_exists($child, $item) and $item[$child] != '') {
  753. $value = $item[$child];
  754. }
  755. }
  756. else {
  757. $value = $item['value'];
  758. }
  759. }
  760. return $value;
  761. }
  762. /**
  763. * Sets the values of the field from the $form_state.
  764. */
  765. function tripal_chado_set_field_form_values($field_name, &$form_state, $newvalue, $delta = 0, $child = NULL) {
  766. // The form_state must have the 'values' key. If not then just return.
  767. if (!array_key_exists('values', $form_state)) {
  768. return FALSE;
  769. }
  770. // If the field name is not in the form_state['values'] then reutrn.
  771. if (!array_key_exists($field_name, $form_state['values'])) {
  772. return FALSE;
  773. }
  774. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  775. if ($child) {
  776. $form_state['values'][$field_name][$langcode][$delta][$child] = $newvalue;
  777. }
  778. else {
  779. $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
  780. }
  781. }
  782. return TRUE;
  783. }
  784. /**
  785. * Implements hook_theme().
  786. */
  787. function tripal_chado_theme($existing, $type, $theme, $path) {
  788. return array(
  789. 'tripal_chado_dbxref_id_widget' => array(
  790. 'render element' => 'element',
  791. 'file' => 'includes/fields/dbxref_id.inc',
  792. ),
  793. 'tripal_chado_kvproperty_addr_widget' => array(
  794. 'render element' => 'element',
  795. 'file' => 'includes/fields/dbxref_id.inc',
  796. ),
  797. 'tripal_chado_date_combo' => array(
  798. 'render element' => 'element',
  799. 'file' => 'theme/tripal_chado.theme.inc',
  800. ),
  801. );
  802. }
  803. /**
  804. * Implements hook_add_bundle_fields().
  805. */
  806. function tripal_chado_add_bundle_fields($entity_type, $bundle, $term) {
  807. $bundle_name = $bundle->name;
  808. // This array will hold details that map the bundle to tables in Chado.
  809. $bundle_data = array();
  810. // Get the cvterm that corresponds to this TripalTerm object.
  811. $vocab = entity_load('TripalVocab', array($term->vocab_id));
  812. $vocab = reset($vocab);
  813. $match = array(
  814. 'dbxref_id' => array(
  815. 'db_id' => array(
  816. 'name' => $vocab->namespace,
  817. ),
  818. 'accession' => $term->accession
  819. ),
  820. );
  821. $cvterm = chado_generate_var('cvterm', $match);
  822. // The organism table does not have a type_id so we won't ever find
  823. // a record for it in the tripal_cv_defaults table.
  824. if ($cvterm->name == 'organism') {
  825. $bundle_data = array(
  826. 'cv_id' => $cvterm->cv_id->cv_id,
  827. 'cvterm_id' => $cvterm->cvterm_id,
  828. 'data_table' => 'organism',
  829. 'type_table' => 'organism',
  830. 'field' => '',
  831. );
  832. }
  833. // The analysis table does not have a type_id so we won't ever find
  834. // a record for it in the tripalcv_defaults table.
  835. else if ($cvterm->name == 'analysis') {
  836. $bundle_data = array(
  837. 'cv_id' => $cvterm->cv_id->cv_id,
  838. 'cvterm_id' => $cvterm->cvterm_id,
  839. 'data_table' => 'analysis',
  840. 'type_table' => 'analysis',
  841. 'field' => '',
  842. );
  843. }
  844. else if ($cvterm->name == 'project') {
  845. $bundle_data = array(
  846. 'cv_id' => $cvterm->cv_id->cv_id,
  847. 'cvterm_id' => $cvterm->cvterm_id,
  848. 'data_table' => 'project',
  849. 'type_table' => 'project',
  850. 'field' => '',
  851. );
  852. }
  853. else {
  854. // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
  855. // TABLES.
  856. // Look to see if this vocabulary is used as a default for any table.
  857. $default = db_select('tripal_cv_defaults', 't')
  858. ->fields('t')
  859. ->condition('cv_id', $cvterm->cv_id->cv_id)
  860. ->execute()
  861. ->fetchObject();
  862. if ($default) {
  863. $bundle_data = array(
  864. 'cv_id' => $cvterm->cv_id->cv_id,
  865. 'cvterm_id' => $cvterm->cvterm_id,
  866. 'data_table' => $default->table_name,
  867. 'type_table' => $default->table_name,
  868. 'field' => $default->field_name,
  869. );
  870. }
  871. }
  872. // Adds the fields for the base table to the entity.
  873. tripal_chado_add_bundle_base_fields($entity_type, $bundle_name, $bundle_data);
  874. // Save the mapping information so that we can reuse it when we need to
  875. // look things up for later for an entity
  876. tripal_set_bundle_variable('chado_cvterm_id', $bundle->id, $bundle_data['cvterm_id']);
  877. tripal_set_bundle_variable('chado_table', $bundle->id, $bundle_data['data_table']);
  878. tripal_set_bundle_variable('chado_column', $bundle->id, $bundle_data['field']);
  879. // Check to see if there are any kv-property tables associated to this
  880. // base table. If so, add the fields for that type of table.
  881. $proptable = $bundle_data['data_table'] . 'prop';
  882. if (chado_table_exists($proptable)) {
  883. tripal_chado_add_bundle_kvproperty_adder_field($entity_type, $bundle_name, $proptable);
  884. }
  885. }
  886. /**
  887. * Adds the fields for a kv-property table fields
  888. *
  889. * @param $entity_type_name
  890. * @param $bundle_name
  891. * @param $kv_table
  892. */
  893. function tripal_chado_add_bundle_kvproperty_adder_field($entity_type_name, $bundle_name, $kv_table) {
  894. // First add a generic property field so that users can add new proeprty types.
  895. $field_name = $kv_table;
  896. // Initialize the field array.
  897. $field_info = array(
  898. 'field_type' => 'kvproperty_adder',
  899. 'widget_type' => 'tripal_fields_kvproperty_adder_widget',
  900. 'field_settings' => array(),
  901. 'widget_settings' => array('display_label' => 1),
  902. 'description' => '',
  903. 'label' => 'Additional Properties',
  904. 'is_required' => 0,
  905. );
  906. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  907. }
  908. /**
  909. * Adds the fields for the base table to the entity.
  910. */
  911. function tripal_chado_add_bundle_base_fields($entity_type_name, $bundle_name, $bundle_data) {
  912. $table_name = $bundle_data['data_table'];
  913. $type_table = $bundle_data['type_table'];
  914. $type_field = $bundle_data['field'];
  915. // Iterate through the columns of the table and see if fields have been
  916. // created for each one. If not, then create them.
  917. $schema = chado_get_schema($table_name);
  918. $columns = $schema['fields'];
  919. foreach ($columns as $column_name => $details) {
  920. $field_name = $table_name . '__' . $column_name;
  921. // Skip the primary key field.
  922. if ($column_name == $schema['primary key'][0]) {
  923. continue;
  924. }
  925. // Skip the type field.
  926. if ($table_name == $type_table and $column_name == $type_field) {
  927. continue;
  928. }
  929. // Get the field defaults for this column.
  930. $field_info = tripal_chado_get_table_column_field_default($table_name, $schema, $column_name);
  931. // Determine if the field is required.
  932. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  933. $field_info['is_required'] = array_key_exists('default', $details) ? 0 : 1;
  934. }
  935. // If we don't have a field type then we don't need to create a field.
  936. if (!$field_info['field_type']) {
  937. // If we don't have a field type but it is required and doesn't have
  938. // a default value then we are in trouble.
  939. if ($field_info['is_required'] and !array_key_exists('default', $details)) {
  940. throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
  941. array('%table' => $table_name, '%field' => $column_name, '%type' => $details['type'])));
  942. }
  943. continue;
  944. }
  945. // If this field is a foreign key field then we will have a special custom
  946. // field provided by Tripal.
  947. $is_fk = FALSE;
  948. if (array_key_exists('foreign keys', $schema)) {
  949. foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
  950. if (array_key_exists($column_name, $fk_details['columns'])) {
  951. $is_fk = TRUE;
  952. }
  953. }
  954. }
  955. // Add the field to the bundle.
  956. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  957. }
  958. }
  959. /**
  960. * Returns a $field_info array for a field based on a database column.
  961. *
  962. */
  963. function tripal_chado_get_table_column_field_default($table_name, $schema, $column_name) {
  964. $details = $schema['fields'][$column_name];
  965. // Create an array with information about this field.
  966. $field = array(
  967. 'field_type' => '',
  968. 'widget_type' => '',
  969. 'field_settings' => array(
  970. 'chado_table' => $table_name,
  971. 'chado_column' => $column_name,
  972. 'semantic_web' => array(
  973. // The type is the term from a vocabulary that desribes this field..
  974. 'type' => '',
  975. // The namepsace for the vocabulary (e.g. 'foaf').
  976. 'ns' => '',
  977. // The URL for the namespace. It must be that the type can be
  978. // appended to the URL.
  979. 'nsurl' => '',
  980. ),
  981. ),
  982. 'widget_settings' => array(
  983. 'display_label' => 1
  984. ),
  985. 'description' => '',
  986. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  987. 'is_required' => 0,
  988. );
  989. // Alter the field info array depending on the column details.
  990. switch($details['type']) {
  991. case 'char':
  992. $field['field_type'] = 'text';
  993. $field['widget_type'] = 'text_textfield';
  994. $field['field_settings']['max_length'] = $details['length'];
  995. break;
  996. case 'varchar':
  997. $field['field_type'] = 'text';
  998. $field['widget_type'] = 'text_textfield';
  999. $field['field_settings']['max_length'] = $details['length'];
  1000. break;
  1001. case 'text':
  1002. $field['field_type'] = 'text';
  1003. $field['widget_type'] = 'text_textarea';
  1004. $field['field_settings']['max_length'] = 17179869184;
  1005. $field['field_settings']['text_processing'] = 1;
  1006. $field['format'] = filter_default_format();
  1007. break;
  1008. case 'blob':
  1009. // not sure how to support a blob field.
  1010. continue;
  1011. break;
  1012. case 'int':
  1013. $field['field_type'] = 'number_integer';
  1014. $field['widget_type'] = 'number';
  1015. break;
  1016. case 'float':
  1017. $field['field_type'] = 'number_float';
  1018. $field['widget_type'] = 'number';
  1019. $field['field_settings']['precision'] = 10;
  1020. $field['field_settings']['scale'] = 2;
  1021. $field['field_settings']['decimal_separator'] = '.';
  1022. break;
  1023. case 'numeric':
  1024. $field['field_type'] = 'number_decimal';
  1025. $field['widget_type'] = 'number';
  1026. break;
  1027. case 'serial':
  1028. // Serial fields are most likely not needed as a field.
  1029. break;
  1030. case 'boolean':
  1031. $field['field_type'] = 'list_boolean';
  1032. $field['widget_type'] = 'options_onoff';
  1033. $field['field_settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  1034. break;
  1035. case 'datetime':
  1036. // Use the Drupal Date and Date API to create the field/widget
  1037. $field['field_type'] = 'datetime';
  1038. $field['widget_type'] = 'date_select';
  1039. $field['widget_settings']['increment'] = 1;
  1040. $field['widget_settings']['tz_handling'] = 'none';
  1041. $field['widget_settings']['collapsible'] = TRUE;
  1042. // TODO: Add settings so that the minutes increment by 1.
  1043. // And turn off the timezone, as the Chado field doesn't support it.
  1044. break;
  1045. }
  1046. // Set some default semantic web information
  1047. if ($column_name == 'name') {
  1048. $field['field_settings']['semantic_web']['type'] = 'name';
  1049. $field['field_settings']['semantic_web']['ns'] = 'foaf';
  1050. $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
  1051. }
  1052. if ($column_name == 'description' or $column_name == 'definition' or
  1053. $column_name == 'comment') {
  1054. $field['field_settings']['semantic_web']['type'] = 'description';
  1055. $field['field_settings']['semantic_web']['ns'] = 'hydra';
  1056. $field['field_settings']['semantic_web']['nsurl'] = 'http://www.w3.org/ns/hydra/core#';
  1057. }
  1058. //
  1059. // GENERIC COLUMNS
  1060. //
  1061. if ($field['field_settings']['chado_column'] =='organism_id') {
  1062. $field['field_type'] = 'organism_id';
  1063. $field['widget_type'] = 'tripal_chado_organism_select_widget';
  1064. $field['label'] = 'Organism';
  1065. $field['description'] = 'Select an organism.';
  1066. }
  1067. elseif ($field['field_settings']['chado_column'] =='dbxref_id') {
  1068. $field['field_type'] = 'dbxref_id';
  1069. $field['widget_type'] = 'tripal_chado_primary_dbxref_widget';
  1070. $field['label'] = 'Primary Cross Reference';;
  1071. $field['description'] = 'This record can be cross-referenced with a ' .
  1072. 'record in another online database. The primary reference is for the ' .
  1073. 'most prominent reference. At a minimum, the database and accession ' .
  1074. 'must be provided. To remove a set reference, change the database ' .
  1075. 'field to "Select a Database".';
  1076. }
  1077. elseif ($field['label'] == 'Timeaccessioned') {
  1078. $field['label'] = 'Time Accessioned';
  1079. $field['description'] = 'Please enter the time that this record was first added to the database.';
  1080. }
  1081. elseif ($field['label'] == 'Timelastmodified') {
  1082. $field['label'] = 'Time Last Modified';
  1083. $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  1084. }
  1085. //
  1086. // ORGANISM TABLE
  1087. //
  1088. elseif ($field['field_settings']['chado_table'] == 'organism' and $field['field_settings']['chado_column'] == 'comment') {
  1089. $field['label'] = 'Description';
  1090. }
  1091. //
  1092. // FEATURE TABLE
  1093. //
  1094. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'uniquename') {
  1095. $field['field_type'] = 'text';
  1096. $field['widget_type'] = 'text_textfield';
  1097. $field['field_settings']['text_processing'] = 0;
  1098. $field['field_settings']['semantic_web']['type'] = 'name';
  1099. $field['field_settings']['semantic_web']['ns'] = 'foaf';
  1100. $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
  1101. }
  1102. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'md5checksum') {
  1103. $field['field_type'] = 'md5checksum';
  1104. $field['widget_type'] = 'tripal_chado_md5checksum_checkbox_widget';
  1105. $field['label'] = 'MD5 Checksum';
  1106. $field['description'] = 'Generating MD5 checksum for the sequence.';
  1107. }
  1108. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'seqlen') {
  1109. $field['field_type'] = 'seqlen';
  1110. $field['widget_type'] = 'tripal_chado_seqlen_hidden_widget';
  1111. $field['label'] = 'Seqlen';
  1112. $field['description'] = 'The length of the residues.';
  1113. }
  1114. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'residues') {
  1115. $field['field_type'] = 'residues';
  1116. $field['widget_type'] = 'tripal_chado_residues_textarea_widget';
  1117. $field['label'] = 'Residues';
  1118. $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
  1119. }
  1120. //
  1121. // ANALYSIS TABLE
  1122. //
  1123. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'program') {
  1124. $field['field_settings']['semantic_web']['type'] = 'SoftwareApplication';
  1125. $field['field_settings']['semantic_web']['ns'] = 'schema';
  1126. $field['field_settings']['semantic_web']['nsurl'] = 'https://schema.org/';
  1127. $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.';
  1128. $field['label'] = 'Program, Pipeline, Workflow or Method Name.';
  1129. }
  1130. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceuri') {
  1131. $field['field_type'] = 'text';
  1132. $field['widget_type'] = 'text_textfield';
  1133. $field['field_settings']['text_processing'] = 0;
  1134. $field['label'] = 'Source URL';
  1135. $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.';
  1136. }
  1137. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourcename') {
  1138. $field['label'] = 'Source Name';
  1139. $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.';
  1140. }
  1141. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceversion') {
  1142. $field['label'] = 'Source Version';
  1143. $field['description'] = 'If hte source data set has a version include it here.';
  1144. }
  1145. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'algorithm') {
  1146. $field['label'] = 'Source Version';
  1147. $field['description'] = 'The name of the algorithm used to produce the dataset if different from the program.';
  1148. }
  1149. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'programversion') {
  1150. $field['label'] = 'Program Version';
  1151. $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.';
  1152. }
  1153. return $field;
  1154. }
  1155. /**
  1156. * Implements hook_exclude_type_by_default()
  1157. *
  1158. * This hooks allows fields of a specified type that match a specified criteria
  1159. * to be excluded by default from any table when chado_generate_var() is called.
  1160. * Keep in mind that if fields are excluded by default they can always be
  1161. * expanded at a later date using chado_expand_var().
  1162. *
  1163. * Criteria are php strings that evaluate to either TRUE or FALSE. These
  1164. * strings are evaluated using drupal_eval() which suppresses syntax errors and
  1165. * throws watchdog entries of type php. There are also watchdog entries of type
  1166. * tripal stating the exact criteria evaluated. Criteria can
  1167. * contain the following tokens:
  1168. * - <field_name>
  1169. * Replaced by the name of the field to be excluded
  1170. * - <field_value>
  1171. * Replaced by the value of the field in the current record
  1172. * Also keep in mind that if your criteria doesn't contain the
  1173. * &gt;field_value&lt; token then it will be evaluated before the query is
  1174. * executed and if the field is excluded it won't be included in the
  1175. * query.
  1176. *
  1177. * @return
  1178. * An array of type => criteria where the type is excluded if the criteria
  1179. * evaluates to TRUE
  1180. *
  1181. * @ingroup tripal
  1182. */
  1183. function tripal_chado_exclude_type_by_default() {
  1184. return array('text' => 'strlen("<field_value> ") > 250');
  1185. }
  1186. /**
  1187. * Implements hook_job_describe_args().
  1188. *
  1189. * Describes the arguements for the tripal_populate_mview job to allow for
  1190. * greater readability in the jobs details pages.
  1191. *
  1192. * @param $callback
  1193. * The callback of the current tripal job (this is the function that will be
  1194. * executed when tripal_launch_jobs.php is run.
  1195. * @param $args
  1196. * An array of arguments passed in when the job was registered.
  1197. *
  1198. * @return
  1199. * A more readable $args array
  1200. *
  1201. * @ingroup tripal
  1202. */
  1203. function tripal_chado_describe_args($callback, $args) {
  1204. $new_args = array();
  1205. if ($callback == 'tripal_populate_mview') {
  1206. // get this mview details
  1207. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
  1208. $results = db_query($sql, array(':mview_id' => $args[0]));
  1209. $mview = $results->fetchObject();
  1210. $new_args['View Name'] = $mview->name;
  1211. }
  1212. elseif ($callback == 'tripal_install_chado') {
  1213. $new_args['Action'] = $args[0];
  1214. }
  1215. return $new_args;
  1216. }
  1217. /**
  1218. * Implements hook_entity_property_info_alter().
  1219. *
  1220. * This is being implemented to ensure chado fields are exposed for search api indexing.
  1221. * All fields are available for index by default but the getter function set by default
  1222. * is not actually capable of getting the value from chado. Thus we change the getter
  1223. * function to one that can :-).
  1224. */
  1225. function tripal_chado_entity_property_info_alter(&$info) {
  1226. // Get a list of fields with the chado storage backend.
  1227. // Loop through all of the bundles.
  1228. if (isset($info['TripalEntity']['bundles'])) {
  1229. foreach ($info['TripalEntity']['bundles'] as $bundle_id => $bundle) {
  1230. // Loop through each of the fields for a given bundle.
  1231. foreach ($bundle['properties'] as $field_name => $field_info) {
  1232. // If the field is a chado field, then change the callback.
  1233. // @todo check this properly.
  1234. if (preg_match('/(\w+)__(\w+)/', $field_name, $matches)) {
  1235. $info['TripalEntity']['bundles'][$bundle_id]['properties'][$field_name]['getter callback'] =
  1236. 'tripal_chado_entity_property_get_value';
  1237. }
  1238. }
  1239. }
  1240. }
  1241. }
  1242. /**
  1243. * Provides a way for the search api to grab the value of a chado field.
  1244. *
  1245. * @param $entity
  1246. * The fully-loaded entity object to be indexed.
  1247. * @param $options
  1248. * Options that can be ued when retrieving the value.
  1249. * @param $field_name
  1250. * The machine name of the field we want to retrieve.
  1251. * @param $entity_type
  1252. * The type of entity (ie: TripalEntity).
  1253. *
  1254. * @return
  1255. * The rendered value of the field specified by $field_name.
  1256. */
  1257. function tripal_chado_entity_property_get_value($entity, $options, $field_name, $entity_type) {
  1258. $display = array(
  1259. 'type' => '',
  1260. 'label' => 'hidden',
  1261. );
  1262. $langcode = LANGUAGE_NONE;
  1263. $items = field_get_items($entity_type, $entity, $field_name);
  1264. if (count($items) == 1) {
  1265. $render_array = field_view_value($entity_type, $entity, $field_name, $items[0], $display, $langcode);
  1266. }
  1267. // @todo: handle fields with multiple values.
  1268. else {
  1269. $render_array = field_view_value($entity_type, $entity, $field_name, $items[0], $display, $langcode);
  1270. drupal_set_message('Tripal Chado currently only supports views integration for single value fields. The first value has been shown.', 'warning');
  1271. }
  1272. return drupal_render($render_array);
  1273. }