tripal_views_integration_UI.inc 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  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. // 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/views-integration/new") . '</li>';
  21. $output .= '<li style="float: right;">' . l(t('Delete ALL Entries'), "admin/tripal/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/views-integration/edit/" . $tview->setup_id) . "<br />"
  42. . l(t('Export'), "admin/tripal/views-integration/export/" . $tview->setup_id) . "<br />"
  43. . l(t('Delete'), "admin/tripal/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/views-integration/edit/" . $tview->setup_id) . "<br />"
  70. . l(t('Export'), "admin/tripal/views-integration/export/" . $tview->setup_id) . "<br />"
  71. . l(t('Delete'), "admin/tripal/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/views-integration/edit/" . $tview->setup_id) . "<br />"
  97. . l(t('Export'), "admin/tripal/views-integration/export/" . $tview->setup_id) . "<br />"
  98. . l(t('Delete'), "admin/tripal/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_views
  117. */
  118. function tripal_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/views-integration');
  122. }
  123. /**
  124. * Purpose: Deletes ALL Tripal 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_views
  129. */
  130. function tripal_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/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 Tripal 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_views
  163. */
  164. function tripal_views_integration_delete_all_form_submit($form, &$form_state) {
  165. tripal_rebuild_views_integrations(TRUE);
  166. $form_state['redirect'] = 'admin/tripal/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_views
  186. */
  187. function tripal_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->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' => '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. // 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' => 'tripal-views-integration-form',
  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 = array_key_exists('field', $default_handlers[$column_name]) ? $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 = array_key_exists("fields_field_handler_$table_id-$i", $form_state['values']) ? $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 = '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 = 'views_handler_field';
  602. }
  603. elseif ($column_type == 'boolean') {
  604. $default_field_handler = 'views_handler_field_boolean';
  605. }
  606. elseif ($column_type == 'float') {
  607. $default_field_handler = 'views_handler_field_numeric';
  608. }
  609. elseif ($column_type == 'datetime') {
  610. $default_field_handler = '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. // Assign the default filter handler for this field. Use the handler provided
  623. // by the user through the form if provided, or set the default using the
  624. // column type.
  625. $default_filter_handler = 0;
  626. if (isset($setup_id) && !isset($form_state['values']["fields_filter_handler_$table_id-$i"])) {
  627. $default_filter_handler = array_key_exists('filter', $default_handlers[$column_name]) ? $default_handlers[$column_name]['filter']['handler_name'] : "";
  628. $form_state['values']["fields_filter_handler_$table_id-$i"]= $default_filter_handler;
  629. }
  630. else {
  631. $default_filter_handler = array_key_exists("fields_filter_handler_$table_id-$i", $form_state['values']) ? $form_state['values']["fields_filter_handler_$table_id-$i"] : "";
  632. if (!$default_filter_handler) {
  633. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  634. $default_filter_handler = 'views_handler_filter_numeric';
  635. }
  636. elseif (preg_match("/^character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  637. $default_filter_handler = 'views_handler_filter_string';
  638. }
  639. elseif ($column_type == 'boolean') {
  640. $default_filter_handler = 'views_handler_filter_boolean';
  641. }
  642. elseif ($column_type == 'float') {
  643. $default_filter_handler = 'views_handler_filter_numeric';
  644. }
  645. elseif ($column_type == 'datetime') {
  646. $default_filter_handler = 'views_handler_filter_date';
  647. }
  648. }
  649. }
  650. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_filter_handler_$table_id-$i"] = array(
  651. '#type' => 'select',
  652. '#prefix' => "<div class=\"fields-filter-handler\">",
  653. '#suffix' => "</div>",
  654. '#options' => $handlers_filters,
  655. '#required' => FALSE,
  656. '#default_value' => $default_filter_handler,
  657. );
  658. $default_sort_handler = 0;
  659. if (isset($setup_id) && !isset($form_state['values']["fields_sort_handler_$table_id-$i"])) {
  660. $default_sort_handler = array_key_exists('sort', $default_handlers[$column_name]) ? $default_handlers[$column_name]['sort']['handler_name'] : "";
  661. $form_state['values']["fields_sort_handler_$table_id-$i"] = $default_sort_handler;
  662. }
  663. else {
  664. $default_sort_handler = array_key_exists("fields_sort_handler_$table_id-$i", $form_state['values']) ? $form_state['values']["fields_sort_handler_$table_id-$i"] : "";
  665. if (!$default_sort_handler) {
  666. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  667. $default_sort_handler = 'views_handler_sort';
  668. }
  669. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  670. $default_sort_handler = 'views_handler_sort';
  671. }
  672. elseif ($column_type == 'boolean') {
  673. $default_sort_handler = 'views_handler_sort';
  674. }
  675. elseif ($column_type == 'float') {
  676. $default_sort_handler = 'views_handler_sort';
  677. }
  678. elseif ($column_type == 'datetime') {
  679. $default_sort_handler = 'views_handler_sort_date';
  680. }
  681. }
  682. }
  683. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_sort_handler_$table_id-$i"] = array(
  684. '#type' => 'select',
  685. '#prefix' => "<div class=\"fields-sort-handler\">",
  686. '#suffix' => "</div>",
  687. '#options' => $handlers_sort,
  688. '#required' => FALSE,
  689. '#default_value' => $default_sort_handler,
  690. );
  691. $default_argument_handler = 0;
  692. if (isset($setup_id)
  693. && !isset($form_state['values']["fields_argument_handler_$table_id-$i"])
  694. && isset($default_handlers[$column_name]['argument']))
  695. {
  696. $default_argument_handler = $default_handlers[$column_name]['argument']['handler_name'];
  697. $form_state['values']["fields_argument_handler_$table_id-$i"] = $default_argument_handler ;
  698. }
  699. else {
  700. if (isset($form_state['values']["fields_argument_handler_$table_id-$i"])) {
  701. $default_argument_handler = $form_state['values']["fields_argument_handler_$table_id-$i"];
  702. }
  703. if (!$default_argument_handler) {
  704. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  705. $default_argument_handler = 'views_handler_argument_numeric';
  706. }
  707. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  708. $default_argument_handler = 'views_handler_argument_string';
  709. }
  710. elseif ($column_type == 'boolean') {
  711. $default_argument_handler = 'views_handler_argument_numeric';
  712. }
  713. elseif ($column_type == 'float') {
  714. $default_argument_handler = 'views_handler_argument_numeric';
  715. }
  716. elseif ($column_type == 'datetime') {
  717. $default_argument_handler = 'views_handler_argument_date';
  718. }
  719. }
  720. }
  721. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_argument_handler_$table_id-$i"] = array(
  722. '#type' => 'select',
  723. '#prefix' => "<div class=\"fields-argument-handler\">",
  724. '#suffix' => "</div>",
  725. '#options' => $handlers_argument,
  726. '#required' => FALSE,
  727. '#default_value' => $default_argument_handler,
  728. );
  729. $i++;
  730. } // end foreach ($columns as $column) {
  731. $form['view_setup_join'] = array(
  732. '#type' => 'fieldset',
  733. '#title' => t('Joins & Relationships'),
  734. '#prefix' => '<div id="fieldset-join-rows-wrapper">',
  735. '#suffix' => '</div>',
  736. '#description' => t('Below is a list of the joins/relationships between the '
  737. . 'current base table and other chado tables.'),
  738. '#collapsible' => TRUE,
  739. '#collapsed' => TRUE
  740. );
  741. $form['view_setup_join']['header'] = array(
  742. '#type' => 'markup',
  743. '#prefix' => '<div class="joins-new-row field-headers">',
  744. '#suffix' => '</div>',
  745. );
  746. $form['view_setup_join']['header']['column-1'] = array(
  747. '#type' => 'markup',
  748. '#prefix' => "<div class=\"join-column-one\">",
  749. '#suffix' => "</div>",
  750. '#markup' => 'Base'
  751. );
  752. $form['view_setup_join']['header']['column-2'] = array(
  753. '#type' => 'markup',
  754. '#prefix' => "<div class=\"join-column-two\">",
  755. '#suffix' => "</div>",
  756. '#markup' => 'Handlers'
  757. );
  758. $form['view_setup_join']['header']['column-3'] = array(
  759. '#type' => 'markup',
  760. '#prefix' => "<div class=\"join-column-three\">",
  761. '#suffix' => "</div>",
  762. '#markup' => 'Joined to'
  763. );
  764. $base_field_options = array('Select the Base Column');
  765. $table_desc = chado_get_schema($table_name);
  766. foreach ($table_desc['fields'] as $column => $def) {
  767. $base_field_options[$column] = $column;
  768. }
  769. $chado_join_tables[0] = 'Select the Left Table';
  770. unset($handlers_join[0]);
  771. $i=0;
  772. $query = db_select('tripal_views_join','tvj');
  773. $query->fields('tvj')
  774. ->condition('tvj.setup_id',$setup_id,'=')
  775. ->orderBy('tvj.relationship_only','ASC')
  776. ->orderBy('tvj.base_field', 'ASC')
  777. ->orderBy('tvj.left_table', 'ASC');
  778. foreach ($query->execute() as $i => $result) {
  779. $form['view_setup_join']["$table_id-$i"] = array(
  780. '#type' => 'markup',
  781. '#prefix' => "<div class=\"joins-new-row\">",
  782. '#suffix' => "</div>",
  783. '#value' => filter_xss('')
  784. );
  785. // COLUMN I
  786. $form['view_setup_join']["$table_id-$i"]['column-1'] = array(
  787. '#type' => 'markup',
  788. '#prefix' => "<div class=\"join-column-one\">",
  789. '#suffix' => "</div>",
  790. );
  791. $form['view_setup_join']["$table_id-$i"]['column-1']['join_base_table'] = array(
  792. '#type' => 'item',
  793. '#markup' => '<span class="column-name">' . filter_xss($result->base_table) . '</span>'
  794. );
  795. $form['view_setup_join']["$table_id-$i"]['column-1']["join_base_table-$i"] = array(
  796. '#type' => 'hidden',
  797. '#value' => $result->base_table
  798. );
  799. $form['view_setup_join']["$table_id-$i"]['column-1']["join_base_field-$i"] = array(
  800. '#type' => 'select',
  801. '#options' => $base_field_options,
  802. '#required' => FALSE,
  803. '#default_value' => $result->base_field
  804. );
  805. // COLUMN II
  806. $form['view_setup_join']["$table_id-$i"]['column-2'] = array(
  807. '#type' => 'markup',
  808. '#prefix' => "<div class=\"join-column-two\">",
  809. '#suffix' => "</div>",
  810. '#value' => filter_xss('')
  811. );
  812. $form['view_setup_join']["$table_id-$i"]['column-2']["join_join_handler-$i"] = array(
  813. '#type' => 'select',
  814. '#prefix' => "<div class=\"fields-join-handler\">",
  815. '#suffix' => "</div>",
  816. '#options' => $handlers_join,
  817. '#required' => FALSE,
  818. '#default_value' => $result->handler,
  819. );
  820. $form['view_setup_join']["$table_id-$i"]['column-2']["join_relationship_handler-$i"] = array(
  821. '#type' => 'select',
  822. '#prefix' => "<div class=\"fields-relationship-handler\">",
  823. '#suffix' => "</div>",
  824. '#options' => $handlers_rel,
  825. '#required' => FALSE,
  826. '#default_value' => $result->relationship_handler,
  827. );
  828. $form['view_setup_join']["$table_id-$i"]['column-2']["join_relationship_only-$i"] = array(
  829. '#type' => 'checkbox',
  830. '#title' => 'Relationship Only?',
  831. '#default_value' => $result->relationship_only
  832. );
  833. // COLUMN III
  834. $form['view_setup_join']["$table_id-$i"]['column-3'] = array(
  835. '#type' => 'markup',
  836. '#prefix' => "<div class=\"join-column-three\">",
  837. '#suffix' => "</div>",
  838. '#value' => filter_xss('')
  839. );
  840. $form['view_setup_join']["$table_id-$i"]['column-3']["join_left_table-$i"] = array(
  841. '#type' => 'select',
  842. '#options' => $chado_join_tables,
  843. '#default_value' => $result->left_table,
  844. );
  845. $columns = array();
  846. if ($result->left_table) {
  847. // get the table description
  848. $table_desc = chado_get_schema($result->left_table);
  849. foreach ($table_desc['fields'] as $column => $def) {
  850. $columns[$column] = $column;
  851. }
  852. }
  853. else {
  854. $columns = array('Select Left Column');
  855. }
  856. $form['view_setup_join']["$table_id-$i"]['column-3']["join_left_field-$i"] = array(
  857. '#type' => 'select',
  858. '#prefix' => " <div id=\"fields-column-join-column-$table_id-$i\" class=\"fields-column-join-column\">",
  859. '#suffix' => "</div>",
  860. '#options' => $columns,
  861. '#required' => FALSE,
  862. '#default_value' => $result->left_field
  863. );
  864. } // end foreach ($query->execute() as $i => $result) {
  865. $form['num_joins'] = array(
  866. '#type' => 'hidden',
  867. '#value' => $i
  868. );
  869. // Form to add a new join
  870. $form['view_setup_join']['new_join'] = array(
  871. '#type' => 'fieldset',
  872. '#title' => 'New Join',
  873. '#description' => "Add a new join to the $table_name table",
  874. '#prefix' => '<div id="new-join">',
  875. '#suffix' => '</div>',
  876. );
  877. $form['view_setup_join']['new_join']['column-1'] = array(
  878. '#type' => 'markup',
  879. '#prefix' => "<div class=\"join-column-one\">",
  880. '#suffix' => "</div>",
  881. );
  882. $form['view_setup_join']['new_join']['column-1']['join_base_table'] = array(
  883. '#type' => 'item',
  884. '#markup' => '<span class="column-name">' . $table_name . '</span>'
  885. );
  886. $form['view_setup_join']['new_join']['column-1']['new_join_base_table'] = array(
  887. '#type' => 'hidden',
  888. '#value' => $table_name
  889. );
  890. $form['view_setup_join']['new_join']['column-1']['new_join_base_field'] = array(
  891. '#type' => 'select',
  892. '#options' => $base_field_options,
  893. '#required' => FALSE,
  894. );
  895. // COLUMN II
  896. $form['view_setup_join']['new_join']['column-2'] = array(
  897. '#type' => 'markup',
  898. '#prefix' => "<div class=\"join-column-two\">",
  899. '#suffix' => "</div>",
  900. '#value' => filter_xss('')
  901. );
  902. $form['view_setup_join']['new_join']['column-2']['new_join_join_handler'] = array(
  903. '#type' => 'select',
  904. '#prefix' => "<div class=\"fields-join-handler\">",
  905. '#suffix' => "</div>",
  906. '#options' => $handlers_join,
  907. '#required' => FALSE,
  908. );
  909. $form['view_setup_join']['new_join']['column-2']['new_join_relationship_handler'] = array(
  910. '#type' => 'select',
  911. '#prefix' => "<div class=\"fields-relationship-handler\">",
  912. '#suffix' => "</div>",
  913. '#options' => $handlers_rel,
  914. '#required' => FALSE,
  915. '#default_value' => 'views_handler_relationship'
  916. );
  917. $form['view_setup_join']['new_join']['column-2']['new_join_relationship_only'] = array(
  918. '#type' => 'checkbox',
  919. '#title' => 'Relationship Only?',
  920. '#default_value' => TRUE
  921. );
  922. // COLUMN III
  923. $form['view_setup_join']['new_join']['column-3'] = array(
  924. '#type' => 'markup',
  925. '#prefix' => "<div class=\"join-column-three\">",
  926. '#suffix' => "</div>",
  927. '#value' => filter_xss('')
  928. );
  929. $form['view_setup_join']['new_join']['column-3']['new_join_left_table'] = array(
  930. '#type' => 'select',
  931. '#options' => $chado_join_tables,
  932. '#ajax' => array(
  933. 'callback' => 'tripal_views_integration_ajax_new_join_left_field_callback',
  934. 'wrapper' => 'new-join',
  935. )
  936. );
  937. $new_join_table = (isset($form_state['values']['new_join_left_table']))? $form_state['values']['new_join_left_table'] : FALSE;
  938. if ($new_join_table) {
  939. // get the table description
  940. $table_desc = chado_get_schema($new_join_table);
  941. foreach ($table_desc['fields'] as $column => $def) {
  942. $columns[$column] = $column;
  943. }
  944. }
  945. else {
  946. $columns = array('Select Left Column');
  947. }
  948. $form['view_setup_join']['new_join']['column-3']['new_join_left_field'] = array(
  949. '#type' => 'select',
  950. '#prefix' => " <div id=\"fields-column-join-column-$table_id-$i\" class=\"fields-column-join-column\">",
  951. '#suffix' => "</div>",
  952. '#options' => $columns,
  953. '#required' => FALSE,
  954. );
  955. $form['view_setup_join']['new_join']['save'] = array(
  956. '#type' => 'submit',
  957. '#value' => t('Save & Add Join'),
  958. );
  959. $form['save'] = array(
  960. '#type' => 'submit',
  961. '#value' => t('Save'),
  962. );
  963. $data['row_count'] = $i - 1;
  964. } // end of if table/mview
  965. // use this to put values into $form_state['values']
  966. $form['data'] = array();
  967. // Ensure that we don't store an array
  968. // since we will get a check_plain:htmlspecial_characters error if we do
  969. foreach ($data as $key => $value) {
  970. if (is_array($value)) {
  971. $form['data'][$key] = array(
  972. '#type' => 'hidden',
  973. '#value' => serialize($value),
  974. );
  975. }
  976. else {
  977. $form['data'][$key] = array(
  978. '#type' => 'hidden',
  979. '#value' => $value,
  980. );
  981. }
  982. }
  983. $form['#prefix'] = '<div id="tripal-views-integration-form">';
  984. $form['#suffix'] = '</div>';
  985. $form['#redirect'] = 'admin/tripal/views-integration/list';
  986. return $form;
  987. }
  988. /**
  989. * Purpose: validates the tripal_views_integration_form after submission
  990. *
  991. * @param $form
  992. * The form object which is passed automatically by drupal
  993. *
  994. * @param $form_state
  995. * The form state pbject which is passed automatically by drupal
  996. *
  997. * @ingroup tripal_views
  998. */
  999. function tripal_views_integration_form_validate($form, &$form_state) {
  1000. $name_array = explode(" ", $form_state['values']['row_name']);
  1001. $mview_id = $form_state['values']['mview_id'];
  1002. $table_name = $form_state['values']['table_name'];
  1003. // if (count($name_array) > 1) {
  1004. // form_set_error($form_state['values']['row_name'], 'The View type name must be a single word only.');
  1005. // }
  1006. if ($mview_id and $table_name) {
  1007. form_set_error('mview_id', 'Please select either a materialized view or a Chado table but not both');
  1008. }
  1009. if (!$mview_id and !$table_name) {
  1010. form_set_error('mview_id', 'Please select either a materialized view or a Chado table');
  1011. }
  1012. // Ensure that users don't override the default integrations
  1013. if ($form_state['values']['row_priority'] >= 9) {
  1014. form_set_error('row_priority', 'A priority of 10 or 9 is reserved for default tripal '
  1015. .'views integrations created by core modules. Please set the priority between '
  1016. .'0 and -10 to ensure your changes are used rather over the defaults.');
  1017. }
  1018. // Check that if some fields for a new join are entered, all of them are
  1019. if (!empty($form_state['values']['new_join_base_field'])
  1020. OR !empty($form_state['values']['new_join_left_table'])
  1021. OR !empty($form_state['values']['new_join_left_field'])) {
  1022. if (!(!empty($form_state['values']['new_join_base_field'])
  1023. AND !empty($form_state['values']['new_join_left_table'])
  1024. AND !empty($form_state['values']['new_join_left_field']))) {
  1025. form_set_error('new_join_base_field','You need to select the Base Column, Left Table and Left Column to create a new join');
  1026. }
  1027. }
  1028. }
  1029. /**
  1030. * Purpose: inserts or updates the record in the tripal views integration
  1031. * tables. This function is only called if validation is passed.
  1032. *
  1033. * @param $form
  1034. * The form object which is passed automatically by drupal
  1035. *
  1036. * @param $form_state
  1037. * The form state pbject which is passed automatically by drupal
  1038. *
  1039. * @ingroup tripal_views
  1040. */
  1041. function tripal_views_integration_form_submit($form, &$form_state) {
  1042. $name = trim($form_state['values']['row_name']);
  1043. $mview_id = $form_state['values']['mview_id'];
  1044. $table_name = $form_state['values']['table_name'];
  1045. $setup_id = array_key_exists('setup_id', $form_state['values']) ? $form_state['values']['setup_id'] : "";
  1046. $priority = $form_state['values']['row_priority'];
  1047. $comment = $form_state['values']['row_description'];
  1048. // get details about this mview
  1049. if ($mview_id) {
  1050. // D7 TODO: Check DBTNG changes work
  1051. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :id";
  1052. $mview = db_query($sql, array(':id' => $mview_id));
  1053. $mview = $mview->fetchObject();
  1054. $table_name = $mview->mv_table;
  1055. $table_id = $mview_id;
  1056. $type = 'mview';
  1057. }
  1058. else {
  1059. $type = 'chado';
  1060. $table_id = $table_name;
  1061. }
  1062. // If this is for a materialized view then we want to add/update that record
  1063. $tripal_views_record = array();
  1064. if ($mview_id) {
  1065. // get details about this mview
  1066. // D7 TODO: Check DBTNG changes work
  1067. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :id";
  1068. $mview = db_query($sql, array(':id' => $mview_id));
  1069. $mview = $mview->fetchObject();
  1070. // build the record for insert/update
  1071. $tripal_views_record = array(
  1072. 'mview_id' => $mview_id,
  1073. 'table_name' => $mview->mv_table,
  1074. 'name' => $name,
  1075. 'priority' => $priority,
  1076. 'comment' => $comment,
  1077. 'base_table' => $form_state['values']['base_table']
  1078. );
  1079. }
  1080. // if a chado table then...
  1081. else {
  1082. // build the record for insert/update
  1083. $tripal_views_record = array(
  1084. 'table_name' => $table_name,
  1085. 'name' => $name,
  1086. 'priority' => $priority,
  1087. 'comment' => $comment,
  1088. 'base_table' => $form_state['values']['base_table']
  1089. );
  1090. }
  1091. // perform the insert or update
  1092. if (!$setup_id) { // this is an insert
  1093. if (!drupal_write_record('tripal_views', $tripal_views_record)) {
  1094. drupal_set_message(t("Failed to add record."), 'error');
  1095. return;
  1096. }
  1097. }
  1098. else { // this is an update
  1099. // check to see if it was specified to create a new integration
  1100. if ($form_state['values']['new_integration']) {
  1101. $setup_id = NULL;
  1102. if (!drupal_write_record('tripal_views', $tripal_views_record)) {
  1103. drupal_set_message(t("Failed to add record."), 'error');
  1104. return;
  1105. }
  1106. }
  1107. else {
  1108. $tripal_views_record['setup_id'] = $setup_id;
  1109. if (!drupal_write_record('tripal_views', $tripal_views_record, array('setup_id'))) {
  1110. drupal_set_message(t("Failed to update record."), 'error');
  1111. return;
  1112. }
  1113. }
  1114. }
  1115. // if this is an update then clean out the existing joins and handlers so we can add new ones
  1116. if ($setup_id) {
  1117. db_query("DELETE FROM {tripal_views_field} WHERE setup_id = :setup", array(':setup' => $setup_id));
  1118. db_query("DELETE FROM {tripal_views_join} WHERE setup_id = :setup", array(':setup' => $setup_id));
  1119. db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id = :setup", array(':setup' => $setup_id));
  1120. }
  1121. // iterate through the columns of the form and add
  1122. // the joins if provided, and the handlers
  1123. $i = 1;
  1124. foreach (unserialize($form_state['values']['field_types']) as $key => $value) {
  1125. // add the field definition
  1126. $view_field_record = array(
  1127. 'setup_id' => $tripal_views_record['setup_id'],
  1128. 'column_name' => $key,
  1129. 'name' => $form_state['values']["fields_readable_name_$table_id-$i"],
  1130. 'description' => $form_state['values']["fields_description_$table_id-$i"],
  1131. 'type' => $value,
  1132. );
  1133. drupal_write_record('tripal_views_field', $view_field_record);
  1134. // add the hanlders
  1135. $handlers = array('filter', 'field', 'sort', 'argument');
  1136. foreach ($handlers as $handler) {
  1137. $handler_name = $form_state['values']["fields_" . $handler . "_handler_$table_id-$i"];
  1138. if ($handler_name) {
  1139. $handler_record = array(
  1140. 'setup_id' => $tripal_views_record['setup_id'],
  1141. 'column_name' => $key,
  1142. 'handler_type' => $handler,
  1143. 'handler_name' => $handler_name,
  1144. );
  1145. drupal_write_record('tripal_views_handlers', $handler_record);
  1146. }
  1147. }
  1148. $i++;
  1149. }
  1150. // Now add all the joins
  1151. for($i = 0; $i <= $form_state['values']['num_joins']; $i++) {
  1152. if (isset($form_state['values']["join_base_table-$i"])) {
  1153. $join_record = array(
  1154. 'setup_id' => $tripal_views_record['setup_id'],
  1155. 'base_table' => $form_state['values']["join_base_table-$i"],
  1156. 'base_field' => $form_state['values']["join_base_field-$i"],
  1157. 'left_table' => $form_state['values']["join_left_table-$i"],
  1158. 'left_field' => $form_state['values']["join_left_field-$i"],
  1159. 'handler' => $form_state['values']["join_join_handler-$i"],
  1160. 'relationship_handler' => $form_state['values']["join_relationship_handler-$i"],
  1161. 'relationship_only' => $form_state['values']["join_relationship_only-$i"]
  1162. );
  1163. drupal_write_record('tripal_views_join', $join_record);
  1164. }
  1165. }
  1166. // Now add the new join if there is one
  1167. if ($form_state['values']['new_join_base_field']) {
  1168. $join_record = array(
  1169. 'setup_id' => $tripal_views_record['setup_id'],
  1170. 'base_table' => $form_state['values']["new_join_base_table"],
  1171. 'base_field' => $form_state['values']["new_join_base_field"],
  1172. 'left_table' => $form_state['values']["new_join_left_table"],
  1173. 'left_field' => $form_state['values']["new_join_left_field"],
  1174. 'handler' => $form_state['values']["new_join_join_handler"],
  1175. 'relationship_handler' => $form_state['values']["new_join_relationship_handler"],
  1176. 'relationship_only' => $form_state['values']["new_join_relationship_only"]
  1177. );
  1178. drupal_write_record('tripal_views_join', $join_record);
  1179. }
  1180. if ($setup_id) {
  1181. drupal_set_message(t('Record Updated'));
  1182. }
  1183. else {
  1184. drupal_set_message(t('Record Added'));
  1185. }
  1186. $form_state['redirect'] = 'admin/tripal/views-integration/integrations';
  1187. // now clear all the caches so that Drupal views picks up our chages
  1188. views_invalidate_cache();
  1189. }
  1190. /**
  1191. * AJAX callback to replace the left field select list in the new join fieldset of the
  1192. * tripal_views_integration_form form
  1193. */
  1194. function tripal_views_integration_ajax_new_join_left_field_callback($form, $form_state) {
  1195. return $form['view_setup_join']['new_join'];
  1196. }
  1197. /**
  1198. * Purpose: this function queries all modules currently enabled on the site
  1199. * looking for custom handlers and returns a list of all available handerls.
  1200. * The base View handlers are also included.
  1201. *
  1202. * @return
  1203. * Returns an array of handler names
  1204. *
  1205. * @ingroup tripal_views
  1206. */
  1207. function tripal_views_integration_discover_handlers() {
  1208. $handlers = array();
  1209. // Get handlers from all modules.
  1210. foreach (module_implements('views_handlers') as $module) {
  1211. $function = $module . '_views_handlers';
  1212. $result = $function();
  1213. if (!is_array($result)) {
  1214. continue;
  1215. }
  1216. foreach ($result['handlers'] as $handler => $parent) {
  1217. $handlers[] = $handler;
  1218. }
  1219. }
  1220. // these handlers are hard coded because I could not
  1221. // get the views_handlers() function to be called
  1222. // in the code above. However, we will be creating
  1223. // Chado wrappers for many of these and once that work
  1224. // is done these will no longer be needed.
  1225. // argument handlers
  1226. $handlers[] = 'views_handler_argument';
  1227. $handlers[] = 'views_handler_argument_numeric';
  1228. $handlers[] = 'views_handler_argument_formula';
  1229. $handlers[] = 'views_handler_argument_date';
  1230. $handlers[] = 'views_handler_argument_string';
  1231. $handlers[] = 'views_handler_argument_many_to_one';
  1232. $handlers[] = 'views_handler_argument_null';
  1233. // field handlers
  1234. $handlers[] = 'views_handler_field';
  1235. $handlers[] = 'views_handler_field_date';
  1236. $handlers[] = 'views_handler_field_boolean';
  1237. $handlers[] = 'views_handler_field_markup';
  1238. $handlers[] = 'views_handler_field_xss';
  1239. $handlers[] = 'views_handler_field_url';
  1240. $handlers[] = 'views_handler_field_file_size';
  1241. $handlers[] = 'views_handler_field_prerender_list';
  1242. $handlers[] = 'views_handler_field_numeric';
  1243. $handlers[] = 'views_handler_field_custom';
  1244. $handlers[] = 'views_handler_field_counter';
  1245. // filter handlers
  1246. $handlers[] = 'views_handler_filter';
  1247. $handlers[] = 'views_handler_filter_equality';
  1248. $handlers[] = 'views_handler_filter_string';
  1249. $handlers[] = 'views_handler_filter_boolean_operator';
  1250. $handlers[] = 'views_handler_filter_boolean_operator_string';
  1251. $handlers[] = 'views_handler_filter_in_operator';
  1252. $handlers[] = 'views_handler_filter_numeric';
  1253. $handlers[] = 'views_handler_filter_date';
  1254. $handlers[] = 'views_handler_filter_many_to_one';
  1255. // relationship handlers
  1256. $handlers[] = 'views_handler_relationship';
  1257. // sort handlers
  1258. $handlers[] = 'views_handler_sort';
  1259. $handlers[] = 'views_handler_sort_formula';
  1260. $handlers[] = 'views_handler_sort_date';
  1261. $handlers[] = 'views_handler_sort_menu_hierarchy';
  1262. $handlers[] = 'views_handler_sort_random';
  1263. // join handler
  1264. $handlers[] = 'views_join';
  1265. return $handlers;
  1266. }
  1267. /*
  1268. * Ajax Callback: Tripal Views Integration Form
  1269. * Replaces the entire fields table when the table or materialized view is set/changed
  1270. *
  1271. * @ingroup tripal_views
  1272. */
  1273. function tripal_views_integration_ajax_view_setup_table($form, $form_state) {
  1274. //return $form['view_setup_table'];
  1275. return $form;
  1276. }
  1277. /*
  1278. * Ajax Callback: Tripal Views Integration Form
  1279. * Replaces the join db field dropdown when the join table dropdown is changed
  1280. *
  1281. * @ingroup tripal_views
  1282. */
  1283. function tripal_views_integration_ajax_join_field($form, $form_state) {
  1284. // Determine which row we are dealing with from the name of the triggering element
  1285. if (preg_match('/fields_join_(\w+-\d+)/', $form_state['triggering_element']['#name'], $matches)) {
  1286. $field = $matches[1];
  1287. $join_field = 'fields_join_column_' . $matches[1];
  1288. // return the form element to be updated
  1289. return $form['view_setup_table'][$field]['column-3'][$join_field];
  1290. }
  1291. else {
  1292. tripal_report_error('tripal_views', TRIPAL_ERROR,
  1293. 'Tripal Views Integration Ajax failed due to being unable to determine which row needs updating', array());
  1294. return $form;
  1295. }
  1296. }