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. // each ahah element must have it's own matching menu item, maybe this is why our created ahahs don't work
  150. // for ($i = 0; $i < $howmany; $i++) {
  151. $item = array(
  152. 'link_path' => "admin/tripal/tripal_views_setup/ajax/field_col_join-$i",
  153. 'title' => 'Get Join Columns',
  154. 'page callback' => 'tripal_view_setup_ajax_field_col_join',
  155. 'type' => MENU_CALLBACK,
  156. 'access arguments' => array('access administration pages'),
  157. );
  158. // }
  159. menu_link_save($item);// saves the menu item
  160. $column = trim($column); // trim trailing and leading spaces
  161. preg_match("/^(.*?)\ (.*?)$/",$column,$matches);
  162. $column_name = $matches[1];
  163. $column_type = $matches[2];
  164. // first print the field name
  165. $form['view_setup_table']["fields_start_$mview_id-$i"] = array(
  166. '#type' => 'markup',
  167. '#value' => "<div class=\"fields-new-row\">",
  168. );
  169. $form['view_setup_table']["fields_column_name_$mview_id-$i"] = array(
  170. '#type' => 'markup',
  171. '#attributes' => array('class' => 'fields-column-name'),
  172. '#value' => "<div class=\"column-id\"><span class=\"column-name\">$column_name</span>".
  173. "<br><span class=\"column-type\">$column_type</span></div>",
  174. );
  175. // second print the table join drop down
  176. $table = $form_state['values']["fields_column_join_$mview_id_$i"];
  177. $form['view_setup_table']["fields_column_join_$mview_id-$i"] = array(
  178. '#type' => 'select',
  179. '#prefix' => "<div class=\"fields-column-join\">",
  180. '#suffix' => "</div>",
  181. '#options' => $chado_tables,
  182. '#required' => FALSE,
  183. '#default_value' => $table,
  184. '#ahah' => array(
  185. 'path' => "admin/tripal/tripal_views_setup/ajax/field_col_join_$i",
  186. 'wrapper' => "fields-column-join-column-$mview_id-$i",
  187. 'effect' => 'fade',
  188. 'event' => 'change',
  189. 'method' => 'replace',
  190. ),
  191. );
  192. if($table){
  193. $table_desc = module_invoke_all('chado_'.$table.'_schema');
  194. $columns = array_keys($table_desc['fields']);
  195. } else {
  196. $columns = array();
  197. }
  198. $form['view_setup_table']["fields_column_join_column_$mview_id-$i"] = array(
  199. '#type' => 'select',
  200. '#prefix' => "<div id=\"fields-column-join-column-$mview_id-$i\" class=\"fields-column-join-column\">",
  201. '#suffix' => "</div>",
  202. '#options' => $columns,
  203. '#required' => FALSE,
  204. );
  205. $form['view_setup_table']["fields_column_handler_$mview_id-$i"] = array(
  206. '#type' => 'select',
  207. '#prefix' => "<div class=\"fields-column-handler\">",
  208. '#suffix' => "</div>",
  209. '#options' => $handlers,
  210. '#required' => FALSE,
  211. );
  212. $form['view_setup_table']["fields_end_$i"] = array(
  213. '#type' => 'markup',
  214. '#value' => "</div>",
  215. );
  216. $i++;
  217. }
  218. $form['view_setup_table']['submit'] = array(
  219. '#type' => 'submit',
  220. '#value' => 'Create',
  221. );
  222. $form['row_counter'] = array(
  223. '#type' => 'hidden',
  224. '#value' => $i,
  225. );
  226. } // end if($form_state['values']['mview_id'])
  227. return $form;
  228. }
  229. /**
  230. *
  231. * @ingroup tripal_view_setup
  232. */
  233. function tripal_view_setup_ajax_mview_cols(){
  234. // regenerate the form
  235. // $form = tripal_views_setup_ajax_rebuild_form($form_state, $args, $_POST);
  236. $form = tripal_views_setup_ajax_rebuild_form();
  237. // because the AJAX sent a post of the mview_id, the form now has in it
  238. // form components for the rows for the materialized view. We want to add
  239. // these components to the page, but we don't need to add the existing form
  240. // elements because they are already there.
  241. $view_setup_table_form = $form['view_setup_table'];
  242. unset($view_setup_table_form['#prefix'], $view_setup_table_form['#suffix']);
  243. $output = theme('status_message') . drupal_render($view_setup_table_form);
  244. // Final rendering callback.
  245. // $javascript = drupal_add_js(NULL, NULL, 'header');
  246. drupal_json(array('status' => TRUE, 'data' => $output,));// 'settings' => call_user_func('array_merge_recursive', $javascript['setting'])));
  247. }
  248. /**
  249. *
  250. * @ingroup tripal_view_setup
  251. */
  252. function tripal_view_setup_ajax_field_col_join(){
  253. // regenerate the form
  254. $form = tripal_views_setup_ajax_rebuild_form();
  255. // // get the information we need to pull out table select box that was changed
  256. // $mview_id = $form_state['values']['mview_id'];
  257. // // $row = $form_state['values']['mview_id'];
  258. // // $row_select_box = $form['view_setup_table']["fields-column-join-column-$mview_id-1"];
  259. // // unset($row_select_box['#prefix'], $row_select_box['#suffix']);
  260. // // $output = theme('status_message') . drupal_render($row_select_box);
  261. // // Final rendering callback.
  262. // // $javascript = $javascript = drupal_add_js(NULL);
  263. // // drupal_json(array('status' => TRUE, 'data' => $output, 'settings' => call_user_func('array_merge_recursive', $javascript['setting'])));
  264. // $javascript = drupal_add_js(NULL, NULL, 'header');
  265. // drupal_json(array('status' => TRUE, 'data' => 'helloworld', 'settings' => call_user_func('array_merge_recursive', $javascript['setting'])));
  266. $changed_elements = $form['fields-column-join-column-$mview_1-1'];
  267. unset($changed_elements['#prefix'], $changed_elements['#suffix']);
  268. $output = theme('status_messages') . drupal_render($changed_elements);
  269. drupal_json(array(
  270. 'status' => TRUE,
  271. 'data' => $output,
  272. ));
  273. }
  274. function tripal_views_setup_ajax_rebuild_form(){
  275. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  276. $form_build_id = $_POST['form_build_id'];
  277. $form = form_get_cache($form_build_id, $form_state);
  278. $args = $form['#parameters'];
  279. $form_id = array_shift($args);
  280. $form_state['post'] = $form['#post'] = $_POST;
  281. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  282. $form_state['ahah_submission'] = TRUE;
  283. $form['#programmed'] = $form['#redirect'] = FALSE;
  284. drupal_process_form($form_id, $form, $form_state);
  285. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  286. return $form;
  287. }
  288. /**
  289. *
  290. * @ingroup tripal_view_setup
  291. */
  292. function tripal_views_setup_ajax_rebuild_formz(&$form_state, &$args, &$_POST){
  293. // Retrieve the form from the cache
  294. $form_state = array('storage' => NULL, 'submitted' => FALSE,);
  295. $form_build_id = $_POST['form_build_id'];
  296. $form = form_get_cache($form_build_id, $form_state);
  297. // Preparing to process the form
  298. $args = $form['#parameters'];
  299. $form_id = array_shift($args);
  300. $form_state['post'] = $form['#post'] = $_POST;
  301. $form['#programmed'] = $form['#redirect'] = FALSE;
  302. // Sets the form_state so that the validate and submit handlers can tell
  303. // when the form is submitted via AHAH
  304. $form_state['ahah_submission'] = TRUE;
  305. // Process the form with drupal_process_form. This function calls the submit
  306. // handlers, which put whatever was worthy of keeping into $form_state.
  307. drupal_process_form($form_id, $form, $form_state);
  308. // You call drupal_rebuild_form which destroys $_POST.
  309. // The form generator function is called and creates the form again but since
  310. // it knows to use $form_state, the form will be different.
  311. // The new form gets cached and processed again, but because $_POST is
  312. // destroyed, the submit handlers will not be called again.
  313. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  314. dpm($form,'form at rebuild');
  315. dpm($form_state['post'], 'post at rebuild');
  316. return $form;
  317. }
  318. /**
  319. *
  320. * @ingroup tripal_view_setup
  321. */
  322. // function tripal_views_setup_fields(&$form_state=NULL, $mview_id = NULL){
  323. // if(!$mview_id){
  324. // return;
  325. // }
  326. // // get the columns in this materialized view. They are separated by commas
  327. // // where the first word is the column name and the rest is the type
  328. // $sql = "SELECT mv_specs FROM {tripal_mviews} WHERE mview_id = $mview_id";
  329. // $mview = db_fetch_object(db_query($sql));
  330. // $columns = explode(",",$mview->mv_specs);
  331. // $i=1;
  332. // $chado_tables = tripal_core_get_chado_tables();
  333. // $handlers = array();
  334. // $form["fields_headers"] = array(
  335. // '#type' => 'markup',
  336. // '#value' => "<div class=\"field-headers\">".
  337. // "<div class=\"column-id\">Field Name and Type</div>".
  338. // "<div class=\"fields-column-join\">Join Table</div>".
  339. // "<div class=\"fields-column-join-column\">Join Column</div>".
  340. // "<div class=\"fields-column-handler\">Handler</div></div>",
  341. // );
  342. // foreach ($columns as $column){
  343. // $column = trim($column); // trim trailing and leading spaces
  344. // preg_match("/^(.*?)\ (.*?)$/",$column,$matches);
  345. // $column_name = $matches[1];
  346. // $column_type = $matches[2];
  347. // // first print the field name
  348. // $form["fields_start_$i"] = array(
  349. // '#type' => 'markup',
  350. // '#value' => "<div class=\"fields-new-row\">",
  351. // );
  352. // $form["fields_column_name_$i"] = array(
  353. // '#type' => 'markup',
  354. // '#attributes' => array('class' => 'fields-column-name'),
  355. // '#value' => "<div class=\"column-id\"><span class=\"column-name\">$column_name</span>".
  356. // "<br><span class=\"column-type\">$column_type</span></div>",
  357. // );
  358. // // second print the table join drop down
  359. // $chado_tables = array_merge(array(NULL,), $chado_tables);
  360. // $form["fields_column_join_$i"] = array(
  361. // '#type' => 'select',
  362. // '#prefix' => "<div class=\"fields-column-join\">",
  363. // '#suffix' => "</div>",
  364. // '#options' => $chado_tables,
  365. // '#required' => FALSE,
  366. // '#ahah' => array(
  367. // 'path' => 'admin/tripal/tripal_views_setup/ajax/field_col_join',
  368. // 'wrapper' => "fields-column-join-column-$i",
  369. // 'effect' => 'fade',
  370. // 'event' => 'change',
  371. // 'method' => 'replace',
  372. // ),
  373. // );
  374. // $form["fields_column_join_column_$i"] = array(
  375. // '#type' => 'select',
  376. // '#prefix' => "<div id=\"fields-column-join-column-$i\" class=\"fields-column-join-column\">",
  377. // '#suffix' => "</div>",
  378. // '#options' => array(),
  379. // '#required' => FALSE,
  380. // );
  381. // $form["fields_column_handler_$i"] = array(
  382. // '#type' => 'select',
  383. // '#prefix' => "<div class=\"fields-column-handler\">",
  384. // '#suffix' => "</div>",
  385. // '#options' => $handlers,
  386. // '#required' => FALSE,
  387. // );
  388. // $form["fields_end_$i"] = array(
  389. // '#type' => 'markup',
  390. // '#value' => "</div>",
  391. // );
  392. // $i++;
  393. // }
  394. // $form['row_counter'] = array(
  395. // '#type' => 'hidden',
  396. // '#value' => $i,
  397. // );
  398. // $form['submit'] = array(
  399. // '#type' => 'submit',
  400. // '#value' => 'Create',
  401. // );
  402. // return $form;
  403. // }
  404. // function tripal_views_setup_fields_form_submit($form, &$form_state){
  405. // ----($form,'formfield');
  406. // ----($form_state, 'formstatefield');
  407. // }
  408. /**
  409. *
  410. * @ingroup tripal_view_setup
  411. */
  412. function tripal_views_setup_new_setup_form_submit($form, &$form_state){
  413. dpm($form, 'form on submit');
  414. dpm($form_state, 'forms_state on submit');
  415. }