mviews.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions for viewing and editing of Materialized Views
  5. * on a Tripal website.
  6. */
  7. /**
  8. * A template function which returns markup to display details for the current materialized view
  9. *
  10. * @param $mview_id
  11. * The unique ID of the materialized view to render
  12. *
  13. * @ingroup tripal_core
  14. */
  15. function tripal_mview_report($mview_id) {
  16. // get this mview details
  17. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
  18. $results = db_query($sql, array(':mview_id' => $mview_id));
  19. $mview = $results->fetchObject();
  20. $rows = array();
  21. // create a table with each row containig stats for
  22. // an individual job in the results set.
  23. $output = "<p>" . l("Return to table of materialized views", "admin/tripal/schema/mviews/") . "</p>";
  24. $output .= "<p>Details for <b>$mview->name</b>:</p>";
  25. // build the URLs using the url function so we can handle installations where
  26. // clean URLs are or are not used
  27. $update_url = url("admin/tripal/schema/mviews/action/update/$mview->mview_id");
  28. $delete_url = url("admin/tripal/schema/mviews/action/delete/$mview->mview_id");
  29. $edit_url = url("admin/tripal/schema/mviews/edit/$mview->mview_id");
  30. $rows[] = array('Actions', "<a href='$update_url'>Populate</a>, <a href='$edit_url'>Edit</a>, <a href='$delete_url'>Delete</a>");
  31. if ($mview->last_update > 0) {
  32. $update = format_date($mview->last_update);
  33. }
  34. else {
  35. $update = 'Not yet populated';
  36. }
  37. $rows[] = array('Last Update', $update);
  38. if ($mview->name) {
  39. $rows[] = array('View Name', $mview->name);
  40. }
  41. if ($mview->modulename) {
  42. $rows[] = array('Module Name', $mview->modulename);
  43. }
  44. if ($mview->mv_table) {
  45. $rows[] = array('Table Name', $mview->mv_table);
  46. }
  47. if ($mview->mv_specs) {
  48. $rows[] = array('Table Field Definitions', $mview->mv_specs);
  49. }
  50. if ($mview->query) {
  51. $rows[] = array('Query', "<textarea rows=\"15\" cols=\"120\" style=\"font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;\">" . $mview->query . "</textarea>");
  52. }
  53. if ($mview->indexed) {
  54. $rows[] = array('Indexed Fields', $mview->indexed);
  55. }
  56. if ($mview->special_index) {
  57. $rows[] = array('Special Indexed Fields', $mview->special_index);
  58. }
  59. if ($mview->mv_schema) {
  60. $rows[] = array('Drupal Schema API Definition', "<textarea rows=\"20\" cols=\"120\" style=\"font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;\">" . $mview->mv_schema . "</textarea>");
  61. }
  62. $header = array('Detail', 'Value');
  63. $table = array(
  64. 'header' => $header,
  65. 'rows' => $rows,
  66. 'attributes' => array(),
  67. 'sticky' => FALSE,
  68. 'caption' => '',
  69. 'colgroups' => array(),
  70. 'empty' => 'There are no materialized views',
  71. );
  72. $table = theme_table($table);
  73. $output .= $table;
  74. return $output;
  75. }
  76. /**
  77. * A template function to render a listing of all Materialized Views
  78. *
  79. * @ingroup tripal_core
  80. */
  81. function tripal_mviews_report() {
  82. $header = array('', 'MView Name', 'Last Update', 'Status', 'Description', '');
  83. $rows = array();
  84. $mviews = db_query("SELECT * FROM {tripal_mviews} ORDER BY name");
  85. foreach ($mviews as $mview) {
  86. if ($mview->last_update > 0) {
  87. $update = format_date($mview->last_update);
  88. }
  89. else {
  90. $update = 'Not yet populated';
  91. }
  92. $rows[] = array(
  93. l(t('View'), "admin/tripal/schema/mviews/report/$mview->mview_id") . " | " .
  94. l(t('Edit'), "admin/tripal/schema/mviews/edit/$mview->mview_id") . " | " .
  95. l(t('Populate'), "admin/tripal/schema/mviews/action/update/$mview->mview_id"),
  96. $mview->name,
  97. $update,
  98. $mview->status,
  99. $mview->comment,
  100. l(t('Delete'), "admin/tripal/schema/mviews/action/delete/$mview->mview_id"),
  101. );
  102. }
  103. $rows[] = array(
  104. 'data' => array(
  105. array('data' => l(t('Create a new materialized view.'), "admin/tripal/schema/mviews/new"),
  106. 'colspan' => 6),
  107. )
  108. );
  109. $page = '</p>' . t("Materialized Views (MViews) are custom tables populated with a defined SQL statement.
  110. Because Chado is highly normalized and highly constrained it serves as a wonderful
  111. data storage platform, but unfortunately some queries may be slow. MViews alleviate slowness by aggregating data
  112. into tables that are more easy to query. Use MViews to create tables for custom search pages or custom Tripal
  113. module development.") . '</p>';
  114. $page .= '<p><b>' . t("MViews behaves in the following way:") . '</b><ul>' .
  115. '<li>' . t("The SQL statement defined for an MVIEW will be used to populate the table") . '</li>' .
  116. '<li>' . t("Altering the table structure of an MView will cause the MView table to be dropped and recreated. All records in the MView will be lost.") . '</li>' .
  117. '<li>' . t("Altering the query of an existing view will not change the MView table. No records will be lost. ") . '</li>' .
  118. '<li>' . t("Repopulating an MView that is already populated will result in replacement of all records.") . '</li>' .
  119. '<li>' . t("A database transaction will be used when populating MViews. Therefore replacement of records does not occur until the query completes. Any search forms or pages dependent on the MView will continue to function.") . '</li>' .
  120. '</ul></p>';
  121. $page .= '<b>' . t("Existing MViews") . '</b>';
  122. $table = array(
  123. 'header' => $header,
  124. 'rows' => $rows,
  125. 'attributes' => array(),
  126. 'sticky' => FALSE,
  127. 'caption' => '',
  128. 'colgroups' => array(),
  129. 'empty' => 'There are no materialized views',
  130. );
  131. $page .= theme_table($table);
  132. return $page;
  133. }
  134. /**
  135. * A Form to Create/Edit a Materialized View
  136. *
  137. * @param $form_state
  138. * The current state of the form (Form API)
  139. * @param $mview_id
  140. * The unique ID of the Materialized View to Edit or NULL if creating a new materialized view
  141. *
  142. * @return
  143. * A form array (Form API)
  144. *
  145. * @ingroup tripal_core
  146. */
  147. function tripal_mviews_form($form, &$form_state = NULL, $mview_id = NULL) {
  148. if (!$mview_id) {
  149. $action = 'Add';
  150. }
  151. else {
  152. $action = 'Edit';
  153. }
  154. // set defaults for collapsed fieldsets
  155. $schema_collapsed = 0;
  156. $traditional_collapsed = 1;
  157. $default_name = '';
  158. $default_mv_table = '';
  159. $default_mv_specs = '';
  160. $default_indexed = '';
  161. $default_mvquery = '';
  162. $default_special_index = '';
  163. $default_comment = '';
  164. $default_modulename = '';
  165. $default_schema = '';
  166. // if the view is the older style legacy view then this value get's set to 1
  167. $is_legacy = 0;
  168. // get this requested view
  169. if (strcmp($action, 'Edit') == 0 ) {
  170. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
  171. $results = db_query($sql, array(':mview_id' => $mview_id));
  172. $mview = $results->fetchObject();
  173. // set the default values. If there is a value set in the
  174. // form_state then let's use that, otherwise, we'll pull
  175. // the values from the database
  176. if (array_key_exists('values', $form_state)) {
  177. $default_name = $form_state['values']['name'];
  178. $default_mv_table = $form_state['values']['mv_table'];
  179. $default_mv_specs = $form_state['values']['mv_specs'];
  180. $default_indexed = $form_state['values']['indexed'];
  181. $default_mvquery = $form_state['values']['mvquery'];
  182. $default_special_index = $form_state['values']['special_index'];
  183. $default_comment = $form_state['values']['comment'];
  184. $default_modulename = $form_state['values']['modulename'];
  185. }
  186. if (!$default_name) {
  187. $default_name = $mview->name;
  188. }
  189. if (!$default_mv_table) {
  190. $default_mv_table = $mview->mv_table;
  191. }
  192. if (!$default_mv_specs) {
  193. $default_mv_specs = $mview->mv_specs;
  194. }
  195. if (!$default_indexed) {
  196. $default_indexed = $mview->indexed;
  197. }
  198. if (!$default_mvquery) {
  199. $default_mvquery = $mview->query;
  200. }
  201. if (!$default_special_index) {
  202. $default_special_index = $mview->special_index;
  203. }
  204. if (!$default_comment) {
  205. $default_comment = $mview->comment;
  206. }
  207. if (!$default_schema) {
  208. $default_schema = $mview->mv_schema;
  209. }
  210. if (!$default_modulename) {
  211. $default_modulename = $mview->modulename ? $mview->modulename : 'tripal_core';
  212. }
  213. if ($mview->mv_specs) {
  214. $is_legacy = 1;
  215. }
  216. // the mv_table column of the tripal_mviews table always has the table
  217. // name even if it is a custom table. However, for the sake of the form,
  218. // we do not want this to show up as the mv_table is needed for the
  219. // traditional style input. We'll blank it out if we have a custom
  220. // table and it will get reset in the submit function using the
  221. // 'table' value from the schema array
  222. if ($default_schema) {
  223. $default_mv_table = '';
  224. }
  225. // set which fieldset is collapsed
  226. if (!$default_schema) {
  227. $schema_collapsed = 1;
  228. $traditional_collapsed = 0;
  229. }
  230. }
  231. // Build the form
  232. $form['action'] = array(
  233. '#type' => 'value',
  234. '#value' => $action
  235. );
  236. $form['is_legacy'] = array(
  237. '#type' => 'value',
  238. '#value' => $is_legacy
  239. );
  240. $form['mview_id'] = array(
  241. '#type' => 'value',
  242. '#value' => $mview_id
  243. );
  244. $form['modulename'] = array(
  245. '#type' => 'value',
  246. '#value' => $default_modulename,
  247. );
  248. $form['return_link'] = array(
  249. '#type' => 'item',
  250. '#description' => l("Return to table of materialized views", "admin/tripal/schema/mviews/"),
  251. );
  252. $form['name']= array(
  253. '#type' => 'textfield',
  254. '#title' => t('View Name'),
  255. '#description' => t('Please enter the name for this materialized view.'),
  256. '#required' => TRUE,
  257. '#default_value' => $default_name,
  258. );
  259. $form['comment']= array(
  260. '#type' => 'textarea',
  261. '#title' => t('MView Description'),
  262. '#description' => t('Optional. Please provide a description of the purpose for this materialized vieww.'),
  263. '#required' => FALSE,
  264. '#default_value' => $default_comment,
  265. );
  266. // add a fieldset for the Drupal Schema API
  267. $form['schema'] = array(
  268. '#type' => 'fieldset',
  269. '#title' => 'Drupal Schema API Setup',
  270. '#description' => t('Use the Drupal Schema API array to describe a table. The benefit is that it ' .
  271. 'can be fully integrated with Tripal Views. Tripal supports an extended ' .
  272. 'array format to allow for descriptoin of foreign key relationships.'),
  273. '#collapsible' => 1,
  274. '#collapsed' => $schema_collapsed ,
  275. );
  276. $form['schema']['schema']= array(
  277. '#type' => 'textarea',
  278. '#title' => t('Schema Array'),
  279. '#description' => t('Please enter the Drupal Schema API compatible array that defines the table.'),
  280. '#required' => FALSE,
  281. '#default_value' => $default_schema,
  282. '#rows' => 25,
  283. '#attributes' => array(
  284. 'style' => "font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;",
  285. ),
  286. );
  287. // only let folks edit legacy MViews, not create new ones
  288. if ($is_legacy) {
  289. // add a fieldset for the Original Table Description fields
  290. $form['traditional'] = array(
  291. '#type' => 'fieldset',
  292. '#title' => 'Legacy MViews Setup',
  293. '#description' => t('Traditionally MViews were created by specifying PostgreSQL style ' .
  294. 'column types. This method can be used but is deprecated in favor of the ' .
  295. 'newer Drupal schema API method provided above. In rare cases where the Drupal Schema API ' .
  296. 'does not support a desired data type the Legacy Mviews should be used'),
  297. '#collapsible' => 1,
  298. '#collapsed' => $traditional_collapsed,
  299. );
  300. $form['traditional']['mv_table']= array(
  301. '#type' => 'textfield',
  302. '#title' => t('Table Name'),
  303. '#description' => t('Please enter the table name that this view will generate in the database. You can use the schema and table name for querying the view'),
  304. '#required' => FALSE,
  305. '#default_value' => $default_mv_table,
  306. );
  307. $form['traditional']['mv_specs']= array(
  308. '#type' => 'textarea',
  309. '#title' => t('Table Definition'),
  310. '#description' => t('Please enter the field definitions for this view. Each field should be separated by a comma or enter each field definition on each line.'),
  311. '#required' => FALSE,
  312. '#default_value' => $default_mv_specs,
  313. );
  314. $form['traditional']['indexed']= array(
  315. '#type' => 'textarea',
  316. '#title' => t('Indexed Fields'),
  317. '#description' => t('Please enter the field names (as provided in the table definition above) that will be indexed for this view. Separate by a comma or enter each field on a new line.'),
  318. '#required' => FALSE,
  319. '#default_value' => $default_indexed,
  320. );
  321. /**
  322. $form['traditional']['special_index']= array(
  323. '#type' => 'textarea',
  324. '#title' => t('View Name'),
  325. '#description' => t('Please enter the name for this materialized view.'),
  326. '#required' => TRUE,
  327. '#default_value' => $default_special_index,
  328. );
  329. */
  330. }
  331. $form['mvquery']= array(
  332. '#type' => 'textarea',
  333. '#title' => t('Query'),
  334. '#description' => t('Please enter the SQL statement used to populate the table.'),
  335. '#required' => TRUE,
  336. '#default_value' => $default_mvquery,
  337. '#rows' => 25,
  338. '#attributes' => array(
  339. 'style' => "font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;",
  340. ),
  341. );
  342. if ($action == 'Edit') {
  343. $value = 'Save';
  344. }
  345. if ($action == 'Add') {
  346. $value = 'Add';
  347. }
  348. $form['submit'] = array(
  349. '#type' => 'submit',
  350. '#value' => t($value),
  351. '#weight' => 9,
  352. '#executes_submit_callback' => TRUE,
  353. );
  354. $form['#redirect'] = 'admin/tripal/schema/mviews';
  355. return $form;
  356. }
  357. /**
  358. * Validate the Create/Edit Materialized View Form
  359. * Implements hook_form_validate().
  360. *
  361. * @ingroup tripal_core
  362. */
  363. function tripal_mviews_form_validate($form, &$form_state) {
  364. $action = $form_state['values']['action'];
  365. $mview_id = $form_state['values']['mview_id'];
  366. $name = $form_state['values']['name'];
  367. $is_legacy = $form_state['values']['is_legacy'];
  368. $query = $form_state['values']['mvquery'];
  369. if ($is_legacy) {
  370. $mv_table = $form_state['values']['mv_table'];
  371. $mv_specs = $form_state['values']['mv_specs'];
  372. $indexed = $form_state['values']['indexed'];
  373. $special_index = '';//$form_state['values']['special_index'];
  374. }
  375. else {
  376. $mv_table = '';
  377. $mv_specs = '';
  378. $indexed = '';
  379. $special_index = '';
  380. }
  381. $comment = $form_state['values']['comment'];
  382. $schema = $form_state['values']['schema'];
  383. if ($schema and ($mv_table or $mv_specs or $indexed or $special_index)) {
  384. form_set_error($form_state['values']['schema'],
  385. t('You can create an MView using the Drupal Schema API method or the ' .
  386. 'traditional method but not both.'));
  387. }
  388. if (!$schema) {
  389. if (!$mv_specs) {
  390. form_set_error($form_state['values']['mv_specs'],
  391. t('The Table Definition field is required.'));
  392. }
  393. if (!$mv_table) {
  394. form_set_error($form_state['values']['mv_table'],
  395. t('The Table Name field is required.'));
  396. }
  397. }
  398. // make sure the array is valid
  399. if ($schema) {
  400. $success = eval("\$schema_array = $schema;");
  401. if ($success === FALSE) {
  402. $error = error_get_last();
  403. form_set_error($form_state['values']['schema'],
  404. t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
  405. }
  406. if (!array_key_exists('table', $schema_array)) {
  407. form_set_error($form_state['values']['schema'],
  408. t("The schema array must have key named 'table'"));
  409. }
  410. // TODO: add in more validation checks of the array to help the user
  411. }
  412. }
  413. /**
  414. * Submit the Create/Edit Materialized View Form
  415. * Implements hook_form_submit().
  416. *
  417. * @ingroup tripal_core
  418. */
  419. function tripal_mviews_form_submit($form, &$form_state) {
  420. $ret = array();
  421. $action = $form_state['values']['action'];
  422. $mview_id = $form_state['values']['mview_id'];
  423. $name = $form_state['values']['name'];
  424. $is_legacy = $form_state['values']['is_legacy'];
  425. $query = $form_state['values']['mvquery'];
  426. if ($is_legacy) {
  427. $mv_table = $form_state['values']['mv_table'];
  428. $mv_specs = $form_state['values']['mv_specs'];
  429. $indexed = $form_state['values']['indexed'];
  430. $special_index = '';//$form_state['values']['special_index'];
  431. }
  432. else {
  433. $mv_table = '';
  434. $mv_specs = '';
  435. $indexed = '';
  436. $special_index = '';
  437. }
  438. $comment = $form_state['values']['comment'];
  439. $schema = $form_state['values']['schema'];
  440. $modulename = $form_state['values']['modulename'];
  441. if (!$modulename) {
  442. $modulename = 'tripal_core';
  443. }
  444. if (strcmp($action, 'Edit') == 0) {
  445. tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
  446. $indexed, $query, $special_index, $comment, $schema);
  447. }
  448. elseif (strcmp($action, 'Add') == 0) {
  449. tripal_add_mview($name, $modulename, $mv_table, $mv_specs,
  450. $indexed, $query, $special_index, $comment, $schema);
  451. }
  452. else {
  453. drupal_set_message(t("No action performed."));
  454. }
  455. return '';
  456. }