tripal_views.views.inc 28 KB

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