tripal_chado_views_integration_UI.inc 53 KB

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