tripal_views.views.inc 27 KB

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