tripal_views_integration.admin.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * Purpose: Provide Guidance to new Tripal Admin
  4. *
  5. * @return
  6. * HTML Formatted text
  7. *
  8. * @ingroup tripal_views_integration
  9. */
  10. function tripal_views_integration_module_description_page() {
  11. $text .= '<h3>Tripal Views Integration Administrative Tools Quick Links:</h3>';
  12. $text .= "<ul>
  13. <li><a href=\"".url("admin/tripal/tripal_views_integration/list") . "\">List of integrated views</a></li>
  14. <li><a href=\"".url("admin/tripal/tripal_views_integration/new"). "\">Setup integration of a materialized view</a></li>
  15. </ul>";
  16. # <li><a href=\"".url("admin/tripal/tripal_feature/aggregate"). "\">Feature Relationship Aggegators</a></li>
  17. $text .= '<h3>Module Description:</h3>';
  18. $text .= '<p>This module provides an interface for integrating <a href="http://drupal.org/project/views">Drupal Views</a>
  19. with Tripal materialized views. This will allow site administrators to create custom queries for the materialized views
  20. and in turn provide custom content pages, custom blocks and custom search forms. The forms allow a site administrator
  21. to select a materialized view and associate other Chado tables on which the view can join. Usage of this module requires
  22. a good understanding of foreign-key relationships in Chado.
  23. </p>';
  24. $text .= '<h3>Setup Instructions:</h3>';
  25. $text .= '<p>After installation of the feature module. The following tasks should be performed
  26. <ol>
  27. <li><b>Install Drupal Views</b>: The <a href="http://drupal.org/project/views">Drupal Views</a> module
  28. must first be installed before this module can be used. If you are reading this page then you must have
  29. Drupal Views installed</li>
  30. <li><b>Set Permissions</b>: To allow access to site administrators for this module, simply
  31. <a href="'.url('admin/user/permissions').'">assign permissions</a> to the appropriate user roles for the
  32. permission type "manage tripal_views_integration". </li>
  33. </ol>
  34. </p>';
  35. $text .= '<h3>Usage Instructions:</h3>';
  36. $text .= "<p>To use this module follow these steps:
  37. <ol>
  38. <li><b>Identify or create a materialized view:</b> Using the <a href=\"".url("admin/tripal/tripal_mviews") . "\">
  39. Tripal materialized View</a> interface, identify the view you would like to integrate or create a new one.</li>
  40. <li><b>Setup the Views Integration</b>: Navigate to the <a href=\"".url("admin/tripal/tripal_views_integration/new") . "\">
  41. Tripal views integration setup page</a> to integrate the selected materialized view. Provide a user friendly name
  42. and description to help you remember the purpose for integrating the view. Next, select the view you want to integrate
  43. from the provided select box. If your materialized view has fields that can join with other Chado tables, you may
  44. provide those relationships in the provided form. Finally, if your fields require a special handler for display, you
  45. may select it from the drop down provided</li>
  46. <li><b>Create custom pages/block/search form</b>: After saving setup information from step 2 above, you will be redirected to the
  47. Drupal Views interface</a> where you can create a custom page, block or search form.</li>
  48. <li><b>Review your integrated views</b>: A page providing a
  49. <a href=\"".url("admin/tripal/tripal_views_integration/list") . "\">list of all integrated views</a> is provided. You may
  50. view this page to see all integrated views, but also to remove any unwanted integrations.</li>
  51. </ol>
  52. </p>";
  53. $text .= '<h3>Features of this Module:</h3>';
  54. $text .= '<p>This module provides the following functionality
  55. <ul>
  56. <li>A dynamic form for integration of an existing materialized view with Drupal Views. This form allows the site
  57. administrator to select an existing view and indicate Chado tables on which the fields may join.</li>
  58. </ul>
  59. </p>';
  60. return $text;
  61. }
  62. /**
  63. *
  64. * @ingroup tripal_views_integration
  65. */
  66. function tripal_views_integration_admin_form(){
  67. $form = array();
  68. $form['#theme'] = 'tripal';
  69. $query_results = db_query('SELECT * FROM public.tripal_views_integration;');
  70. $header = array('Setup ID', 'Name', 'Materialized View ID', 'Base Table Name', 'Description');
  71. $rows = array();
  72. $results = array();
  73. while($result = db_fetch_object($query_results)){
  74. $rows[] = array($result->setup_id, $result->name, $result->mview_id, $result->base_table_name, $result->description,);
  75. $results[] = $result;
  76. }
  77. $options = array();
  78. foreach ($results as $key => $value) {
  79. if(!empty($value))
  80. $options[] = $value->setup_id;// . ' | ' . $value->name . ' | ' . $value->mview_id . ' | ' . $value->base_table_name;
  81. }
  82. $form['existing_rows'] = array(
  83. '#type' => 'select',
  84. '#options' => $options,
  85. '#description' => '<strong>Select a View Setup to delete from the database.</strong>',
  86. '#prefix' => theme('table', $header, $rows),
  87. );
  88. $form['submit'] = array(
  89. '#type' => 'submit',
  90. '#value' => t('Remove'),
  91. );
  92. $form['cancel'] = array(
  93. '#type' => 'markup',
  94. '#value' => l(t('Cancel '), 'admin/tripal/'),
  95. );
  96. $form['new'] = array(
  97. '#type' => 'markup',
  98. '#value' => l(t(' New'), 'admin/tripal/tripal_views_integration/new'),
  99. );
  100. return $form;
  101. }
  102. /**
  103. *
  104. * @ingroup tripal_views_integration
  105. */
  106. function tripal_views_integration_admin_form_submit($form, &$form_state){
  107. $value = $form['existing_rows']['#options'][$form_state['values']['existing_rows']];
  108. db_query("DELETE FROM public.tripal_views_integration WHERE setup_id = $value;");
  109. db_query("DELETE FROM public.tripal_views_handlers WHERE setup_id = $value;");
  110. db_query("DELETE FROM public.tripal_mviews_join WHERE setup_id = $value;");
  111. }
  112. /**
  113. *
  114. * @ingroup tripal_views_integration
  115. */
  116. function tripal_views_integration_new_setup_form(&$form_state){
  117. $form = array();
  118. $data = array();
  119. $form['#cache'] = TRUE;
  120. //ahah_helper requires this to register the form with it's module
  121. ahah_helper_register($form, $form_state);
  122. // field for the name of the
  123. $form['row_name'] = array(
  124. '#title' => t('Name'),
  125. '#type' => 'textfield',
  126. '#size' => 60,
  127. '#maxlength' => 128,
  128. '#description' => 'Name of the Views Setup',
  129. '#required' => TRUE,
  130. );
  131. $form['row_description'] = array(
  132. '#title' => t('Description'),
  133. '#type' => 'textfield',
  134. '#size' => 60,
  135. '#maxlength' => 255,
  136. '#description' => 'Briefly describe in which view this will be used',
  137. '#required' => TRUE,
  138. );
  139. $mview_query = db_query("SELECT mview_id,name FROM {tripal_mviews} ORDER BY name;");
  140. $mview_options = array();
  141. $mview_options['0'] = 'Select';
  142. while ($mview_option = db_fetch_array($mview_query)){
  143. $mview_options[$mview_option['mview_id']] = $mview_option['name'];
  144. }
  145. $form['mview_id'] = array(
  146. '#title' => t('Materialized View'),
  147. '#type' => 'select',
  148. '#options' => $mview_options,
  149. '#description' => 'Which materialized view to use.',
  150. '#required' => TRUE,
  151. '#default_value' => $mview_default_value,
  152. '#ahah' => array(
  153. 'path' => ahah_helper_path(array('view_setup_table')),
  154. 'wrapper' => 'table-rows-div',
  155. 'effect' => 'fade',
  156. 'event' => 'change',
  157. 'method' => 'replace',
  158. ),
  159. );
  160. // ignore this for now... we'll come back to it later -- spf
  161. // $form['row_base_table_name'] = array(
  162. // '#title' => t('Base Table Name'),
  163. // '#type' => 'select',
  164. // // '#options' => array('stub'),
  165. // '#options' => tripal_core_get_chado_tables(),
  166. // '#description' => 'Select which chado table to use for this view.',
  167. // '#required' => TRUE,
  168. // );
  169. $form['view_setup_table'] = array(
  170. '#type' => 'item',
  171. '#prefix' => '<div id="table-rows-div">',
  172. '#suffix' => '</div>',
  173. );
  174. if ($form_state['storage']['mview_id']){
  175. $mview_id = $form_state['storage']['mview_id'];
  176. $form['view_setup_table'] = array(
  177. '#type' => 'fieldset',
  178. '#title' => 'Join Selection',
  179. '#prefix' => '<div id="fieldset-table-rows-wrapper">',
  180. '#suffix' => '</div>',
  181. );
  182. // get the columns in this materialized view. They are separated by commas
  183. // where the first word is the column name and the rest is the type
  184. $sql = "SELECT mv_specs FROM {tripal_mviews} WHERE mview_id = $mview_id";
  185. $mview = db_fetch_object(db_query($sql));
  186. $columns = explode(",",$mview->mv_specs);
  187. $i=1;
  188. $chado_tables = tripal_core_get_chado_tables();
  189. $chado_tables = array_merge(array('',), $chado_tables);
  190. $handlers_filters = array('', 'hello' , 'world');
  191. $handlers_fields = array('', 'foo', 'bar');
  192. $form['view_setup_table']["instructions"] = array(
  193. '#type' => 'markup',
  194. '#value' => "Select an optional table to which the fields of the materialized view can join. If a field does not need to join you may leave the selection blank.",
  195. );
  196. $form['view_setup_table']["fields_headers"] = array(
  197. '#type' => 'markup',
  198. '#value' => "<div class=\"field-headers\">".
  199. "<div class=\"column-id\">Field Name and Type</div>".
  200. "<div class=\"fields-column-join\">Join Table</div>".
  201. "<div class=\"fields-column-join-column\">Join Column</div>".
  202. "<div class=\"fields-filter-handler\">Filter Handler</div>".
  203. "<div class=\"fields-field-handler\">Field Handler</div></div>",
  204. );
  205. $data['field_types'] = array();
  206. foreach ($columns as $column){
  207. $column = trim($column); // trim trailing and leading spaces
  208. preg_match("/^(.*?)\ (.*?)$/",$column,$matches);
  209. $column_name = $matches[1];
  210. $column_type = $matches[2];
  211. $form['view_setup_table']["fields_start_$mview_id-$i"] = array(
  212. '#type' => 'markup',
  213. '#value' => "<div class=\"fields-new-row\">",
  214. );
  215. $form['view_setup_table']["fields_name_$mview_id-$i"] = array(
  216. '#type' => 'markup',
  217. '#attributes' => array('class' => 'fields-column-name'),
  218. '#value' => "<div class=\"column-id\"><span class=\"column-name\">$column_name</span>".
  219. "<br><span class=\"column-type\">$column_type</span></div>",
  220. );
  221. $data['field_types'][$column_name] = $column_type;
  222. $table = $form_state['storage']["fields_join_$mview_id-$i"];
  223. $form['view_setup_table']["fields_join_$mview_id-$i"] = array(
  224. '#type' => 'select',
  225. '#prefix' => "<div class=\"fields-column-join\">",
  226. '#suffix' => "</div>",
  227. '#options' => $chado_tables,
  228. '#required' => FALSE,
  229. '#default_value' => $table,
  230. '#ahah' => array(
  231. 'path' => ahah_helper_path(array("view_setup_table","fields_join_column_$mview_id-$i")),
  232. 'wrapper' => "fields-column-join-column-$mview_id-$i",
  233. 'effect' => 'fade',
  234. 'event' => 'change',
  235. 'method' => 'replace',
  236. ),
  237. );
  238. if($table){
  239. $table_desc = module_invoke_all('chado_'.$table.'_schema');
  240. $columns = array_keys($table_desc['fields']);
  241. } else {
  242. $columns = array();
  243. }
  244. $form['view_setup_table']["fields_join_column_$mview_id-$i"] = array(
  245. '#type' => 'select',
  246. '#prefix' => "<div id=\"fields-column-join-column-$mview_id-$i\" class=\"fields-column-join-column\">",
  247. '#suffix' => "</div>",
  248. '#options' => $columns,
  249. '#required' => FALSE,
  250. );
  251. $form['view_setup_table']["fields_filter_handler_$mview_id-$i"] = array(
  252. '#type' => 'select',
  253. '#prefix' => "<div class=\"fields-filter-handler\">",
  254. '#suffix' => "</div>",
  255. '#options' => $handlers_filters,
  256. '#required' => FALSE,
  257. );
  258. $form['view_setup_table']["fields_field_handler_$mview_id-$i"] = array(
  259. '#type' => 'select',
  260. '#prefix' => "<div class=\"fields-field-handler\">",
  261. '#suffix' => "</div>",
  262. '#options' => $handlers_fields,
  263. '#required' => FALSE,
  264. );
  265. $form['view_setup_table']["fields_end_$i"] = array(
  266. '#type' => 'markup',
  267. '#value' => "</div>",
  268. );
  269. $i++;
  270. }
  271. $form['view_setup_table']['save'] = array(
  272. '#type' => 'submit',
  273. '#value' => t('Save'),
  274. );
  275. $data['row_count'] = $i - 1;
  276. }
  277. //use this to put values into $form_state['values']
  278. $form['data'] = array();
  279. //need to find out if storing $form['data'][$key]['#value'] = $value <- is an issue
  280. //since it will give me errors if i try to stare an array instead of $value
  281. //and yet $value can be an array ie "field_types"
  282. foreach ($data as $key => $value) {
  283. $form['data'][$key] = array(
  284. '#type' => 'hidden',
  285. '#value' => $value,
  286. );
  287. }
  288. return $form;
  289. }
  290. /**
  291. *
  292. * @ingroup tripal_views_integration
  293. */
  294. function tripal_views_integration_new_setup_form_validate($form, &$form_state){
  295. $name_array = explode(" ", $form_state['values']['row_name']);
  296. if(count($name_array) > 1){
  297. form_set_error($form_state['values']['row_name'], 'Name must be ONE word only.');
  298. }
  299. //TODO: write validation function for this new form
  300. }
  301. /**
  302. *
  303. * @ingroup tripal_views_integration
  304. */
  305. function tripal_views_integration_new_setup_form_submit($form, &$form_state){
  306. $name = $form_state['values']['row_name'];
  307. $mview_id = $form_state['values']['mview_id'];
  308. $tripal_views_integration_record = array(
  309. 'mview_id' => $mview_id,
  310. 'name' => $name,
  311. 'description' => $form_state['values']['row_description'],
  312. );
  313. drupal_write_record('tripal_views_integration', $tripal_views_integration_record);
  314. $i = 1;
  315. foreach ($form_state['values']['field_types'] as $key => $value){
  316. $mview_join_record = array(
  317. 'setup_id' => $tripal_views_integration_record['setup_id'],
  318. 'view_table' => $form['mview_id']['#options'][$form_state['values']['mview_id']],
  319. 'view_column' => $key,
  320. 'chado_table_join' => $form_state['values']["fields_join_$mview_id-$i"],
  321. 'chado_column' => $form['view_setup_table']["fields_join_column_$mview_id-$i"]['#options'][$form_state['values']["fields_join_column_$mview_id-$i"]],
  322. );
  323. drupal_write_record('tripal_mviews_join', $mview_join_record);
  324. $handlers_record = array(
  325. 'setup_id' => $tripal_views_integration_record['setup_id'],
  326. 'column_name' => $key,//TODO: should we change this to an mview_join_id from tripal_view_join?
  327. 'handler_filter' => $form['view_setup_table']["fields_filter_handler_$mview_id-$i"]['#options'][$form_state['values']["fields_filter_handler_$mview_id-$i"]],
  328. 'handler_field' => $form['view_setup_table']["fields_field_handler_$mview_id-$i"]['#options'][$form_state['values']["fields_field_handler_$mview_id-$i"]],
  329. );
  330. drupal_write_record('tripal_views_handlers', $handlers_record);
  331. $i++;
  332. }
  333. }