tripal_chado_views_integration_UI.inc 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. <?php
  2. /**
  3. * @file
  4. * Functions related to the UI for integrating tables with views
  5. */
  6. /**
  7. * Purpose: Generates a themable table containing the list of integrated tables
  8. * The look-and-feel of the table can be altered by overriding the theme for
  9. * tables.
  10. *
  11. * @return
  12. * a themed HTML table
  13. *
  14. * @ingroup tripal_chado_views
  15. */
  16. function tripal_chado_views_integration_setup_list() {
  17. $output = '';
  18. // TODO: change the following to use theme_item_list()
  19. $output .= '<ul class="action-links">';
  20. $output .= '<li>' . l(t('Add a New Entry'), "admin/tripal/storage/chado/views-integration/new") . '</li>';
  21. $output .= '<li style="float: right;">' . l(t('Delete ALL Entries'), "admin/tripal/storage/chado/views-integration/delete-all/confirm") . '</li>';
  22. $output .= '</ul>';
  23. $output .= '<p>' . t('The following tables are available for integration with Drupal Views. If '
  24. . 'a table is integrated more than once, then the setup with the lightest '
  25. . 'priority will be used. For example, if you have created a custom setup with a priority of -5 then '
  26. . 'that will be used instead of the default setup with priority 10. '
  27. . 'Priorities range from -10 to +10 where a setup with -10 has '
  28. . 'greater precedent than any other and +10 has the least.') . '</p>';
  29. // Start with materialized views
  30. $output .= '<br /><h3>Legacy Materialized Views</h3>';
  31. $header = array('', 'Drupal Views Type Name', 'Table Name', 'Is Legacy?', 'Priority', 'Comment');
  32. $rows = array();
  33. // get the list of materialized views
  34. $tviews = db_query('SELECT tv.setup_id, tv.name, tv.table_name, tc.table_id, tv.priority, tv.comment '
  35. .'FROM {tripal_views} tv '
  36. .'LEFT JOIN {tripal_custom_tables} tc ON tc.table_name=tv.table_name '
  37. .'WHERE tv.mview_id IS NOT NULL '
  38. .'ORDER BY tv.table_name ASC, tv.priority ASC');
  39. foreach ($tviews as $tview) {
  40. $rows[] = array(
  41. l(t('Edit'), "admin/tripal/storage/chado/views-integration/edit/" . $tview->setup_id) . "<br />"
  42. . l(t('Export'), "admin/tripal/storage/chado/views-integration/export/" . $tview->setup_id) . "<br />"
  43. . l(t('Delete'), "admin/tripal/storage/chado/views-integration/delete/" . $tview->setup_id),
  44. $tview->name,
  45. $tview->table_name,
  46. ($tview->table_id) ? 'No' : 'Yes',
  47. $tview->priority,
  48. $tview->comment,
  49. );
  50. }
  51. if ($rows) {
  52. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  53. }
  54. else {
  55. $output .= '<p>There are currently no Materialized Views defined. ';
  56. }
  57. // Now list non-mview custom tables
  58. $output .= '<br /><h3>Custom Tables & Non-Legacy Materialized Views</h3>';
  59. $header = array('', 'Drupal Views Type Name', 'Table Name', 'Priority', 'Comment');
  60. $rows = array();
  61. // get the list of chado tables
  62. $tviews = db_query('SELECT tv.setup_id, tv.name, tv.table_name, tv.priority, tv.comment '
  63. .'FROM {tripal_views} tv '
  64. .'LEFT JOIN {tripal_custom_tables} tc ON tc.table_name=tv.table_name '
  65. .'WHERE mview_id IS NULL AND tc.table_id IS NOT NULL '
  66. .'ORDER BY table_name ASC, priority ASC');
  67. foreach ($tviews as $tview) {
  68. $rows[] = array(
  69. l(t('Edit'), "admin/tripal/storage/chado/views-integration/edit/" . $tview->setup_id) . "<br />"
  70. . l(t('Export'), "admin/tripal/storage/chado/views-integration/export/" . $tview->setup_id) . "<br />"
  71. . l(t('Delete'), "admin/tripal/storage/chado/views-integration/delete/" . $tview->setup_id),
  72. $tview->name,
  73. $tview->table_name,
  74. $tview->priority,
  75. $tview->comment,
  76. );
  77. }
  78. if ($rows) {
  79. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  80. }
  81. else {
  82. $output .= '<p>There are currently no non-Materialized View Custom Tables defined.</p>';
  83. }
  84. // Now list chado tables
  85. $output .= '<br /><h3>Chado Tables</h3>';
  86. $header = array('', 'Drupal Views Type Name', 'Table Name', 'Priority', 'Comment');
  87. $rows = array();
  88. // get the list of chado tables
  89. $tviews = db_query('SELECT tv.setup_id, tv.name, tv.table_name, tv.priority, tv.comment '
  90. .'FROM {tripal_views} tv '
  91. .'LEFT JOIN {tripal_custom_tables} tc ON tc.table_name=tv.table_name '
  92. .'WHERE mview_id IS NULL AND tc.table_id IS NULL '
  93. .'ORDER BY table_name ASC, priority ASC');
  94. foreach ($tviews as $tview) {
  95. $rows[] = array(
  96. l(t('Edit'), "admin/tripal/storage/chado/views-integration/edit/" . $tview->setup_id) . "<br />"
  97. . l(t('Export'), "admin/tripal/storage/chado/views-integration/export/" . $tview->setup_id) . "<br />"
  98. . l(t('Delete'), "admin/tripal/storage/chado/views-integration/delete/" . $tview->setup_id),
  99. $tview->name,
  100. $tview->table_name,
  101. $tview->priority,
  102. $tview->comment,
  103. );
  104. }
  105. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  106. return $output;
  107. }
  108. /**
  109. * Purpose: Deletes integration of a table with the Views module. This
  110. * function is meant to be called from a menu item. After completion it
  111. * redirects the user to the views intergation page.
  112. *
  113. * @param $setup_id
  114. * the unique setup id for the integrated table
  115. *
  116. * @ingroup tripal_chado_views
  117. */
  118. function tripal_chado_views_integration_delete($setup_id) {
  119. tripal_remove_views_integration(array('setup_id' => ($setup_id)));
  120. drupal_set_message(t("Record Deleted"));
  121. drupal_goto('admin/tripal/storage/chado/views-integration');
  122. }
  123. /**
  124. * Purpose: Deletes ALL Chado Views Integrations. This
  125. * function is meant to be called from a menu item. After completion it
  126. * redirects the user to the views intergation page.
  127. *
  128. * @ingroup tripal_chado_views
  129. */
  130. function tripal_chado_views_integration_delete_all_form($form, &$form_state) {
  131. $form['extra'] = array(
  132. '#type' => 'item',
  133. '#markup' => t('This will REMOVE ALL views integrations (both custom and default) '
  134. . 'from your website. This allows integrations to be rebuilt to new default '
  135. . 'settings and is especially useful after an upgrade to the views integration system.'),
  136. );
  137. $form['description'] = array(
  138. '#type' => 'item',
  139. '#markup' => t('Are you sure you want to REMOVE ALL Views Integrations (including custom integrations) from your system?'),
  140. );
  141. $form['actions'] = array('#type' => 'actions');
  142. $form['actions']['submit'] = array(
  143. '#type' => 'submit',
  144. '#value' => t('Confirm'),
  145. );
  146. $form['actions']['cancel'] = array(
  147. '#type' => 'link',
  148. '#title' => t('Cancel'),
  149. '#href' => 'admin/tripal/storage/chado/views-integration/list',
  150. );
  151. // By default, render the form using theme_confirm_form().
  152. if (!isset($form['#theme'])) {
  153. $form['#theme'] = 'confirm_form';
  154. }
  155. return $form;
  156. }
  157. /**
  158. * Purpose: Deletes ALL Chado Views Integrations. This
  159. * function is meant to be called from a menu item. After completion it
  160. * redirects the user to the views intergation page.
  161. *
  162. * @ingroup tripal_chado_views
  163. */
  164. function tripal_chado_views_integration_delete_all_form_submit($form, &$form_state) {
  165. tripal_rebuild_views_integrations(TRUE);
  166. $form_state['redirect'] = 'admin/tripal/storage/chado/views-integration/list';
  167. }
  168. /**
  169. * Purpose: defines the web form used for specifing the base table, joins and
  170. * handlers when integrating a table with views. This form is used for both
  171. * creating a new record and editing an existing record.
  172. *
  173. * @param $form
  174. * The form array which is passed automatically by drupal
  175. *
  176. * @param $form_state
  177. * The form state which is passed automatically by drupal
  178. *
  179. * @return
  180. * A proper Drupal form associative array.
  181. *
  182. * D7 @todo: Add ability to manage custom fields
  183. * D7 @todo: Update relationship handler to work with the new tripal_views_join method
  184. *
  185. * @ingroup tripal_chado_views
  186. */
  187. function tripal_chado_views_integration_form($form, &$form_state) {
  188. $data = array();
  189. $form['#cache'] = TRUE;
  190. if (isset($form_state['build_info']['args'][0])) {
  191. $setup_id = $form_state['build_info']['args'][0];
  192. }
  193. else {
  194. $setup_id = NULL;
  195. }
  196. // if Ajax is triggered to change the fields table
  197. // then make some tweaks before the form is rendered
  198. if (isset($form_state['triggering_element'])) {
  199. $triggering_element = $form_state['triggering_element']['#name'];
  200. if (($triggering_element == 'table_name' OR $triggering_element == 'mview_id')) {
  201. $form_state['values'][$triggering_element] = $form_state['triggering_element']['#value'];
  202. $setup_id = NULL;
  203. }
  204. }
  205. // if a setup_id is provided then we want to get the form defaults
  206. $setup_obj = array();
  207. if (isset($setup_id)) {
  208. // get the default setup values
  209. $sql = "SELECT * FROM {tripal_views} WHERE setup_id = :setup";
  210. $setup_obj = db_query($sql, array(':setup' => $setup_id));
  211. $setup_obj = $setup_obj->fetchObject();
  212. $mview_id = $setup_obj->mview_id;
  213. $table_name = $setup_obj->table_name;
  214. $form_state['values']['mview_id'] = $mview_id;
  215. $form_state['values']['table_name'] = $table_name;
  216. // get the default field name/description
  217. $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=:setup";
  218. $query = db_query($sql, array(':setup' => $setup_id));
  219. $default_fields = array();
  220. foreach ($query as $field) {
  221. $default_fields[$field->column_name]['name'] = $field->name;
  222. $default_fields[$field->column_name]['description'] = $field->description;
  223. }
  224. // get the default join settings and handlers
  225. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = :setup";
  226. $query = db_query($sql, array(':setup' => $setup_id));
  227. $default_joins = array();
  228. foreach ($query as $join) {
  229. $default_joins[$join->base_field]['left_table'] = $join->left_table;
  230. $default_joins[$join->base_field]['left_field'] = $join->left_field;
  231. }
  232. // get the default handlers
  233. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = :setup";
  234. $query = db_query($sql, array(':setup' => $setup_id));
  235. $default_handlers = array();
  236. foreach ($query as $handler) {
  237. $default_handlers[$handler->column_name][$handler->handler_type]['handler_name'] = $handler->handler_name;
  238. $default_handlers[$handler->column_name][$handler->handler_type]['arguments'] = $handler->arguments;
  239. }
  240. // get the default join handlers
  241. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = :setup";
  242. $query = db_query($sql, array(':setup' => $setup_id));
  243. foreach ($query as $handler) {
  244. $default_handlers[$handler->base_field]['join']['handler_name'] = $handler->handler;
  245. //$default_handlers[$handler->base_field]['join']['arguments'] = $handler->arguments;
  246. }
  247. // add in the setup_id for the form so we know this is an update not an insert
  248. $form['setup_id'] = array(
  249. '#type' => 'hidden',
  250. '#value' => $setup_id,
  251. );
  252. } // end if (isset($setup_id)) {
  253. // add a fieldset for the MView & Chado table selectors
  254. $form['base_table_type'] = array(
  255. '#type' => 'fieldset',
  256. '#title' => 'Base Table',
  257. '#description' => 'Please select either a materialized view or a Chado table for integration with '.
  258. 'Drupal Views. In Drupal Views terminology, the selected table becomes the "base table". '.
  259. 'After you select a table from either list, the fields from that table will appear below '.
  260. 'and you can specify other tables to join with and handlers.',
  261. );
  262. // build the form element for the Chado tables
  263. $chado_tables = chado_get_table_names(TRUE);
  264. $chado_tables = array_merge(array('Select'), $chado_tables);
  265. $default = '';
  266. if ($setup_id) {
  267. $default = ($setup_obj->table_name) ? $setup_obj->table_name : '';
  268. }
  269. $form['base_table_type']['table_name'] = array(
  270. '#title' => t('Chado/Custom Table'),
  271. '#type' => 'select',
  272. '#options' => $chado_tables,
  273. '#description' => t('Tables from Chado, custom tables and materialized view tables (non-legacy MViews) can be selected for integration.'),
  274. '#default_value' => $default,
  275. '#ajax' => array(
  276. //D6: 'path' => 'tripal/views-integration/ajax/view_setup_table',
  277. 'callback' => 'tripal_chado_views_integration_ajax_view_setup_table',
  278. 'wrapper' => 'tripal-views-integration-form',
  279. 'effect' => 'fade',
  280. 'event' => 'change',
  281. 'method' => 'replace',
  282. ),
  283. );
  284. // build the form element that lists the materialized views
  285. $query = db_query("SELECT mview_id, name FROM {tripal_mviews} WHERE mv_schema is NULL or mv_schema = '' ORDER BY name");
  286. $mview_tables = array();
  287. $mview_tables['0'] = 'Select';
  288. foreach ($query as $mview) {
  289. $mview_tables[$mview->mview_id] = $mview->name;
  290. }
  291. $default = '';
  292. $legacy_mview = FALSE;
  293. if ($setup_id && !empty($setup_obj->mview_id)) {
  294. $default = $setup_obj->mview_id;
  295. }
  296. if (isset($mview_tables[$setup_obj->mview_id])) {
  297. $legacy_mview = TRUE;
  298. $form['base_table_type']['mview_id'] = array(
  299. '#title' => t('Legacy Materialized View'),
  300. '#type' => 'select',
  301. '#options' => $mview_tables,
  302. '#description' => 'Which materialized view to use.',
  303. '#default_value' => $default,
  304. '#ajax' => array(
  305. //D6: 'path' => 'tripal/views-integration/ajax/view_setup_table',
  306. 'callback' => 'tripal_chado_views_integration_ajax_view_setup_table',
  307. 'wrapper' => 'tripal-views-integration-form',
  308. 'effect' => 'fade',
  309. 'event' => 'change',
  310. 'method' => 'replace',
  311. ),
  312. );
  313. }
  314. $form['views_type'] = array(
  315. '#type' => 'fieldset',
  316. '#title' => 'View Type',
  317. '#description' => 'Here you can provide the "type" of View you want to create.',
  318. );
  319. // field for the name of the
  320. $default = '';
  321. if ($setup_id && isset($setup_obj->name)) {
  322. $default = $setup_obj->name;
  323. }
  324. $form['views_type']['row_name'] = array(
  325. '#title' => t('View Type Name'),
  326. '#type' => 'textfield',
  327. '#default_value' => $default,
  328. '#size' => 60,
  329. '#maxlength' => 128,
  330. '#description' => 'Provide the view type name. This is the name that will appear in '.
  331. 'the Drupal Views interface when adding a new view. The view type name '.
  332. 'must be unique.',
  333. '#required' => TRUE,
  334. );
  335. if (isset($setup_id)) {
  336. $form['row_name']['#attributes'] = array('readonly' => 'readonly');
  337. }
  338. $priorities = array();
  339. foreach (range(-10, 10) as $v) {
  340. $priorities[$v] = (string) $v;
  341. }
  342. $default = -1;
  343. if ($setup_id && isset($setup_obj->priority)) {
  344. $default = $setup_obj->priority;
  345. if ($setup_obj->priority >= 9) {
  346. drupal_set_message('You are editing a default views integration. To ensure your changes
  347. are used, change the priority to -10.', 'warning');
  348. }
  349. }
  350. $form['views_type']['row_priority'] = array(
  351. '#type' => 'select',
  352. '#title' => t('Priority'),
  353. '#description' => t('The level of priority your Views integration has in relation to the '
  354. .'default core and module definitions. The views integration definition with the '
  355. .'lightest priority will be used. For example, if there is a definition created by '
  356. .'core with a priority of 10 and another by a custom module of 5 and yours is -1 then '
  357. .'your definition will be used for that table because -1 is lighter than both 5 and 10.'),
  358. '#options' => $priorities,
  359. '#default_value' => $default,
  360. );
  361. $default = true;
  362. if ($setup_id && isset($setup_obj->priority)) {
  363. $default = ($setup_obj->priority >= 9) ? true : false;
  364. }
  365. $form['views_type']['new_integration'] = array(
  366. '#type' => 'checkbox',
  367. '#title' => t('Create a New Chado Views Integration Record'),
  368. '#description' => t('If this checkbox is checked then a new tripal views integration '
  369. .'will be created rather then overriding the current one with your changes. This '
  370. .'especially important if you are editing one of the default views integrations '
  371. .'(ie: if the original priority was either 10 or 9).'),
  372. '#default_value' => $default,
  373. );
  374. $default = true;
  375. if ($setup_id && isset($setup_obj->base_table)) {
  376. $default = $setup_obj->base_table;
  377. }
  378. $form['views_type']['base_table'] = array(
  379. '#type' => 'checkbox',
  380. '#title' => t('Base Table?'),
  381. '#description' => t('If you want this table to show up as one of the options in the '
  382. . 'add view page, then check this checkbox. It allows you to create listings '
  383. . 'primarily from this table'),
  384. '#default_value' => $default,
  385. );
  386. $default = '';
  387. if ($setup_id && isset($setup_obj->comment)) {
  388. $default = $setup_obj->comment;
  389. }
  390. $form['views_type']['row_description'] = array(
  391. '#title' => t('Comment'),
  392. '#type' => 'textarea',
  393. '#description' => '(Optional). Provide any details regarding this setup you would like. This '.
  394. 'description will appear when selecting a type for a new Drupal View',
  395. '#required' => FALSE,
  396. '#default_value' => $default,
  397. );
  398. // we need a div block where the table fields will get put when the
  399. // AHAH callback is made
  400. $form['view_setup_table'] = array(
  401. '#type' => 'item',
  402. '#prefix' => '<div id="fieldset-table-rows-wrapper">',
  403. '#suffix' => '</div>',
  404. );
  405. // add the fieldset for the table fields, but only if the $mview_id or $table_name
  406. // is set. The only times these values are set is if we're editing an existing
  407. // record or if the AHAH callback is being made.
  408. if (isset($form_state['values']['mview_id']) or isset($form_state['values']['table_name'])) {
  409. $mview_id = $form_state['values']['mview_id'];
  410. $table_name = $form_state['values']['table_name'];
  411. $form['view_setup_table'] = array(
  412. '#type' => 'fieldset',
  413. '#title' => t('Table Fields'),
  414. '#prefix' => '<div id="fieldset-table-rows-wrapper">',
  415. '#suffix' => '</div>',
  416. '#collapsible' => TRUE
  417. );
  418. // get the columns in this materialized view. They are separated by commas
  419. // where the first word is the column name and the rest is the type
  420. $columns = array();
  421. if ($legacy_mview) {
  422. $sql = "SELECT mv_specs FROM {tripal_mviews} WHERE mview_id = :id";
  423. $mview = db_query($sql, array(':id' => $mview_id));
  424. $mview = $mview->fetchObject();
  425. $columns = explode(",", $mview->mv_specs);
  426. }
  427. else {
  428. $table_desc = chado_get_schema($table_name);
  429. if ($table_desc) {
  430. $fields = $table_desc['fields'];
  431. // iterate through the columns and build the format
  432. // compatible with the code below. The column name is first followed
  433. // by the type with a separating space
  434. foreach ($fields as $column => $attrs) {
  435. $columns[] = "$column " . $attrs['type'];
  436. }
  437. }
  438. }
  439. $i=1;
  440. $form['view_setup_table']["instructions"] = array(
  441. '#type' => 'markup',
  442. '#value' => filter_xss("Select an optional table to which the fields of the ".
  443. "materialized view can join. If a field does not need to ".
  444. "join you may leave the selection blank."),
  445. );
  446. $data['field_types'] = array();
  447. $form['view_setup_table']['header'] = array(
  448. '#type' => 'markup',
  449. '#prefix' => '<div class="joins-new-row field-headers">',
  450. '#suffix' => '</div>',
  451. );
  452. $form['view_setup_table']['header']['column-1'] = array(
  453. '#type' => 'markup',
  454. '#prefix' => "<div class=\"column-one\">",
  455. '#suffix' => "</div>",
  456. '#markup' => 'Field'
  457. );
  458. $form['view_setup_table']['header']['column-2'] = array(
  459. '#type' => 'markup',
  460. '#prefix' => "<div class=\"column-two\">",
  461. '#suffix' => "</div>",
  462. '#markup' => 'Labels'
  463. );
  464. $form['view_setup_table']['header']['column-3'] = array(
  465. '#type' => 'markup',
  466. '#prefix' => "<div class=\"column-three\">",
  467. '#suffix' => "</div>",
  468. '#markup' => ''
  469. );
  470. $form['view_setup_table']['header']['column-4'] = array(
  471. '#type' => 'markup',
  472. '#prefix' => "<div class=\"column-four\">",
  473. '#suffix' => "</div>",
  474. '#markup' => 'Handlers'
  475. );
  476. // get the list of chado tables to join on
  477. $chado_join_tables = chado_get_table_names(TRUE);
  478. $chado_join_tables = array_merge(array('Select a Join Table'), $chado_join_tables);
  479. // get list of all handlers
  480. $all_handlers = tripal_chado_views_integration_discover_handlers();
  481. $handlers_fields = array(0 => "Select a field handler");
  482. $handlers_filters = array(0 => "Select a filter handler");
  483. $handlers_sort = array(0 => "Select a sort handler");
  484. $handlers_argument = array(0 => "Select an argument handler");
  485. $handlers_join = array(0 => "Select a join handler");
  486. $handlers_rel = array(0 => "Select a relationship handler");
  487. foreach ($all_handlers as $handler) {
  488. if (preg_match("/views_handler_field/", $handler)) {
  489. $handlers_fields[$handler] = $handler;
  490. }
  491. if (preg_match("/views_handler_filter/", $handler)) {
  492. $handlers_filters[$handler] = $handler;
  493. }
  494. if (preg_match("/views_handler_sort/", $handler)) {
  495. $handlers_sort[$handler] = $handler;
  496. }
  497. if (preg_match("/views_handler_argument/", $handler)) {
  498. $handlers_argument[$handler] = $handler;
  499. }
  500. if (preg_match("/_join/", $handler)) {
  501. $handlers_join[$handler] = $handler;
  502. }
  503. if (preg_match("/views_handler_relationship/", $handler)) {
  504. $handlers_rel[$handler] = $handler;
  505. }
  506. }
  507. // generate a unique $table_id for keeping track of the table
  508. if ($mview_id) {
  509. $table_id = $mview_id;
  510. }
  511. else {
  512. $table_id = $table_name;
  513. }
  514. // Per Row (Fields) --------------
  515. // now iterate through the columns of the materialized view or
  516. // chado table and generate the join and handler fields
  517. foreach ($columns as $column) {
  518. $column = trim($column); // trim trailing and leading spaces
  519. preg_match("/^(.*?)\ (.*?)$/", $column, $matches);
  520. $column_name = $matches[1];
  521. $column_type = $matches[2];
  522. $form['view_setup_table']["$table_id-$i"] = array(
  523. '#type' => 'markup',
  524. '#prefix' => "<div class=\"fields-new-row\">",
  525. '#suffix' => "</div>",
  526. '#value' => filter_xss('')
  527. );
  528. // COLUMN I
  529. $form['view_setup_table']["$table_id-$i"]["fields_name_$table_id-$i"] = array(
  530. '#type' => 'item',
  531. '#prefix' => "<div class=\"column-one\">",
  532. '#markup' => '<span class="column-name">' . filter_xss($column_name) . '</span>'.
  533. '<br /><span class="column-type">' . filter_xss($column_type) . '</span>',
  534. '#suffix' => "</div>",
  535. );
  536. $data['field_types'][$column_name] = $column_type;
  537. // COLUMN II
  538. $form['view_setup_table']["$table_id-$i"]['column-2'] = array(
  539. '#type' => 'markup',
  540. '#prefix' => "<div class=\"column-two\">",
  541. '#suffix' => "</div>",
  542. '#value' => filter_xss('')
  543. );
  544. // set the default values for the human-readable name and description
  545. $default_name = '';
  546. $default_descrip = '';
  547. if (isset($setup_id) && !isset($form_state['values']["fields_readable_name_$table_id-$i"])) {
  548. $default_name = $default_fields[$column_name]['name'];
  549. $default_descrip = $default_fields[$column_name]['description'];
  550. }
  551. elseif (isset($form_state['values']["fields_readable_name_$table_id-$i"])) {
  552. $default_name = $form_state['values']["fields_readable_name_$table_id-$i"];
  553. $default_descrip = $form_state['values']["fields_description_$table_id-$i"];
  554. }
  555. else {
  556. $default_name = ucwords(str_replace('_',' ', $column_name));
  557. $default_descrip = 'TODO: please describe this field!';
  558. }
  559. $form['view_setup_table']["$table_id-$i"]['column-2']["fields_readable_name_$table_id-$i"] = array(
  560. '#type' => 'textfield',
  561. '#title' => 'Human-Readable Name',
  562. '#description' => 'This is the name of the field in the Views UI',
  563. '#required' => TRUE,
  564. '#default_value' => $default_name,
  565. '#size' => 42,
  566. );
  567. $form['view_setup_table']["$table_id-$i"]['column-2']["fields_description_$table_id-$i"] = array(
  568. '#type' => 'textarea',
  569. '#title' => 'Short Description',
  570. '#description' => 'This is the field help in the Views UI',
  571. '#required' => TRUE,
  572. '#cols' => 42,
  573. '#rows' => 3,
  574. '#default_value' => $default_descrip,
  575. );
  576. // COLUMN III
  577. $form['view_setup_table']["$table_id-$i"]['column-3'] = array(
  578. '#type' => 'markup',
  579. '#prefix' => "<div class=\"column-three\">",
  580. '#suffix' => "</div>",
  581. '#value' => filter_xss('')
  582. );
  583. // COLUMN 4
  584. $form['view_setup_table']["$table_id-$i"]['column-4'] = array(
  585. '#type' => 'markup',
  586. '#prefix' => "<div class=\"column-four\">",
  587. '#suffix' => "</div>",
  588. '#value' => filter_xss('')
  589. );
  590. // create the handler fields
  591. $default_field_handler = 0;
  592. if (isset($setup_id) && !isset($form_state['values']["fields_field_handler_$table_id-$i"])) {
  593. $default_field_handler = array_key_exists('field', $default_handlers[$column_name]) ? $default_handlers[$column_name]['field']['handler_name'] : "";
  594. $form_state['values']["fields_field_handler_$table_id-$i"] = $default_field_handler;
  595. }
  596. else {
  597. $default_field_handler = array_key_exists("fields_field_handler_$table_id-$i", $form_state['values']) ? $form_state['values']["fields_field_handler_$table_id-$i"] : '';
  598. if (!$default_field_handler) {
  599. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  600. $default_field_handler = 'views_handler_field_numeric';
  601. }
  602. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  603. $default_field_handler = 'views_handler_field';
  604. }
  605. elseif ($column_type == 'boolean') {
  606. $default_field_handler = 'views_handler_field_boolean';
  607. }
  608. elseif ($column_type == 'float') {
  609. $default_field_handler = 'views_handler_field_numeric';
  610. }
  611. elseif ($column_type == 'datetime') {
  612. $default_field_handler = 'views_handler_field_date';
  613. }
  614. }
  615. }
  616. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_field_handler_$table_id-$i"] = array(
  617. '#type' => 'select',
  618. '#prefix' => "<div class=\"fields-field-handler\">",
  619. '#suffix' => "</div>",
  620. '#options' => $handlers_fields,
  621. '#required' => FALSE,
  622. '#default_value' => $default_field_handler,
  623. );
  624. // Assign the default filter handler for this field. Use the handler provided
  625. // by the user through the form if provided, or set the default using the
  626. // column type.
  627. $default_filter_handler = 0;
  628. if (isset($setup_id) && !isset($form_state['values']["fields_filter_handler_$table_id-$i"])) {
  629. $default_filter_handler = array_key_exists('filter', $default_handlers[$column_name]) ? $default_handlers[$column_name]['filter']['handler_name'] : "";
  630. $form_state['values']["fields_filter_handler_$table_id-$i"]= $default_filter_handler;
  631. }
  632. else {
  633. $default_filter_handler = array_key_exists("fields_filter_handler_$table_id-$i", $form_state['values']) ? $form_state['values']["fields_filter_handler_$table_id-$i"] : "";
  634. if (!$default_filter_handler) {
  635. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  636. $default_filter_handler = 'views_handler_filter_numeric';
  637. }
  638. elseif (preg_match("/^character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  639. $default_filter_handler = 'views_handler_filter_string';
  640. }
  641. elseif ($column_type == 'boolean') {
  642. $default_filter_handler = 'views_handler_filter_boolean';
  643. }
  644. elseif ($column_type == 'float') {
  645. $default_filter_handler = 'views_handler_filter_numeric';
  646. }
  647. elseif ($column_type == 'datetime') {
  648. $default_filter_handler = 'views_handler_filter_date';
  649. }
  650. }
  651. }
  652. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_filter_handler_$table_id-$i"] = array(
  653. '#type' => 'select',
  654. '#prefix' => "<div class=\"fields-filter-handler\">",
  655. '#suffix' => "</div>",
  656. '#options' => $handlers_filters,
  657. '#required' => FALSE,
  658. '#default_value' => $default_filter_handler,
  659. );
  660. $default_sort_handler = 0;
  661. if (isset($setup_id) && !isset($form_state['values']["fields_sort_handler_$table_id-$i"])) {
  662. $default_sort_handler = array_key_exists('sort', $default_handlers[$column_name]) ? $default_handlers[$column_name]['sort']['handler_name'] : "";
  663. $form_state['values']["fields_sort_handler_$table_id-$i"] = $default_sort_handler;
  664. }
  665. else {
  666. $default_sort_handler = array_key_exists("fields_sort_handler_$table_id-$i", $form_state['values']) ? $form_state['values']["fields_sort_handler_$table_id-$i"] : "";
  667. if (!$default_sort_handler) {
  668. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  669. $default_sort_handler = 'views_handler_sort';
  670. }
  671. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  672. $default_sort_handler = 'views_handler_sort';
  673. }
  674. elseif ($column_type == 'boolean') {
  675. $default_sort_handler = 'views_handler_sort';
  676. }
  677. elseif ($column_type == 'float') {
  678. $default_sort_handler = 'views_handler_sort';
  679. }
  680. elseif ($column_type == 'datetime') {
  681. $default_sort_handler = 'views_handler_sort_date';
  682. }
  683. }
  684. }
  685. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_sort_handler_$table_id-$i"] = array(
  686. '#type' => 'select',
  687. '#prefix' => "<div class=\"fields-sort-handler\">",
  688. '#suffix' => "</div>",
  689. '#options' => $handlers_sort,
  690. '#required' => FALSE,
  691. '#default_value' => $default_sort_handler,
  692. );
  693. $default_argument_handler = 0;
  694. if (isset($setup_id)
  695. && !isset($form_state['values']["fields_argument_handler_$table_id-$i"])
  696. && isset($default_handlers[$column_name]['argument']))
  697. {
  698. $default_argument_handler = $default_handlers[$column_name]['argument']['handler_name'];
  699. $form_state['values']["fields_argument_handler_$table_id-$i"] = $default_argument_handler ;
  700. }
  701. else {
  702. if (isset($form_state['values']["fields_argument_handler_$table_id-$i"])) {
  703. $default_argument_handler = $form_state['values']["fields_argument_handler_$table_id-$i"];
  704. }
  705. if (!$default_argument_handler) {
  706. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  707. $default_argument_handler = 'views_handler_argument_numeric';
  708. }
  709. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  710. $default_argument_handler = 'views_handler_argument_string';
  711. }
  712. elseif ($column_type == 'boolean') {
  713. $default_argument_handler = 'views_handler_argument_numeric';
  714. }
  715. elseif ($column_type == 'float') {
  716. $default_argument_handler = 'views_handler_argument_numeric';
  717. }
  718. elseif ($column_type == 'datetime') {
  719. $default_argument_handler = 'views_handler_argument_date';
  720. }
  721. }
  722. }
  723. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_argument_handler_$table_id-$i"] = array(
  724. '#type' => 'select',
  725. '#prefix' => "<div class=\"fields-argument-handler\">",
  726. '#suffix' => "</div>",
  727. '#options' => $handlers_argument,
  728. '#required' => FALSE,
  729. '#default_value' => $default_argument_handler,
  730. );
  731. $i++;
  732. } // end foreach ($columns as $column) {
  733. $form['view_setup_join'] = array(
  734. '#type' => 'fieldset',
  735. '#title' => t('Joins & Relationships'),
  736. '#prefix' => '<div id="fieldset-join-rows-wrapper">',
  737. '#suffix' => '</div>',
  738. '#description' => t('Below is a list of the joins/relationships between the '
  739. . 'current base table and other chado tables.'),
  740. '#collapsible' => TRUE,
  741. '#collapsed' => TRUE
  742. );
  743. $form['view_setup_join']['header'] = array(
  744. '#type' => 'markup',
  745. '#prefix' => '<div class="joins-new-row field-headers">',
  746. '#suffix' => '</div>',
  747. );
  748. $form['view_setup_join']['header']['column-1'] = array(
  749. '#type' => 'markup',
  750. '#prefix' => "<div class=\"join-column-one\">",
  751. '#suffix' => "</div>",
  752. '#markup' => 'Base'
  753. );
  754. $form['view_setup_join']['header']['column-2'] = array(
  755. '#type' => 'markup',
  756. '#prefix' => "<div class=\"join-column-two\">",
  757. '#suffix' => "</div>",
  758. '#markup' => 'Handlers'
  759. );
  760. $form['view_setup_join']['header']['column-3'] = array(
  761. '#type' => 'markup',
  762. '#prefix' => "<div class=\"join-column-three\">",
  763. '#suffix' => "</div>",
  764. '#markup' => 'Joined to'
  765. );
  766. $base_field_options = array('Select the Base Column');
  767. $table_desc = chado_get_schema($table_name);
  768. foreach ($table_desc['fields'] as $column => $def) {
  769. $base_field_options[$column] = $column;
  770. }
  771. $chado_join_tables[0] = 'Select the Left Table';
  772. unset($handlers_join[0]);
  773. $i=0;
  774. $query = db_select('tripal_views_join','tvj');
  775. $query->fields('tvj')
  776. ->condition('tvj.setup_id',$setup_id,'=')
  777. ->orderBy('tvj.relationship_only','ASC')
  778. ->orderBy('tvj.base_field', 'ASC')
  779. ->orderBy('tvj.left_table', 'ASC');
  780. foreach ($query->execute() as $i => $result) {
  781. $form['view_setup_join']["$table_id-$i"] = array(
  782. '#type' => 'markup',
  783. '#prefix' => "<div class=\"joins-new-row\">",
  784. '#suffix' => "</div>",
  785. '#value' => filter_xss('')
  786. );
  787. // COLUMN I
  788. $form['view_setup_join']["$table_id-$i"]['column-1'] = array(
  789. '#type' => 'markup',
  790. '#prefix' => "<div class=\"join-column-one\">",
  791. '#suffix' => "</div>",
  792. );
  793. $form['view_setup_join']["$table_id-$i"]['column-1']['join_base_table'] = array(
  794. '#type' => 'item',
  795. '#markup' => '<span class="column-name">' . filter_xss($result->base_table) . '</span>'
  796. );
  797. $form['view_setup_join']["$table_id-$i"]['column-1']["join_base_table-$i"] = array(
  798. '#type' => 'hidden',
  799. '#value' => $result->base_table
  800. );
  801. $form['view_setup_join']["$table_id-$i"]['column-1']["join_base_field-$i"] = array(
  802. '#type' => 'select',
  803. '#options' => $base_field_options,
  804. '#required' => FALSE,
  805. '#default_value' => $result->base_field
  806. );
  807. // COLUMN II
  808. $form['view_setup_join']["$table_id-$i"]['column-2'] = array(
  809. '#type' => 'markup',
  810. '#prefix' => "<div class=\"join-column-two\">",
  811. '#suffix' => "</div>",
  812. '#value' => filter_xss('')
  813. );
  814. $form['view_setup_join']["$table_id-$i"]['column-2']["join_join_handler-$i"] = array(
  815. '#type' => 'select',
  816. '#prefix' => "<div class=\"fields-join-handler\">",
  817. '#suffix' => "</div>",
  818. '#options' => $handlers_join,
  819. '#required' => FALSE,
  820. '#default_value' => $result->handler,
  821. );
  822. $form['view_setup_join']["$table_id-$i"]['column-2']["join_relationship_handler-$i"] = array(
  823. '#type' => 'select',
  824. '#prefix' => "<div class=\"fields-relationship-handler\">",
  825. '#suffix' => "</div>",
  826. '#options' => $handlers_rel,
  827. '#required' => FALSE,
  828. '#default_value' => $result->relationship_handler,
  829. );
  830. $form['view_setup_join']["$table_id-$i"]['column-2']["join_relationship_only-$i"] = array(
  831. '#type' => 'checkbox',
  832. '#title' => 'Relationship Only?',
  833. '#default_value' => $result->relationship_only
  834. );
  835. // COLUMN III
  836. $form['view_setup_join']["$table_id-$i"]['column-3'] = array(
  837. '#type' => 'markup',
  838. '#prefix' => "<div class=\"join-column-three\">",
  839. '#suffix' => "</div>",
  840. '#value' => filter_xss('')
  841. );
  842. $form['view_setup_join']["$table_id-$i"]['column-3']["join_left_table-$i"] = array(
  843. '#type' => 'select',
  844. '#options' => $chado_join_tables,
  845. '#default_value' => $result->left_table,
  846. );
  847. $columns = array();
  848. if ($result->left_table) {
  849. // get the table description
  850. $table_desc = chado_get_schema($result->left_table);
  851. foreach ($table_desc['fields'] as $column => $def) {
  852. $columns[$column] = $column;
  853. }
  854. }
  855. else {
  856. $columns = array('Select Left Column');
  857. }
  858. $form['view_setup_join']["$table_id-$i"]['column-3']["join_left_field-$i"] = array(
  859. '#type' => 'select',
  860. '#prefix' => " <div id=\"fields-column-join-column-$table_id-$i\" class=\"fields-column-join-column\">",
  861. '#suffix' => "</div>",
  862. '#options' => $columns,
  863. '#required' => FALSE,
  864. '#default_value' => $result->left_field
  865. );
  866. } // end foreach ($query->execute() as $i => $result) {
  867. $form['num_joins'] = array(
  868. '#type' => 'hidden',
  869. '#value' => $i
  870. );
  871. // Form to add a new join
  872. $form['view_setup_join']['new_join'] = array(
  873. '#type' => 'fieldset',
  874. '#title' => 'New Join',
  875. '#description' => "Add a new join to the $table_name table",
  876. '#prefix' => '<div id="new-join">',
  877. '#suffix' => '</div>',
  878. );
  879. $form['view_setup_join']['new_join']['column-1'] = array(
  880. '#type' => 'markup',
  881. '#prefix' => "<div class=\"join-column-one\">",
  882. '#suffix' => "</div>",
  883. );
  884. $form['view_setup_join']['new_join']['column-1']['join_base_table'] = array(
  885. '#type' => 'item',
  886. '#markup' => '<span class="column-name">' . $table_name . '</span>'
  887. );
  888. $form['view_setup_join']['new_join']['column-1']['new_join_base_table'] = array(
  889. '#type' => 'hidden',
  890. '#value' => $table_name
  891. );
  892. $form['view_setup_join']['new_join']['column-1']['new_join_base_field'] = array(
  893. '#type' => 'select',
  894. '#options' => $base_field_options,
  895. '#required' => FALSE,
  896. );
  897. // COLUMN II
  898. $form['view_setup_join']['new_join']['column-2'] = array(
  899. '#type' => 'markup',
  900. '#prefix' => "<div class=\"join-column-two\">",
  901. '#suffix' => "</div>",
  902. '#value' => filter_xss('')
  903. );
  904. $form['view_setup_join']['new_join']['column-2']['new_join_join_handler'] = array(
  905. '#type' => 'select',
  906. '#prefix' => "<div class=\"fields-join-handler\">",
  907. '#suffix' => "</div>",
  908. '#options' => $handlers_join,
  909. '#required' => FALSE,
  910. );
  911. $form['view_setup_join']['new_join']['column-2']['new_join_relationship_handler'] = array(
  912. '#type' => 'select',
  913. '#prefix' => "<div class=\"fields-relationship-handler\">",
  914. '#suffix' => "</div>",
  915. '#options' => $handlers_rel,
  916. '#required' => FALSE,
  917. '#default_value' => 'views_handler_relationship'
  918. );
  919. $form['view_setup_join']['new_join']['column-2']['new_join_relationship_only'] = array(
  920. '#type' => 'checkbox',
  921. '#title' => 'Relationship Only?',
  922. '#default_value' => TRUE
  923. );
  924. // COLUMN III
  925. $form['view_setup_join']['new_join']['column-3'] = array(
  926. '#type' => 'markup',
  927. '#prefix' => "<div class=\"join-column-three\">",
  928. '#suffix' => "</div>",
  929. '#value' => filter_xss('')
  930. );
  931. $form['view_setup_join']['new_join']['column-3']['new_join_left_table'] = array(
  932. '#type' => 'select',
  933. '#options' => $chado_join_tables,
  934. '#ajax' => array(
  935. 'callback' => 'tripal_chado_views_integration_ajax_new_join_left_field_callback',
  936. 'wrapper' => 'new-join',
  937. )
  938. );
  939. $new_join_table = (isset($form_state['values']['new_join_left_table']))? $form_state['values']['new_join_left_table'] : FALSE;
  940. if ($new_join_table) {
  941. // get the table description
  942. $table_desc = chado_get_schema($new_join_table);
  943. foreach ($table_desc['fields'] as $column => $def) {
  944. $columns[$column] = $column;
  945. }
  946. }
  947. else {
  948. $columns = array('Select Left Column');
  949. }
  950. $form['view_setup_join']['new_join']['column-3']['new_join_left_field'] = array(
  951. '#type' => 'select',
  952. '#prefix' => " <div id=\"fields-column-join-column-$table_id-$i\" class=\"fields-column-join-column\">",
  953. '#suffix' => "</div>",
  954. '#options' => $columns,
  955. '#required' => FALSE,
  956. );
  957. $form['view_setup_join']['new_join']['save'] = array(
  958. '#type' => 'submit',
  959. '#value' => t('Save & Add Join'),
  960. );
  961. $form['save'] = array(
  962. '#type' => 'submit',
  963. '#value' => t('Save'),
  964. );
  965. $data['row_count'] = $i - 1;
  966. } // end of if table/mview
  967. // use this to put values into $form_state['values']
  968. $form['data'] = array();
  969. // Ensure that we don't store an array
  970. // since we will get a check_plain:htmlspecial_characters error if we do
  971. foreach ($data as $key => $value) {
  972. if (is_array($value)) {
  973. $form['data'][$key] = array(
  974. '#type' => 'hidden',
  975. '#value' => serialize($value),
  976. );
  977. }
  978. else {
  979. $form['data'][$key] = array(
  980. '#type' => 'hidden',
  981. '#value' => $value,
  982. );
  983. }
  984. }
  985. $form['#prefix'] = '<div id="tripal-views-integration-form">';
  986. $form['#suffix'] = '</div>';
  987. $form['#redirect'] = 'admin/tripal/storage/chado/views-integration/list';
  988. return $form;
  989. }
  990. /**
  991. * Purpose: validates the tripal_chado_views_integration_form after submission
  992. *
  993. * @param $form
  994. * The form object which is passed automatically by drupal
  995. *
  996. * @param $form_state
  997. * The form state pbject which is passed automatically by drupal
  998. *
  999. * @ingroup tripal_chado_views
  1000. */
  1001. function tripal_chado_views_integration_form_validate($form, &$form_state) {
  1002. $name_array = explode(" ", $form_state['values']['row_name']);
  1003. $mview_id = $form_state['values']['mview_id'];
  1004. $table_name = $form_state['values']['table_name'];
  1005. // if (count($name_array) > 1) {
  1006. // form_set_error($form_state['values']['row_name'], 'The View type name must be a single word only.');
  1007. // }
  1008. if ($mview_id and $table_name) {
  1009. form_set_error('mview_id', 'Please select either a materialized view or a Chado table but not both');
  1010. }
  1011. if (!$mview_id and !$table_name) {
  1012. form_set_error('mview_id', 'Please select either a materialized view or a Chado table');
  1013. }
  1014. // Ensure that users don't override the default integrations
  1015. if ($form_state['values']['row_priority'] >= 9) {
  1016. form_set_error('row_priority', 'A priority of 10 or 9 is reserved for default tripal '
  1017. .'views integrations created by core modules. Please set the priority between '
  1018. .'0 and -10 to ensure your changes are used rather over the defaults.');
  1019. }
  1020. // Check that if some fields for a new join are entered, all of them are
  1021. if (!empty($form_state['values']['new_join_base_field'])
  1022. OR !empty($form_state['values']['new_join_left_table'])
  1023. OR !empty($form_state['values']['new_join_left_field'])) {
  1024. if (!(!empty($form_state['values']['new_join_base_field'])
  1025. AND !empty($form_state['values']['new_join_left_table'])
  1026. AND !empty($form_state['values']['new_join_left_field']))) {
  1027. form_set_error('new_join_base_field','You need to select the Base Column, Left Table and Left Column to create a new join');
  1028. }
  1029. }
  1030. }
  1031. /**
  1032. * Purpose: inserts or updates the record in the tripal views integration
  1033. * tables. This function is only called if validation is passed.
  1034. *
  1035. * @param $form
  1036. * The form object which is passed automatically by drupal
  1037. *
  1038. * @param $form_state
  1039. * The form state pbject which is passed automatically by drupal
  1040. *
  1041. * @ingroup tripal_chado_views
  1042. */
  1043. function tripal_chado_views_integration_form_submit($form, &$form_state) {
  1044. $name = trim($form_state['values']['row_name']);
  1045. $mview_id = $form_state['values']['mview_id'];
  1046. $table_name = $form_state['values']['table_name'];
  1047. $setup_id = array_key_exists('setup_id', $form_state['values']) ? $form_state['values']['setup_id'] : "";
  1048. $priority = $form_state['values']['row_priority'];
  1049. $comment = $form_state['values']['row_description'];
  1050. // get details about this mview
  1051. if ($mview_id) {
  1052. // D7 TODO: Check DBTNG changes work
  1053. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :id";
  1054. $mview = db_query($sql, array(':id' => $mview_id));
  1055. $mview = $mview->fetchObject();
  1056. $table_name = $mview->mv_table;
  1057. $table_id = $mview_id;
  1058. $type = 'mview';
  1059. }
  1060. else {
  1061. $type = 'chado';
  1062. $table_id = $table_name;
  1063. }
  1064. // If this is for a materialized view then we want to add/update that record
  1065. $tripal_views_record = array();
  1066. if ($mview_id) {
  1067. // get details about this mview
  1068. // D7 TODO: Check DBTNG changes work
  1069. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :id";
  1070. $mview = db_query($sql, array(':id' => $mview_id));
  1071. $mview = $mview->fetchObject();
  1072. // build the record for insert/update
  1073. $tripal_views_record = array(
  1074. 'mview_id' => $mview_id,
  1075. 'table_name' => $mview->mv_table,
  1076. 'name' => $name,
  1077. 'priority' => $priority,
  1078. 'comment' => $comment,
  1079. 'base_table' => $form_state['values']['base_table']
  1080. );
  1081. }
  1082. // if a chado table then...
  1083. else {
  1084. // build the record for insert/update
  1085. $tripal_views_record = array(
  1086. 'table_name' => $table_name,
  1087. 'name' => $name,
  1088. 'priority' => $priority,
  1089. 'comment' => $comment,
  1090. 'base_table' => $form_state['values']['base_table']
  1091. );
  1092. }
  1093. // perform the insert or update
  1094. if (!$setup_id) { // this is an insert
  1095. if (!drupal_write_record('tripal_views', $tripal_views_record)) {
  1096. drupal_set_message(t("Failed to add record."), 'error');
  1097. return;
  1098. }
  1099. }
  1100. else { // this is an update
  1101. // check to see if it was specified to create a new integration
  1102. if ($form_state['values']['new_integration']) {
  1103. $setup_id = NULL;
  1104. if (!drupal_write_record('tripal_views', $tripal_views_record)) {
  1105. drupal_set_message(t("Failed to add record."), 'error');
  1106. return;
  1107. }
  1108. }
  1109. else {
  1110. $tripal_views_record['setup_id'] = $setup_id;
  1111. if (!drupal_write_record('tripal_views', $tripal_views_record, array('setup_id'))) {
  1112. drupal_set_message(t("Failed to update record."), 'error');
  1113. return;
  1114. }
  1115. }
  1116. }
  1117. // if this is an update then clean out the existing joins and handlers so we can add new ones
  1118. if ($setup_id) {
  1119. db_query("DELETE FROM {tripal_views_field} WHERE setup_id = :setup", array(':setup' => $setup_id));
  1120. db_query("DELETE FROM {tripal_views_join} WHERE setup_id = :setup", array(':setup' => $setup_id));
  1121. db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id = :setup", array(':setup' => $setup_id));
  1122. }
  1123. // iterate through the columns of the form and add
  1124. // the joins if provided, and the handlers
  1125. $i = 1;
  1126. foreach (unserialize($form_state['values']['field_types']) as $key => $value) {
  1127. // add the field definition
  1128. $view_field_record = array(
  1129. 'setup_id' => $tripal_views_record['setup_id'],
  1130. 'column_name' => $key,
  1131. 'name' => $form_state['values']["fields_readable_name_$table_id-$i"],
  1132. 'description' => $form_state['values']["fields_description_$table_id-$i"],
  1133. 'type' => $value,
  1134. );
  1135. drupal_write_record('tripal_views_field', $view_field_record);
  1136. // add the hanlders
  1137. $handlers = array('filter', 'field', 'sort', 'argument');
  1138. foreach ($handlers as $handler) {
  1139. $handler_name = $form_state['values']["fields_" . $handler . "_handler_$table_id-$i"];
  1140. if ($handler_name) {
  1141. $handler_record = array(
  1142. 'setup_id' => $tripal_views_record['setup_id'],
  1143. 'column_name' => $key,
  1144. 'handler_type' => $handler,
  1145. 'handler_name' => $handler_name,
  1146. );
  1147. drupal_write_record('tripal_views_handlers', $handler_record);
  1148. }
  1149. }
  1150. $i++;
  1151. }
  1152. // Now add all the joins
  1153. for($i = 0; $i <= $form_state['values']['num_joins']; $i++) {
  1154. if (isset($form_state['values']["join_base_table-$i"])) {
  1155. $join_record = array(
  1156. 'setup_id' => $tripal_views_record['setup_id'],
  1157. 'base_table' => $form_state['values']["join_base_table-$i"],
  1158. 'base_field' => $form_state['values']["join_base_field-$i"],
  1159. 'left_table' => $form_state['values']["join_left_table-$i"],
  1160. 'left_field' => $form_state['values']["join_left_field-$i"],
  1161. 'handler' => $form_state['values']["join_join_handler-$i"],
  1162. 'relationship_handler' => $form_state['values']["join_relationship_handler-$i"],
  1163. 'relationship_only' => $form_state['values']["join_relationship_only-$i"]
  1164. );
  1165. drupal_write_record('tripal_views_join', $join_record);
  1166. }
  1167. }
  1168. // Now add the new join if there is one
  1169. if ($form_state['values']['new_join_base_field']) {
  1170. $join_record = array(
  1171. 'setup_id' => $tripal_views_record['setup_id'],
  1172. 'base_table' => $form_state['values']["new_join_base_table"],
  1173. 'base_field' => $form_state['values']["new_join_base_field"],
  1174. 'left_table' => $form_state['values']["new_join_left_table"],
  1175. 'left_field' => $form_state['values']["new_join_left_field"],
  1176. 'handler' => $form_state['values']["new_join_join_handler"],
  1177. 'relationship_handler' => $form_state['values']["new_join_relationship_handler"],
  1178. 'relationship_only' => $form_state['values']["new_join_relationship_only"]
  1179. );
  1180. drupal_write_record('tripal_views_join', $join_record);
  1181. }
  1182. if ($setup_id) {
  1183. drupal_set_message(t('Record Updated'));
  1184. }
  1185. else {
  1186. drupal_set_message(t('Record Added'));
  1187. }
  1188. $form_state['redirect'] = 'admin/tripal/storage/chado/views-integration/integrations';
  1189. // now clear all the caches so that Drupal views picks up our chages
  1190. views_invalidate_cache();
  1191. }
  1192. /**
  1193. * AJAX callback to replace the left field select list in the new join fieldset of the
  1194. * tripal_chado_views_integration_form form
  1195. */
  1196. function tripal_chado_views_integration_ajax_new_join_left_field_callback($form, $form_state) {
  1197. return $form['view_setup_join']['new_join'];
  1198. }
  1199. /**
  1200. * Purpose: this function queries all modules currently enabled on the site
  1201. * looking for custom handlers and returns a list of all available handerls.
  1202. * The base View handlers are also included.
  1203. *
  1204. * @return
  1205. * Returns an array of handler names
  1206. *
  1207. * @ingroup tripal_chado_views
  1208. */
  1209. function tripal_chado_views_integration_discover_handlers() {
  1210. $handlers = array();
  1211. // Get handlers from all modules.
  1212. foreach (module_implements('views_handlers') as $module) {
  1213. $function = $module . '_views_handlers';
  1214. $result = $function();
  1215. if (!is_array($result)) {
  1216. continue;
  1217. }
  1218. foreach ($result['handlers'] as $handler => $parent) {
  1219. $handlers[] = $handler;
  1220. }
  1221. }
  1222. // these handlers are hard coded because I could not
  1223. // get the views_handlers() function to be called
  1224. // in the code above. However, we will be creating
  1225. // Chado wrappers for many of these and once that work
  1226. // is done these will no longer be needed.
  1227. // argument handlers
  1228. $handlers[] = 'views_handler_argument';
  1229. $handlers[] = 'views_handler_argument_numeric';
  1230. $handlers[] = 'views_handler_argument_formula';
  1231. $handlers[] = 'views_handler_argument_date';
  1232. $handlers[] = 'views_handler_argument_string';
  1233. $handlers[] = 'views_handler_argument_many_to_one';
  1234. $handlers[] = 'views_handler_argument_null';
  1235. // field handlers
  1236. $handlers[] = 'views_handler_field';
  1237. $handlers[] = 'views_handler_field_date';
  1238. $handlers[] = 'views_handler_field_boolean';
  1239. $handlers[] = 'views_handler_field_markup';
  1240. $handlers[] = 'views_handler_field_xss';
  1241. $handlers[] = 'views_handler_field_url';
  1242. $handlers[] = 'views_handler_field_file_size';
  1243. $handlers[] = 'views_handler_field_prerender_list';
  1244. $handlers[] = 'views_handler_field_numeric';
  1245. $handlers[] = 'views_handler_field_custom';
  1246. $handlers[] = 'views_handler_field_counter';
  1247. // filter handlers
  1248. $handlers[] = 'views_handler_filter';
  1249. $handlers[] = 'views_handler_filter_equality';
  1250. $handlers[] = 'views_handler_filter_string';
  1251. $handlers[] = 'views_handler_filter_boolean_operator';
  1252. $handlers[] = 'views_handler_filter_boolean_operator_string';
  1253. $handlers[] = 'views_handler_filter_in_operator';
  1254. $handlers[] = 'views_handler_filter_numeric';
  1255. $handlers[] = 'views_handler_filter_date';
  1256. $handlers[] = 'views_handler_filter_many_to_one';
  1257. // relationship handlers
  1258. $handlers[] = 'views_handler_relationship';
  1259. // sort handlers
  1260. $handlers[] = 'views_handler_sort';
  1261. $handlers[] = 'views_handler_sort_formula';
  1262. $handlers[] = 'views_handler_sort_date';
  1263. $handlers[] = 'views_handler_sort_menu_hierarchy';
  1264. $handlers[] = 'views_handler_sort_random';
  1265. // join handler
  1266. $handlers[] = 'views_join';
  1267. return $handlers;
  1268. }
  1269. /*
  1270. * Ajax Callback: Chado Views Integration Form
  1271. * Replaces the entire fields table when the table or materialized view is set/changed
  1272. *
  1273. * @ingroup tripal_chado_views
  1274. */
  1275. function tripal_chado_views_integration_ajax_view_setup_table($form, $form_state) {
  1276. //return $form['view_setup_table'];
  1277. return $form;
  1278. }
  1279. /*
  1280. * Ajax Callback: Chado Views Integration Form
  1281. * Replaces the join db field dropdown when the join table dropdown is changed
  1282. *
  1283. * @ingroup tripal_chado_views
  1284. */
  1285. function tripal_chado_views_integration_ajax_join_field($form, $form_state) {
  1286. // Determine which row we are dealing with from the name of the triggering element
  1287. if (preg_match('/fields_join_(\w+-\d+)/', $form_state['triggering_element']['#name'], $matches)) {
  1288. $field = $matches[1];
  1289. $join_field = 'fields_join_column_' . $matches[1];
  1290. // return the form element to be updated
  1291. return $form['view_setup_table'][$field]['column-3'][$join_field];
  1292. }
  1293. else {
  1294. tripal_report_error('tripal_views', TRIPAL_ERROR,
  1295. 'Chado Views Integration Ajax failed due to being unable to determine which row needs updating', array());
  1296. return $form;
  1297. }
  1298. }