tripal_views_setup.admin.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. $form['row_name'] = array(
  60. '#title' => t('Name'),
  61. '#type' => 'textfield',
  62. '#size' => 60,
  63. '#maxlength' => 128,
  64. '#description' => 'Name of the Views Setup',
  65. '#required' => TRUE,
  66. );
  67. $form['row_description'] = array(
  68. '#title' => t('Description'),
  69. '#type' => 'textfield',
  70. '#size' => 60,
  71. '#maxlength' => 255,
  72. '#description' => 'Briefly describe in which view this will be used',
  73. '#required' => TRUE,
  74. );
  75. $mview_query = db_query("SELECT mview_id,name FROM {tripal_mviews} ORDER BY name;");
  76. $mview_options = array();
  77. $mview_options['0'] = 'Select';
  78. while ($mview_option = db_fetch_array($mview_query)){
  79. $mview_options[$mview_option['mview_id']] = $mview_option['name'];
  80. }
  81. $form['mview_id'] = array(
  82. '#title' => t('Materialized View'),
  83. '#type' => 'select',
  84. '#options' => $mview_options,
  85. '#description' => 'Which materialized view to use.',
  86. '#required' => TRUE,
  87. '#ahah' => array(
  88. 'path' => 'admin/tripal/tripal_views_setup/ajax/mview_cols',
  89. 'wrapper' => 'table-rows-div',
  90. 'effect' => 'fade',
  91. 'event' => 'change',
  92. 'method' => 'replace',
  93. ),
  94. );
  95. // ignore this for now... we'll come back to it later -- spf
  96. // $form['row_base_table_name'] = array(
  97. // '#title' => t('Base Table Name'),
  98. // '#type' => 'select',
  99. // // '#options' => array('stub'),
  100. // '#options' => tripal_core_get_chado_tables(),
  101. // '#description' => 'Select which chado table to use for this view.',
  102. // '#required' => TRUE,
  103. // );
  104. $form['view_setup_table'] = array(
  105. '#type' => 'item',
  106. '#prefix' => '<div id="table-rows-div">',
  107. '#suffix' => '</div>',
  108. );
  109. if($form_state['values']['mview_id']){
  110. $mview_id = $form_state['values']['mview_id'];
  111. $form['view_setup_table'] = array(
  112. '#type' => 'fieldset',
  113. '#title' => 'Table Fields Setup',
  114. '#prefix' => '<div id="table-rows-div">',
  115. '#suffix' => '</div>',
  116. );
  117. // get the columns in this materialized view. They are separated by commas
  118. // where the first word is the column name and the rest is the type
  119. $sql = "SELECT mv_specs FROM {tripal_mviews} WHERE mview_id = $mview_id";
  120. $mview = db_fetch_object(db_query($sql));
  121. $columns = explode(",",$mview->mv_specs);
  122. $i=1;
  123. $chado_tables = tripal_core_get_chado_tables();
  124. $chado_tables = array_merge(array('select or leave blank',), $chado_tables);
  125. $handlers = array();
  126. $form['view_setup_table']["fields_headers"] = array(
  127. '#type' => 'markup',
  128. '#value' => "<div class=\"field-headers\">".
  129. "<div class=\"column-id\">Field Name and Type</div>".
  130. "<div class=\"fields-column-join\">Join Table</div>".
  131. "<div class=\"fields-column-join-column\">Join Column</div>".
  132. "<div class=\"fields-column-handler\">Handler</div></div>",
  133. );
  134. foreach ($columns as $column){
  135. $column = trim($column); // trim trailing and leading spaces
  136. preg_match("/^(.*?)\ (.*?)$/",$column,$matches);
  137. $column_name = $matches[1];
  138. $column_type = $matches[2];
  139. // first print the field name
  140. $form['view_setup_table']["fields_start_$mview_id-$i"] = array(
  141. '#type' => 'markup',
  142. '#value' => "<div class=\"fields-new-row\">",
  143. );
  144. $form['view_setup_table']["fields_column_name_$mview_id-$i"] = array(
  145. '#type' => 'markup',
  146. '#attributes' => array('class' => 'fields-column-name'),
  147. '#value' => "<div class=\"column-id\"><span class=\"column-name\">$column_name</span>".
  148. "<br><span class=\"column-type\">$column_type</span></div>",
  149. );
  150. // second print the table join drop down
  151. $table = $form_state['values']["fields_column_join_$mview_id_$i"];
  152. $form['view_setup_table']["fields_column_join_$mview_id-$i"] = array(
  153. '#type' => 'select',
  154. '#prefix' => "<div class=\"fields-column-join\">",
  155. '#suffix' => "</div>",
  156. '#options' => $chado_tables,
  157. '#required' => FALSE,
  158. '#default_value' => $table,
  159. '#ahah' => array(
  160. 'path' => 'admin/tripal/tripal_views_setup/ajax/field_col_join',
  161. 'wrapper' => "fields-column-join-column-$mview_id-$i",
  162. 'effect' => 'fade',
  163. 'event' => 'change',
  164. 'method' => 'replace',
  165. ),
  166. );
  167. if($table){
  168. $table_desc = module_invoke_all('chado_'.$table.'_schema');
  169. $columns = array_keys($table_desc['fields']);
  170. } else {
  171. $columns = array();
  172. }
  173. $form['view_setup_table']["fields_column_join_column_$mview_id-$i"] = array(
  174. '#type' => 'select',
  175. '#prefix' => "<div id=\"fields-column-join-column-$mview_id-$i\" class=\"fields-column-join-column\">",
  176. '#suffix' => "</div>",
  177. '#options' => $columns,
  178. '#required' => FALSE,
  179. );
  180. $form['view_setup_table']["fields_column_handler_$mview_id-$i"] = array(
  181. '#type' => 'select',
  182. '#prefix' => "<div class=\"fields-column-handler\">",
  183. '#suffix' => "</div>",
  184. '#options' => $handlers,
  185. '#required' => FALSE,
  186. );
  187. $form['view_setup_table']["fields_end_$i"] = array(
  188. '#type' => 'markup',
  189. '#value' => "</div>",
  190. );
  191. $i++;
  192. }
  193. $form['view_setup_table']['submit'] = array(
  194. '#type' => 'submit',
  195. '#value' => 'Create',
  196. );
  197. $form['row_counter'] = array(
  198. '#type' => 'hidden',
  199. '#value' => $i,
  200. );
  201. } // end if($form_state['values']['mview_id'])
  202. return $form;
  203. }
  204. /**
  205. *
  206. * @ingroup tripal_view_setup
  207. */
  208. function tripal_view_setup_ajax_mview_cols(){
  209. // regenerate the form
  210. $form = tripal_views_setup_ajax_rebuild_form($form_state, $args, $_POST);
  211. // because the AJAX sent a post of the mview_id, the form now has in it
  212. // form components for the rows for the materialized view. We want to add
  213. // these components to the page, but we don't need to add the existing form
  214. // elements because they are already there.
  215. $view_setup_table_form = $form['view_setup_table'];
  216. unset($view_setup_table_form['#prefix'], $view_setup_table_form['#suffix']);
  217. $output = theme('status_message') . drupal_render($view_setup_table_form);
  218. // Final rendering callback.
  219. drupal_json(array('status' => TRUE, 'data' => $output));
  220. }
  221. /**
  222. *
  223. * @ingroup tripal_view_setup
  224. */
  225. function tripal_view_setup_ajax_field_col_join(){
  226. // regenerate the form
  227. $form = tripal_views_setup_ajax_rebuild_form($form_state, $args, $_POST);
  228. // get the information we need to pull out table select box that was changed
  229. $mview_id = $form_state['values']['mview_id'];
  230. // $row = $form_state['values']['mview_id'];
  231. // $row_select_box = $form['view_setup_table']["fields-column-join-column-$mview_id-1"];
  232. // unset($row_select_box['#prefix'], $row_select_box['#suffix']);
  233. // $output = theme('status_message') . drupal_render($row_select_box);
  234. // Final rendering callback.
  235. // drupal_json(array('status' => TRUE, 'data' => $output));
  236. drupal_json(array('status' => TRUE, 'data' => 'test'));
  237. }
  238. /**
  239. *
  240. * @ingroup tripal_view_setup
  241. */
  242. function tripal_views_setup_ajax_rebuild_form(&$form_state, &$args, &$_POST){
  243. // Retrieve the form from the cache
  244. $form_state = array('storage' => NULL);
  245. $form_build_id = $_POST['form_build_id'];
  246. $form = form_get_cache($form_build_id, $form_state);
  247. // Preparing to process the form
  248. $args = $form['#parameters'];
  249. $form_id = array_shift($args);
  250. $form_state['post'] = $form['#post'] = $_POST;
  251. $form['#programmed'] = $form['#redirect'] = FALSE;
  252. // Sets the form_state so that the validate and submit handlers can tell
  253. // when the form is submitted via AHAH
  254. $form_state['ahah_submission'] = TRUE;
  255. // Process the form with drupal_process_form. This function calls the submit
  256. // handlers, which put whatever was worthy of keeping into $form_state.
  257. drupal_process_form($form_id, $form, $form_state);
  258. // You call drupal_rebuild_form which destroys $_POST.
  259. // The form generator function is called and creates the form again but since
  260. // it knows to use $form_state, the form will be different.
  261. // The new form gets cached and processed again, but because $_POST is
  262. // destroyed, the submit handlers will not be called again.
  263. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  264. return $form;
  265. }
  266. /**
  267. *
  268. * @ingroup tripal_view_setup
  269. */
  270. // function tripal_views_setup_fields(&$form_state=NULL, $mview_id = NULL){
  271. // if(!$mview_id){
  272. // return;
  273. // }
  274. // // get the columns in this materialized view. They are separated by commas
  275. // // where the first word is the column name and the rest is the type
  276. // $sql = "SELECT mv_specs FROM {tripal_mviews} WHERE mview_id = $mview_id";
  277. // $mview = db_fetch_object(db_query($sql));
  278. // $columns = explode(",",$mview->mv_specs);
  279. // $i=1;
  280. // $chado_tables = tripal_core_get_chado_tables();
  281. // $handlers = array();
  282. // $form["fields_headers"] = array(
  283. // '#type' => 'markup',
  284. // '#value' => "<div class=\"field-headers\">".
  285. // "<div class=\"column-id\">Field Name and Type</div>".
  286. // "<div class=\"fields-column-join\">Join Table</div>".
  287. // "<div class=\"fields-column-join-column\">Join Column</div>".
  288. // "<div class=\"fields-column-handler\">Handler</div></div>",
  289. // );
  290. // foreach ($columns as $column){
  291. // $column = trim($column); // trim trailing and leading spaces
  292. // preg_match("/^(.*?)\ (.*?)$/",$column,$matches);
  293. // $column_name = $matches[1];
  294. // $column_type = $matches[2];
  295. // // first print the field name
  296. // $form["fields_start_$i"] = array(
  297. // '#type' => 'markup',
  298. // '#value' => "<div class=\"fields-new-row\">",
  299. // );
  300. // $form["fields_column_name_$i"] = array(
  301. // '#type' => 'markup',
  302. // '#attributes' => array('class' => 'fields-column-name'),
  303. // '#value' => "<div class=\"column-id\"><span class=\"column-name\">$column_name</span>".
  304. // "<br><span class=\"column-type\">$column_type</span></div>",
  305. // );
  306. // // second print the table join drop down
  307. // $chado_tables = array_merge(array(NULL,), $chado_tables);
  308. // $form["fields_column_join_$i"] = array(
  309. // '#type' => 'select',
  310. // '#prefix' => "<div class=\"fields-column-join\">",
  311. // '#suffix' => "</div>",
  312. // '#options' => $chado_tables,
  313. // '#required' => FALSE,
  314. // '#ahah' => array(
  315. // 'path' => 'admin/tripal/tripal_views_setup/ajax/field_col_join',
  316. // 'wrapper' => "fields-column-join-column-$i",
  317. // 'effect' => 'fade',
  318. // 'event' => 'change',
  319. // 'method' => 'replace',
  320. // ),
  321. // );
  322. // $form["fields_column_join_column_$i"] = array(
  323. // '#type' => 'select',
  324. // '#prefix' => "<div id=\"fields-column-join-column-$i\" class=\"fields-column-join-column\">",
  325. // '#suffix' => "</div>",
  326. // '#options' => array(),
  327. // '#required' => FALSE,
  328. // );
  329. // $form["fields_column_handler_$i"] = array(
  330. // '#type' => 'select',
  331. // '#prefix' => "<div class=\"fields-column-handler\">",
  332. // '#suffix' => "</div>",
  333. // '#options' => $handlers,
  334. // '#required' => FALSE,
  335. // );
  336. // $form["fields_end_$i"] = array(
  337. // '#type' => 'markup',
  338. // '#value' => "</div>",
  339. // );
  340. // $i++;
  341. // }
  342. // $form['row_counter'] = array(
  343. // '#type' => 'hidden',
  344. // '#value' => $i,
  345. // );
  346. // $form['submit'] = array(
  347. // '#type' => 'submit',
  348. // '#value' => 'Create',
  349. // );
  350. // return $form;
  351. // }
  352. // function tripal_views_setup_fields_form_submit($form, &$form_state){
  353. // ----($form,'formfield');
  354. // ----($form_state, 'formstatefield');
  355. // }
  356. /**
  357. *
  358. * @ingroup tripal_view_setup
  359. */
  360. function tripal_views_setup_new_setup_form_submit($form, &$form_state){
  361. dpm($form, 'form on submit');
  362. dpm($form_state, 'forms_state on submit');
  363. }