tripal_views_integration.inc 42 KB

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