tripal_core.mviews.inc

Contains functions for viewing and editing of Materialized Views on a Tripal website.

File

tripal_core/includes/tripal_core.mviews.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions for viewing and editing of Materialized Views
  5. * on a Tripal website.
  6. */
  7. /**
  8. * @defgroup tripal_mviews Tripal Materialized Views
  9. * @ingroup tripal_core
  10. * @{
  11. * Contains functions for viewing and editing of Materialized Views
  12. * on a Tripal website.
  13. * @}
  14. */
  15. /**
  16. * Provides a landing page for tripal jobs admin
  17. *
  18. * @ingroup tripal_mviews
  19. */
  20. function tripal_mview_admin_view() {
  21. $output = '';
  22. // set the breadcrumb
  23. $breadcrumb = array();
  24. $breadcrumb[] = l('Home', '<front>');
  25. $breadcrumb[] = l('Administration', 'admin');
  26. $breadcrumb[] = l('Tripal', 'admin/tripal');
  27. $breadcrumb[] = l('Chado Schema', 'admin/tripal/schema');
  28. $breadcrumb[] = l('Materialized Views', 'admin/tripal/schema/mviews');
  29. drupal_set_breadcrumb($breadcrumb);
  30. // Add the view
  31. $view = views_embed_view('tripal_core_admin_mviews','default');
  32. if (isset($view)) {
  33. $output .= $view;
  34. }
  35. else {
  36. $output .= '<p>The Tripal Materalized View management system uses primarily views to provide an '
  37. . 'administrative interface. Currently one or more views needed for this '
  38. . 'administrative interface are disabled. <strong>Click each of the following links to '
  39. . 'enable the pertinent views</strong>:</p>';
  40. $output .= '<ul>';
  41. $output .= '<li>'.l('MViews View', 'admin/tripal/schema/mviews/views/mviews/enable').'</li>';
  42. $output .= '</ul>';
  43. }
  44. return $output;
  45. }
  46. /**
  47. * A template function which returns markup to display details for the current materialized view
  48. *
  49. * @param $mview_id
  50. * The unique ID of the materialized view to render
  51. *
  52. * @ingroup tripal_mviews
  53. */
  54. function tripal_mview_report($mview_id) {
  55. // get this mview details
  56. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
  57. $results = db_query($sql, array(':mview_id' => $mview_id));
  58. $mview = $results->fetchObject();
  59. $rows = array();
  60. // create a table with each row containig stats for
  61. // an individual job in the results set.
  62. $output = "<p>" . l("Return to table of materialized views", "admin/tripal/schema/mviews/") . "</p>";
  63. $output .= "<p>Details for <b>$mview->name</b>:</p>";
  64. // build the URLs using the url function so we can handle installations where
  65. // clean URLs are or are not used
  66. $update_url = url("admin/tripal/schema/mviews/action/update/$mview->mview_id");
  67. $delete_url = url("admin/tripal/schema/mviews/action/delete/$mview->mview_id");
  68. $edit_url = url("admin/tripal/schema/mviews/edit/$mview->mview_id");
  69. $export_url = url("admin/tripal/schema/mviews/export/$mview->mview_id");
  70. $rows[] = array('Actions', "<a href='$update_url'>Populate</a>, <a href='$edit_url'>Edit</a>, <a href='$delete_url'>Delete</a>");
  71. if ($mview->last_update > 0) {
  72. $update = format_date($mview->last_update);
  73. }
  74. else {
  75. $update = 'Not yet populated';
  76. }
  77. $rows[] = array('Last Update', $update);
  78. if ($mview->name) {
  79. $rows[] = array('View Name', $mview->name);
  80. }
  81. if ($mview->modulename) {
  82. $rows[] = array('Module Name', $mview->modulename);
  83. }
  84. if ($mview->mv_table) {
  85. $rows[] = array('Table Name', $mview->mv_table);
  86. }
  87. if ($mview->mv_specs) {
  88. $rows[] = array('Table Field Definitions', $mview->mv_specs);
  89. }
  90. if ($mview->query) {
  91. $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>");
  92. }
  93. if ($mview->indexed) {
  94. $rows[] = array('Indexed Fields', $mview->indexed);
  95. }
  96. if ($mview->special_index) {
  97. $rows[] = array('Special Indexed Fields', $mview->special_index);
  98. }
  99. if ($mview->mv_schema) {
  100. $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>");
  101. }
  102. $header = array('Detail', 'Value');
  103. $table = array(
  104. 'header' => $header,
  105. 'rows' => $rows,
  106. 'attributes' => array('class' => 'tripal-data-table'),
  107. 'sticky' => FALSE,
  108. 'caption' => '',
  109. 'colgroups' => array(),
  110. 'empty' => 'There are no materialized views',
  111. );
  112. $table = theme_table($table);
  113. $output .= $table;
  114. return $output;
  115. }
  116. /**
  117. * A Form to Create/Edit a Materialized View
  118. *
  119. * @param $form_state
  120. * The current state of the form (Form API)
  121. * @param $mview_id
  122. * The unique ID of the Materialized View to Edit or NULL if creating a new materialized view
  123. *
  124. * @return
  125. * A form array (Form API)
  126. *
  127. * @ingroup tripal_mviews
  128. */
  129. function tripal_mviews_form($form, &$form_state = NULL, $mview_id = NULL) {
  130. if (!$mview_id) {
  131. $action = 'Add';
  132. }
  133. else {
  134. $action = 'Edit';
  135. }
  136. // set defaults for collapsed fieldsets
  137. $schema_collapsed = 0;
  138. $traditional_collapsed = 1;
  139. $default_name = '';
  140. $default_mv_table = '';
  141. $default_mv_specs = '';
  142. $default_indexed = '';
  143. $default_mvquery = '';
  144. $default_special_index = '';
  145. $default_comment = '';
  146. $default_modulename = '';
  147. $default_schema = '';
  148. // if the view is the older style legacy view then this value get's set to 1
  149. $is_legacy = 0;
  150. // get this requested view
  151. if (strcmp($action, 'Edit') == 0 ) {
  152. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
  153. $mview = db_query($sql, array(':mview_id' => $mview_id))->fetchObject();
  154. // set the default values. If there is a value set in the
  155. // form_state then let's use that, otherwise, we'll pull
  156. // the values from the database
  157. if (array_key_exists('values', $form_state)) {
  158. $default_name = $form_state['values']['name'];
  159. $default_mv_table = $form_state['values']['mv_table'];
  160. $default_mv_specs = $form_state['values']['mv_specs'];
  161. $default_indexed = $form_state['values']['indexed'];
  162. $default_mvquery = $form_state['values']['mvquery'];
  163. $default_special_index = $form_state['values']['special_index'];
  164. $default_comment = $form_state['values']['comment'];
  165. $default_modulename = $form_state['values']['modulename'];
  166. }
  167. if (!$default_name) {
  168. $default_name = $mview->name;
  169. }
  170. if (!$default_mv_table) {
  171. $default_mv_table = $mview->mv_table;
  172. }
  173. if (!$default_mv_specs) {
  174. $default_mv_specs = $mview->mv_specs;
  175. }
  176. if (!$default_indexed) {
  177. $default_indexed = $mview->indexed;
  178. }
  179. if (!$default_mvquery) {
  180. $default_mvquery = $mview->query;
  181. }
  182. if (!$default_special_index) {
  183. $default_special_index = $mview->special_index;
  184. }
  185. if (!$default_comment) {
  186. $default_comment = $mview->comment;
  187. }
  188. if (!$default_schema) {
  189. $default_schema = $mview->mv_schema;
  190. }
  191. if (!$default_modulename) {
  192. $default_modulename = $mview->modulename ? $mview->modulename : 'tripal_core';
  193. }
  194. if ($mview->mv_specs) {
  195. $is_legacy = 1;
  196. }
  197. // the mv_table column of the tripal_mviews table always has the table
  198. // name even if it is a custom table. However, for the sake of the form,
  199. // we do not want this to show up as the mv_table is needed for the
  200. // traditional style input. We'll blank it out if we have a custom
  201. // table and it will get reset in the submit function using the
  202. // 'table' value from the schema array
  203. if ($default_schema) {
  204. $default_mv_table = '';
  205. }
  206. // set which fieldset is collapsed
  207. if (!$default_schema) {
  208. $schema_collapsed = 1;
  209. $traditional_collapsed = 0;
  210. }
  211. }
  212. // Build the form
  213. $form['action'] = array(
  214. '#type' => 'value',
  215. '#value' => $action
  216. );
  217. $form['is_legacy'] = array(
  218. '#type' => 'value',
  219. '#value' => $is_legacy
  220. );
  221. $form['mview_id'] = array(
  222. '#type' => 'value',
  223. '#value' => $mview_id
  224. );
  225. $form['modulename'] = array(
  226. '#type' => 'value',
  227. '#value' => $default_modulename,
  228. );
  229. $form['return_link'] = array(
  230. '#type' => 'item',
  231. '#description' => l("Return to table of materialized views", "admin/tripal/schema/mviews/"),
  232. );
  233. $form['instructions'] = array(
  234. '#type' => 'fieldset',
  235. '#title' => 'Instructions',
  236. '#collapsible' => TRUE,
  237. '#collapsed' => TRUE,
  238. );
  239. $form['instructions']['text'] = array(
  240. '#type' => 'item',
  241. '#markup' => t('Materialized views are used to help speed data
  242. querying, particularly for searching. A materialized view is essentially
  243. a database table that is pre-populated with the desired data to search on.
  244. Rows in the materialized view are typically a combination of data from
  245. multiple tables with indexes on searchable columns. The table structure
  246. for materialized views is defined using the ' .
  247. l('Drupal Schema API', 'https://api.drupal.org/api/drupal/includes!database!schema.inc/group/schemaapi/7',
  248. array('attributes' => array('target' => '_blank'))) . '. '. t('Additionally,
  249. an SQL statement is provided that populates the table with data. ' .
  250. 'Please note that table names should be all lower-case.')
  251. ),
  252. );
  253. $form['instructions']['example_schema']= array(
  254. '#type' => 'item',
  255. '#markup' => "An example Schema API definition for a materialized view: <pre>
  256. array (
  257. 'description' => 'Stores the type and number of features per organism',
  258. 'table' => 'organism_feature_count',
  259. 'fields' => array (
  260. 'organism_id' => array (
  261. 'type' => 'int',
  262. 'not null' => true,
  263. ),
  264. 'genus' => array (
  265. 'type' => 'varchar',
  266. 'length' => '255',
  267. 'not null' => true,
  268. ),
  269. 'species' => array (
  270. 'type' => 'varchar',
  271. 'length' => '255',
  272. 'not null' => true,
  273. ),
  274. 'common_name' => array (
  275. 'type' => 'varchar',
  276. 'length' => '255',
  277. 'not null' => false,
  278. ),
  279. 'num_features' => array (
  280. 'type' => 'int',
  281. 'not null' => true,
  282. ),
  283. 'cvterm_id' => array (
  284. 'type' => 'int',
  285. 'not null' => true,
  286. ),
  287. 'feature_type' => array (
  288. 'type' => 'varchar',
  289. 'length' => '255',
  290. 'not null' => true,
  291. ),
  292. ),
  293. 'indexes' => array (
  294. 'organism_id_idx' => array ('organism_id'),
  295. 'cvterm_id_idx' => array ('cvterm_id'),
  296. 'feature_type_idx' => array ('feature_type'),
  297. ),
  298. )
  299. </pre>"
  300. );
  301. $form['instructions']['example_sql'] = array(
  302. '#type' => 'item',
  303. '#markup' => "An example SQL statement to populate the table: <pre>
  304. SELECT
  305. O.organism_id, O.genus, O.species, O.common_name,
  306. count(F.feature_id) as num_features,
  307. CVT.cvterm_id, CVT.name as feature_type
  308. FROM organism O
  309. INNER JOIN feature F ON O.Organism_id = F.organism_id
  310. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  311. GROUP BY
  312. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  313. </pre>"
  314. );
  315. $form['name']= array(
  316. '#type' => 'textfield',
  317. '#title' => t('View Name'),
  318. '#description' => t('Please enter the name for this materialized view.'),
  319. '#required' => TRUE,
  320. '#default_value' => $default_name,
  321. );
  322. $form['comment']= array(
  323. '#type' => 'textarea',
  324. '#title' => t('MView Description'),
  325. '#description' => t('Optional. Please provide a description of the purpose for this materialized vieww.'),
  326. '#required' => FALSE,
  327. '#default_value' => $default_comment,
  328. );
  329. // add a fieldset for the Drupal Schema API
  330. $form['schema'] = array(
  331. '#type' => 'fieldset',
  332. '#title' => 'Table Schema',
  333. '#description' => t('Use a ' . l("Drupal Schema API", "https://api.drupal.org/api/drupal/includes!database!schema.inc/group/schemaapi/7", array('attributes' => array('target' => '_blank'))) .
  334. ' array to describe the table. See the bottom of this page for an example.'),
  335. '#collapsible' => 1,
  336. '#collapsed' => $schema_collapsed ,
  337. );
  338. $form['schema']['schema']= array(
  339. '#type' => 'textarea',
  340. '#title' => t('Schema Array'),
  341. '#description' => t('Please enter the ' . l("Drupal Schema API", "https://api.drupal.org/api/drupal/includes!database!schema.inc/group/schemaapi/7", array('attributes' => array('target' => '_blank'))) .
  342. ' compatible array that defines the table. There must also be a "table" key with the name of the table as the value. See the example at the bottom of this page.'),
  343. '#required' => FALSE,
  344. '#default_value' => $default_schema,
  345. '#rows' => 25,
  346. '#attributes' => array(
  347. 'style' => "font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;",
  348. ),
  349. );
  350. // only let folks edit legacy MViews, not create new ones
  351. if ($is_legacy) {
  352. // add a fieldset for the Original Table Description fields
  353. $form['traditional'] = array(
  354. '#type' => 'fieldset',
  355. '#title' => 'Legacy MViews Setup',
  356. '#description' => t('<font color="red">Tripal no longer supports editing of legacy style materialized views. </font> This view will continue to function and you can populate it, however, to update you must convert this view to the newer Drupal Schema API format using the "Table Schema" section above. Unfortunately, after converting this view to the Schema API and saving, the materialized view be recreated and emptied. You will need to re-populate it. Therefore, you may want to schedule update of this or any other legacy materialized during your next site maintenance.'),
  357. '#collapsible' => 1,
  358. '#collapsed' => $traditional_collapsed,
  359. );
  360. $form['traditional']['mv_table']= array(
  361. '#type' => 'textfield',
  362. '#title' => t('Table Name'),
  363. '#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'),
  364. '#required' => FALSE,
  365. '#default_value' => $default_mv_table,
  366. '#attributes' => array('disabled' => 'disabled'),
  367. );
  368. $form['traditional']['mv_specs']= array(
  369. '#type' => 'textarea',
  370. '#title' => t('Table Definition'),
  371. '#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.'),
  372. '#required' => FALSE,
  373. '#default_value' => $default_mv_specs,
  374. '#attributes' => array('disabled' => 'disabled'),
  375. );
  376. $form['traditional']['indexed']= array(
  377. '#type' => 'textarea',
  378. '#title' => t('Indexed Fields'),
  379. '#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.'),
  380. '#required' => FALSE,
  381. '#default_value' => $default_indexed,
  382. '#attributes' => array('disabled' => 'disabled'),
  383. );
  384. /**
  385. $form['traditional']['special_index']= array(
  386. '#type' => 'textarea',
  387. '#title' => t('View Name'),
  388. '#description' => t('Please enter the name for this materialized view.'),
  389. '#required' => TRUE,
  390. '#default_value' => $default_special_index,
  391. );
  392. */
  393. }
  394. $form['mvquery']= array(
  395. '#type' => 'textarea',
  396. '#title' => t('Query'),
  397. '#description' => t('Please enter the SQL statement used to populate the table.'),
  398. '#required' => TRUE,
  399. '#default_value' => $default_mvquery,
  400. '#rows' => 25,
  401. '#attributes' => array(
  402. 'style' => "font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;",
  403. ),
  404. );
  405. if ($action == 'Edit') {
  406. $value = 'Save';
  407. }
  408. if ($action == 'Add') {
  409. $value = 'Add';
  410. }
  411. $form['submit'] = array(
  412. '#type' => 'submit',
  413. '#value' => t($value),
  414. '#executes_submit_callback' => TRUE,
  415. );
  416. return $form;
  417. }
  418. /**
  419. * Validate the Create/Edit Materialized View Form
  420. * Implements hook_form_validate().
  421. *
  422. * @ingroup tripal_mviews
  423. */
  424. function tripal_mviews_form_validate($form, &$form_state) {
  425. $action = $form_state['values']['action'];
  426. $mview_id = $form_state['values']['mview_id'];
  427. $name = trim($form_state['values']['name']);
  428. $is_legacy = $form_state['values']['is_legacy'];
  429. $query = $form_state['values']['mvquery'];
  430. // if this is a legacy materialized view (no longer supported in Tripal v2.0
  431. // but code left just in case)
  432. if ($is_legacy) {
  433. $mv_table = trim($form_state['values']['mv_table']);
  434. $mv_specs = $form_state['values']['mv_specs'];
  435. $indexed = $form_state['values']['indexed'];
  436. $special_index = '';//$form_state['values']['special_index'];
  437. }
  438. else {
  439. $mv_table = '';
  440. $mv_specs = '';
  441. $indexed = '';
  442. $special_index = '';
  443. }
  444. $comment = trim($form_state['values']['comment']);
  445. $schema = $form_state['values']['schema'];
  446. // validate the contents of the array
  447. $schema_array = array();
  448. $success = eval("\$schema_array = $schema;");
  449. $error = chado_validate_custom_table_schema($schema_array);
  450. if ($error) {
  451. form_set_error('schema', $error);
  452. }
  453. // if both the schema and the older fields for the legacy view are populated then
  454. // this is an error and we need to let the user know.
  455. if ($schema and ($mv_table or $mv_specs or $indexed or $special_index)) {
  456. form_set_error($form_state['values']['schema'],
  457. t('You can create an MView using the Drupal Schema API method or the ' .
  458. 'traditional method but not both.'));
  459. }
  460. // if we don't have a schema and are missing fields for the legacy views then
  461. // inform the user.
  462. if (!$schema) {
  463. if (!$mv_specs) {
  464. form_set_error($form_state['values']['mv_specs'],
  465. t('The Table Definition field is required.'));
  466. }
  467. if (!$mv_table) {
  468. form_set_error($form_state['values']['mv_table'],
  469. t('The Table Name field is required.'));
  470. }
  471. }
  472. // make sure the array is valid
  473. if ($schema) {
  474. $success = eval("\$schema_array = $schema;");
  475. if ($success === FALSE) {
  476. $error = error_get_last();
  477. form_set_error($form_state['values']['schema'],
  478. t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
  479. }
  480. if (!array_key_exists('table', $schema_array)) {
  481. form_set_error($form_state['values']['schema'],
  482. t("The schema array must have key named 'table'"));
  483. }
  484. // TODO: add in more validation checks of the array to help the user
  485. }
  486. }
  487. /**
  488. * Submit the Create/Edit Materialized View Form
  489. * Implements hook_form_submit().
  490. *
  491. * @ingroup tripal_mviews
  492. */
  493. function tripal_mviews_form_submit($form, &$form_state) {
  494. $ret = array();
  495. $action = $form_state['values']['action'];
  496. $mview_id = $form_state['values']['mview_id'];
  497. $name = trim($form_state['values']['name']);
  498. $is_legacy = $form_state['values']['is_legacy'];
  499. $query = $form_state['values']['mvquery'];
  500. $comment = trim($form_state['values']['comment']);
  501. $schema = $form_state['values']['schema'];
  502. $modulename = trim($form_state['values']['modulename']);
  503. $mv_table = '';
  504. $mv_specs = '';
  505. $indexed = '';
  506. $special_index = '';
  507. // if this is a legacy materialized view (no longer supported in Tripal v2.0
  508. // but code left just in case)
  509. if ($is_legacy) {
  510. $mv_table = $form_state['values']['mv_table'];
  511. $mv_specs = $form_state['values']['mv_specs'];
  512. $indexed = $form_state['values']['indexed'];
  513. $special_index = '';//$form_state['values']['special_index'];
  514. }
  515. if (!$modulename) {
  516. $modulename = 'tripal_core';
  517. }
  518. // if this is an edit action
  519. if (strcmp($action, 'Edit') == 0) {
  520. tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
  521. $indexed, $query, $special_index, $comment, $schema);
  522. }
  523. // else an add action
  524. elseif (strcmp($action, 'Add') == 0) {
  525. // convert the schema into a PHP array
  526. $schema_arr = array();
  527. eval("\$schema_arr = $schema;");
  528. tripal_add_mview($name, $modulename, $schema_arr, $query, $comment);
  529. drupal_goto("admin/tripal/schema/mviews");
  530. }
  531. else {
  532. drupal_set_message(t("No action performed."));
  533. }
  534. return '';
  535. }
  536. /**
  537. * Just a simple form for confirming deletion of a custom table
  538. *
  539. * @ingroup tripal_custom_tables
  540. */
  541. function tripal_mviews_delete_form($form, &$form_state, $mview_id) {
  542. // get details about this table entry
  543. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
  544. $results = db_query($sql, array(':mview_id' => $mview_id));
  545. $entry = $results->fetchObject();
  546. $form = array();
  547. $form['mview_id'] = array(
  548. '#type' => 'value',
  549. '#value' => $mview_id
  550. );
  551. $form['sure'] = array(
  552. '#type' => 'markup',
  553. '#markup' => '<p>Are you sure you want to delete the "' . $entry->name . '" materialized view?</p>'
  554. );
  555. $form['submit'] = array(
  556. '#type' => 'submit',
  557. '#value' => 'Delete',
  558. );
  559. $form['cancel'] = array(
  560. '#type' => 'submit',
  561. '#value' => 'Cancel',
  562. );
  563. return $form;
  564. }
  565. /**
  566. * form submit hook for the tripal_custom_tables_delete_form form.
  567. *
  568. * @param $form
  569. * @param $form_state
  570. */
  571. function tripal_mviews_delete_form_submit($form, &$form_state) {
  572. $action = $form_state['clicked_button']['#value'];
  573. $mview_id = $form_state['values']['mview_id'];
  574. if (strcmp($action, 'Delete') == 0) {
  575. tripal_delete_mview($mview_id);
  576. }
  577. else {
  578. drupal_set_message(t("No action performed."));
  579. }
  580. drupal_goto("admin/tripal/schema/mviews");
  581. }
  582. /**
  583. * A wrapper for the tripal_refresh_mview() API function, which
  584. * then redirects back to the admin page for mviews.
  585. *
  586. * @param $mview_id
  587. */
  588. function tripal_mviews_add_populate_job($mview_id) {
  589. tripal_refresh_mview($mview_id);
  590. drupal_goto("admin/tripal/schema/mviews");
  591. }