tripal_views_integration_UI.inc 53 KB

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