tripal_views.views.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. // D7 TODO: Check DBTNG changes work
  222. foreach ($tvi_query as $tvi_row) {
  223. // check to see if this is the lightest (drupal-style) priority setup for this table
  224. // if not then don't use this definition
  225. $lightest_priority_setup = tripal_views_is_lightest_priority_setup($tvi_row->setup_id, $tvi_row->table_name);
  226. if (!$lightest_priority_setup) {
  227. continue;
  228. }
  229. // ids we'll use for queries
  230. $setup_id = $tvi_row->setup_id;
  231. $mview_id = $tvi_row->mview_id;
  232. // holds the base table name and fields
  233. $base_table = '';
  234. $base_fields = array();
  235. // indicated whether the current table is a base table or not
  236. $is_base_table = $tvi_row->base_table;
  237. // populate the base table name and fields. If an $mview_id is given
  238. // then get the materialized view info, otherwise get the Chado table info
  239. if ($mview_id) {
  240. // get the base table name from the materialized view
  241. // D7 TODO: Check DBTNG changes work
  242. $sql = "SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = :id";
  243. $mview_table = db_query($sql, array(':id' => $mview_id));
  244. $mview_table = $mview_table->fetchObject();
  245. $base_table = $mview_table->name;
  246. // get the columns in this materialized view. They are separated by commas
  247. // where the first word is the column name and the rest is the type
  248. $columns = explode(",", $mview_table->mv_specs);
  249. foreach ($columns as $column) {
  250. $column = trim($column); // trim trailing and leading spaces
  251. preg_match("/^(.*?)\ (.*?)$/", $column, $matches);
  252. $column_name = $matches[1];
  253. $column_type = $matches[2];
  254. $base_fields[$column_name] = array(
  255. 'column_name' => $column_name,
  256. 'type' => $column_type,
  257. );
  258. }
  259. // get the field name and descriptions
  260. // D7 TODO: Check DBTNG changes work
  261. $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=:setup";
  262. $query = db_query($sql, array(':setup' => $setup_id));
  263. foreach($query as $field) {
  264. $base_fields[$field->column_name]['name'] = $field->name;
  265. $base_fields[$field->column_name]['help'] = $field->description;
  266. }
  267. }
  268. // if this is not a legacy materialized view
  269. else {
  270. $base_table = $tvi_row->table_name;
  271. // Check if we are trying to integrate the node table
  272. // if we are we want to add to a current integration
  273. // as compared to create a whole new one
  274. if ($base_table == 'node') {
  275. /* -- SPF Jun 13, 2013 -- not sure why the node table
  276. * is included in the Chado integrated tables. I've commented
  277. * out. I think it was here for integration of chado tables wiht
  278. * nodes, but code to do this has been added below and it's not
  279. * necessary to have the node table in the list of integrated tables
  280. *
  281. // Add any joins between the node table and other tables
  282. // D7 TODO: Check DBTNG changes work
  283. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
  284. $joins = db_query($sql, $setup_id);
  285. foreach ($joins as $join) {
  286. $left_table = $join->left_table;
  287. $left_field = $join->left_field;
  288. $base_field = $join->base_field;
  289. $handler = $join->handler;
  290. // add join entry
  291. $data[$base_table]['table']['join'][$left_table] = array(
  292. 'left_field' => $left_field,
  293. 'field' => $base_field,
  294. );
  295. if ($handler) {
  296. $data[$base_table]['table']['join'][$left_table]['handler'] = $handler;
  297. }
  298. }
  299. // Add in any handlers for node fields
  300. // D7 TODO: Check DBTNG changes work
  301. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id=%d";
  302. $query = db_query($sql, $setup_id);
  303. foreach ($query as $handler) {
  304. $data[$base_table][$handler->column_name][$handler->handler_type]['handler'] = $handler->handler_name;
  305. // Add in any additional arguments
  306. // This should be a serialized array including (at a minimum) name => <handler name>
  307. if ($handler->arguments) {
  308. $data[$base_table][$handler->column_name][$handler->handler_type] = array_merge($data[$base_table][$handler->column_name][$handler->handler_type], unserialize($handler->arguments));
  309. }
  310. }
  311. */
  312. continue;
  313. }
  314. // get the table description
  315. $table_desc = tripal_core_get_chado_table_schema($base_table);
  316. $fields = $table_desc['fields'];
  317. if (!is_array($fields)) {
  318. $fields = array();
  319. }
  320. foreach ($fields as $column => $attrs) {
  321. $base_fields[$column] = array(
  322. 'column_name' => $column,
  323. 'type' => $attrs['type'],
  324. );
  325. }
  326. // get the field name and descriptions
  327. $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=:setup";
  328. $query = db_query($sql, array(':setup' => $setup_id));
  329. foreach ($query as $field) {
  330. $base_fields[$field->column_name]['name'] = $field->name;
  331. $base_fields[$field->column_name]['help'] = $field->description;
  332. }
  333. }
  334. // Setup the base table info in the data array
  335. $data[$base_table]['table']['group'] = t("$tvi_row->name");
  336. if ($is_base_table) {
  337. $data[$base_table]['table']['base'] = array(
  338. 'group' => "$tvi_row->name",
  339. 'title' => "$tvi_row->name",
  340. 'help' => $tvi_row->comment,
  341. );
  342. }
  343. else {
  344. $data[$base_table]['table'] = array(
  345. 'group' => "$tvi_row->name",
  346. 'title' => "$tvi_row->name",
  347. 'help' => $tvi_row->comment,
  348. );
  349. }
  350. // first add the fields
  351. foreach ($base_fields as $column_name => $base_field) {
  352. $data[$base_table][$column_name] = array(
  353. 'title' => t($base_field['name']),
  354. 'help' => t($base_field['help']),
  355. 'field' => array(
  356. 'click sortable' => TRUE,
  357. ),
  358. );
  359. // now add the handlers
  360. // D7 TODO: Check DBTNG changes work
  361. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = :setup AND column_name = :column";
  362. $handlers = db_query($sql, array(':setup' => $setup_id, ':column' => $column_name));
  363. foreach ($handlers as $handler) {
  364. $data[$base_table][$column_name][$handler->handler_type]['handler'] = $handler->handler_name;
  365. // Add in any additional arguments
  366. // This should be a serialized array including (at a minimum) name => <handler name>
  367. if ($handler->arguments) {
  368. $data[$base_table][$column_name][$handler->handler_type] = array_merge($data[$base_table][$column_name][$handler->handler_type], unserialize($handler->arguments));
  369. }
  370. };
  371. // add in joins to the node tables if the Chado schema is local
  372. if (tripal_core_chado_schema_exists()) {
  373. $linker_table = 'chado_' . $base_table;
  374. $linker_name = ucwords(str_replace('_', ' ', $base_table));
  375. $schema = tripal_core_get_chado_table_schema($base_table);
  376. $pkey = $schema['primary key'][0];
  377. // if a node linking table exists then add in the joins
  378. if (db_table_exists($linker_table)) {
  379. // add the linker table to the data array
  380. $data[$linker_table] = array(
  381. // describe the table
  382. 'table' => array(
  383. 'title' => 'Chado ' . $linker_name,
  384. 'group' => 'Chado ' . $linker_name,
  385. // join to the base table and the node table
  386. 'join' => array(
  387. $base_table => array(
  388. 'left_field' => $pkey,
  389. 'field' => $pkey,
  390. ),
  391. 'node' => array(
  392. 'left_field' => 'nid',
  393. 'field' => 'nid',
  394. ),
  395. ),
  396. ),
  397. // add the nid field
  398. 'nid' => array(
  399. 'title' => t($linker_name . ' Node ID'),
  400. 'help' => t("The node ID for this $linker_name"),
  401. 'field' => array(
  402. 'handler' => 'views_handler_field_numeric',
  403. 'click sortable' => TRUE,
  404. ),
  405. 'filter' => array(
  406. 'handler' => 'views_handler_filter_numeric',
  407. ),
  408. 'sort' => array(
  409. 'handler' => 'views_handler_sort',
  410. ),
  411. ),
  412. // add the primary key field
  413. $pkey => array(
  414. 'title' => t($linker_name . ' ' . $pkey),
  415. 'help' => t("The $base_table primary key"),
  416. 'field' => array(
  417. 'handler' => 'views_handler_field_numeric',
  418. 'click sortable' => TRUE,
  419. ),
  420. 'filter' => array(
  421. 'handler' => 'views_handler_filter_numeric',
  422. ),
  423. 'sort' => array(
  424. 'handler' => 'views_handler_sort',
  425. ),
  426. ),
  427. );
  428. // add the join instructions for the base table
  429. // (we'll add the recipricol joins for the node table in the hook_views_data_alter
  430. $data[$base_table]['table']['join'][$linker_table] = array(
  431. 'left_field' => 'organism_id',
  432. 'field' => 'organism_id',
  433. );
  434. $data[$base_table]['table']['join']['node'] = array(
  435. 'left_table' => $linker_table,
  436. 'left_field' => 'organism_id',
  437. 'field' => 'organism_id',
  438. );
  439. // Add relationship between linker and base table
  440. $data[$linker_table][$base_table . '_nid'] = array(
  441. 'group' => $linker_name,
  442. 'title' => $linker_name . 'Node',
  443. 'help' => "Links Chado $linker_name Fields/Data to the Nodes in the current View.",
  444. 'real field' => $pkey,
  445. 'relationship' => array(
  446. 'handler' => 'views_handler_relationship',
  447. 'title' => t("Chado => $linker_name"),
  448. 'label' => t("Chado => $linker_name"),
  449. 'real field' => $pkey,
  450. 'base' => $base_table,
  451. 'base field' => $pkey
  452. ),
  453. );
  454. // Add node relationship to base table
  455. $data[$linker_table][$linker_table .'_nid'] = array(
  456. 'group' => $linker_name,
  457. 'title' => $linker_name . 'Node',
  458. 'help' => "Links Chado $linker_name Fields/Data to the Nodes in the current View.",
  459. 'real field' => 'nid',
  460. 'relationship' => array(
  461. 'handler' => 'views_handler_relationship',
  462. 'title' => t('Chado => Node'),
  463. 'label' => t('Chado => Node'),
  464. 'real field' => 'nid',
  465. 'base' => 'node',
  466. 'base field' => 'nid'
  467. ),
  468. );
  469. }
  470. }
  471. }
  472. // now add the joins
  473. // D7 TODO: Check DBTNG changes work
  474. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = :setup";
  475. $joins = db_query($sql, array(':setup' => $setup_id));
  476. if (!isset($joins)) {
  477. $joins = array();
  478. }
  479. foreach ($joins as $join) {
  480. $left_table = $join->left_table;
  481. $left_field = $join->left_field;
  482. $base_field = $join->base_field;
  483. $handler = $join->handler;
  484. // if the 'node' table is in our integrated list then
  485. // we want to skip it. It shouldn't be there.
  486. if ($left_table == 'node') {
  487. continue;
  488. }
  489. // add join entry
  490. $data[$base_table]['table']['join'][$left_table] = array(
  491. 'left_field' => $left_field,
  492. 'field' => $base_field,
  493. );
  494. if ($handler) {
  495. $data[$base_table]['table']['join'][$left_table]['handler'] = $handler;
  496. }
  497. }
  498. }
  499. return $data;
  500. }
  501. /**
  502. *
  503. * @ingroup tripal_views
  504. */
  505. function tripal_views_views_data_alter(&$data) {
  506. // D7 TODO: Check DBTNG changes work
  507. $tvi_query = db_query('SELECT * FROM {tripal_views}');
  508. // iterate through the views that we manage
  509. foreach ($tvi_query as $tvi_row) {
  510. //ids we'll use for queries
  511. $mview_id = $tvi_row->mview_id;
  512. $setup_id = $tvi_row->setup_id;
  513. // iterate through the columns and alter the existing data array for
  514. // joins to other tables
  515. // D7 TODO: Check DBTNG changes work
  516. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = :setup";
  517. $joins = db_query($sql, array(':setup' => $setup_id));
  518. foreach ($joins as $join) {
  519. $left_table = $join->left_table;
  520. $left_field = $join->left_field;
  521. $base_field = $join->base_field;
  522. $base_table = $join->base_table;
  523. // add the recipricol join entries for each column
  524. if (array_key_exists($left_table, $data) and $base_table != 'node') {
  525. $data[$left_table]['table']['join'][$base_table] = array(
  526. 'left_field' => $base_field,
  527. 'field' => $left_field,
  528. );
  529. }
  530. }
  531. // add in joins to the node tables if the Chado schema is local
  532. if (tripal_core_chado_schema_exists()) {
  533. $base_table = $tvi_row->table_name;
  534. $linker_table = 'chado_' . $base_table;
  535. // if a node linking table exists then add in the joins
  536. if (db_table_exists($linker_table)) {
  537. $data['node']['table']['join'][$linker_table] = array(
  538. 'left_field' => 'nid',
  539. 'field' => 'nid',
  540. );
  541. $data['node']['table']['join'][$base_table] = array(
  542. 'left_table' => $linker_table,
  543. 'left_field' => 'nid',
  544. 'field' => 'nid',
  545. );
  546. }
  547. }
  548. }
  549. return $data;
  550. }
  551. /**
  552. * Implementation of hook_views_plugins().
  553. */
  554. function tripal_views_views_plugins() {
  555. $tc_path = drupal_get_path('module', 'tripal_views');
  556. $style_defaults = array(
  557. 'path' => $tc_path . '/views_data_export/plugins',
  558. 'parent' => 'views_data_export',
  559. 'theme' => 'views_data_export',
  560. 'theme path' => $tc_path . '/views_data_export/theme',
  561. 'theme file' => 'tripal_views_data_export.theme.inc',
  562. 'uses row plugin' => FALSE,
  563. 'uses fields' => TRUE,
  564. 'uses options' => TRUE,
  565. 'type' => 'data_export',
  566. );
  567. // add FASTA format as a new style for the existing views_export_data Display
  568. return array(
  569. 'style' => array(
  570. 'views_data_export_fasta' => array(
  571. 'title' => t('FASTA file'),
  572. 'help' => t('Display results in FASTA format. All fields in results are on the definition line while the feature.residues field must be present .'),
  573. 'handler' => 'tripal_views_plugin_style_export_fasta',
  574. // Views Data Export element that will be used to set additional headers when serving the feed.
  575. 'export headers' => array('Content-type: text/plain; charset=utf-8'),
  576. // Views Data Export element mostly used for creating some additional classes and template names.
  577. 'export feed type' => 'fasta',
  578. 'export feed text' => 'FASTA',
  579. 'export feed file' => '%view.fna',
  580. 'export feed icon' => $tc_path . '/views_data_export/images/fasta.png',
  581. 'additional themes' => array(
  582. 'views_data_export_fasta_header' => 'style',
  583. 'views_data_export_fasta_body' => 'style',
  584. 'views_data_export_fasta_footer' => 'style',
  585. ),
  586. 'additional themes base' => 'views_data_export_fasta',
  587. ) + $style_defaults,
  588. ),
  589. );
  590. }
  591. /**
  592. * Implementation of hook_views_pre_view().
  593. */
  594. function tripal_views_views_pre_view(&$view, &$display_id, &$args) {
  595. // merge the $_GET and $_POST into the $_GET. This is because
  596. // Views and Views Data Export modules only uses the $_GET variable but
  597. // file uploads require $_POST. We need to make sure these two modules
  598. // have access to everything needed for this view to work properlys
  599. $_GET = array_merge($_GET, $_POST);
  600. }
  601. /**
  602. * Implementation of hook_views_pre_build().
  603. */
  604. /* function tripal_views_views_pre_render(&$view, &$display_id, &$args){
  605. // we want to add to the bottom of the views the form for downloading
  606. // results in other formats (e.g. Excel, FASTA, CSV, etc.). The Views Data
  607. // Export module provides small images at the bottom, but we want to provide
  608. // a more intutitive interface for getting different file formats
  609. $form = drupal_get_form('tripal_views_data_export_download_form',$view,$display_id,$args);
  610. $view->attachment_after = $form;
  611. }*/
  612. /*
  613. *
  614. function tripal_views_views_query_alter(&$view, &$query){
  615. // iterate through the tables and see if they are chado tables.
  616. // if they are then prefix them with a "chado." prefix
  617. dpm($query);
  618. $tables = $query->tables;
  619. foreach ($tables as $base => $subtables) {
  620. $desc = tripal_core_get_chado_table_schema($base);
  621. if ($desc) {
  622. $query->tables["chado." . $base] = $subtables;
  623. unset($query->tables[$base]);
  624. $base = "chado." . $base;
  625. }
  626. foreach ($subtables as $subtable => $values) {
  627. $desc = tripal_core_get_chado_table_schema($subtable);
  628. if ($desc) {
  629. $query->tables[$base]["chado." . $subtable] = $values;
  630. unset($query->tables[$base][$subtable]);
  631. }
  632. }
  633. }
  634. dpm($query);
  635. } */
  636. /**
  637. * Implementation of hook_views_pre_execute().
  638. */
  639. function tripal_views_views_pre_execute(&$view) {
  640. // if the base table is a chado table then we want to set the
  641. // search path so it can find all of the tables.
  642. // this will break views that use tables that have the same name
  643. // as chado tables (e.g. contact).
  644. $desc = tripal_core_get_chado_table_schema($view->base_table);
  645. if ($desc) {
  646. tripal_db_set_chado_search_path('chado');
  647. }
  648. }
  649. /**
  650. * Implementation of hook_views_post_render().
  651. */
  652. function tripal_views_views_post_render(&$view, &$output, &$cache) {
  653. // if the base table and the query is completed we want to set
  654. // the search path back to the default.
  655. $desc = tripal_core_get_chado_table_schema($view->base_table);
  656. if ($desc) {
  657. tripal_db_set_default_search_path();
  658. }
  659. }
  660. /**
  661. * the Download Views data export form
  662. */
  663. function tripal_views_data_export_download_form(&$form_state, $view, $display_id, $args) {
  664. $form = array();
  665. $urls = array();
  666. // get any export_data_export displays
  667. $displays = $view->display;
  668. $options = array();
  669. $default = '';
  670. $current_display = $view->current_display;
  671. foreach ($displays as $name => $display) {
  672. if (preg_match("/^views_data_export/", $name)) {
  673. // only add this display to the form if it is attached
  674. $display_options = $display->display_options;
  675. if (strcmp($display_options['displays'][$current_display], $current_display) != 0) {
  676. continue;
  677. }
  678. // set the first item as default
  679. if (!$default) {
  680. $default = $display->id;
  681. }
  682. $path = $display->display_options['path'];
  683. $query = $view->get_exposed_input(); // retrieves elements in $_GET array
  684. $urls[$display->id]['path'] = $path;
  685. $urls[$display->id]['query'] = $query;
  686. // add the new item to the options array
  687. $options[$display->id] = $display->display_title;
  688. }
  689. }
  690. // only generate the form if we have views_data_export displays attached
  691. // to this view
  692. if (count($options) > 0) {
  693. $form_state['storage']['urls'] = $urls;
  694. $form['#cache'] = TRUE;
  695. // we want the form to redirect to a new window
  696. $form['#attributes']['target'] = "_blank";
  697. // now build the form elements
  698. $form['downloads'] = array(
  699. '#type' => 'fieldset',
  700. '#title' => 'Download Results',
  701. '#collapsible' => TRUE,
  702. '#collapsed' => FALSE
  703. );
  704. $form['downloads']['file_type'] = array(
  705. '#title' => t('File format'),
  706. '#type' => 'radios',
  707. '#options' => $options,
  708. '#required' => TRUE,
  709. '#default_value' => $default,
  710. '#description' => t('Please select a file format to download'),
  711. );
  712. $form['downloads']['submit'] = array(
  713. '#value' => t('Download Results'),
  714. '#type' => 'submit',
  715. );
  716. }
  717. return $form;
  718. }
  719. /**
  720. * Submit for the Download Views data export form
  721. */
  722. function tripal_views_data_export_download_form_submit($form, &$form_state) {
  723. $urls = $form_state['storage']['urls'];
  724. $display_id = $form_state['values']['file_type'];
  725. drupal_goto($urls[$display_id]['path'], $urls[$display_id]['query']);
  726. }