123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658 |
- <?php
- function tripal_mview_admin_view() {
- $output = '';
-
- $breadcrumb = array();
- $breadcrumb[] = l('Home', '<front>');
- $breadcrumb[] = l('Administration', 'admin');
- $breadcrumb[] = l('Tripal', 'admin/tripal');
- $breadcrumb[] = l('Chado Schema', 'admin/tripal/storage/chado');
- $breadcrumb[] = l('Materialized Views', 'admin/tripal/storage/chado/mviews');
- drupal_set_breadcrumb($breadcrumb);
-
- $view = views_embed_view('tripal_admin_mviews', 'default');
- if (isset($view)) {
- $output .= $view;
- }
- else {
- $output .= '<p>The Tripal Materalized View management system uses Drupal Views to provide an '
- . 'administrative interface. Currently one or more views needed for this '
- . 'administrative interface are disabled. <strong>Click each of the following links to '
- . 'enable the pertinent views</strong>:</p>';
- $output .= '<ul>';
- $output .= '<li>'.l('MViews View', 'admin/tripal/storage/chado/mviews/views/mviews/enable').'</li>';
- $output .= '</ul>';
- }
- return $output;
- }
- function tripal_mview_report($mview_id) {
-
- $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
- $results = db_query($sql, array(':mview_id' => $mview_id));
- $mview = $results->fetchObject();
- $rows = array();
-
-
- $output = "<p>" . l("Return to table of materialized views", "admin/tripal/storage/chado/mviews/") . "</p>";
- $output .= "<p>Details for <b>$mview->name</b>:</p>";
-
-
- $update_url = url("admin/tripal/storage/chado/mviews/action/update/$mview->mview_id");
- $delete_url = url("admin/tripal/storage/chado/mviews/action/delete/$mview->mview_id");
- $edit_url = url("admin/tripal/storage/chado/mviews/edit/$mview->mview_id");
- $export_url = url("admin/tripal/storage/chado/mviews/export/$mview->mview_id");
- $rows[] = array('Actions', "<a href='$update_url'>Populate</a>, <a href='$edit_url'>Edit</a>, <a href='$delete_url'>Delete</a>");
- if ($mview->last_update > 0) {
- $update = format_date($mview->last_update);
- }
- else {
- $update = 'Not yet populated';
- }
- $rows[] = array('Last Update', $update);
- if ($mview->name) {
- $rows[] = array('View Name', $mview->name);
- }
- if ($mview->modulename) {
- $rows[] = array('Module Name', $mview->modulename);
- }
- if ($mview->mv_table) {
- $rows[] = array('Table Name', $mview->mv_table);
- }
- if ($mview->mv_specs) {
- $rows[] = array('Table Field Definitions', $mview->mv_specs);
- }
- if ($mview->query) {
- $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>");
- }
- if ($mview->indexed) {
- $rows[] = array('Indexed Fields', $mview->indexed);
- }
- if ($mview->special_index) {
- $rows[] = array('Special Indexed Fields', $mview->special_index);
- }
- if ($mview->mv_schema) {
- $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>");
- }
- $header = array('Detail', 'Value');
- $table = array(
- 'header' => $header,
- 'rows' => $rows,
- 'attributes' => array('class' => 'tripal-data-table'),
- 'sticky' => FALSE,
- 'caption' => '',
- 'colgroups' => array(),
- 'empty' => 'There are no materialized views',
- );
- $table = theme_table($table);
- $output .= $table;
- return $output;
- }
- function tripal_mviews_form($form, &$form_state = NULL, $mview_id = NULL) {
- if (!$mview_id) {
- $action = 'Add';
- }
- else {
- $action = 'Edit';
- }
-
- $schema_collapsed = 0;
- $traditional_collapsed = 1;
- $default_name = '';
- $default_mv_table = '';
- $default_mv_specs = '';
- $default_indexed = '';
- $default_mvquery = '';
- $default_special_index = '';
- $default_comment = '';
- $default_modulename = '';
- $default_schema = '';
-
- $is_legacy = 0;
-
- if (strcmp($action, 'Edit') == 0 ) {
- $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
- $mview = db_query($sql, array(':mview_id' => $mview_id))->fetchObject();
-
-
-
- if (array_key_exists('values', $form_state)) {
- $default_name = $form_state['values']['name'];
- $default_mv_table = $form_state['values']['mv_table'];
- $default_mv_specs = $form_state['values']['mv_specs'];
- $default_indexed = $form_state['values']['indexed'];
- $default_mvquery = $form_state['values']['mvquery'];
- $default_special_index = $form_state['values']['special_index'];
- $default_comment = $form_state['values']['comment'];
- $default_modulename = $form_state['values']['modulename'];
- }
- if (!$default_name) {
- $default_name = $mview->name;
- }
- if (!$default_mv_table) {
- $default_mv_table = $mview->mv_table;
- }
- if (!$default_mv_specs) {
- $default_mv_specs = $mview->mv_specs;
- }
- if (!$default_indexed) {
- $default_indexed = $mview->indexed;
- }
- if (!$default_mvquery) {
- $default_mvquery = $mview->query;
- }
- if (!$default_special_index) {
- $default_special_index = $mview->special_index;
- }
- if (!$default_comment) {
- $default_comment = $mview->comment;
- }
- if (!$default_schema) {
- $default_schema = $mview->mv_schema;
- }
- if (!$default_modulename) {
- $default_modulename = $mview->modulename ? $mview->modulename : 'tripal_chado';
- }
- if ($mview->mv_specs) {
- $is_legacy = 1;
- }
-
-
-
-
-
-
- if ($default_schema) {
- $default_mv_table = '';
- }
-
- if (!$default_schema) {
- $schema_collapsed = 1;
- $traditional_collapsed = 0;
- }
- }
-
- $form['action'] = array(
- '#type' => 'value',
- '#value' => $action
- );
- $form['is_legacy'] = array(
- '#type' => 'value',
- '#value' => $is_legacy
- );
- $form['mview_id'] = array(
- '#type' => 'value',
- '#value' => $mview_id
- );
- $form['modulename'] = array(
- '#type' => 'value',
- '#value' => $default_modulename,
- );
- $form['return_link'] = array(
- '#type' => 'item',
- '#description' => l("Return to table of materialized views", "admin/tripal/storage/chado/mviews/"),
- );
- $form['instructions'] = array(
- '#type' => 'fieldset',
- '#title' => 'Instructions',
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- );
- $form['instructions']['text'] = array(
- '#type' => 'item',
- '#markup' => t('Materialized views are used to help speed data
- querying, particularly for searching. A materialized view is essentially
- a database table that is pre-populated with the desired data to search on.
- Rows in the materialized view are typically a combination of data from
- multiple tables with indexes on searchable columns. The table structure
- for materialized views is defined using the ' .
- l('Drupal Schema API', 'https://api.drupal.org/api/drupal/includes!database!schema.inc/group/schemaapi/7',
- array('attributes' => array('target' => '_blank'))) . '. '. t('Additionally,
- an SQL statement is provided that populates the table with data. ' .
- 'Please note that table names should be all lower-case.')
- ),
- );
- $form['instructions']['example_schema']= array(
- '#type' => 'item',
- '#markup' => "An example Schema API definition for a materialized view: <pre>
- array (
- 'description' => 'Stores the type and number of features per organism',
- 'table' => 'organism_feature_count',
- 'fields' => array (
- 'organism_id' => array (
- 'type' => 'int',
- 'not null' => true,
- ),
- 'genus' => array (
- 'type' => 'varchar',
- 'length' => '255',
- 'not null' => true,
- ),
- 'species' => array (
- 'type' => 'varchar',
- 'length' => '255',
- 'not null' => true,
- ),
- 'common_name' => array (
- 'type' => 'varchar',
- 'length' => '255',
- 'not null' => false,
- ),
- 'num_features' => array (
- 'type' => 'int',
- 'not null' => true,
- ),
- 'cvterm_id' => array (
- 'type' => 'int',
- 'not null' => true,
- ),
- 'feature_type' => array (
- 'type' => 'varchar',
- 'length' => '255',
- 'not null' => true,
- ),
- ),
- 'indexes' => array (
- 'organism_id_idx' => array ('organism_id'),
- 'cvterm_id_idx' => array ('cvterm_id'),
- 'feature_type_idx' => array ('feature_type'),
- ),
- )
- </pre>"
- );
- $form['instructions']['example_sql'] = array(
- '#type' => 'item',
- '#markup' => "An example SQL statement to populate the table: <pre>
- SELECT
- O.organism_id, O.genus, O.species, O.common_name,
- count(F.feature_id) as num_features,
- CVT.cvterm_id, CVT.name as feature_type
- FROM organism O
- INNER JOIN feature F ON O.Organism_id = F.organism_id
- INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
- GROUP BY
- O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
- </pre>"
- );
- $form['name']= array(
- '#type' => 'textfield',
- '#title' => t('View Name'),
- '#description' => t('Please enter the name for this materialized view.'),
- '#required' => TRUE,
- '#default_value' => $default_name,
- );
- $form['comment']= array(
- '#type' => 'textarea',
- '#title' => t('MView Description'),
- '#description' => t('Optional. Please provide a description of the purpose for this materialized vieww.'),
- '#required' => FALSE,
- '#default_value' => $default_comment,
- );
-
- $form['schema'] = array(
- '#type' => 'fieldset',
- '#title' => 'Table Schema',
- '#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'))) .
- ' array to describe the table. See the bottom of this page for an example.'),
- '#collapsible' => 1,
- '#collapsed' => $schema_collapsed ,
- );
- $form['schema']['schema']= array(
- '#type' => 'textarea',
- '#title' => t('Schema Array'),
- '#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'))) .
- ' 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.'),
- '#required' => FALSE,
- '#default_value' => $default_schema,
- '#rows' => 25,
- '#attributes' => array(
- 'style' => "font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;",
- ),
- );
-
- if ($is_legacy) {
-
- $form['traditional'] = array(
- '#type' => 'fieldset',
- '#title' => 'Legacy MViews Setup',
- '#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.'),
- '#collapsible' => 1,
- '#collapsed' => $traditional_collapsed,
- );
- $form['traditional']['mv_table']= array(
- '#type' => 'textfield',
- '#title' => t('Table Name'),
- '#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'),
- '#required' => FALSE,
- '#default_value' => $default_mv_table,
- '#attributes' => array('disabled' => 'disabled'),
- );
- $form['traditional']['mv_specs']= array(
- '#type' => 'textarea',
- '#title' => t('Table Definition'),
- '#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.'),
- '#required' => FALSE,
- '#default_value' => $default_mv_specs,
- '#attributes' => array('disabled' => 'disabled'),
- );
- $form['traditional']['indexed']= array(
- '#type' => 'textarea',
- '#title' => t('Indexed Fields'),
- '#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.'),
- '#required' => FALSE,
- '#default_value' => $default_indexed,
- '#attributes' => array('disabled' => 'disabled'),
- );
-
- }
- $form['mvquery']= array(
- '#type' => 'textarea',
- '#title' => t('Query'),
- '#description' => t('Please enter the SQL statement used to populate the table.'),
- '#required' => TRUE,
- '#default_value' => $default_mvquery,
- '#rows' => 25,
- '#attributes' => array(
- 'style' => "font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;",
- ),
- );
- if ($action == 'Edit') {
- $value = 'Save';
- }
- if ($action == 'Add') {
- $value = 'Add';
- }
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t($value),
- '#executes_submit_callback' => TRUE,
- );
- return $form;
- }
- function tripal_mviews_form_validate($form, &$form_state) {
- $action = $form_state['values']['action'];
- $mview_id = $form_state['values']['mview_id'];
- $name = trim($form_state['values']['name']);
- $is_legacy = $form_state['values']['is_legacy'];
- $query = $form_state['values']['mvquery'];
-
-
- if ($is_legacy) {
- $mv_table = trim($form_state['values']['mv_table']);
- $mv_specs = $form_state['values']['mv_specs'];
- $indexed = $form_state['values']['indexed'];
- $special_index = '';
- }
- else {
- $mv_table = '';
- $mv_specs = '';
- $indexed = '';
- $special_index = '';
- }
- $comment = trim($form_state['values']['comment']);
- $schema = $form_state['values']['schema'];
-
- $schema_array = array();
- $success = eval("\$schema_array = $schema;");
- $error = chado_validate_custom_table_schema($schema_array);
- if ($error) {
- form_set_error('schema', $error);
- }
-
-
- if ($schema and ($mv_table or $mv_specs or $indexed or $special_index)) {
- form_set_error($form_state['values']['schema'],
- t('You can create an MView using the Drupal Schema API method or the ' .
- 'traditional method but not both.'));
- }
-
-
- if (!$schema) {
- if (!$mv_specs) {
- form_set_error($form_state['values']['mv_specs'],
- t('The Table Definition field is required.'));
- }
- if (!$mv_table) {
- form_set_error($form_state['values']['mv_table'],
- t('The Table Name field is required.'));
- }
- }
-
- if ($schema) {
- $success = eval("\$schema_array = $schema;");
- if ($success === FALSE) {
- $error = error_get_last();
- form_set_error($form_state['values']['schema'],
- t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
- }
- if (!array_key_exists('table', $schema_array)) {
- form_set_error($form_state['values']['schema'],
- t("The schema array must have key named 'table'"));
- }
-
- }
- }
- function tripal_mviews_form_submit($form, &$form_state) {
- $ret = array();
- $action = $form_state['values']['action'];
- $mview_id = $form_state['values']['mview_id'];
- $name = trim($form_state['values']['name']);
- $is_legacy = $form_state['values']['is_legacy'];
- $query = $form_state['values']['mvquery'];
- $comment = trim($form_state['values']['comment']);
- $schema = $form_state['values']['schema'];
- $modulename = trim($form_state['values']['modulename']);
- $mv_table = '';
- $mv_specs = '';
- $indexed = '';
- $special_index = '';
-
-
- if ($is_legacy) {
- $mv_table = $form_state['values']['mv_table'];
- $mv_specs = $form_state['values']['mv_specs'];
- $indexed = $form_state['values']['indexed'];
- $special_index = '';
- }
- if (!$modulename) {
- $modulename = 'tripal_chado';
- }
-
- if (strcmp($action, 'Edit') == 0) {
- tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
- $indexed, $query, $special_index, $comment, $schema);
- }
-
- elseif (strcmp($action, 'Add') == 0) {
-
- $schema_arr = array();
- eval("\$schema_arr = $schema;");
- tripal_add_mview($name, $modulename, $schema_arr, $query, $comment);
- drupal_goto("admin/tripal/storage/chado/mviews");
- }
- else {
- drupal_set_message(t("No action performed."));
- }
- return '';
- }
- function tripal_mviews_delete_form($form, &$form_state, $mview_id) {
-
- $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
- $results = db_query($sql, array(':mview_id' => $mview_id));
- $entry = $results->fetchObject();
- $form = array();
- $form['mview_id'] = array(
- '#type' => 'value',
- '#value' => $mview_id
- );
- $form['sure'] = array(
- '#type' => 'markup',
- '#markup' => '<p>Are you sure you want to delete the "' . $entry->name . '" materialized view?</p>'
- );
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => 'Delete',
- );
- $form['cancel'] = array(
- '#type' => 'submit',
- '#value' => 'Cancel',
- );
- return $form;
- }
- function tripal_mviews_delete_form_submit($form, &$form_state) {
- $action = $form_state['clicked_button']['#value'];
- $mview_id = $form_state['values']['mview_id'];
- if (strcmp($action, 'Delete') == 0) {
- tripal_delete_mview($mview_id);
- }
- else {
- drupal_set_message(t("No action performed."));
- }
- drupal_goto("admin/tripal/storage/chado/mviews");
- }
- function tripal_mviews_add_populate_job($mview_id) {
- tripal_refresh_mview($mview_id);
- drupal_goto("admin/tripal/storage/chado/mviews");
- }
|