tripal_views_setup.admin.inc 16 KB

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