tripal_views.views.inc 28 KB

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