tripal_views_setup.admin.inc 12 KB

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