tripal_views_integration_UI.inc 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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. $default_filter_handler = 0;
  623. if (isset($setup_id) && !isset($form_state['values']["fields_filter_handler_$table_id-$i"])) {
  624. $default_filter_handler = array_key_exists('filter', $default_handlers[$column_name]) ? $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 = array_key_exists("fields_filter_handler_$table_id-$i", $form_state['values']) ? $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 = '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 = 'views_handler_filter_string';
  635. }
  636. elseif ($column_type == 'boolean') {
  637. $default_filter_handler = 'views_handler_filter_boolean';
  638. }
  639. elseif ($column_type == 'float') {
  640. $default_filter_handler = 'views_handler_filter_float';
  641. }
  642. elseif ($column_type == 'datetime') {
  643. $default_filter_handler = '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 = array_key_exists('sort', $default_handlers[$column_name]) ? $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 = array_key_exists("fields_sort_handler_$table_id-$i", $form_state['values']) ? $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 = '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 = 'views_handler_sort';
  668. }
  669. elseif ($column_type == 'boolean') {
  670. $default_sort_handler = 'views_handler_sort';
  671. }
  672. elseif ($column_type == 'float') {
  673. $default_sort_handler = 'views_handler_sort';
  674. }
  675. elseif ($column_type == 'datetime') {
  676. $default_sort_handler = '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. } // end foreach ($columns as $column) {
  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. } // end foreach ($query->execute() as $i => $result) {
  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['#prefix'] = '<div id="tripal-views-integration-form">';
  981. $form['#suffix'] = '</div>';
  982. $form['#redirect'] = 'admin/tripal/views-integration/list';
  983. return $form;
  984. }
  985. /**
  986. * Purpose: validates the tripal_views_integration_form after submission
  987. *
  988. * @param $form
  989. * The form object which is passed automatically by drupal
  990. *
  991. * @param $form_state
  992. * The form state pbject which is passed automatically by drupal
  993. *
  994. * @ingroup tripal_views
  995. */
  996. function tripal_views_integration_form_validate($form, &$form_state) {
  997. $name_array = explode(" ", $form_state['values']['row_name']);
  998. $mview_id = $form_state['values']['mview_id'];
  999. $table_name = $form_state['values']['table_name'];
  1000. // if (count($name_array) > 1) {
  1001. // form_set_error($form_state['values']['row_name'], 'The View type name must be a single word only.');
  1002. // }
  1003. if ($mview_id and $table_name) {
  1004. form_set_error('mview_id', 'Please select either a materialized view or a Chado table but not both');
  1005. }
  1006. if (!$mview_id and !$table_name) {
  1007. form_set_error('mview_id', 'Please select either a materialized view or a Chado table');
  1008. }
  1009. // Ensure that users don't override the default integrations
  1010. if ($form_state['values']['row_priority'] >= 9) {
  1011. form_set_error('row_priority', 'A priority of 10 or 9 is reserved for default tripal '
  1012. .'views integrations created by core modules. Please set the priority between '
  1013. .'0 and -10 to ensure your changes are used rather over the defaults.');
  1014. }
  1015. // Check that if some fields for a new join are entered, all of them are
  1016. if (!empty($form_state['values']['new_join_base_field'])
  1017. OR !empty($form_state['values']['new_join_left_table'])
  1018. OR !empty($form_state['values']['new_join_left_field'])) {
  1019. if (!(!empty($form_state['values']['new_join_base_field'])
  1020. AND !empty($form_state['values']['new_join_left_table'])
  1021. AND !empty($form_state['values']['new_join_left_field']))) {
  1022. form_set_error('new_join_base_field','You need to select the Base Column, Left Table and Left Column to create a new join');
  1023. }
  1024. }
  1025. }
  1026. /**
  1027. * Purpose: inserts or updates the record in the tripal views integration
  1028. * tables. This function is only called if validation is passed.
  1029. *
  1030. * @param $form
  1031. * The form object which is passed automatically by drupal
  1032. *
  1033. * @param $form_state
  1034. * The form state pbject which is passed automatically by drupal
  1035. *
  1036. * @ingroup tripal_views
  1037. */
  1038. function tripal_views_integration_form_submit($form, &$form_state) {
  1039. $name = trim($form_state['values']['row_name']);
  1040. $mview_id = $form_state['values']['mview_id'];
  1041. $table_name = $form_state['values']['table_name'];
  1042. $setup_id = array_key_exists('setup_id', $form_state['values']) ? $form_state['values']['setup_id'] : "";
  1043. $priority = $form_state['values']['row_priority'];
  1044. $comment = $form_state['values']['row_description'];
  1045. // get details about this mview
  1046. if ($mview_id) {
  1047. // D7 TODO: Check DBTNG changes work
  1048. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :id";
  1049. $mview = db_query($sql, array(':id' => $mview_id));
  1050. $mview = $mview->fetchObject();
  1051. $table_name = $mview->mv_table;
  1052. $table_id = $mview_id;
  1053. $type = 'mview';
  1054. }
  1055. else {
  1056. $type = 'chado';
  1057. $table_id = $table_name;
  1058. }
  1059. // If this is for a materialized view then we want to add/update that record
  1060. $tripal_views_record = array();
  1061. if ($mview_id) {
  1062. // get details about this mview
  1063. // D7 TODO: Check DBTNG changes work
  1064. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :id";
  1065. $mview = db_query($sql, array(':id' => $mview_id));
  1066. $mview = $mview->fetchObject();
  1067. // build the record for insert/update
  1068. $tripal_views_record = array(
  1069. 'mview_id' => $mview_id,
  1070. 'table_name' => $mview->mv_table,
  1071. 'name' => $name,
  1072. 'priority' => $priority,
  1073. 'comment' => $comment,
  1074. 'base_table' => $form_state['values']['base_table']
  1075. );
  1076. }
  1077. // if a chado table then...
  1078. else {
  1079. // build the record for insert/update
  1080. $tripal_views_record = array(
  1081. 'table_name' => $table_name,
  1082. 'name' => $name,
  1083. 'priority' => $priority,
  1084. 'comment' => $comment,
  1085. 'base_table' => $form_state['values']['base_table']
  1086. );
  1087. }
  1088. // perform the insert or update
  1089. if (!$setup_id) { // this is an insert
  1090. if (!drupal_write_record('tripal_views', $tripal_views_record)) {
  1091. drupal_set_message(t("Failed to add record."), 'error');
  1092. return;
  1093. }
  1094. }
  1095. else { // this is an update
  1096. // check to see if it was specified to create a new integration
  1097. if ($form_state['values']['new_integration']) {
  1098. $setup_id = NULL;
  1099. if (!drupal_write_record('tripal_views', $tripal_views_record)) {
  1100. drupal_set_message(t("Failed to add record."), 'error');
  1101. return;
  1102. }
  1103. }
  1104. else {
  1105. $tripal_views_record['setup_id'] = $setup_id;
  1106. if (!drupal_write_record('tripal_views', $tripal_views_record, array('setup_id'))) {
  1107. drupal_set_message(t("Failed to update record."), 'error');
  1108. return;
  1109. }
  1110. }
  1111. }
  1112. // if this is an update then clean out the existing joins and handlers so we can add new ones
  1113. if ($setup_id) {
  1114. db_query("DELETE FROM {tripal_views_field} WHERE setup_id = :setup", array(':setup' => $setup_id));
  1115. db_query("DELETE FROM {tripal_views_join} WHERE setup_id = :setup", array(':setup' => $setup_id));
  1116. db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id = :setup", array(':setup' => $setup_id));
  1117. }
  1118. // iterate through the columns of the form and add
  1119. // the joins if provided, and the handlers
  1120. $i = 1;
  1121. foreach (unserialize($form_state['values']['field_types']) as $key => $value) {
  1122. // add the field definition
  1123. $view_field_record = array(
  1124. 'setup_id' => $tripal_views_record['setup_id'],
  1125. 'column_name' => $key,
  1126. 'name' => $form_state['values']["fields_readable_name_$table_id-$i"],
  1127. 'description' => $form_state['values']["fields_description_$table_id-$i"],
  1128. 'type' => $value,
  1129. );
  1130. drupal_write_record('tripal_views_field', $view_field_record);
  1131. // add the hanlders
  1132. $handlers = array('filter', 'field', 'sort', 'argument');
  1133. foreach ($handlers as $handler) {
  1134. $handler_name = $form_state['values']["fields_" . $handler . "_handler_$table_id-$i"];
  1135. if ($handler_name) {
  1136. $handler_record = array(
  1137. 'setup_id' => $tripal_views_record['setup_id'],
  1138. 'column_name' => $key,
  1139. 'handler_type' => $handler,
  1140. 'handler_name' => $handler_name,
  1141. );
  1142. drupal_write_record('tripal_views_handlers', $handler_record);
  1143. }
  1144. }
  1145. $i++;
  1146. }
  1147. // Now add all the joins
  1148. for($i = 0; $i <= $form_state['values']['num_joins']; $i++) {
  1149. if (isset($form_state['values']["join_base_table-$i"])) {
  1150. $join_record = array(
  1151. 'setup_id' => $tripal_views_record['setup_id'],
  1152. 'base_table' => $form_state['values']["join_base_table-$i"],
  1153. 'base_field' => $form_state['values']["join_base_field-$i"],
  1154. 'left_table' => $form_state['values']["join_left_table-$i"],
  1155. 'left_field' => $form_state['values']["join_left_field-$i"],
  1156. 'handler' => $form_state['values']["join_join_handler-$i"],
  1157. 'relationship_handler' => $form_state['values']["join_relationship_handler-$i"],
  1158. 'relationship_only' => $form_state['values']["join_relationship_only-$i"]
  1159. );
  1160. drupal_write_record('tripal_views_join', $join_record);
  1161. }
  1162. }
  1163. // Now add the new join if there is one
  1164. if ($form_state['values']['new_join_base_field']) {
  1165. $join_record = array(
  1166. 'setup_id' => $tripal_views_record['setup_id'],
  1167. 'base_table' => $form_state['values']["new_join_base_table"],
  1168. 'base_field' => $form_state['values']["new_join_base_field"],
  1169. 'left_table' => $form_state['values']["new_join_left_table"],
  1170. 'left_field' => $form_state['values']["new_join_left_field"],
  1171. 'handler' => $form_state['values']["new_join_join_handler"],
  1172. 'relationship_handler' => $form_state['values']["new_join_relationship_handler"],
  1173. 'relationship_only' => $form_state['values']["new_join_relationship_only"]
  1174. );
  1175. drupal_write_record('tripal_views_join', $join_record);
  1176. }
  1177. if ($setup_id) {
  1178. drupal_set_message(t('Record Updated'));
  1179. }
  1180. else {
  1181. drupal_set_message(t('Record Added'));
  1182. }
  1183. $form_state['redirect'] = 'admin/tripal/views-integration/integrations';
  1184. // now clear all the caches so that Drupal views picks up our chages
  1185. views_invalidate_cache();
  1186. }
  1187. /**
  1188. * AJAX callback to replace the left field select list in the new join fieldset of the
  1189. * tripal_views_integration_form form
  1190. */
  1191. function tripal_views_integration_ajax_new_join_left_field_callback($form, $form_state) {
  1192. return $form['view_setup_join']['new_join'];
  1193. }
  1194. /**
  1195. * Purpose: this function queries all modules currently enabled on the site
  1196. * looking for custom handlers and returns a list of all available handerls.
  1197. * The base View handlers are also included.
  1198. *
  1199. * @return
  1200. * Returns an array of handler names
  1201. *
  1202. * @ingroup tripal_views
  1203. */
  1204. function tripal_views_integration_discover_handlers() {
  1205. $handlers = array();
  1206. // Get handlers from all modules.
  1207. foreach (module_implements('views_handlers') as $module) {
  1208. $function = $module . '_views_handlers';
  1209. $result = $function();
  1210. if (!is_array($result)) {
  1211. continue;
  1212. }
  1213. foreach ($result['handlers'] as $handler => $parent) {
  1214. $handlers[] = $handler;
  1215. }
  1216. }
  1217. // these handlers are hard coded because I could not
  1218. // get the views_handlers() function to be called
  1219. // in the code above. However, we will be creating
  1220. // Chado wrappers for many of these and once that work
  1221. // is done these will no longer be needed.
  1222. // argument handlers
  1223. $handlers[] = 'views_handler_argument';
  1224. $handlers[] = 'views_handler_argument_numeric';
  1225. $handlers[] = 'views_handler_argument_formula';
  1226. $handlers[] = 'views_handler_argument_date';
  1227. $handlers[] = 'views_handler_argument_string';
  1228. $handlers[] = 'views_handler_argument_many_to_one';
  1229. $handlers[] = 'views_handler_argument_null';
  1230. // field handlers
  1231. $handlers[] = 'views_handler_field';
  1232. $handlers[] = 'views_handler_field_date';
  1233. $handlers[] = 'views_handler_field_boolean';
  1234. $handlers[] = 'views_handler_field_markup';
  1235. $handlers[] = 'views_handler_field_xss';
  1236. $handlers[] = 'views_handler_field_url';
  1237. $handlers[] = 'views_handler_field_file_size';
  1238. $handlers[] = 'views_handler_field_prerender_list';
  1239. $handlers[] = 'views_handler_field_numeric';
  1240. $handlers[] = 'views_handler_field_custom';
  1241. $handlers[] = 'views_handler_field_counter';
  1242. // filter handlers
  1243. $handlers[] = 'views_handler_filter';
  1244. $handlers[] = 'views_handler_filter_equality';
  1245. $handlers[] = 'views_handler_filter_string';
  1246. $handlers[] = 'views_handler_filter_boolean_operator';
  1247. $handlers[] = 'views_handler_filter_boolean_operator_string';
  1248. $handlers[] = 'views_handler_filter_in_operator';
  1249. $handlers[] = 'views_handler_filter_numeric';
  1250. $handlers[] = 'views_handler_filter_float';
  1251. $handlers[] = 'views_handler_filter_date';
  1252. $handlers[] = 'views_handler_filter_many_to_one';
  1253. // relationship handlers
  1254. $handlers[] = 'views_handler_relationship';
  1255. // sort handlers
  1256. $handlers[] = 'views_handler_sort';
  1257. $handlers[] = 'views_handler_sort_formula';
  1258. $handlers[] = 'views_handler_sort_date';
  1259. $handlers[] = 'views_handler_sort_menu_hierarchy';
  1260. $handlers[] = 'views_handler_sort_random';
  1261. // join handler
  1262. $handlers[] = 'views_join';
  1263. return $handlers;
  1264. }
  1265. /*
  1266. * Ajax Callback: Tripal Views Integration Form
  1267. * Replaces the entire fields table when the table or materialized view is set/changed
  1268. *
  1269. * @ingroup tripal_views
  1270. */
  1271. function tripal_views_integration_ajax_view_setup_table($form, $form_state) {
  1272. //return $form['view_setup_table'];
  1273. return $form;
  1274. }
  1275. /*
  1276. * Ajax Callback: Tripal Views Integration Form
  1277. * Replaces the join db field dropdown when the join table dropdown is changed
  1278. *
  1279. * @ingroup tripal_views
  1280. */
  1281. function tripal_views_integration_ajax_join_field($form, $form_state) {
  1282. // Determine which row we are dealing with from the name of the triggering element
  1283. if (preg_match('/fields_join_(\w+-\d+)/', $form_state['triggering_element']['#name'], $matches)) {
  1284. $field = $matches[1];
  1285. $join_field = 'fields_join_column_' . $matches[1];
  1286. // return the form element to be updated
  1287. return $form['view_setup_table'][$field]['column-3'][$join_field];
  1288. }
  1289. else {
  1290. tripal_report_error('tripal_views', TRIPAL_ERROR,
  1291. 'Tripal Views Integration Ajax failed due to being unable to determine which row needs updating', array());
  1292. return $form;
  1293. }
  1294. }