tripal_views.views.inc 27 KB

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