tripal_views_setup.admin.inc 13 KB

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