tripal_views_integration.inc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. <?php
  2. /**
  3. * @file
  4. * Functions related to the UI for integrating tables with views
  5. */
  6. /**
  7. * Purpose: Provide Guidance to new Tripal Admin
  8. *
  9. * @return
  10. * HTML Formatted text
  11. *
  12. * @ingroup tripal_views_integration
  13. */
  14. function tripal_views_description_page() {
  15. $text .= '<h3>Tripal Views Quick Links:</h3>';
  16. $text .= "<ul>
  17. <li><a href=\"" . url("admin/tripal/views/integration/list") . "\">List of integrated tables</a></li>
  18. <li><a href=\"" . url("admin/tripal/views/integration/new") . "\">Integrate a new table</a></li>
  19. <li><a href=\"" . url("admin/tripal/views/integration/import") . "\">Import a previously exported table integration.</a></li>
  20. </ul>";
  21. $text .= '<h3>Views Integration Description:</h3>';
  22. $text .= '<p>Tripal Views provides an interface for integrating <a href="http://drupal.org/project/views">Drupal Views</a>
  23. with Tripal materialized views. This will allow site administrators to create custom queries for the materialized views
  24. and in turn provide custom content pages, custom blocks and custom search forms. The forms allow a site administrator
  25. to select a materialized view and associate other Chado tables on which the view can join. Usage of this module requires
  26. a good understanding of foreign-key relationships in Chado.
  27. </p>';
  28. $text .= '<h3>Setup Instructions:</h3>';
  29. $text .= '<p>After installation of the Tripal core module. The following tasks should be performed
  30. <ol>
  31. <li><b>Set Permissions</b>: To allow access to site administrators for this module, simply
  32. <a href="' . url('admin/user/permissions') . '">assign permissions</a> to the appropriate user roles for the
  33. permission type "manage tripal_views_integration". </li>
  34. </ol>
  35. </p>';
  36. $text .= '<h3>Usage Instructions:</h3>';
  37. $text .= "<p>To use Tripal Views integration follow these steps:
  38. <ol>
  39. <li><b>Identify or create a materialized view:</b> Using the <a href=\"" . url("admin/tripal/views/mviews") . "\">
  40. Tripal materialized View</a> interface, identify the view you would like to integrate or create a new one.</li>
  41. <li><b>Setup the Views Integration</b>: Navigate to the <a href=\"" . url("admin/tripal/views/integration/mviews/new") . "\">
  42. Tripal views integration setup page</a> to integrate the selected materialized view. Provide a user friendly name
  43. and description to help you remember the purpose for integrating the view. Next, select the view you want to integrate
  44. from the provided select box. If your materialized view has fields that can join with other Chado tables, you may
  45. provide those relationships in the provided form. Finally, if your fields require a special handler for display, you
  46. may select it from the drop down provided</li>
  47. <li><b>Create custom pages/block/search form</b>: After saving setup information from step 2 above, you will be redirected to the
  48. Drupal Views interface</a> where you can create a custom page, block or search form.</li>
  49. <li><b>Review your integrated views</b>: A page providing a
  50. <a href=\"" . url("admin/tripal/views/integration/mviews/list") . "\">list of all integrated views</a> is provided. You may
  51. view this page to see all integrated views, but also to remove any unwanted integrations.</li>
  52. </ol>
  53. </p>";
  54. return $text;
  55. }
  56. /**
  57. * Purpose: Generates a themable table containing the list of integrated tables
  58. * The look-and-feel of the table can be altered by overriding the theme for
  59. * tables.
  60. *
  61. * @return
  62. * a themed HTML table
  63. *
  64. * @ingroup tripal_views_integration
  65. */
  66. function tripal_views_integration_setup_list() {
  67. $output = '';
  68. $output .= '<h3>' . l(t('Add a new entry'), "admin/tripal/views/integration/new") . " | " .
  69. l(t("Create Materialized View"), 'admin/build/views/add') . '</h3>';
  70. $output .= '<p>The following tables list the views integration setups available. If '
  71. .'there is more then one setup for a given table, then the setup with the lightest '
  72. .'priority will be used. For example, if you have created a custom setup for the '
  73. .'feature chado table and your setup has a priority of -5 then your setup will be '
  74. .'used instead of the default feature integration because -5 is lighter then 10.'
  75. .'Priorities use the Drupal -10 to +10 scale where a record with -10 has a '
  76. .'greater priority then one with 0 and both have a greater priority then one with +10.</p>';
  77. // Start with materialized views
  78. $output .= '<br /><h3>Materialized Views</h3>';
  79. $header = array('', 'Drupal Views Type Name', 'Table Name', 'Is Legacy?', 'Priority', 'Comment');
  80. $rows = array();
  81. // get the list of materialized views
  82. $tviews = db_query('SELECT tv.setup_id, tv.name, tv.table_name, tc.table_id, tv.priority, tv.comment '
  83. .'FROM {tripal_views} tv '
  84. .'LEFT JOIN {tripal_custom_tables} tc ON tc.table_name=tv.table_name '
  85. .'WHERE tv.mview_id IS NOT NULL '
  86. .'ORDER BY tv.table_name ASC, tv.priority ASC');
  87. while ($tview = db_fetch_object($tviews)) {
  88. $rows[] = array(
  89. l(t('Edit'), "admin/tripal/views/integration/edit/" . $tview->setup_id) . "<br />"
  90. . l(t('Export'), "admin/tripal/views/integration/export/" . $tview->setup_id) . "<br />"
  91. . l(t('Delete'), "admin/tripal/views/integration/delete/" . $tview->setup_id),
  92. $tview->name,
  93. $tview->table_name,
  94. ($tview->table_id) ? 'No' : 'Yes',
  95. $tview->priority,
  96. $tview->comment,
  97. );
  98. }
  99. if ($rows) {
  100. $output .= theme('table', $header, $rows);
  101. }
  102. else {
  103. $output .= '<p>There are currently no Materialized Views defined.</p>';
  104. }
  105. // Now list non-mview custom tables
  106. $output .= '<br /><h3>Custom Tables</h3>';
  107. $header = array('', 'Drupal Views Type Name', 'Table Name', 'Priority', 'Comment');
  108. $rows = array();
  109. // get the list of chado tables
  110. $tviews = db_query('SELECT tv.setup_id, tv.name, tv.table_name, tv.priority, tv.comment '
  111. .'FROM {tripal_views} tv '
  112. .'LEFT JOIN {tripal_custom_tables} tc ON tc.table_name=tv.table_name '
  113. .'WHERE mview_id IS NULL AND tc.table_id IS NOT NULL '
  114. .'ORDER BY table_name ASC, priority ASC');
  115. while ($tview = db_fetch_object($tviews)) {
  116. $rows[] = array(
  117. l(t('Edit'), "admin/tripal/views/integration/edit/" . $tview->setup_id) . "<br />"
  118. . l(t('Export'), "admin/tripal/views/integration/export/" . $tview->setup_id) . "<br />"
  119. . l(t('Delete'), "admin/tripal/views/integration/delete/" . $tview->setup_id),
  120. $tview->name,
  121. $tview->table_name,
  122. $tview->priority,
  123. $tview->comment,
  124. );
  125. }
  126. if ($rows) {
  127. $output .= theme('table', $header, $rows);
  128. }
  129. else {
  130. $output .= '<p>There are currently no non-Materialized View Custom Tables defined.</p>';
  131. }
  132. // Now list chado tables
  133. $output .= '<br /><h3>Chado Tables</h3>';
  134. $header = array('', 'Drupal Views Type Name', 'Table Name', 'Priority', 'Comment');
  135. $rows = array();
  136. // get the list of chado tables
  137. $tviews = db_query('SELECT tv.setup_id, tv.name, tv.table_name, tv.priority, tv.comment '
  138. .'FROM {tripal_views} tv '
  139. .'LEFT JOIN {tripal_custom_tables} tc ON tc.table_name=tv.table_name '
  140. .'WHERE mview_id IS NULL AND tc.table_id IS NULL '
  141. .'ORDER BY table_name ASC, priority ASC');
  142. while ($tview = db_fetch_object($tviews)) {
  143. $rows[] = array(
  144. l(t('Edit'), "admin/tripal/views/integration/edit/" . $tview->setup_id) . "<br />"
  145. . l(t('Export'), "admin/tripal/views/integration/export/" . $tview->setup_id) . "<br />"
  146. . l(t('Delete'), "admin/tripal/views/integration/delete/" . $tview->setup_id),
  147. $tview->name,
  148. $tview->table_name,
  149. $tview->priority,
  150. $tview->comment,
  151. );
  152. }
  153. $output .= theme('table', $header, $rows);
  154. return $output;
  155. }
  156. /**
  157. * Purpose: Deletes integration of a table with the Views module. 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. * @param $setup_id
  162. * the unique setup id for the integrated table
  163. *
  164. * @ingroup tripal_views_integration
  165. */
  166. function tripal_views_integration_delete($setup_id) {
  167. tripal_views_integration_remove_entry_by_setup_id($setup_id);
  168. drupal_set_message(t("Record Deleted"));
  169. drupal_goto('admin/tripal/views/integration');
  170. }
  171. /**
  172. * Purpose: defines the web form used for specifing the base table, joins and
  173. * handlers when integrating a table with views. This form is used for both
  174. * creating a new record and editing an existing record.
  175. *
  176. * @param &$form_state
  177. * The form state which is passed automatically by drupal
  178. *
  179. * @param $setup_id
  180. * The unique setup for an integrated table. This value is only set when
  181. * the form is used for updating an existing record.
  182. *
  183. * @return
  184. * A proper Drupal form associative array.
  185. *
  186. * @ingroup tripal_views_integration
  187. */
  188. function tripal_views_integration_form(&$form_state, $setup_id = NULL) {
  189. $form = array();
  190. $data = array();
  191. $form['#cache'] = TRUE;
  192. // ahah_helper requires us to register the form with it's module
  193. ahah_helper_register($form, $form_state);
  194. // if a setup_id is provided then we want to get the form defaults
  195. $setup_obj = array();
  196. if (isset($setup_id)) {
  197. // get the deafult setup values
  198. $sql = "SELECT * FROM {tripal_views} WHERE setup_id = %d";
  199. $setup_obj = db_fetch_object(db_query($sql, $setup_id));
  200. $mview_id = $setup_obj->mview_id;
  201. $table_name = $setup_obj->table_name;
  202. $form_state['storage']['mview_id'] = $mview_id;
  203. $form_state['storage']['table_name'] = $table_name;
  204. // get the default field name/description
  205. $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=%d";
  206. $query = db_query($sql, $setup_id);
  207. $default_fields = array();
  208. while ($field = db_fetch_object($query)) {
  209. $default_fields[$field->column_name]['name'] = $field->name;
  210. $default_fields[$field->column_name]['description'] = $field->description;
  211. }
  212. // get the default join settings and handlers
  213. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
  214. $query = db_query($sql, $setup_id);
  215. $default_joins = array();
  216. while ($join = db_fetch_object($query)) {
  217. $default_joins[$join->base_field]['left_table'] = $join->left_table;
  218. $default_joins[$join->base_field]['left_field'] = $join->left_field;
  219. }
  220. // get the default handlers
  221. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = %d";
  222. $query = db_query($sql, $setup_id);
  223. $default_handlers = array();
  224. while ($handler = db_fetch_object($query)) {
  225. $default_handlers[$handler->column_name][$handler->handler_type]['handler_name'] = $handler->handler_name;
  226. $default_handlers[$handler->column_name][$handler->handler_type]['arguments'] = $handler->arguments;
  227. }
  228. // get the default join handlers
  229. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
  230. $query = db_query($sql, $setup_id);
  231. while ($handler = db_fetch_object($query)) {
  232. $default_handlers[$handler->base_field]['join']['handler_name'] = $handler->handler;
  233. //$default_handlers[$handler->base_field]['join']['arguments'] = $handler->arguments;
  234. }
  235. // add in the setup_id for the form so we know this is an update not an insert
  236. $form['setup_id'] = array(
  237. '#type' => 'hidden',
  238. '#value' => $setup_id,
  239. );
  240. }
  241. // add a fieldset for the MView & Chado table selectors
  242. $form['base_table_type'] = array(
  243. '#type' => 'fieldset',
  244. '#title' => 'Base Table',
  245. '#description' => 'Please select either a materialized view or a Chado table for integration with '.
  246. 'Drupal Views. In Drupal Views terminology, the selected table becomes the "base table". '.
  247. 'After you select a table from either list, the fields from that table will appear below '.
  248. 'and you can specify other tables to join with and handlers.',
  249. );
  250. // build the form element for the Chado tables
  251. $chado_tables = tripal_core_get_chado_tables(TRUE);
  252. $chado_tables = array_merge(array('Select'), $chado_tables);
  253. $form['base_table_type']['table_name'] = array(
  254. '#title' => t('Chado/Custom Table'),
  255. '#type' => 'select',
  256. '#options' => $chado_tables,
  257. '#description' => t('Tables from Chado, custom tables and materialized view tables (non-legacy MViews) can be selected for integration.'),
  258. '#default_value' => (!$setup_obj->mview_id) ? $setup_obj->table_name : '',
  259. '#ahah' => array(
  260. 'path' => ahah_helper_path(array('view_setup_table')),
  261. 'wrapper' => 'table-rows-div',
  262. 'effect' => 'fade',
  263. 'event' => 'change',
  264. 'method' => 'replace',
  265. ),
  266. );
  267. // build the form element that lists the materialized views
  268. $query = db_query("SELECT mview_id, name FROM {tripal_mviews} WHERE mv_schema is NULL or mv_schema = '' ORDER BY name");
  269. $mview_tables = array();
  270. $mview_tables['0'] = 'Select';
  271. while ($mview = db_fetch_object($query)) {
  272. $mview_tables[$mview->mview_id] = $mview->name;
  273. }
  274. $form['base_table_type']['mview_id'] = array(
  275. '#title' => t('Legacy Materialized View'),
  276. '#type' => 'select',
  277. '#options' => $mview_tables,
  278. '#description' => 'Which materialized view to use.',
  279. '#default_value' => $setup_obj->mview_id,
  280. '#ahah' => array(
  281. 'path' => ahah_helper_path(array('view_setup_table')),
  282. 'wrapper' => 'table-rows-div',
  283. 'effect' => 'fade',
  284. 'event' => 'change',
  285. 'method' => 'replace',
  286. ),
  287. );
  288. $form['views_type'] = array(
  289. '#type' => 'fieldset',
  290. '#title' => 'View Type',
  291. '#description' => 'Here you can provide the "type" of View you want to create.',
  292. );
  293. // field for the name of the
  294. $form['views_type']['row_name'] = array(
  295. '#title' => t('View Type Name'),
  296. '#type' => 'textfield',
  297. '#default_value' => $setup_obj->name,
  298. '#size' => 60,
  299. '#maxlength' => 128,
  300. '#description' => 'Provide the view type name. This is the name that will appear in '.
  301. 'the Drupal Views interface when adding a new view. The view type name '.
  302. 'must be unique.',
  303. '#required' => TRUE,
  304. );
  305. if (isset($setup_id)) {
  306. $form['row_name']['#attributes'] = array('readonly' => 'readonly');
  307. }
  308. $priorities = array();
  309. foreach (range(-10, 10) as $v) {
  310. $priorities[$v] = (string) $v;
  311. }
  312. $form['views_type']['row_priority'] = array(
  313. '#type' => 'select',
  314. '#title' => t('Priority'),
  315. '#description' => t('The level of priority your Views integration has in relation to the '
  316. .'default core and module definitions. The views integration definition with the '
  317. .'lightest priority will be used. For example, if there is a definition created by '
  318. .'core with a priority of 10 and another by a custom module of 5 and yours is -1 then '
  319. .'you definition will be used for that table because -1 is lighter then both 5 and 10.'),
  320. '#options' => $priorities,
  321. '#default_value' => (isset($setup_obj->priority)) ? $setup_obj->priority : -1,
  322. );
  323. if ($setup_obj->priority >= 9) {
  324. drupal_set_message('You are editing a default views integration. To ensure your changes
  325. are used, change the priority to -10.', 'warning');
  326. }
  327. $form['views_type']['base_table'] = array(
  328. '#type' => 'checkbox',
  329. '#title' => t('Base Table?'),
  330. '#description' => t('If you want this table to show up as one of the options in the '
  331. . 'add view page, then check this checkbox. It allows you to create listings '
  332. . 'primarily from this table'),
  333. '#default_value' => (isset($setup_obj->base_table)) ? $setup_obj->base_table : 1,
  334. );
  335. $form['views_type']['row_description'] = array(
  336. '#title' => t('Comment'),
  337. '#type' => 'textarea',
  338. '#description' => '(Optional). Provide any details regarding this setup you would like. This '.
  339. 'description will appear when selecting a type for a new Drupal View',
  340. '#required' => FALSE,
  341. '#default_value' => (isset($setup_obj->comment)) ? $setup_obj->comment : '',
  342. );
  343. // we need a div block where the table fields will get put when the
  344. // AHAH callback is made
  345. $form['view_setup_table'] = array(
  346. '#type' => 'item',
  347. '#prefix' => '<div id="table-rows-div">',
  348. '#suffix' => '</div>',
  349. );
  350. // add the fieldset for the table fields, but only if the $mview_id or $table_name
  351. // is set. The only times these values are set is if we're editing an existing
  352. // record or if the AHAH callback is being made.
  353. if ($form_state['storage']['mview_id'] or $form_state['storage']['table_name']) {
  354. $mview_id = $form_state['storage']['mview_id'];
  355. $table_name = $form_state['storage']['table_name'];
  356. $form['view_setup_table'] = array(
  357. '#type' => 'fieldset',
  358. '#title' => 'Join Selection',
  359. '#prefix' => '<div id="fieldset-table-rows-wrapper">',
  360. '#suffix' => '</div>',
  361. );
  362. // get the columns in this materialized view. They are separated by commas
  363. // where the first word is the column name and the rest is the type
  364. $columns = array();
  365. if ($mview_id) {
  366. $sql = "SELECT mv_specs FROM {tripal_mviews} WHERE mview_id = %d";
  367. $mview = db_fetch_object(db_query($sql, $mview_id));
  368. $columns = explode(",", $mview->mv_specs);
  369. }
  370. else {
  371. $table_desc = tripal_core_get_chado_table_schema($table_name);
  372. if ($table_desc) {
  373. $fields = $table_desc['fields'];
  374. // iterate through the columns and build the format
  375. // compatible with the code below. The column name is first followed
  376. // by the type with a separating space
  377. foreach ($fields as $column => $attrs) {
  378. $columns[] = "$column " . $attrs['type'];
  379. }
  380. }
  381. }
  382. $i=1;
  383. $form['view_setup_table']["instructions"] = array(
  384. '#type' => 'markup',
  385. '#value' => filter_xss("Select an optional table to which the fields of the ".
  386. "materialized view can join. If a field does not need to ".
  387. "join you may leave the selection blank."),
  388. );
  389. $data['field_types'] = array();
  390. // get the list of chado tables to join on
  391. $chado_join_tables = tripal_core_get_chado_tables(TRUE);
  392. $chado_join_tables = array_merge(array('Select a Join Table'), $chado_join_tables);
  393. // get list of all handlers
  394. $all_handlers = tripal_views_integration_discover_handlers();
  395. $handlers_fields = array(0 => "Select a field handler");
  396. $handlers_filters = array(0 => "Select a filter handler");
  397. $handlers_sort = array(0 => "Select a sort handler");
  398. $handlers_argument = array(0 => "Select an argument handler");
  399. $handlers_join = array(0 => "Select a join handler");
  400. $handlers_rel = array(0 => "Select a relationship handler");
  401. foreach ($all_handlers as $handler) {
  402. if (preg_match("/views_handler_field/", $handler)) {
  403. $handlers_fields[$handler] = $handler;
  404. }
  405. if (preg_match("/views_handler_filter/", $handler)) {
  406. $handlers_filters[$handler] = $handler;
  407. }
  408. if (preg_match("/views_handler_sort/", $handler)) {
  409. $handlers_sort[$handler] = $handler;
  410. }
  411. if (preg_match("/views_handler_argument/", $handler)) {
  412. $handlers_argument[$handler] = $handler;
  413. }
  414. if (preg_match("/_join/", $handler)) {
  415. $handlers_join[$handler] = $handler;
  416. }
  417. if (preg_match("/views_handler_relationship/", $handler)) {
  418. $handlers_rel[$handler] = $handler;
  419. }
  420. }
  421. // generate a unique $table_id for keeping track of the table
  422. if ($mview_id) {
  423. $table_id = $mview_id;
  424. }
  425. else {
  426. $table_id = $table_name;
  427. }
  428. // Per Row (Fields) --------------
  429. // now iterate through the columns of the materialized view or
  430. // chado table and generate the join and handler fields
  431. foreach ($columns as $column) {
  432. $column = trim($column); // trim trailing and leading spaces
  433. preg_match("/^(.*?)\ (.*?)$/", $column, $matches);
  434. $column_name = $matches[1];
  435. $column_type = $matches[2];
  436. $form['view_setup_table']["$table_id-$i"] = array(
  437. '#type' => 'markup',
  438. '#prefix' => "<div class=\"fields-new-row\">",
  439. '#suffix' => "</div>",
  440. '#value' => filter_xss('')
  441. );
  442. // COLUMN I
  443. $form['view_setup_table']["$table_id-$i"]["fields_name_$table_id-$i"] = array(
  444. '#type' => 'markup',
  445. '#prefix' => "<div class=\"column-one\">",
  446. '#value' => "<span class=\"column-name\">" . filter_xss($column_name) . "</span>".
  447. "<br /><span class=\"column-type\">" . filter_xss($column_type) . "</span>",
  448. '#suffix' => "</div>",
  449. );
  450. $data['field_types'][$column_name] = $column_type;
  451. // COLUMN II
  452. $form['view_setup_table']["$table_id-$i"]['column-2'] = array(
  453. '#type' => 'markup',
  454. '#prefix' => "<div class=\"column-two\">",
  455. '#suffix' => "</div>",
  456. '#value' => filter_xss('')
  457. );
  458. // set the default values for the human-readable name and description
  459. $default_name = '';
  460. $default_descrip = '';
  461. if (isset($setup_id) && !isset($form_state['storage']["fields_readable_name_$table_id-$i"])) {
  462. $default_name = $default_fields[$column_name]['name'];
  463. $default_descrip = $default_fields[$column_name]['description'];
  464. }
  465. else {
  466. $default_name = $form_state['storage']["fields_readable_name_$table_id-$i"];
  467. $default_descrip = $form_state['storage']["fields_description_$table_id-$i"];
  468. }
  469. $form['view_setup_table']["$table_id-$i"]['column-2']["fields_readable_name_$table_id-$i"] = array(
  470. '#type' => 'textfield',
  471. '#title' => 'Human-Readable Name',
  472. '#description' => 'This is the name of the field in the Views UI',
  473. '#required' => TRUE,
  474. '#default_value' => $default_name,
  475. '#size' => 42,
  476. );
  477. $form['view_setup_table']["$table_id-$i"]['column-2']["fields_description_$table_id-$i"] = array(
  478. '#type' => 'textarea',
  479. '#title' => 'Short Description',
  480. '#description' => 'This is the field help in the Views UI',
  481. '#required' => TRUE,
  482. '#cols' => 42,
  483. '#rows' => 3,
  484. '#default_value' => $default_descrip,
  485. );
  486. // COLUMN III
  487. $form['view_setup_table']["$table_id-$i"]['column-3'] = array(
  488. '#type' => 'markup',
  489. '#prefix' => "<div class=\"column-three\">",
  490. '#suffix' => "</div>",
  491. '#value' => filter_xss('')
  492. );
  493. // set the default values for the join table and columns
  494. $default_join_table = 0;
  495. $default_join_field = 0;
  496. if (isset($setup_id) && !isset($form_state['storage']["fields_join_$table_id-$i"])) {
  497. $default_join_table = $default_joins[$column_name]['left_table'];
  498. $default_join_field = $default_joins[$column_name]['left_field'];
  499. $form_state['storage']["fields_join_$table_id-$i"] = $default_join_table;
  500. $form_state['storage']["fields_join_column_$table_id-$i"] = $default_join_field;
  501. }
  502. else{
  503. $default_join_table = $form_state['storage']["fields_join_$table_id-$i"];
  504. $default_join_field = $form_state['storage']["fields_join_column_$table_id-$i"];
  505. }
  506. $form['view_setup_table']["$table_id-$i"]['column-3']["fields_join_$table_id-$i"] = array(
  507. '#type' => 'select',
  508. '#prefix' => "<div class=\"fields-column-join\">",
  509. '#suffix' => "</div>",
  510. '#options' => $chado_join_tables,
  511. '#required' => FALSE,
  512. '#default_value' => $default_join_table,
  513. '#ahah' => array(
  514. 'path' => ahah_helper_path(array("view_setup_table", "$table_id-$i", 'column-3', "fields_join_column_$table_id-$i")),
  515. 'wrapper' => "fields-column-join-column-$table_id-$i",
  516. 'effect' => 'fade',
  517. 'event' => 'change',
  518. 'method' => 'replace',
  519. ),
  520. );
  521. $columns = array();
  522. if ($default_join_table) {
  523. // get the table description
  524. $table_desc = tripal_core_get_chado_table_schema($default_join_table);
  525. foreach ($table_desc['fields'] as $column => $def) {
  526. $columns[$column] = $column;
  527. }
  528. }
  529. else {
  530. $columns = array('Select Join Column');
  531. }
  532. $form['view_setup_table']["$table_id-$i"]['column-3']["fields_join_column_$table_id-$i"] = array(
  533. '#type' => 'select',
  534. '#prefix' => " <div id=\"fields-column-join-column-$table_id-$i\" class=\"fields-column-join-column\">",
  535. '#suffix' => "</div>",
  536. '#options' => $columns,
  537. '#required' => FALSE,
  538. '#default_value' => $default_join_field
  539. );
  540. $default_join_handler = 0;
  541. if (isset($setup_id) && !isset($form_state['storage']["fields_join_handler_$table_id-$i"])) {
  542. $default_join_handler = $default_handlers[$column_name]['join']['handler_name'];
  543. $form_state['storage']["fields_join_handler_$table_id-$i"] = $default_join_handler;
  544. }
  545. else {
  546. $default_join_handler = $form_state['storage']["fields_join_handler_$table_id-$i"];
  547. }
  548. $form['view_setup_table']["$table_id-$i"]['column-3']["fields_join_handler_$table_id-$i"] = array(
  549. '#type' => 'select',
  550. '#prefix' => "<div class=\"fields-join-handler\">",
  551. '#suffix' => "</div>",
  552. '#options' => $handlers_join,
  553. '#required' => FALSE,
  554. '#default_value' => $default_join_handler,
  555. );
  556. // COLUMN 4
  557. $form['view_setup_table']["$table_id-$i"]['column-4'] = array(
  558. '#type' => 'markup',
  559. '#prefix' => "<div class=\"column-four\">",
  560. '#suffix' => "</div>",
  561. '#value' => filter_xss('')
  562. );
  563. // create the handler fields
  564. $default_field_handler = 0;
  565. if (isset($setup_id) && !isset($form_state['storage']["fields_field_handler_$table_id-$i"])) {
  566. $default_field_handler = $default_handlers[$column_name]['field']['handler_name'];
  567. $form_state['storage']["fields_field_handler_$table_id-$i"] = $default_field_handler;
  568. }
  569. else {
  570. $default_field_handler = $form_state['storage']["fields_field_handler_$table_id-$i"];
  571. if (!$default_field_handler) {
  572. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  573. $default_field_handler = 'chado_views_handler_field_numeric';
  574. }
  575. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  576. $default_field_handler = 'chado_views_handler_field';
  577. }
  578. elseif ($column_type == 'boolean') {
  579. $default_field_handler = 'chado_views_handler_field_boolean';
  580. }
  581. elseif ($column_type == 'float') {
  582. $default_field_handler = 'chado_views_handler_field_numeric';
  583. }
  584. elseif ($column_type == 'datetime') {
  585. $default_field_handler = 'chado_views_handler_field_date';
  586. }
  587. }
  588. }
  589. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_field_handler_$table_id-$i"] = array(
  590. '#type' => 'select',
  591. '#prefix' => "<div class=\"fields-field-handler\">",
  592. '#suffix' => "</div>",
  593. '#options' => $handlers_fields,
  594. '#required' => FALSE,
  595. '#default_value' => $default_field_handler,
  596. );
  597. $default_filter_handler = 0;
  598. if (isset($setup_id) && !isset($form_state['storage']["fields_filter_handler_$table_id-$i"])) {
  599. $default_filter_handler = $default_handlers[$column_name]['filter']['handler_name'];
  600. $form_state['storage']["fields_filter_handler_$table_id-$i"]= $default_filter_handler;
  601. }
  602. else {
  603. $default_filter_handler = $form_state['storage']["fields_filter_handler_$table_id-$i"];
  604. if (!$default_filter_handler) {
  605. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  606. $default_filter_handler = 'chado_views_handler_filter_numeric';
  607. }
  608. elseif (preg_match("/^character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  609. $default_filter_handler = 'chado_views_handler_filter_string';
  610. }
  611. elseif ($column_type == 'boolean') {
  612. $default_filter_handler = 'chado_views_handler_filter_boolean';
  613. }
  614. elseif ($column_type == 'float') {
  615. $default_filter_handler = 'chado_views_handler_filter_float';
  616. }
  617. elseif ($column_type == 'datetime') {
  618. $default_filter_handler = 'chado_views_handler_filter_date';
  619. }
  620. }
  621. }
  622. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_filter_handler_$table_id-$i"] = array(
  623. '#type' => 'select',
  624. '#prefix' => "<div class=\"fields-filter-handler\">",
  625. '#suffix' => "</div>",
  626. '#options' => $handlers_filters,
  627. '#required' => FALSE,
  628. '#default_value' => $default_filter_handler,
  629. );
  630. $default_sort_handler = 0;
  631. if (isset($setup_id) && !isset($form_state['storage']["fields_sort_handler_$table_id-$i"])) {
  632. $default_sort_handler = $default_handlers[$column_name]['sort']['handler_name'];
  633. $form_state['storage']["fields_sort_handler_$table_id-$i"] = $default_sort_handler;
  634. }
  635. else {
  636. $default_sort_handler = $form_state['storage']["fields_sort_handler_$table_id-$i"];
  637. if (!$default_sort_handler) {
  638. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  639. $default_sort_handler = 'chado_views_handler_sort';
  640. }
  641. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  642. $default_sort_handler = 'chado_views_handler_sort';
  643. }
  644. elseif ($column_type == 'boolean') {
  645. $default_sort_handler = 'chado_views_handler_sort';
  646. }
  647. elseif ($column_type == 'float') {
  648. $default_sort_handler = 'chado_views_handler_sort';
  649. }
  650. elseif ($column_type == 'datetime') {
  651. $default_sort_handler = 'chado_views_handler_sort_date';
  652. }
  653. }
  654. }
  655. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_sort_handler_$table_id-$i"] = array(
  656. '#type' => 'select',
  657. '#prefix' => "<div class=\"fields-sort-handler\">",
  658. '#suffix' => "</div>",
  659. '#options' => $handlers_sort,
  660. '#required' => FALSE,
  661. '#default_value' => $default_sort_handler,
  662. );
  663. $default_argument_handler = 0;
  664. if (isset($setup_id) && !isset($form_state['storage']["fields_argument_handler_$table_id-$i"])) {
  665. $default_argument_handler = $default_handlers[$column_name]['argument']['handler_name'];
  666. $form_state['storage']["fields_argument_handler_$table_id-$i"]=$default_argument_handler ;
  667. }
  668. else {
  669. $default_argument_handler = $form_state['storage']["fields_argument_handler_$table_id-$i"];
  670. if (!$default_argument_handler) {
  671. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  672. $default_argument_handler = 'views_handler_argument_numeric';
  673. }
  674. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  675. $default_argument_handler = 'views_handler_argument_string';
  676. }
  677. elseif ($column_type == 'boolean') {
  678. $default_argument_handler = 'views_handler_argument_numeric';
  679. }
  680. elseif ($column_type == 'float') {
  681. $default_argument_handler = 'views_handler_argument_numeric';
  682. }
  683. elseif ($column_type == 'datetime') {
  684. $default_argument_handler = 'views_handler_argument_date';
  685. }
  686. }
  687. }
  688. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_argument_handler_$table_id-$i"] = array(
  689. '#type' => 'select',
  690. '#prefix' => "<div class=\"fields-argument-handler\">",
  691. '#suffix' => "</div>",
  692. '#options' => $handlers_argument,
  693. '#required' => FALSE,
  694. '#default_value' => $default_argument_handler,
  695. );
  696. $default_relationship_handler = 0;
  697. if (isset($setup_id) && !isset($form_state['storage']["fields_relationship_handler_$table_id-$i"])) {
  698. $default_relationship_handler = $default_handlers[$column_name]['relationship']['handler_name'];
  699. $form_state['storage']["fields_relationship_handler_$table_id-$i"]=$default_relationship_handler;
  700. }
  701. else {
  702. $default_relationship_handler = $form_state['storage']["fields_relationship_handler_$table_id-$i"];
  703. if (!$default_relationship_handler) {
  704. if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
  705. $default_relationship_handler = 'views_handler_relationship';
  706. }
  707. elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
  708. $default_relationship_handler = 'views_handler_relationship';
  709. }
  710. elseif ($column_type == 'boolean') {
  711. $default_relationship_handler = 'views_handler_relationship';
  712. }
  713. elseif ($column_type == 'float') {
  714. $default_relationship_handler = 'views_handler_relationship';
  715. }
  716. elseif ($column_type == 'datetime') {
  717. $default_relationship_handler = 'views_handler_relationship';
  718. }
  719. }
  720. }
  721. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_relationship_handler_$table_id-$i"] = array(
  722. '#type' => 'select',
  723. '#prefix' => "<div class=\"fields-relationship-handler\">",
  724. '#suffix' => "</div>",
  725. '#options' => $handlers_rel,
  726. '#required' => FALSE,
  727. '#default_value' => $default_relationship_handler,
  728. );
  729. $i++;
  730. }
  731. $form['view_setup_table']['save'] = array(
  732. '#type' => 'submit',
  733. '#value' => t('Save'),
  734. );
  735. $data['row_count'] = $i - 1;
  736. }
  737. //use this to put values into $form_state['values']
  738. $form['data'] = array();
  739. // Ensure that we don't store an array
  740. // since we will get a check_plain:htmlspecial_characters error if we do
  741. foreach ($data as $key => $value) {
  742. if (is_array($value)) {
  743. $form['data'][$key] = array(
  744. '#type' => 'hidden',
  745. '#value' => serialize($value),
  746. );
  747. }
  748. else {
  749. $form['data'][$key] = array(
  750. '#type' => 'hidden',
  751. '#value' => $value,
  752. );
  753. }
  754. }
  755. $form['#redirect'] = 'admin/tripal/views/integration/list';
  756. return $form;
  757. }
  758. /**
  759. * Purpose: validates the tripal_views_integration_form after submission
  760. *
  761. * @param $form
  762. * The form object which is passed automatically by drupal
  763. *
  764. * @param &$form_state
  765. * The form state pbject which is passed automatically by drupal
  766. *
  767. * @ingroup tripal_views_integration
  768. */
  769. function tripal_views_integration_form_validate($form, &$form_state) {
  770. $name_array = explode(" ", $form_state['values']['row_name']);
  771. $mview_id = $form_state['values']['mview_id'];
  772. $table_name = $form_state['values']['table_name'];
  773. // if (count($name_array) > 1) {
  774. // form_set_error($form_state['values']['row_name'], 'The View type name must be a single word only.');
  775. // }
  776. if ($mview_id and $table_name) {
  777. form_set_error($form_state['values']['mview_id'], 'Please select either a materialized view or a Chado table but not both');
  778. }
  779. if (!$mview_id and !$table_name) {
  780. form_set_error($form_state['values']['mview_id'], 'Please select either a materialized view or a Chado table');
  781. }
  782. // TODO: do we need to require that a handler be set for each field and each type of handler?
  783. }
  784. /**
  785. * Purpose: inserts or updates the record in the tripal views integration
  786. * tables. This function is only called if validation is passed.
  787. *
  788. * @param $form
  789. * The form object which is passed automatically by drupal
  790. *
  791. * @param &$form_state
  792. * The form state pbject which is passed automatically by drupal
  793. *
  794. * @ingroup tripal_views_integration
  795. */
  796. function tripal_views_integration_form_submit($form, &$form_state) {
  797. $name = $form_state['values']['row_name'];
  798. $mview_id = $form_state['values']['mview_id'];
  799. $table_name = $form_state['values']['table_name'];
  800. $setup_id = $form_state['values']['setup_id'];
  801. $priority = $form_state['values']['row_priority'];
  802. $comment = $form_state['values']['row_description'];
  803. // get details about this mview
  804. if ($mview_id) {
  805. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d";
  806. $mview = db_fetch_object(db_query($sql, $mview_id));
  807. $table_name = $mview->mv_table;
  808. $table_id = $mview_id;
  809. $type = 'mview';
  810. }
  811. else {
  812. $type = 'chado';
  813. $table_id = $table_name;
  814. }
  815. // If this is for a materialized view then we want to add/update that record
  816. $tripal_views_record = array();
  817. if ($mview_id) {
  818. // get details about this mview
  819. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d";
  820. $mview = db_fetch_object(db_query($sql, $mview_id));
  821. // build the record for insert/update
  822. $tripal_views_record = array(
  823. 'mview_id' => $mview_id,
  824. 'table_name' => $mview->mv_table,
  825. 'name' => $name,
  826. 'priority' => $priority,
  827. 'comment' => $comment,
  828. 'base_table' => $form_state['values']['base_table']
  829. );
  830. }
  831. // if a chado table then...
  832. else {
  833. // build the record for insert/update
  834. $tripal_views_record = array(
  835. 'table_name' => $table_name,
  836. 'name' => $name,
  837. 'priority' => $priority,
  838. 'comment' => $comment,
  839. 'base_table' => $form_state['values']['base_table']
  840. );
  841. }
  842. // perform the insert or update
  843. if (!$setup_id) { // this is an insert
  844. if (!drupal_write_record('tripal_views', $tripal_views_record)) {
  845. drupal_set_message(t("Failed to add record."), 'error');
  846. return;
  847. }
  848. }
  849. else { // this is an update
  850. $tripal_views_record['setup_id'] = $setup_id;
  851. if (!drupal_write_record('tripal_views', $tripal_views_record, array('setup_id'))) {
  852. drupal_set_message(t("Failed to update record."), 'error');
  853. return;
  854. }
  855. }
  856. // if this is an update then clean out the existing joins and handlers so we can add new ones
  857. if ($setup_id) {
  858. db_query("DELETE FROM {tripal_views_field} WHERE setup_id = %d", $setup_id);
  859. db_query("DELETE FROM {tripal_views_join} WHERE setup_id = %d", $setup_id);
  860. db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id = %d", $setup_id);
  861. }
  862. // iterate through the columns of the form and add
  863. // the joins if provided, and the handlers
  864. $i = 1;
  865. foreach (unserialize($form_state['values']['field_types']) as $key => $value) {
  866. // add the field definition
  867. $view_field_record = array(
  868. 'setup_id' => $tripal_views_record['setup_id'],
  869. 'column_name' => $key,
  870. 'name' => $form_state['values']["fields_readable_name_$table_id-$i"],
  871. 'description' => $form_state['values']["fields_description_$table_id-$i"],
  872. 'type' => $value,
  873. );
  874. drupal_write_record('tripal_views_field', $view_field_record);
  875. // first add the join if it exists
  876. $left_table = $form_state['values']["fields_join_$table_id-$i"];
  877. $left_column = $form_state['values']["fields_join_column_$table_id-$i"];
  878. if ($left_column) {
  879. if ($mview_id) {
  880. $base_table = $mview->mv_table;
  881. }
  882. else {
  883. $base_table = $table_name;
  884. }
  885. $view_join_record = array(
  886. 'setup_id' => $tripal_views_record['setup_id'],
  887. 'base_table' => $base_table,
  888. 'base_field' => $key,
  889. 'left_table' => $left_table,
  890. 'left_field' => $left_column,
  891. 'handler' => $form_state['values']["fields_join_handler_$table_id-$i"],
  892. );
  893. // write the new joins to the database
  894. drupal_write_record('tripal_views_join', $view_join_record);
  895. }
  896. // add the hanlders
  897. $handlers = array('filter', 'field', 'sort', 'argument', 'join', 'relationship');
  898. foreach ($handlers as $handler) {
  899. $handler_name = $form_state['values']["fields_" . $handler . "_handler_$table_id-$i"];
  900. if ($handler_name) {
  901. $handler_record = array(
  902. 'setup_id' => $tripal_views_record['setup_id'],
  903. 'column_name' => $key,
  904. 'handler_type' => $handler,
  905. 'handler_name' => $handler_name,
  906. );
  907. drupal_write_record('tripal_views_handlers', $handler_record);
  908. }
  909. }
  910. $i++;
  911. }
  912. if ($setup_id) {
  913. drupal_set_message(t('Record Updated'));
  914. }
  915. else {
  916. drupal_set_message(t('Record Added'));
  917. }
  918. // now clear all the caches so that Drupal views picks up our chages
  919. views_invalidate_cache();
  920. }
  921. /**
  922. * Purpose: this function queries all modules currently enabled on the site
  923. * looking for custom handlers and returns a list of all available handerls.
  924. * The base View handlers are also included.
  925. *
  926. * @return
  927. * Returns an array of handler names
  928. *
  929. * @ingroup tripal_views_integration
  930. */
  931. function tripal_views_integration_discover_handlers() {
  932. $handlers = array();
  933. // Get handlers from all modules.
  934. foreach (module_implements('views_handlers') as $module) {
  935. $function = $module . '_views_handlers';
  936. $result = $function();
  937. if (!is_array($result)) {
  938. continue;
  939. }
  940. foreach ($result['handlers'] as $handler => $parent) {
  941. $handlers[] = $handler;
  942. }
  943. }
  944. // these handlers are hard coded because I could not
  945. // get the views_handlers() function to be called
  946. // in the code above. However, we will be creating
  947. // Chado wrappers for many of these and once that work
  948. // is done these will no longer be needed.
  949. // argument handlers
  950. $handlers[] = 'views_handler_argument';
  951. $handlers[] = 'views_handler_argument_numeric';
  952. $handlers[] = 'views_handler_argument_formula';
  953. $handlers[] = 'views_handler_argument_date';
  954. $handlers[] = 'views_handler_argument_string';
  955. $handlers[] = 'views_handler_argument_many_to_one';
  956. $handlers[] = 'views_handler_argument_null';
  957. // field handlers
  958. $handlers[] = 'views_handler_field';
  959. $handlers[] = 'views_handler_field_date';
  960. $handlers[] = 'views_handler_field_boolean';
  961. $handlers[] = 'views_handler_field_markup';
  962. $handlers[] = 'views_handler_field_xss';
  963. $handlers[] = 'views_handler_field_url';
  964. $handlers[] = 'views_handler_field_file_size';
  965. $handlers[] = 'views_handler_field_prerender_list';
  966. $handlers[] = 'views_handler_field_numeric';
  967. $handlers[] = 'views_handler_field_custom';
  968. $handlers[] = 'views_handler_field_counter';
  969. // filter handlers
  970. $handlers[] = 'views_handler_filter';
  971. $handlers[] = 'views_handler_filter_equality';
  972. $handlers[] = 'views_handler_filter_string';
  973. $handlers[] = 'views_handler_filter_boolean_operator';
  974. $handlers[] = 'views_handler_filter_boolean_operator_string';
  975. $handlers[] = 'views_handler_filter_in_operator';
  976. $handlers[] = 'views_handler_filter_numeric';
  977. $handlers[] = 'views_handler_filter_float';
  978. $handlers[] = 'views_handler_filter_date';
  979. $handlers[] = 'views_handler_filter_many_to_one';
  980. // relationship handlers
  981. $handlers[] = 'views_handler_relationship';
  982. // sort handlers
  983. $handlers[] = 'views_handler_sort';
  984. $handlers[] = 'views_handler_sort_formula';
  985. $handlers[] = 'views_handler_sort_date';
  986. $handlers[] = 'views_handler_sort_menu_hierarchy';
  987. $handlers[] = 'views_handler_sort_random';
  988. // join handler
  989. $handlers[] = 'views_join';
  990. return $handlers;
  991. }