tripal_views_setup.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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' => 'New View 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_$i"] = array(
  141. '#type' => 'markup',
  142. '#value' => "<div class=\"fields-new-row\">",
  143. );
  144. $form['view_setup_table']["fields_column_name_$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. $form['view_setup_table']["fields_column_join_$i"] = array(
  152. '#type' => 'select',
  153. '#prefix' => "<div class=\"fields-column-join\">",
  154. '#suffix' => "</div>",
  155. '#options' => $chado_tables,
  156. '#required' => FALSE,
  157. '#ahah' => array(
  158. 'path' => 'admin/tripal/tripal_views_setup/ajax/field_col_join',
  159. 'wrapper' => "table-rows-div",
  160. 'effect' => 'fade',
  161. 'event' => 'change',
  162. 'method' => 'replace',
  163. ),
  164. );
  165. $column_join = array();
  166. if($form_state['values']["fields_column_join_$i"]){
  167. $column_join = array('hello', 'world');
  168. }
  169. $form['view_setup_table']["fields_column_join_column_$i"] = array(
  170. '#type' => 'select',
  171. '#prefix' => "<div id=\"fields-column-join-column-$i\" class=\"fields-column-join-column\">",
  172. '#suffix' => "</div>",
  173. '#options' => $column_join,
  174. '#required' => FALSE,
  175. );
  176. $form['view_setup_table']["fields_column_handler_$i"] = array(
  177. '#type' => 'select',
  178. '#prefix' => "<div class=\"fields-column-handler\">",
  179. '#suffix' => "</div>",
  180. '#options' => $handlers,
  181. '#required' => FALSE,
  182. );
  183. $form['view_setup_table']["fields_end_$i"] = array(
  184. '#type' => 'markup',
  185. '#value' => "</div>",
  186. );
  187. $i++;
  188. }
  189. $form['row_counter'] = array(
  190. '#type' => 'hidden',
  191. '#value' => $i,
  192. );
  193. }
  194. $form['submit'] = array(
  195. '#type' => 'submit',
  196. '#value' => 'Create',
  197. );
  198. return $form;
  199. }
  200. /**
  201. *
  202. * @ingroup tripal_view_setup
  203. */
  204. function tripal_view_setup_ajax_mview_cols(){
  205. $form = tripal_views_setup_ajax_getform($form_state, $args, $_POST);
  206. unset($form['view_setup_table']['#prefix'], $form['view_setup_table']['#suffix']);
  207. $output = theme('status_message') . drupal_render($form['view_setup_table']);
  208. // Final rendering callback.
  209. drupal_json(array('status' => TRUE, 'data' => $output));
  210. }
  211. function tripal_views_setup_ajax_getform(&$form_state, &$args, &$_POST){
  212. // Retrieve the form from the cache
  213. $form_state = array('storage' => NULL);
  214. $form_build_id = $_POST['form_build_id'];
  215. $form = form_get_cache($form_build_id, $form_state);
  216. // Preparing to process the form
  217. $args = $form['#parameters'];
  218. $form_id = array_shift($args);
  219. $form_state['post'] = $form['#post'] = $_POST;
  220. $form['#programmed'] = $form['#redirect'] = FALSE;
  221. // Sets the form_state so that the validate and submit handlers can tell
  222. // when the form is submitted via AHAH
  223. $form_state['ahah_submission'] = TRUE;
  224. // Process the form with drupal_process_form. This function calls the submit
  225. // handlers, which put whatever was worthy of keeping into $form_state.
  226. drupal_process_form($form_id, $form, $form_state);
  227. // You call drupal_rebuild_form which destroys $_POST.
  228. // The form generator function is called and creates the form again but since
  229. // it knows to use $form_state, the form will be different.
  230. // The new form gets cached and processed again, but because $_POST is
  231. // destroyed, the submit handlers will not be called again.
  232. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  233. return $form;
  234. }
  235. /**
  236. *
  237. * @ingroup tripal_view_setup
  238. */
  239. function tripal_view_setup_ajax_field_col_join(){
  240. $form = tripal_views_setup_ajax_getform($form_state, $args, $_POST);
  241. unset($form['view_setup_table']['#prefix'], $form['view_setup_table']['#suffix']);
  242. $output = theme('status_message') . drupal_render($form['view_setup_table']);
  243. // Final rendering callback.
  244. drupal_json(array('status' => TRUE, 'data' => $output));
  245. }
  246. /**
  247. *
  248. * @ingroup tripal_view_setup
  249. */
  250. // function tripal_views_setup_fields(&$form_state=NULL, $mview_id = NULL){
  251. // if(!$mview_id){
  252. // return;
  253. // }
  254. // // get the columns in this materialized view. They are separated by commas
  255. // // where the first word is the column name and the rest is the type
  256. // $sql = "SELECT mv_specs FROM {tripal_mviews} WHERE mview_id = $mview_id";
  257. // $mview = db_fetch_object(db_query($sql));
  258. // $columns = explode(",",$mview->mv_specs);
  259. // $i=1;
  260. // $chado_tables = tripal_core_get_chado_tables();
  261. // $handlers = array();
  262. // $form["fields_headers"] = array(
  263. // '#type' => 'markup',
  264. // '#value' => "<div class=\"field-headers\">".
  265. // "<div class=\"column-id\">Field Name and Type</div>".
  266. // "<div class=\"fields-column-join\">Join Table</div>".
  267. // "<div class=\"fields-column-join-column\">Join Column</div>".
  268. // "<div class=\"fields-column-handler\">Handler</div></div>",
  269. // );
  270. // foreach ($columns as $column){
  271. // $column = trim($column); // trim trailing and leading spaces
  272. // preg_match("/^(.*?)\ (.*?)$/",$column,$matches);
  273. // $column_name = $matches[1];
  274. // $column_type = $matches[2];
  275. // // first print the field name
  276. // $form["fields_start_$i"] = array(
  277. // '#type' => 'markup',
  278. // '#value' => "<div class=\"fields-new-row\">",
  279. // );
  280. // $form["fields_column_name_$i"] = array(
  281. // '#type' => 'markup',
  282. // '#attributes' => array('class' => 'fields-column-name'),
  283. // '#value' => "<div class=\"column-id\"><span class=\"column-name\">$column_name</span>".
  284. // "<br><span class=\"column-type\">$column_type</span></div>",
  285. // );
  286. // // second print the table join drop down
  287. // $chado_tables = array_merge(array(NULL,), $chado_tables);
  288. // $form["fields_column_join_$i"] = array(
  289. // '#type' => 'select',
  290. // '#prefix' => "<div class=\"fields-column-join\">",
  291. // '#suffix' => "</div>",
  292. // '#options' => $chado_tables,
  293. // '#required' => FALSE,
  294. // '#ahah' => array(
  295. // 'path' => 'admin/tripal/tripal_views_setup/ajax/field_col_join',
  296. // 'wrapper' => "fields-column-join-column-$i",
  297. // 'effect' => 'fade',
  298. // 'event' => 'change',
  299. // 'method' => 'replace',
  300. // ),
  301. // );
  302. // $form["fields_column_join_column_$i"] = array(
  303. // '#type' => 'select',
  304. // '#prefix' => "<div id=\"fields-column-join-column-$i\" class=\"fields-column-join-column\">",
  305. // '#suffix' => "</div>",
  306. // '#options' => array(),
  307. // '#required' => FALSE,
  308. // );
  309. // $form["fields_column_handler_$i"] = array(
  310. // '#type' => 'select',
  311. // '#prefix' => "<div class=\"fields-column-handler\">",
  312. // '#suffix' => "</div>",
  313. // '#options' => $handlers,
  314. // '#required' => FALSE,
  315. // );
  316. // $form["fields_end_$i"] = array(
  317. // '#type' => 'markup',
  318. // '#value' => "</div>",
  319. // );
  320. // $i++;
  321. // }
  322. // $form['row_counter'] = array(
  323. // '#type' => 'hidden',
  324. // '#value' => $i,
  325. // );
  326. // $form['submit'] = array(
  327. // '#type' => 'submit',
  328. // '#value' => 'Create',
  329. // );
  330. // return $form;
  331. // }
  332. // function tripal_views_setup_fields_form_submit($form, &$form_state){
  333. // ----($form,'formfield');
  334. // ----($form_state, 'formstatefield');
  335. // }
  336. /**
  337. *
  338. * @ingroup tripal_view_setup
  339. */
  340. function tripal_views_setup_new_setup_form_submit($form, &$form_state){
  341. dpm($form, 'form on submit');
  342. dpm($form_state, 'forms_state on submit');
  343. }