tripal_views.views.inc 22 KB

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