tripal_views_setup.admin.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. *
  4. * @ingroup tripal_view_setup
  5. */
  6. function tripal_views_setup_admin_form(){
  7. $form = array();
  8. $form['#theme'] = 'tripal';
  9. $query_results = db_query('SELECT * FROM public.tripal_views_setup;');
  10. $header = array('Setup ID', 'Name', 'Materialized View ID', 'Base Table Name', 'Description');
  11. $rows = array();
  12. $results = array();
  13. while($result = db_fetch_object($query_results)){
  14. $rows[] = array($result->setup_id, $result->name, $result->mview_id, $result->base_table_name, $result->description,);
  15. $results[] = $result;
  16. }
  17. $options = array();
  18. foreach ($results as $key => $value) {
  19. if(!empty($value))
  20. $options[] = $value->setup_id;// . ' | ' . $value->name . ' | ' . $value->mview_id . ' | ' . $value->base_table_name;
  21. }
  22. $form['existing_rows'] = array(
  23. '#type' => 'select',
  24. '#options' => $options,
  25. '#description' => '<strong>Select a View Setup to delete from the database.</strong>',
  26. '#prefix' => theme('table', $header, $rows),
  27. );
  28. $form['submit'] = array(
  29. '#type' => 'submit',
  30. '#value' => t('Remove'),
  31. );
  32. $form['cancel'] = array(
  33. '#type' => 'markup',
  34. '#value' => l(t('Cancel '), 'admin/tripal/'),
  35. );
  36. $form['new'] = array(
  37. '#type' => 'markup',
  38. '#value' => l(t(' New'), 'admin/tripal/tripal_views_setup_new'),
  39. );
  40. return $form;
  41. }
  42. /**
  43. *
  44. * @ingroup tripal_view_setup
  45. */
  46. function tripal_views_setup_admin_form_submit($form, &$form_state){
  47. $value = $form['existing_rows']['#options'][$form_state['values']['existing_rows']];
  48. db_query("DELETE FROM public.tripal_views_setup WHERE setup_id = $value;");
  49. db_query("DELETE FROM public.tripal_views_handlers WHERE setup_id = $value;");
  50. db_query("DELETE FROM public.tripal_mviews_join WHERE setup_id = $value;");
  51. }
  52. /**
  53. *
  54. * @ingroup tripal_view_setup
  55. */
  56. function tripal_views_setup_new_setup_form(&$form_state){
  57. $form = array();
  58. $form['#cache'] = TRUE;
  59. //ahah_helper requires this to register the form with it's module
  60. ahah_helper_register($form, $form_state);
  61. $form['row_name'] = array(
  62. '#title' => t('Name'),
  63. '#type' => 'textfield',
  64. '#size' => 60,
  65. '#maxlength' => 128,
  66. '#description' => 'Name of the Views Setup',
  67. '#required' => TRUE,
  68. );
  69. $form['row_description'] = array(
  70. '#title' => t('Description'),
  71. '#type' => 'textfield',
  72. '#size' => 60,
  73. '#maxlength' => 255,
  74. '#description' => 'Briefly describe in which view this will be used',
  75. '#required' => TRUE,
  76. );
  77. $mview_query = db_query("SELECT mview_id,name FROM {tripal_mviews} ORDER BY name;");
  78. $mview_options = array();
  79. $mview_options['0'] = 'Select';
  80. while ($mview_option = db_fetch_array($mview_query)){
  81. $mview_options[$mview_option['mview_id']] = $mview_option['name'];
  82. }
  83. if (!isset($form_state['storage']['mview_id'])){
  84. $mview_default_value = '0';
  85. }
  86. else{
  87. $mview_default_value = $form_state['storage']['mview_id'];
  88. }
  89. dpm($mview_default_value, 'mview default value');
  90. dpm($mview_options, 'mview options');
  91. $form['mview_id'] = array(
  92. '#type' => 'fieldset',
  93. '#title' => t('Which materialized view to use'),
  94. '#prefix' => '<div id="mview-id-wrapper">', // This is our wrapper div.
  95. '#suffix' => '</div>',
  96. '#tree' => TRUE, // Don't forget to set #tree!
  97. );
  98. $form['mview_id']['usage'] = array(
  99. '#title' => t('Materialized View'),
  100. '#type' => 'select',
  101. '#options' => $mview_options,
  102. '#description' => 'Which materialized view to use.',
  103. '#required' => TRUE,
  104. '#default_value' => $mview_default_value,
  105. '#ahah' => array(
  106. 'path' => ahah_helper_path(array('mview')),
  107. 'wrapper' => 'mview-id-wrapper',
  108. 'effect' => 'fade',
  109. 'event' => 'change',
  110. 'method' => 'replace',
  111. ),
  112. );
  113. $form['mview_id']['update_usage'] = array(
  114. '#type' => 'submit',
  115. '#value' => t('Update usage'),
  116. // Note that we can simply use the generic submit callback provided by the
  117. // ahah_helper module here!
  118. // All it does, is set $form_state['rebuild'] = TRUE.
  119. '#submit' => array('ahah_helper_generic_submit'),
  120. // We set the 'no-js' class, which means this submit button will be hidden
  121. // automatically by Drupal if JS is enabled.
  122. '#attributes' => array('class' => 'no-js'),
  123. );
  124. // ignore this for now... we'll come back to it later -- spf
  125. // $form['row_base_table_name'] = array(
  126. // '#title' => t('Base Table Name'),
  127. // '#type' => 'select',
  128. // // '#options' => array('stub'),
  129. // '#options' => tripal_core_get_chado_tables(),
  130. // '#description' => 'Select which chado table to use for this view.',
  131. // '#required' => TRUE,
  132. // );
  133. dpm(strcmp($mview_default_value['usage'], '0'), 'second mview');
  134. if (strcmp($mview_default_value['usage'], '0') != 0){
  135. $mview_id = $mview_default_value['usage'];
  136. dpm($mview_id, 'mviewid is');
  137. $form['mview_id'] = array(
  138. '#type' => 'fieldset',
  139. '#title' => 'Table Fields Setup',
  140. '#prefix' => '<div id="fieldset-table-rows-wrapper">',
  141. '#suffix' => '</div>',
  142. );
  143. // get the columns in this materialized view. They are separated by commas
  144. // where the first word is the column name and the rest is the type
  145. $sql = "SELECT mv_specs FROM {tripal_mviews} WHERE mview_id = $mview_id";
  146. $mview = db_fetch_object(db_query($sql));
  147. $columns = explode(",",$mview->mv_specs);
  148. $i=1;
  149. $chado_tables = tripal_core_get_chado_tables();
  150. $chado_tables = array_merge(array('select or leave blank',), $chado_tables);
  151. $handlers = array();
  152. dpm($chado_tables, 'chado tables');
  153. $form['mview_id']["fields_headers"] = array(
  154. '#type' => 'markup',
  155. '#value' => "<div class=\"field-headers\">".
  156. "<div class=\"column-id\">Field Name and Type</div>".
  157. "<div class=\"fields-column-join\">Join Table</div>".
  158. "<div class=\"fields-column-join-column\">Join Column</div>".
  159. "<div class=\"fields-column-handler\">Handler</div></div>",
  160. );
  161. dpm($form,'form here');
  162. foreach ($columns as $column){
  163. dpm($i, 'colmuns');
  164. $column = trim($column); // trim trailing and leading spaces
  165. preg_match("/^(.*?)\ (.*?)$/",$column,$matches);
  166. $column_name = $matches[1];
  167. $column_type = $matches[2];
  168. // first print the field name
  169. $form['mview_id']["fields_start_$mview_id-$i"] = array(
  170. '#type' => 'markup',
  171. '#value' => "<div class=\"fields-new-row\">",
  172. );
  173. dpm($form,'form here 2');
  174. $form['mview_id']["fields_column_name_$mview_id-$i"] = array(
  175. '#type' => 'markup',
  176. '#attributes' => array('class' => 'fields-column-name'),
  177. '#value' => "<div class=\"column-id\"><span class=\"column-name\">$column_name</span>".
  178. "<br><span class=\"column-type\">$column_type</span></div>",
  179. );
  180. // second print the table join drop down
  181. // $table = $form_state['values']["fields_column_join_$mview_id_$i"];
  182. $form['mview_id']["fields_column_join_$mview_id-$i"] = array(
  183. '#type' => 'select',
  184. '#prefix' => "<div class=\"fields-column-join\">",
  185. '#suffix' => "</div>",
  186. '#options' => $chado_tables,
  187. '#required' => FALSE,
  188. '#default_value' => $table,
  189. // '#ahah' => array(
  190. // 'path' => "admin/tripal/tripal_views_setup/ajax/field_col_join_$i",
  191. // 'wrapper' => "fields-column-join-column-$mview_id-$i",
  192. // 'effect' => 'fade',
  193. // 'event' => 'change',
  194. // 'method' => 'replace',
  195. // ),
  196. );
  197. if($table){
  198. $table_desc = module_invoke_all('chado_'.$table.'_schema');
  199. $columns = array_keys($table_desc['fields']);
  200. } else {
  201. $columns = array();
  202. }
  203. $form['mview_id']["fields_column_join_column_$mview_id-$i"] = array(
  204. '#type' => 'select',
  205. '#prefix' => "<div id=\"fields-column-join-column-$mview_id-$i\" class=\"fields-column-join-column\">",
  206. '#suffix' => "</div>",
  207. '#options' => $columns,
  208. '#required' => FALSE,
  209. );
  210. $form['mview_id']["fields_column_handler_$mview_id-$i"] = array(
  211. '#type' => 'select',
  212. '#prefix' => "<div class=\"fields-column-handler\">",
  213. '#suffix' => "</div>",
  214. '#options' => $handlers,
  215. '#required' => FALSE,
  216. );
  217. $form['mview_id']["fields_end_$i"] = array(
  218. '#type' => 'markup',
  219. '#value' => "</div>",
  220. );
  221. $i++;
  222. }
  223. // $form['submit'] = array(
  224. // '#type' => 'submit',
  225. // '#value' => 'Create',
  226. // );
  227. // $form['row_counter'] = array(
  228. // '#type' => 'hidden',
  229. // '#value' => $i,
  230. // );
  231. } // end if($form_state['values']['mview_id'])
  232. // else{
  233. // $form = array(
  234. // '#type' => 'item',
  235. // '#prefix' => '<div id="table-rows-wrapper">',
  236. // '#suffix' => '</div>',
  237. // );
  238. // }
  239. $form['save'] = array(
  240. '#type' => 'submit',
  241. '#value' => t('Save'),
  242. );
  243. return $form;
  244. }
  245. /**
  246. *
  247. * @ingroup tripal_view_setup
  248. */
  249. function tripal_views_setup_new_setup_form_submit($form, &$form_state){
  250. dpm($form, 'form on submit');
  251. dpm($form_state, 'forms_state on submit');
  252. }