tripal_views_integration.inc 39 KB

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