tripal_views_integration.inc 42 KB

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