tripal_views.views.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. <?php
  2. include('views/handlers/views_handler_join_chado_through_linking.inc');
  3. include('views/handlers/views_handler_join_chado_aggregator.inc');
  4. include('api/tripal_views.api.inc');
  5. /**
  6. * @file
  7. * Tripal Views Integration
  8. *
  9. * @defgroup views Views Integration
  10. * @{
  11. * Provide rules for formatting and composition of fields
  12. * @}
  13. *
  14. * @defgroup views_handlers Views Integration Handlers
  15. * @ingroup views
  16. * @{
  17. * Provide rules for formatting and composition of fields
  18. * @}
  19. *
  20. * @defgroup views_field_handlers Views Field Handlers
  21. * @ingroup views_handlers
  22. * @{
  23. * Provide rules for formatting and composition of fields
  24. * @}
  25. *
  26. *
  27. * @defgroup views_filter_handlers Views Filter Handlers
  28. * @ingroup views_handlers
  29. * @{
  30. * Provide the ability to filter based on specified data
  31. * @}
  32. *
  33. * @defgroup views_sort_handlers Views Sort Handlers
  34. * @ingroup views_handlers
  35. * @{
  36. * Provide methods describing how specific data should be sorted
  37. * @}
  38. *
  39. * @defgroup views_argument_handlers Views Arguement Handlers
  40. * @ingroup views_handlers
  41. * @{
  42. * Provide the ability to filter pased on arguments in the path of the view
  43. * @}
  44. */
  45. /**
  46. * Implements hook_views_handlers()
  47. *
  48. * Purpose: Register all custom handlers with views
  49. * where a handler describes either "the type of field",
  50. * "how a field should be filtered", "how a field should be sorted"
  51. *
  52. * @return
  53. * An array of handler definitions
  54. *
  55. * @ingroup tripal_views
  56. */
  57. function tripal_views_views_handlers() {
  58. return array(
  59. 'info' => array(
  60. 'path' => drupal_get_path('module', 'tripal_views') . '/views/handlers',
  61. ),
  62. 'handlers' => array(
  63. // Custom Tripal Filter Handlers
  64. 'tripal_views_handler_filter_file_upload' => array(
  65. 'parent' => 'views_handler_filter',
  66. ),
  67. 'tripal_views_handler_filter_textarea' => array(
  68. 'parent' => 'views_handler_filter',
  69. ),
  70. 'tripal_views_handler_filter_no_results' => array(
  71. 'parent' => 'views_handler_filter'
  72. ),
  73. 'tripal_views_handler_filter_select_cvterm' => array(
  74. 'parent' => 'views_handler_filter_string',
  75. ),
  76. 'tripal_views_handler_filter_select_string' => array(
  77. 'parent' => 'chado_views_handler_filter_string',
  78. ),
  79. 'tripal_views_handler_filter_sequence' => array(
  80. 'parent' => 'chado_views_handler_filter_string',
  81. ),
  82. // Custom Tripal Field Handlers
  83. 'tripal_views_handler_field_aggregate' => array(
  84. 'parent' => 'chado_views_handler_field',
  85. ),
  86. 'tripal_views_handler_field_sequence' => array(
  87. 'parent' => 'chado_views_handler_field',
  88. ),
  89. // Join Handlers
  90. 'views_handler_join_chado_aggregator' => array(
  91. 'parent' => 'views_join',
  92. ),
  93. 'views_handler_join_chado_through_linking' => array(
  94. 'parent' => 'views_join',
  95. ),
  96. // Relationship Handlers
  97. 'chado_views_handler_relationship' => array(
  98. 'parent' => 'views_handler_relationship'
  99. ),
  100. 'chado_views_handler_relationship_to_node' => array(
  101. 'parent' => 'views_handler_relationship'
  102. ),
  103. // Wrappers for Default Views Handlers-----
  104. // Field Handlers
  105. 'chado_views_handler_field' => array(
  106. 'parent' => 'views_handler_field'
  107. ),
  108. 'chado_views_handler_field_boolean' => array(
  109. 'parent' => 'views_handler_field_boolean'
  110. ),
  111. 'chado_views_handler_field_counter' => array(
  112. 'parent' => 'views_handler_field_counter'
  113. ),
  114. 'chado_views_handler_field_custom' => array(
  115. 'parent' => 'views_handler_field_custom'
  116. ),
  117. 'chado_views_handler_field_date' => array(
  118. 'parent' => 'views_handler_field_date'
  119. ),
  120. 'chado_views_handler_field_markup' => array(
  121. 'parent' => 'views_handler_field_markup'
  122. ),
  123. 'chado_views_handler_field_math' => array(
  124. 'parent' => 'views_handler_field_math'
  125. ),
  126. 'chado_views_handler_field_numeric' => array(
  127. 'parent' => 'views_handler_field_numeric'
  128. ),
  129. // Filter Handlers
  130. 'chado_views_handler_filter_string' => array(
  131. 'parent' => 'views_handler_filter_string',
  132. ),
  133. 'chado_views_handler_filter_boolean_operator_string' => array(
  134. 'parent' => 'views_handler_filter_boolean_operator_string',
  135. ),
  136. 'chado_views_handler_filter_boolean_operator' => array(
  137. 'parent' => 'views_handler_filter_boolean_operator',
  138. ),
  139. 'chado_views_handler_filter_date' => array(
  140. 'parent' => 'views_handler_filter_date',
  141. ),
  142. 'chado_views_handler_filter_equality' => array(
  143. 'parent' => 'views_handler_filter_equality',
  144. ),
  145. 'chado_views_handler_filter_float' => array(
  146. 'parent' => 'views_handler_filter_float',
  147. ),
  148. 'chado_views_handler_filter_numeric' => array(
  149. 'parent' => 'views_handler_filter_numeric',
  150. ),
  151. // Sort Handlers
  152. 'chado_views_handler_sort' => array(
  153. 'parent' => 'views_handler_sort'
  154. ),
  155. 'chado_views_handler_sort_date' => array(
  156. 'parent' => 'views_handler_sort_date'
  157. ),
  158. 'chado_views_handler_sort_formula' => array(
  159. 'parent' => 'views_handler_sort_formula'
  160. ),
  161. 'chado_views_handler_sort_menu_hierarchy' => array(
  162. 'parent' => 'views_handler_sort_menu_hierarchy'
  163. ),
  164. 'chado_views_handler_sort_random' => array(
  165. 'parent' => 'views_handler_sort_random'
  166. ),
  167. ),
  168. );
  169. }
  170. /**
  171. * Implements hook_views_pre_render
  172. *
  173. * Purpose: Intercepts the view after the query has been executed
  174. * All the results are stored in $view->result
  175. * Looking up the NID here ensures the query is only executed once
  176. * for all stocks in the table.
  177. *
  178. * @todo add if !<chado/drupal same db> around NID portion
  179. *
  180. * @ingroup tripal_views
  181. */
  182. function tripal_views_views_pre_render(&$view) {
  183. // We need to unset the exposed_input for the view so we can repopulate that
  184. // variable. This is necessary if we're using the file_upload_combo
  185. // custom form element which adds the file_path variable to the $_GET after the
  186. // view has populated the $view->exposed_input variable
  187. unset($view->exposed_input);
  188. // we want to add to the bottom of the views the form for downloading
  189. // results in other formats (e.g. Excel, FASTA, CSV, etc.). The Views Data
  190. // Export module provides small images at the bottom, but we want to provide
  191. // a more intutitive interface for getting different file formats
  192. $form = drupal_get_form('tripal_views_data_export_download_form', $view, $display_id, $args);
  193. $view->attachment_after = $form;
  194. }
  195. /**
  196. * Generates a dynamic data array for Views
  197. *
  198. * Purpose: This function is a hook used by the Views module. It populates and
  199. * returns a data array that specifies for the Views module the base table,
  200. * the tables it joins with and handlers. The data array is populated
  201. * using the data stored in the tripal_views tables.
  202. *
  203. * @return a data array formatted for the Views module
  204. *
  205. * @ingroup tripal_views
  206. */
  207. function tripal_views_views_data() {
  208. // Make sure all chado tables are integrated
  209. tripal_views_integrate_all_chado_tables();
  210. // Define Global Fields
  211. // Filter handler that lets the admin say:
  212. // "Show no results until they enter search parameters"
  213. $data['views']['search_results'] = array(
  214. 'title' => t('Search Results'),
  215. 'help' => t('Delay results until Apply/Search is clicked by the user.'),
  216. 'filter' => array(
  217. 'handler' => 'tripal_views_handler_filter_no_results',
  218. ),
  219. );
  220. $tvi_query = db_query('SELECT * FROM {tripal_views}');
  221. while ($tvi_row = db_fetch_object($tvi_query)) {
  222. // check to see if this is the lightest (drupal-style) priority setup for this table
  223. // if not then don't use this definition
  224. $lightest_priority_setup = tripal_views_is_lightest_priority_setup($tvi_row->setup_id, $tvi_row->table_name);
  225. if (!$lightest_priority_setup) {
  226. continue;
  227. }
  228. // ids we'll use for queries
  229. $setup_id = $tvi_row->setup_id;
  230. $mview_id = $tvi_row->mview_id;
  231. // holds the base table name and fields
  232. $base_table = '';
  233. $base_fields = array();
  234. // indicated whether the current table is a base table or not
  235. $is_base_table = $tvi_row->base_table;
  236. // populate the base table name and fields. If an $mview_id is given
  237. // then get the materialized view info, otherwise get the Chado table info
  238. if ($mview_id) {
  239. // get the base table name from the materialized view
  240. $sql = "SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = %d";
  241. $mview_table = db_fetch_object(db_query($sql, $mview_id));
  242. $base_table = $mview_table->name;
  243. // get the columns in this materialized view. They are separated by commas
  244. // where the first word is the column name and the rest is the type
  245. $columns = explode(",", $mview_table->mv_specs);
  246. foreach ($columns as $column) {
  247. $column = trim($column); // trim trailing and leading spaces
  248. preg_match("/^(.*?)\ (.*?)$/", $column, $matches);
  249. $column_name = $matches[1];
  250. $column_type = $matches[2];
  251. $base_fields[$column_name] = array(
  252. 'column_name' => $column_name,
  253. 'type' => $column_type,
  254. );
  255. }
  256. // get the field name and descriptions
  257. $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=%d";
  258. $query = db_query($sql, $setup_id);
  259. while ($field = db_fetch_object($query)) {
  260. $base_fields[$field->column_name]['name'] = $field->name;
  261. $base_fields[$field->column_name]['help'] = $field->description;
  262. }
  263. }
  264. // if this is not a legacy materialized view
  265. else {
  266. $base_table = $tvi_row->table_name;
  267. // Check if we are trying to integrate the node table
  268. // if we are we want to add to a current integration
  269. // as compared to create a whole new one
  270. if ($base_table == 'node') {
  271. /* -- SPF Jun 13, 2013 -- not sure why the node table
  272. * is included in the Chado integrated tables. I've commented
  273. * out. I think it was here for integration of chado tables wiht
  274. * nodes, but code to do this has been added below and it's not
  275. * necessary to have the node table in the list of integrated tables
  276. *
  277. // Add any joins between the node table and other tables
  278. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
  279. $joins = db_query($sql, $setup_id);
  280. while ($join = db_fetch_object($joins)) {
  281. $left_table = $join->left_table;
  282. $left_field = $join->left_field;
  283. $base_field = $join->base_field;
  284. $handler = $join->handler;
  285. // add join entry
  286. $data[$base_table]['table']['join'][$left_table] = array(
  287. 'left_field' => $left_field,
  288. 'field' => $base_field,
  289. );
  290. if ($handler) {
  291. $data[$base_table]['table']['join'][$left_table]['handler'] = $handler;
  292. }
  293. }
  294. // Add in any handlers for node fields
  295. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id=%d";
  296. $query = db_query($sql, $setup_id);
  297. while ($handler = db_fetch_object($query)) {
  298. $data[$base_table][$handler->column_name][$handler->handler_type]['handler'] = $handler->handler_name;
  299. // Add in any additional arguments
  300. // This should be a serialized array including (at a minimum) name => <handler name>
  301. if ($handler->arguments) {
  302. $data[$base_table][$handler->column_name][$handler->handler_type] = array_merge($data[$base_table][$handler->column_name][$handler->handler_type], unserialize($handler->arguments));
  303. }
  304. }
  305. */
  306. continue;
  307. }
  308. // get the table description
  309. $table_desc = tripal_core_get_chado_table_schema($base_table);
  310. $fields = $table_desc['fields'];
  311. if (!is_array($fields)) {
  312. $fields = array();
  313. }
  314. foreach ($fields as $column => $attrs) {
  315. $base_fields[$column] = array(
  316. 'column_name' => $column,
  317. 'type' => $attrs['type'],
  318. );
  319. }
  320. // get the field name and descriptions
  321. $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=%d";
  322. $query = db_query($sql, $setup_id);
  323. while ($field = db_fetch_object($query)) {
  324. $base_fields[$field->column_name]['name'] = $field->name;
  325. $base_fields[$field->column_name]['help'] = $field->description;
  326. }
  327. }
  328. // Setup the base table info in the data array
  329. $data[$base_table]['table']['group'] = t("$tvi_row->name");
  330. if ($is_base_table) {
  331. $data[$base_table]['table']['base'] = array(
  332. 'group' => "$tvi_row->name",
  333. 'title' => "$tvi_row->name",
  334. 'help' => $tvi_row->comment,
  335. );
  336. }
  337. else {
  338. $data[$base_table]['table'] = array(
  339. 'group' => "$tvi_row->name",
  340. 'title' => "$tvi_row->name",
  341. 'help' => $tvi_row->comment,
  342. );
  343. }
  344. // first add the fields
  345. foreach ($base_fields as $column_name => $base_field) {
  346. $data[$base_table][$column_name] = array(
  347. 'title' => t($base_field['name']),
  348. 'help' => t($base_field['help']),
  349. 'field' => array(
  350. 'click sortable' => TRUE,
  351. ),
  352. );
  353. // now add the handlers
  354. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = %d AND column_name = '%s'";
  355. $handlers = db_query($sql, $setup_id, $column_name);
  356. while ($handler = db_fetch_object($handlers)) {
  357. $data[$base_table][$column_name][$handler->handler_type]['handler'] = $handler->handler_name;
  358. // Add in any additional arguments
  359. // This should be a serialized array including (at a minimum) name => <handler name>
  360. if ($handler->arguments) {
  361. $data[$base_table][$column_name][$handler->handler_type] = array_merge($data[$base_table][$column_name][$handler->handler_type], unserialize($handler->arguments));
  362. }
  363. };
  364. // add in joins to the node tables if the Chado schema is local
  365. if (tripal_core_chado_schema_exists()) {
  366. $linker_table = 'chado_' . $base_table;
  367. $linker_name = ucwords(str_replace('_', ' ', $base_table));
  368. $schema = tripal_core_get_chado_table_schema($base_table);
  369. $pkey = $schema['primary key'][0];
  370. // if a node linking table exists then add in the joins
  371. if (db_table_exists($linker_table)) {
  372. // add the linker table to the data array
  373. $data[$linker_table] = array(
  374. // describe the table
  375. 'table' => array(
  376. 'title' => 'Chado ' . $linker_name,
  377. 'group' => 'Chado ' . $linker_name,
  378. // join to the base table and the node table
  379. 'join' => array(
  380. $base_table => array(
  381. 'left_field' => $pkey,
  382. 'field' => $pkey,
  383. ),
  384. 'node' => array(
  385. 'left_field' => 'nid',
  386. 'field' => 'nid',
  387. ),
  388. ),
  389. ),
  390. // add the nid field
  391. 'nid' => array(
  392. 'title' => t($linker_name . ' Node ID'),
  393. 'help' => t("The node ID for this $linker_name"),
  394. 'field' => array(
  395. 'handler' => 'views_handler_field_numeric',
  396. 'click sortable' => TRUE,
  397. ),
  398. 'filter' => array(
  399. 'handler' => 'views_handler_filter_numeric',
  400. ),
  401. 'sort' => array(
  402. 'handler' => 'views_handler_sort',
  403. ),
  404. ),
  405. // add the primary key field
  406. $pkey => array(
  407. 'title' => t($linker_name . ' ' . $pkey),
  408. 'help' => t("The $base_table primary key"),
  409. 'field' => array(
  410. 'handler' => 'views_handler_field_numeric',
  411. 'click sortable' => TRUE,
  412. ),
  413. 'filter' => array(
  414. 'handler' => 'views_handler_filter_numeric',
  415. ),
  416. 'sort' => array(
  417. 'handler' => 'views_handler_sort',
  418. ),
  419. ),
  420. );
  421. // add the join instructions for the base table
  422. // (we'll add the recipricol joins for the node table in the hook_views_data_alter
  423. $data[$base_table]['table']['join'][$linker_table] = array(
  424. 'left_field' => 'organism_id',
  425. 'field' => 'organism_id',
  426. );
  427. $data[$base_table]['table']['join']['node'] = array(
  428. 'left_table' => $linker_table,
  429. 'left_field' => 'organism_id',
  430. 'field' => 'organism_id',
  431. );
  432. // Add relationship between linker and base table
  433. $data[$linker_table][$base_table . '_nid'] = array(
  434. 'group' => $linker_name,
  435. 'title' => $linker_name . 'Node',
  436. 'help' => "Links Chado $linker_name Fields/Data to the Nodes in the current View.",
  437. 'real field' => $pkey,
  438. 'relationship' => array(
  439. 'handler' => 'views_handler_relationship',
  440. 'title' => t("Chado => $linker_name"),
  441. 'label' => t("Chado => $linker_name"),
  442. 'real field' => $pkey,
  443. 'base' => $base_table,
  444. 'base field' => $pkey
  445. ),
  446. );
  447. // Add node relationship to base table
  448. $data[$linker_table][$linker_table .'_nid'] = array(
  449. 'group' => $linker_name,
  450. 'title' => $linker_name . 'Node',
  451. 'help' => "Links Chado $linker_name Fields/Data to the Nodes in the current View.",
  452. 'real field' => 'nid',
  453. 'relationship' => array(
  454. 'handler' => 'views_handler_relationship',
  455. 'title' => t('Chado => Node'),
  456. 'label' => t('Chado => Node'),
  457. 'real field' => 'nid',
  458. 'base' => 'node',
  459. 'base field' => 'nid'
  460. ),
  461. );
  462. }
  463. }
  464. }
  465. // now add the joins
  466. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
  467. $joins = db_query($sql, $setup_id);
  468. if (!isset($joins)) {
  469. $joins = array();
  470. }
  471. while ($join = db_fetch_object($joins)) {
  472. $left_table = $join->left_table;
  473. $left_field = $join->left_field;
  474. $base_field = $join->base_field;
  475. $handler = $join->handler;
  476. // if the 'node' table is in our integrated list then
  477. // we want to skip it. It shouldn't be there.
  478. if ($left_table == 'node') {
  479. continue;
  480. }
  481. // add join entry
  482. $data[$base_table]['table']['join'][$left_table] = array(
  483. 'left_field' => $left_field,
  484. 'field' => $base_field,
  485. );
  486. if ($handler) {
  487. $data[$base_table]['table']['join'][$left_table]['handler'] = $handler;
  488. }
  489. }
  490. }
  491. return $data;
  492. }
  493. /**
  494. *
  495. * @ingroup tripal_views
  496. */
  497. function tripal_views_views_data_alter(&$data) {
  498. $tvi_query = db_query('SELECT * FROM {tripal_views}');
  499. // iterate through the views that we manage
  500. while ($tvi_row = db_fetch_object($tvi_query)) {
  501. //ids we'll use for queries
  502. $mview_id = $tvi_row->mview_id;
  503. $setup_id = $tvi_row->setup_id;
  504. // iterate through the columns and alter the existing data array for
  505. // joins to other tables
  506. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
  507. $joins = db_query($sql, $setup_id);
  508. while ($join = db_fetch_object($joins)) {
  509. $left_table = $join->left_table;
  510. $left_field = $join->left_field;
  511. $base_field = $join->base_field;
  512. $base_table = $join->base_table;
  513. // add the recipricol join entries for each column
  514. if (array_key_exists($left_table, $data) and $base_table != 'node') {
  515. $data[$left_table]['table']['join'][$base_table] = array(
  516. 'left_field' => $base_field,
  517. 'field' => $left_field,
  518. );
  519. }
  520. }
  521. // add in joins to the node tables if the Chado schema is local
  522. if (tripal_core_chado_schema_exists()) {
  523. $base_table = $tvi_row->table_name;
  524. $linker_table = 'chado_' . $base_table;
  525. // if a node linking table exists then add in the joins
  526. if (db_table_exists($linker_table)) {
  527. $data['node']['table']['join'][$linker_table] = array(
  528. 'left_field' => 'nid',
  529. 'field' => 'nid',
  530. );
  531. $data['node']['table']['join'][$base_table] = array(
  532. 'left_table' => $linker_table,
  533. 'left_field' => 'nid',
  534. 'field' => 'nid',
  535. );
  536. }
  537. }
  538. }
  539. return $data;
  540. }
  541. /**
  542. * Implementation of hook_views_plugins().
  543. */
  544. function tripal_views_views_plugins() {
  545. $tc_path = drupal_get_path('module', 'tripal_views');
  546. $style_defaults = array(
  547. 'path' => $tc_path . '/views_data_export/plugins',
  548. 'parent' => 'views_data_export',
  549. 'theme' => 'views_data_export',
  550. 'theme path' => $tc_path . '/views_data_export/theme',
  551. 'theme file' => 'tripal_views_data_export.theme.inc',
  552. 'uses row plugin' => FALSE,
  553. 'uses fields' => TRUE,
  554. 'uses options' => TRUE,
  555. 'type' => 'data_export',
  556. );
  557. // add FASTA format as a new style for the existing views_export_data Display
  558. return array(
  559. 'style' => array(
  560. 'views_data_export_fasta' => array(
  561. 'title' => t('FASTA file'),
  562. 'help' => t('Display results in FASTA format. All fields in results are on the definition line while the feature.residues field must be present .'),
  563. 'handler' => 'tripal_views_plugin_style_export_fasta',
  564. // Views Data Export element that will be used to set additional headers when serving the feed.
  565. 'export headers' => array('Content-type: text/plain; charset=utf-8'),
  566. // Views Data Export element mostly used for creating some additional classes and template names.
  567. 'export feed type' => 'fasta',
  568. 'export feed text' => 'FASTA',
  569. 'export feed file' => '%view.fna',
  570. 'export feed icon' => $tc_path . '/views_data_export/images/fasta.png',
  571. 'additional themes' => array(
  572. 'views_data_export_fasta_header' => 'style',
  573. 'views_data_export_fasta_body' => 'style',
  574. 'views_data_export_fasta_footer' => 'style',
  575. ),
  576. 'additional themes base' => 'views_data_export_fasta',
  577. ) + $style_defaults,
  578. ),
  579. );
  580. }
  581. /**
  582. * Implementation of hook_views_pre_view().
  583. */
  584. function tripal_views_views_pre_view(&$view, &$display_id, &$args) {
  585. // merge the $_GET and $_POST into the $_GET. This is because
  586. // Views and Views Data Export modules only uses the $_GET variable but
  587. // file uploads require $_POST. We need to make sure these two modules
  588. // have access to everything needed for this view to work properlys
  589. $_GET = array_merge($_GET, $_POST);
  590. }
  591. /**
  592. * Implementation of hook_views_pre_build().
  593. */
  594. /* function tripal_views_views_pre_render(&$view, &$display_id, &$args){
  595. // we want to add to the bottom of the views the form for downloading
  596. // results in other formats (e.g. Excel, FASTA, CSV, etc.). The Views Data
  597. // Export module provides small images at the bottom, but we want to provide
  598. // a more intutitive interface for getting different file formats
  599. $form = drupal_get_form('tripal_views_data_export_download_form',$view,$display_id,$args);
  600. $view->attachment_after = $form;
  601. }*/
  602. /*
  603. *
  604. function tripal_views_views_query_alter(&$view, &$query){
  605. // iterate through the tables and see if they are chado tables.
  606. // if they are then prefix them with a "chado." prefix
  607. dpm($query);
  608. $tables = $query->tables;
  609. foreach ($tables as $base => $subtables) {
  610. $desc = tripal_core_get_chado_table_schema($base);
  611. if ($desc) {
  612. $query->tables["chado." . $base] = $subtables;
  613. unset($query->tables[$base]);
  614. $base = "chado." . $base;
  615. }
  616. foreach ($subtables as $subtable => $values) {
  617. $desc = tripal_core_get_chado_table_schema($subtable);
  618. if ($desc) {
  619. $query->tables[$base]["chado." . $subtable] = $values;
  620. unset($query->tables[$base][$subtable]);
  621. }
  622. }
  623. }
  624. dpm($query);
  625. } */
  626. /**
  627. * Implementation of hook_views_pre_execute().
  628. */
  629. function tripal_views_views_pre_execute(&$view) {
  630. // if the base table is a chado table then we want to set the
  631. // search path so it can find all of the tables.
  632. // this will break views that use tables that have the same name
  633. // as chado tables (e.g. contact).
  634. $desc = tripal_core_get_chado_table_schema($view->base_table);
  635. if ($desc) {
  636. tripal_db_set_chado_search_path('chado');
  637. }
  638. }
  639. /**
  640. * Implementation of hook_views_post_render().
  641. */
  642. function tripal_views_views_post_render(&$view, &$output, &$cache) {
  643. // if the base table and the query is completed we want to set
  644. // the search path back to the default.
  645. $desc = tripal_core_get_chado_table_schema($view->base_table);
  646. if ($desc) {
  647. tripal_db_set_default_search_path();
  648. }
  649. }
  650. /**
  651. * the Download Views data export form
  652. */
  653. function tripal_views_data_export_download_form(&$form_state, $view, $display_id, $args) {
  654. $form = array();
  655. $urls = array();
  656. // get any export_data_export displays
  657. $displays = $view->display;
  658. $options = array();
  659. $default = '';
  660. $current_display = $view->current_display;
  661. foreach ($displays as $name => $display) {
  662. if (preg_match("/^views_data_export/", $name)) {
  663. // only add this display to the form if it is attached
  664. $display_options = $display->display_options;
  665. if (strcmp($display_options['displays'][$current_display], $current_display) != 0) {
  666. continue;
  667. }
  668. // set the first item as default
  669. if (!$default) {
  670. $default = $display->id;
  671. }
  672. $path = $display->display_options['path'];
  673. $query = $view->get_exposed_input(); // retrieves elements in $_GET array
  674. $urls[$display->id]['path'] = $path;
  675. $urls[$display->id]['query'] = $query;
  676. // add the new item to the options array
  677. $options[$display->id] = $display->display_title;
  678. }
  679. }
  680. // only generate the form if we have views_data_export displays attached
  681. // to this view
  682. if (count($options) > 0) {
  683. $form_state['storage']['urls'] = $urls;
  684. $form['#cache'] = TRUE;
  685. // we want the form to redirect to a new window
  686. $form['#attributes']['target'] = "_blank";
  687. // now build the form elements
  688. $form['downloads'] = array(
  689. '#type' => 'fieldset',
  690. '#title' => 'Download Results',
  691. '#collapsible' => TRUE,
  692. '#collapsed' => FALSE
  693. );
  694. $form['downloads']['file_type'] = array(
  695. '#title' => t('File format'),
  696. '#type' => 'radios',
  697. '#options' => $options,
  698. '#required' => TRUE,
  699. '#default_value' => $default,
  700. '#description' => t('Please select a file format to download'),
  701. );
  702. $form['downloads']['submit'] = array(
  703. '#value' => t('Download Results'),
  704. '#type' => 'submit',
  705. );
  706. }
  707. return $form;
  708. }
  709. /**
  710. * Submit for the Download Views data export form
  711. */
  712. function tripal_views_data_export_download_form_submit($form, &$form_state) {
  713. $urls = $form_state['storage']['urls'];
  714. $display_id = $form_state['values']['file_type'];
  715. drupal_goto($urls[$display_id]['path'], $urls[$display_id]['query']);
  716. }