tripal_views.views.inc 25 KB

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