tripal_views_integration.inc 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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. );
  414. // get the columns in this materialized view. They are separated by commas
  415. // where the first word is the column name and the rest is the type
  416. $columns = array();
  417. if ($mview_id) {
  418. // D7 TODO: Check DBTNG changes work
  419. $sql = "SELECT mv_specs FROM {tripal_mviews} WHERE mview_id = :id";
  420. $mview = db_query($sql, array(':id' => $mview_id));
  421. $mview = $mview->fetchObject();
  422. $columns = explode(",", $mview->mv_specs);
  423. }
  424. else {
  425. $table_desc = tripal_core_get_chado_table_schema($table_name);
  426. if ($table_desc) {
  427. $fields = $table_desc['fields'];
  428. // iterate through the columns and build the format
  429. // compatible with the code below. The column name is first followed
  430. // by the type with a separating space
  431. foreach ($fields as $column => $attrs) {
  432. $columns[] = "$column " . $attrs['type'];
  433. }
  434. }
  435. }
  436. $i=1;
  437. $form['view_setup_table']["instructions"] = array(
  438. '#type' => 'markup',
  439. '#value' => filter_xss("Select an optional table to which the fields of the ".
  440. "materialized view can join. If a field does not need to ".
  441. "join you may leave the selection blank."),
  442. );
  443. $data['field_types'] = array();
  444. // get the list of chado tables to join on
  445. $chado_join_tables = tripal_core_get_chado_tables(TRUE);
  446. $chado_join_tables = array_merge(array('Select a Join Table'), $chado_join_tables);
  447. // get list of all handlers
  448. $all_handlers = tripal_views_integration_discover_handlers();
  449. $handlers_fields = array(0 => "Select a field handler");
  450. $handlers_filters = array(0 => "Select a filter handler");
  451. $handlers_sort = array(0 => "Select a sort handler");
  452. $handlers_argument = array(0 => "Select an argument handler");
  453. $handlers_join = array(0 => "Select a join handler");
  454. $handlers_rel = array(0 => "Select a relationship handler");
  455. foreach ($all_handlers as $handler) {
  456. if (preg_match("/views_handler_field/", $handler)) {
  457. $handlers_fields[$handler] = $handler;
  458. }
  459. if (preg_match("/views_handler_filter/", $handler)) {
  460. $handlers_filters[$handler] = $handler;
  461. }
  462. if (preg_match("/views_handler_sort/", $handler)) {
  463. $handlers_sort[$handler] = $handler;
  464. }
  465. if (preg_match("/views_handler_argument/", $handler)) {
  466. $handlers_argument[$handler] = $handler;
  467. }
  468. if (preg_match("/_join/", $handler)) {
  469. $handlers_join[$handler] = $handler;
  470. }
  471. if (preg_match("/views_handler_relationship/", $handler)) {
  472. $handlers_rel[$handler] = $handler;
  473. }
  474. }
  475. // generate a unique $table_id for keeping track of the table
  476. if ($mview_id) {
  477. $table_id = $mview_id;
  478. }
  479. else {
  480. $table_id = $table_name;
  481. }
  482. // Per Row (Fields) --------------
  483. // now iterate through the columns of the materialized view or
  484. // chado table and generate the join and handler fields
  485. foreach ($columns as $column) {
  486. $column = trim($column); // trim trailing and leading spaces
  487. preg_match("/^(.*?)\ (.*?)$/", $column, $matches);
  488. $column_name = $matches[1];
  489. $column_type = $matches[2];
  490. $form['view_setup_table']["$table_id-$i"] = array(
  491. '#type' => 'markup',
  492. '#prefix' => "<div class=\"fields-new-row\">",
  493. '#suffix' => "</div>",
  494. '#value' => filter_xss('')
  495. );
  496. // COLUMN I
  497. $form['view_setup_table']["$table_id-$i"]["fields_name_$table_id-$i"] = array(
  498. '#type' => 'item',
  499. '#prefix' => "<div class=\"column-one\">",
  500. '#markup' => '<span class="column-name">' . filter_xss($column_name) . '</span>'.
  501. '<br /><span class="column-type">' . filter_xss($column_type) . '</span>',
  502. '#suffix' => "</div>",
  503. );
  504. $data['field_types'][$column_name] = $column_type;
  505. // COLUMN II
  506. $form['view_setup_table']["$table_id-$i"]['column-2'] = array(
  507. '#type' => 'markup',
  508. '#prefix' => "<div class=\"column-two\">",
  509. '#suffix' => "</div>",
  510. '#value' => filter_xss('')
  511. );
  512. // set the default values for the human-readable name and description
  513. $default_name = '';
  514. $default_descrip = '';
  515. if (isset($setup_id) && !isset($form_state['values']["fields_readable_name_$table_id-$i"])) {
  516. $default_name = $default_fields[$column_name]['name'];
  517. $default_descrip = $default_fields[$column_name]['description'];
  518. }
  519. elseif (isset($form_state['values']["fields_readable_name_$table_id-$i"])) {
  520. $default_name = $form_state['values']["fields_readable_name_$table_id-$i"];
  521. $default_descrip = $form_state['values']["fields_description_$table_id-$i"];
  522. }
  523. else {
  524. $default_name = ucwords(str_replace('_',' ', $column_name));
  525. $default_descrip = 'TODO: please describe this field!';
  526. }
  527. $form['view_setup_table']["$table_id-$i"]['column-2']["fields_readable_name_$table_id-$i"] = array(
  528. '#type' => 'textfield',
  529. '#title' => 'Human-Readable Name',
  530. '#description' => 'This is the name of the field in the Views UI',
  531. '#required' => TRUE,
  532. '#default_value' => $default_name,
  533. '#size' => 42,
  534. );
  535. $form['view_setup_table']["$table_id-$i"]['column-2']["fields_description_$table_id-$i"] = array(
  536. '#type' => 'textarea',
  537. '#title' => 'Short Description',
  538. '#description' => 'This is the field help in the Views UI',
  539. '#required' => TRUE,
  540. '#cols' => 42,
  541. '#rows' => 3,
  542. '#default_value' => $default_descrip,
  543. );
  544. // COLUMN III
  545. $form['view_setup_table']["$table_id-$i"]['column-3'] = array(
  546. '#type' => 'markup',
  547. '#prefix' => "<div class=\"column-three\">",
  548. '#suffix' => "</div>",
  549. '#value' => filter_xss('')
  550. );
  551. // set the default values for the join table and columns
  552. $default_join_table = 0;
  553. $default_join_field = 0;
  554. if (isset($setup_id) && !isset($form_state['values']["fields_join_$table_id-$i"]) && isset($default_joins[$column_name])) {
  555. $default_join_table = $default_joins[$column_name]['left_table'];
  556. $default_join_field = $default_joins[$column_name]['left_field'];
  557. $form_state['values']["fields_join_$table_id-$i"] = $default_join_table;
  558. $form_state['values']["fields_join_column_$table_id-$i"] = $default_join_field;
  559. }
  560. else {
  561. if (isset($form_state['values']["fields_join_$table_id-$i"])) {
  562. $default_join_table = $form_state['values']["fields_join_$table_id-$i"];
  563. }
  564. if (isset($form_state['values']["fields_join_column_$table_id-$i"])) {
  565. $default_join_field = $form_state['values']["fields_join_column_$table_id-$i"];
  566. }
  567. }
  568. $form['view_setup_table']["$table_id-$i"]['column-3']["fields_join_$table_id-$i"] = array(
  569. '#type' => 'select',
  570. '#prefix' => "<div class=\"fields-column-join\">",
  571. '#suffix' => "</div>",
  572. '#options' => $chado_join_tables,
  573. '#required' => FALSE,
  574. '#default_value' => $default_join_table,
  575. '#ajax' => array(
  576. 'callback' => 'tripal_views_integration_ajax_join_field',
  577. 'wrapper' => "fields-column-join-column-$table_id-$i",
  578. 'effect' => 'fade',
  579. 'event' => 'change',
  580. 'method' => 'replace',
  581. ),
  582. );
  583. $columns = array();
  584. if ($default_join_table) {
  585. // get the table description
  586. $table_desc = tripal_core_get_chado_table_schema($default_join_table);
  587. foreach ($table_desc['fields'] as $column => $def) {
  588. $columns[$column] = $column;
  589. }
  590. }
  591. else {
  592. $columns = array('Select Join Column');
  593. }
  594. $form['view_setup_table']["$table_id-$i"]['column-3']["fields_join_column_$table_id-$i"] = array(
  595. '#type' => 'select',
  596. '#prefix' => " <div id=\"fields-column-join-column-$table_id-$i\" class=\"fields-column-join-column\">",
  597. '#suffix' => "</div>",
  598. '#options' => $columns,
  599. '#required' => FALSE,
  600. '#default_value' => $default_join_field
  601. );
  602. $default_join_handler = 0;
  603. if (isset($setup_id) && !isset($form_state['values']["fields_join_handler_$table_id-$i"]) && isset($default_handlers[$column_name]['join'])) {
  604. $default_join_handler = $default_handlers[$column_name]['join']['handler_name'];
  605. $form_state['values']["fields_join_handler_$table_id-$i"] = $default_join_handler;
  606. }
  607. elseif (isset($form_state['values']["fields_join_handler_$table_id-$i"])) {
  608. $default_join_handler = $form_state['values']["fields_join_handler_$table_id-$i"];
  609. }
  610. $form['view_setup_table']["$table_id-$i"]['column-3']["fields_join_handler_$table_id-$i"] = array(
  611. '#type' => 'select',
  612. '#prefix' => "<div class=\"fields-join-handler\">",
  613. '#suffix' => "</div>",
  614. '#options' => $handlers_join,
  615. '#required' => FALSE,
  616. '#default_value' => $default_join_handler,
  617. );
  618. // COLUMN 4
  619. $form['view_setup_table']["$table_id-$i"]['column-4'] = array(
  620. '#type' => 'markup',
  621. '#prefix' => "<div class=\"column-four\">",
  622. '#suffix' => "</div>",
  623. '#value' => filter_xss('')
  624. );
  625. // create the handler fields
  626. $default_field_handler = 0;
  627. if (isset($setup_id) && !isset($form_state['values']["fields_field_handler_$table_id-$i"])) {
  628. $default_field_handler = $default_handlers[$column_name]['field']['handler_name'];
  629. $form_state['values']["fields_field_handler_$table_id-$i"] = $default_field_handler;
  630. }
  631. else {
  632. $default_field_handler = $form_state['values']["fields_field_handler_$table_id-$i"];
  633. if (!$default_field_handler) {
  634. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  635. $default_field_handler = 'chado_views_handler_field_numeric';
  636. }
  637. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  638. $default_field_handler = 'chado_views_handler_field';
  639. }
  640. elseif ($column_type == 'boolean') {
  641. $default_field_handler = 'chado_views_handler_field_boolean';
  642. }
  643. elseif ($column_type == 'float') {
  644. $default_field_handler = 'chado_views_handler_field_numeric';
  645. }
  646. elseif ($column_type == 'datetime') {
  647. $default_field_handler = 'chado_views_handler_field_date';
  648. }
  649. }
  650. }
  651. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_field_handler_$table_id-$i"] = array(
  652. '#type' => 'select',
  653. '#prefix' => "<div class=\"fields-field-handler\">",
  654. '#suffix' => "</div>",
  655. '#options' => $handlers_fields,
  656. '#required' => FALSE,
  657. '#default_value' => $default_field_handler,
  658. );
  659. $default_filter_handler = 0;
  660. if (isset($setup_id) && !isset($form_state['values']["fields_filter_handler_$table_id-$i"])) {
  661. $default_filter_handler = $default_handlers[$column_name]['filter']['handler_name'];
  662. $form_state['values']["fields_filter_handler_$table_id-$i"]= $default_filter_handler;
  663. }
  664. else {
  665. $default_filter_handler = $form_state['values']["fields_filter_handler_$table_id-$i"];
  666. if (!$default_filter_handler) {
  667. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  668. $default_filter_handler = 'chado_views_handler_filter_numeric';
  669. }
  670. elseif (preg_match("/^character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  671. $default_filter_handler = 'chado_views_handler_filter_string';
  672. }
  673. elseif ($column_type == 'boolean') {
  674. $default_filter_handler = 'chado_views_handler_filter_boolean';
  675. }
  676. elseif ($column_type == 'float') {
  677. $default_filter_handler = 'chado_views_handler_filter_float';
  678. }
  679. elseif ($column_type == 'datetime') {
  680. $default_filter_handler = 'chado_views_handler_filter_date';
  681. }
  682. }
  683. }
  684. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_filter_handler_$table_id-$i"] = array(
  685. '#type' => 'select',
  686. '#prefix' => "<div class=\"fields-filter-handler\">",
  687. '#suffix' => "</div>",
  688. '#options' => $handlers_filters,
  689. '#required' => FALSE,
  690. '#default_value' => $default_filter_handler,
  691. );
  692. $default_sort_handler = 0;
  693. if (isset($setup_id) && !isset($form_state['values']["fields_sort_handler_$table_id-$i"])) {
  694. $default_sort_handler = $default_handlers[$column_name]['sort']['handler_name'];
  695. $form_state['values']["fields_sort_handler_$table_id-$i"] = $default_sort_handler;
  696. }
  697. else {
  698. $default_sort_handler = $form_state['values']["fields_sort_handler_$table_id-$i"];
  699. if (!$default_sort_handler) {
  700. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  701. $default_sort_handler = 'chado_views_handler_sort';
  702. }
  703. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  704. $default_sort_handler = 'chado_views_handler_sort';
  705. }
  706. elseif ($column_type == 'boolean') {
  707. $default_sort_handler = 'chado_views_handler_sort';
  708. }
  709. elseif ($column_type == 'float') {
  710. $default_sort_handler = 'chado_views_handler_sort';
  711. }
  712. elseif ($column_type == 'datetime') {
  713. $default_sort_handler = 'chado_views_handler_sort_date';
  714. }
  715. }
  716. }
  717. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_sort_handler_$table_id-$i"] = array(
  718. '#type' => 'select',
  719. '#prefix' => "<div class=\"fields-sort-handler\">",
  720. '#suffix' => "</div>",
  721. '#options' => $handlers_sort,
  722. '#required' => FALSE,
  723. '#default_value' => $default_sort_handler,
  724. );
  725. $default_argument_handler = 0;
  726. if (isset($setup_id)
  727. && !isset($form_state['values']["fields_argument_handler_$table_id-$i"])
  728. && isset($default_handlers[$column_name]['argument']))
  729. {
  730. $default_argument_handler = $default_handlers[$column_name]['argument']['handler_name'];
  731. $form_state['values']["fields_argument_handler_$table_id-$i"]=$default_argument_handler ;
  732. }
  733. else {
  734. if (isset($form_state['values']["fields_argument_handler_$table_id-$i"])) {
  735. $default_argument_handler = $form_state['values']["fields_argument_handler_$table_id-$i"];
  736. }
  737. if (!$default_argument_handler) {
  738. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  739. $default_argument_handler = 'views_handler_argument_numeric';
  740. }
  741. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  742. $default_argument_handler = 'views_handler_argument_string';
  743. }
  744. elseif ($column_type == 'boolean') {
  745. $default_argument_handler = 'views_handler_argument_numeric';
  746. }
  747. elseif ($column_type == 'float') {
  748. $default_argument_handler = 'views_handler_argument_numeric';
  749. }
  750. elseif ($column_type == 'datetime') {
  751. $default_argument_handler = 'views_handler_argument_date';
  752. }
  753. }
  754. }
  755. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_argument_handler_$table_id-$i"] = array(
  756. '#type' => 'select',
  757. '#prefix' => "<div class=\"fields-argument-handler\">",
  758. '#suffix' => "</div>",
  759. '#options' => $handlers_argument,
  760. '#required' => FALSE,
  761. '#default_value' => $default_argument_handler,
  762. );
  763. $default_relationship_handler = 0;
  764. if (isset($setup_id)
  765. && !isset($form_state['values']["fields_relationship_handler_$table_id-$i"])
  766. && isset($default_handlers[$column_name]['relationship']))
  767. {
  768. $default_relationship_handler = $default_handlers[$column_name]['relationship']['handler_name'];
  769. $form_state['values']["fields_relationship_handler_$table_id-$i"]=$default_relationship_handler;
  770. }
  771. else {
  772. if (isset($form_state['values']["fields_relationship_handler_$table_id-$i"])) {
  773. $default_relationship_handler = $form_state['values']["fields_relationship_handler_$table_id-$i"];
  774. }
  775. if (!$default_relationship_handler) {
  776. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  777. $default_relationship_handler = 'views_handler_relationship';
  778. }
  779. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  780. $default_relationship_handler = 'views_handler_relationship';
  781. }
  782. elseif ($column_type == 'boolean') {
  783. $default_relationship_handler = 'views_handler_relationship';
  784. }
  785. elseif ($column_type == 'float') {
  786. $default_relationship_handler = 'views_handler_relationship';
  787. }
  788. elseif ($column_type == 'datetime') {
  789. $default_relationship_handler = 'views_handler_relationship';
  790. }
  791. }
  792. }
  793. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_relationship_handler_$table_id-$i"] = array(
  794. '#type' => 'select',
  795. '#prefix' => "<div class=\"fields-relationship-handler\">",
  796. '#suffix' => "</div>",
  797. '#options' => $handlers_rel,
  798. '#required' => FALSE,
  799. '#default_value' => $default_relationship_handler,
  800. );
  801. $i++;
  802. }
  803. $form['view_setup_table']['save'] = array(
  804. '#type' => 'submit',
  805. '#value' => t('Save'),
  806. );
  807. $data['row_count'] = $i - 1;
  808. }
  809. //use this to put values into $form_state['values']
  810. $form['data'] = array();
  811. // Ensure that we don't store an array
  812. // since we will get a check_plain:htmlspecial_characters error if we do
  813. foreach ($data as $key => $value) {
  814. if (is_array($value)) {
  815. $form['data'][$key] = array(
  816. '#type' => 'hidden',
  817. '#value' => serialize($value),
  818. );
  819. }
  820. else {
  821. $form['data'][$key] = array(
  822. '#type' => 'hidden',
  823. '#value' => $value,
  824. );
  825. }
  826. }
  827. $form['#redirect'] = 'admin/tripal/views-integration/list';
  828. return $form;
  829. }
  830. /**
  831. * Purpose: validates the tripal_views_integration_form after submission
  832. *
  833. * @param $form
  834. * The form object which is passed automatically by drupal
  835. *
  836. * @param $form_state
  837. * The form state pbject which is passed automatically by drupal
  838. *
  839. * @ingroup tripal_views_integration
  840. */
  841. function tripal_views_integration_form_validate($form, &$form_state) {
  842. $name_array = explode(" ", $form_state['values']['row_name']);
  843. $mview_id = $form_state['values']['mview_id'];
  844. $table_name = $form_state['values']['table_name'];
  845. // if (count($name_array) > 1) {
  846. // form_set_error($form_state['values']['row_name'], 'The View type name must be a single word only.');
  847. // }
  848. if ($mview_id and $table_name) {
  849. form_set_error('mview_id', 'Please select either a materialized view or a Chado table but not both');
  850. }
  851. if (!$mview_id and !$table_name) {
  852. form_set_error('mview_id', 'Please select either a materialized view or a Chado table');
  853. }
  854. // Ensure that users don't override the default integrations
  855. if ($form_state['values']['row_priority'] >= 9) {
  856. form_set_error('row_priority', 'A priority of 10 or 9 is reserved for default tripal '
  857. .'views integrations created by core modules. Please set the priority between '
  858. .'0 and -10 to ensure your changes are used rather over the defaults.');
  859. }
  860. // TODO: do we need to require that a handler be set for each field and each type of handler?
  861. }
  862. /**
  863. * Purpose: inserts or updates the record in the tripal views integration
  864. * tables. This function is only called if validation is passed.
  865. *
  866. * @param $form
  867. * The form object which is passed automatically by drupal
  868. *
  869. * @param $form_state
  870. * The form state pbject which is passed automatically by drupal
  871. *
  872. * @ingroup tripal_views_integration
  873. */
  874. function tripal_views_integration_form_submit($form, &$form_state) {
  875. $name = $form_state['values']['row_name'];
  876. $mview_id = $form_state['values']['mview_id'];
  877. $table_name = $form_state['values']['table_name'];
  878. $setup_id = $form_state['values']['setup_id'];
  879. $priority = $form_state['values']['row_priority'];
  880. $comment = $form_state['values']['row_description'];
  881. // get details about this mview
  882. if ($mview_id) {
  883. // D7 TODO: Check DBTNG changes work
  884. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :id";
  885. $mview = db_query($sql, array(':id' => $mview_id));
  886. $mview = $mview->fetchObject();
  887. $table_name = $mview->mv_table;
  888. $table_id = $mview_id;
  889. $type = 'mview';
  890. }
  891. else {
  892. $type = 'chado';
  893. $table_id = $table_name;
  894. }
  895. // If this is for a materialized view then we want to add/update that record
  896. $tripal_views_record = array();
  897. if ($mview_id) {
  898. // get details about this mview
  899. // D7 TODO: Check DBTNG changes work
  900. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :id";
  901. $mview = db_query($sql, array(':id' => $mview_id));
  902. $mview = $mview->fetchObject();
  903. // build the record for insert/update
  904. $tripal_views_record = array(
  905. 'mview_id' => $mview_id,
  906. 'table_name' => $mview->mv_table,
  907. 'name' => $name,
  908. 'priority' => $priority,
  909. 'comment' => $comment,
  910. 'base_table' => $form_state['values']['base_table']
  911. );
  912. }
  913. // if a chado table then...
  914. else {
  915. // build the record for insert/update
  916. $tripal_views_record = array(
  917. 'table_name' => $table_name,
  918. 'name' => $name,
  919. 'priority' => $priority,
  920. 'comment' => $comment,
  921. 'base_table' => $form_state['values']['base_table']
  922. );
  923. }
  924. // perform the insert or update
  925. if (!$setup_id) { // this is an insert
  926. if (!drupal_write_record('tripal_views', $tripal_views_record)) {
  927. drupal_set_message(t("Failed to add record."), 'error');
  928. return;
  929. }
  930. }
  931. else { // this is an update
  932. // check to see if it was specified to create a new integration
  933. if ($form_state['values']['new_integration']) {
  934. $setup_id = NULL;
  935. if (!drupal_write_record('tripal_views', $tripal_views_record)) {
  936. drupal_set_message(t("Failed to add record."), 'error');
  937. return;
  938. }
  939. }
  940. else {
  941. $tripal_views_record['setup_id'] = $setup_id;
  942. if (!drupal_write_record('tripal_views', $tripal_views_record, array('setup_id'))) {
  943. drupal_set_message(t("Failed to update record."), 'error');
  944. return;
  945. }
  946. }
  947. }
  948. // if this is an update then clean out the existing joins and handlers so we can add new ones
  949. if ($setup_id) {
  950. db_query("DELETE FROM {tripal_views_field} WHERE setup_id = :setup", array(':setup' => $setup_id));
  951. db_query("DELETE FROM {tripal_views_join} WHERE setup_id = :setup", array(':setup' => $setup_id));
  952. db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id = :setup", array(':setup' => $setup_id));
  953. }
  954. // iterate through the columns of the form and add
  955. // the joins if provided, and the handlers
  956. $i = 1;
  957. foreach (unserialize($form_state['values']['field_types']) as $key => $value) {
  958. // add the field definition
  959. $view_field_record = array(
  960. 'setup_id' => $tripal_views_record['setup_id'],
  961. 'column_name' => $key,
  962. 'name' => $form_state['values']["fields_readable_name_$table_id-$i"],
  963. 'description' => $form_state['values']["fields_description_$table_id-$i"],
  964. 'type' => $value,
  965. );
  966. drupal_write_record('tripal_views_field', $view_field_record);
  967. // first add the join if it exists
  968. $left_table = $form_state['values']["fields_join_$table_id-$i"];
  969. $left_column = $form_state['values']["fields_join_column_$table_id-$i"];
  970. if ($left_column) {
  971. if ($mview_id) {
  972. $base_table = $mview->mv_table;
  973. }
  974. else {
  975. $base_table = $table_name;
  976. }
  977. $view_join_record = array(
  978. 'setup_id' => $tripal_views_record['setup_id'],
  979. 'base_table' => $base_table,
  980. 'base_field' => $key,
  981. 'left_table' => $left_table,
  982. 'left_field' => $left_column,
  983. 'handler' => $form_state['values']["fields_join_handler_$table_id-$i"],
  984. );
  985. // write the new joins to the database
  986. drupal_write_record('tripal_views_join', $view_join_record);
  987. }
  988. // add the hanlders
  989. $handlers = array('filter', 'field', 'sort', 'argument', 'join', 'relationship');
  990. foreach ($handlers as $handler) {
  991. $handler_name = $form_state['values']["fields_" . $handler . "_handler_$table_id-$i"];
  992. if ($handler_name) {
  993. $handler_record = array(
  994. 'setup_id' => $tripal_views_record['setup_id'],
  995. 'column_name' => $key,
  996. 'handler_type' => $handler,
  997. 'handler_name' => $handler_name,
  998. );
  999. drupal_write_record('tripal_views_handlers', $handler_record);
  1000. }
  1001. }
  1002. $i++;
  1003. }
  1004. if ($setup_id) {
  1005. drupal_set_message(t('Record Updated'));
  1006. }
  1007. else {
  1008. drupal_set_message(t('Record Added'));
  1009. }
  1010. $form_state['redirect'] = 'admin/tripal/views-integration/list';
  1011. // now clear all the caches so that Drupal views picks up our chages
  1012. views_invalidate_cache();
  1013. }
  1014. /**
  1015. * Purpose: this function queries all modules currently enabled on the site
  1016. * looking for custom handlers and returns a list of all available handerls.
  1017. * The base View handlers are also included.
  1018. *
  1019. * @return
  1020. * Returns an array of handler names
  1021. *
  1022. * @ingroup tripal_views_integration
  1023. */
  1024. function tripal_views_integration_discover_handlers() {
  1025. $handlers = array();
  1026. // Get handlers from all modules.
  1027. foreach (module_implements('views_handlers') as $module) {
  1028. $function = $module . '_views_handlers';
  1029. $result = $function();
  1030. if (!is_array($result)) {
  1031. continue;
  1032. }
  1033. foreach ($result['handlers'] as $handler => $parent) {
  1034. $handlers[] = $handler;
  1035. }
  1036. }
  1037. // these handlers are hard coded because I could not
  1038. // get the views_handlers() function to be called
  1039. // in the code above. However, we will be creating
  1040. // Chado wrappers for many of these and once that work
  1041. // is done these will no longer be needed.
  1042. // argument handlers
  1043. $handlers[] = 'views_handler_argument';
  1044. $handlers[] = 'views_handler_argument_numeric';
  1045. $handlers[] = 'views_handler_argument_formula';
  1046. $handlers[] = 'views_handler_argument_date';
  1047. $handlers[] = 'views_handler_argument_string';
  1048. $handlers[] = 'views_handler_argument_many_to_one';
  1049. $handlers[] = 'views_handler_argument_null';
  1050. // field handlers
  1051. $handlers[] = 'views_handler_field';
  1052. $handlers[] = 'views_handler_field_date';
  1053. $handlers[] = 'views_handler_field_boolean';
  1054. $handlers[] = 'views_handler_field_markup';
  1055. $handlers[] = 'views_handler_field_xss';
  1056. $handlers[] = 'views_handler_field_url';
  1057. $handlers[] = 'views_handler_field_file_size';
  1058. $handlers[] = 'views_handler_field_prerender_list';
  1059. $handlers[] = 'views_handler_field_numeric';
  1060. $handlers[] = 'views_handler_field_custom';
  1061. $handlers[] = 'views_handler_field_counter';
  1062. // filter handlers
  1063. $handlers[] = 'views_handler_filter';
  1064. $handlers[] = 'views_handler_filter_equality';
  1065. $handlers[] = 'views_handler_filter_string';
  1066. $handlers[] = 'views_handler_filter_boolean_operator';
  1067. $handlers[] = 'views_handler_filter_boolean_operator_string';
  1068. $handlers[] = 'views_handler_filter_in_operator';
  1069. $handlers[] = 'views_handler_filter_numeric';
  1070. $handlers[] = 'views_handler_filter_float';
  1071. $handlers[] = 'views_handler_filter_date';
  1072. $handlers[] = 'views_handler_filter_many_to_one';
  1073. // relationship handlers
  1074. $handlers[] = 'views_handler_relationship';
  1075. // sort handlers
  1076. $handlers[] = 'views_handler_sort';
  1077. $handlers[] = 'views_handler_sort_formula';
  1078. $handlers[] = 'views_handler_sort_date';
  1079. $handlers[] = 'views_handler_sort_menu_hierarchy';
  1080. $handlers[] = 'views_handler_sort_random';
  1081. // join handler
  1082. $handlers[] = 'views_join';
  1083. return $handlers;
  1084. }
  1085. /*
  1086. * Ajax Callback: Tripal Views Integration Form
  1087. * Replaces the entire fields table when the table or materialized view is set/changed
  1088. */
  1089. function tripal_views_integration_ajax_view_setup_table($form, $form_state) {
  1090. return $form['view_setup_table'];
  1091. }
  1092. /*
  1093. * Ajax Callback: Tripal Views Integration Form
  1094. * Replaces the join db field dropdown when the join table dropdown is changed
  1095. */
  1096. function tripal_views_integration_ajax_join_field($form, $form_state) {
  1097. // Determine which row we are dealing with from the name of the triggering element
  1098. if (preg_match('/fields_join_(\w+-\d+)/', $form_state['triggering_element']['#name'], $matches)) {
  1099. $field = $matches[1];
  1100. $join_field = 'fields_join_column_' . $matches[1];
  1101. // return the form element to be updated
  1102. return $form['view_setup_table'][$field]['column-3'][$join_field];
  1103. }
  1104. else {
  1105. watchdog('tripal_views', 'Tripal Views Integration Ajax failed due to being unable to determine which row needs updating', array(), WATCHDOG_ERROR);
  1106. return $form;
  1107. }
  1108. }