tripal_views_integration.inc 48 KB

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