Tripal v1.0 (6.x-1.0)
mviews.php
Go to the documentation of this file.
00001 <?php
00002 
00017 function tripal_mview_report($mview_id) {
00018 
00019   // get this mview details
00020   $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d";
00021   $mview = db_fetch_object(db_query($sql, $mview_id));
00022   
00023   $rows = array();
00024 
00025   // create a table with each row containig stats for
00026   // an individual job in the results set.
00027   $return_url = url("admin/tripal/mviews/");
00028   $output .= "<p><a href=\"$return_url\">Return to table of materialized views.</a></p>";
00029   $output .= "<p>Details for <b>$mview->name</b>:</p>";
00030   
00031   // build the URLs using the url function so we can handle installations where
00032   // clean URLs are or are not used
00033   $update_url = url("admin/tripal/mviews/action/update/$mview->mview_id");
00034   $delete_url = url("admin/tripal/mviews/action/delete/$mview->mview_id");
00035   $edit_url = url("admin/tripal/mviews/edit/$mview->mview_id");
00036   $rows[] = array('Actions', "<a href='$update_url'>Populate</a>, <a href='$edit_url'>Edit</a>,  <a href='$delete_url'>Delete</a>");
00037   
00038   if ($mview->last_update > 0) {    
00039     $update = format_date($mview->last_update);
00040   }
00041   else {
00042     $update = 'Not yet populated';
00043   }
00044   $rows[] = array('Last Update', $update);  
00045   if ($mview->name) {
00046     $rows[] = array('View Name', $mview->name);
00047   }
00048   if ($mview->modulename) {
00049     $rows[] = array('Module Name', $mview->modulename);    
00050   }
00051   if ($mview->mv_table) {
00052     $rows[] = array('Table Name', $mview->mv_table);
00053   }
00054   if ($mview->mv_specs) {
00055     $rows[] = array('Table Field Definitions', $mview->mv_specs);
00056   }
00057   if ($mview->query) {
00058     $rows[] = array('Query', "<pre>" . $mview->query . "</pre>");
00059   }
00060   if ($mview->indexed) {
00061     $rows[] = array('Indexed Fields', $mview->indexed);
00062   }
00063   if ($mview->special_index) {
00064     $rows[] = array('Special Indexed Fields', $mview->special_index);
00065   }
00066   if ($mview->mv_schema) {
00067     $rows[] = array('Drupal Schema API Definition', "<pre>" . $mview->mv_schema . "</pre>");
00068   }
00069   
00070   $table = theme_table(array(), $rows);
00071   $output .= $table;
00072 
00073   return $output;
00074 }
00075 
00081 function tripal_mviews_report() {
00082   $header = array('', 'MView Name', 'Last Update', 'Status', 'Description', '');
00083   $rows = array();
00084   $mviews = db_query("SELECT * FROM {tripal_mviews} ORDER BY name");
00085 
00086   while ($mview = db_fetch_object($mviews)) {
00087     if ($mview->last_update > 0) {
00088       $update = format_date($mview->last_update);
00089     }
00090     else {
00091       $update = 'Not yet populated';
00092     }
00093 
00094     $rows[] = array(
00095       l(t('View'), "admin/tripal/mviews/report/$mview->mview_id") ." | ".
00096       l(t('Edit'), "admin/tripal/mviews/edit/$mview->mview_id") ." | ".
00097       l(t('Populate'), "admin/tripal/mviews/action/update/$mview->mview_id"),
00098       $mview->name,
00099       $update,
00100       $mview->status,
00101       $mview->comment,
00102       l(t('Delete'), "admin/tripal/mviews/action/delete/$mview->mview_id"),
00103     );
00104   }
00105 
00106   $rows[] = array(
00107     'data' => array(
00108       array('data' => l(t('Create a new materialized view.'), "admin/tripal/mviews/new"),
00109         'colspan' => 6),
00110     )
00111   );
00112   $page = '</p>' . t("Materialized Views (MViews) are custom tables populated with a defined SQL statement.  
00113     Because Chado is highly normalized and highly constrained it serves as a wonderful 
00114     data storage platform, but unfortunately some queries may be slow.  MViews alleviate slowness by aggregating data
00115     into tables that are more easy to query.  Use MViews to create tables for custom search pages or custom Tripal
00116     module development.") . '</p>';
00117   $page .= '<p><b>' . t("MViews behaves in the following way:") . '</b><ul>'.
00118            '<li>' . t("The SQL statement defined for an MVIEW will be used to populate the table") . '</li>' .
00119            '<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>' .
00120            '<li>' . t("Altering the query of an existing view will not change the MView table. No records will be lost. ") . '</li>' .
00121            '<li>' . t("Repopulating an MView that is already populated will result in replacement of all records.") . '</li>' .
00122            '<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>' .
00123            '</ul></p>';
00124   $page .= '<b>' . t("Existing MViews") . '</b>';
00125   $page .= theme('table', $header, $rows);
00126   return $page;
00127 }
00128 
00142 function tripal_mviews_form(&$form_state = NULL, $mview_id = NULL) {
00143 
00144   if (!$mview_id) {
00145     $action = 'Add';
00146   }
00147   else {
00148     $action = 'Edit';
00149   }
00150 
00151   // set defaults for collapsed fieldsets
00152   $schema_collapsed = 0;
00153   $traditional_collapsed = 1;  
00154   
00155   
00156   // get this requested view
00157   if (strcmp($action, 'Edit')==0) {
00158     $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d ";
00159     $mview = db_fetch_object(db_query($sql, $mview_id));
00160 
00161     // set the default values.  If there is a value set in the
00162     // form_state then let's use that, otherwise, we'll pull
00163     // the values from the database
00164     $default_name = $form_state['values']['name'];
00165     $default_mv_table = $form_state['values']['mv_table'];
00166     $default_mv_specs = $form_state['values']['mv_specs'];
00167     $default_indexed = $form_state['values']['indexed'];
00168     $default_mvquery = $form_state['values']['mvquery'];
00169     $default_special_index = $form_state['values']['special_index'];
00170     $default_comment = $form_state['values']['comment'];
00171     $default_modulename = $form_state['values']['modulename'];
00172 
00173     if (!$default_name) {
00174       $default_name = $mview->name;
00175     }
00176     if (!$default_mv_table) {
00177       $default_mv_table = $mview->mv_table;
00178     }
00179     if (!$default_mv_specs) {
00180       $default_mv_specs = $mview->mv_specs;
00181     }
00182     if (!$default_indexed) {
00183       $default_indexed = $mview->indexed;
00184     }
00185     if (!$default_mvquery) {
00186       $default_mvquery = $mview->query;
00187     }
00188     if (!$default_special_index) {
00189       $default_special_index = $mview->special_index;
00190     }
00191     if (!$default_comment) {
00192       $default_comment = $mview->comment;
00193     }
00194     if (!$default_schema) {
00195       $default_schema = $mview->mv_schema;
00196     }
00197     if (!$default_modulename) {      
00198       $default_modulename = $mview->modulename ? $mview->modulename : 'tripal_core';
00199     }
00200     
00201 
00202     // the mv_table column of the tripal_mviews table always has the table
00203     // name even if it is a custom table. However, for the sake of the form,
00204     // we do not want this to show up as the mv_table is needed for the
00205     // traditional style input.  We'll blank it out if we have a custom
00206     // table and it will get reset in the submit function using the
00207     // 'table' value from the schema array
00208     if ($default_schema) {
00209       $default_mv_table = '';
00210     }
00211     // set which fieldset is collapsed
00212     if (!$default_schema) {
00213       $schema_collapsed = 1;
00214       $traditional_collapsed = 0;
00215     }
00216   }
00217 
00218   // Build the form
00219   $form['action'] = array(
00220     '#type' => 'value',
00221     '#value' => $action
00222   );
00223 
00224   $form['mview_id'] = array(
00225     '#type' => 'value',
00226     '#value' => $mview_id
00227   );
00228   
00229   $form['modulename'] = array(
00230     '#type' => 'value',
00231     '#value' => $default_modulename,
00232   );
00233 
00234   $form['name']= array(
00235     '#type'          => 'textfield',
00236     '#title'         => t('View Name'),
00237     '#description'   => t('Please enter the name for this materialized view.'),
00238     '#required'      => TRUE,
00239     '#default_value' => $default_name,
00240   );
00241 
00242   $form['comment']= array(
00243     '#type'          => 'textarea',
00244     '#title'         => t('MView Description'),
00245     '#description'   => t('Optional.  Please provide a description of the purpose for this materialized vieww.'),
00246     '#required'      => FALSE,
00247     '#default_value' => $default_comment,
00248   );
00249 
00250   // add a fieldset for the Drupal Schema API
00251   $form['schema'] = array(
00252     '#type' => 'fieldset',
00253     '#title' => 'Drupal Schema API Setup',
00254     '#description' => t('Use the Drupal Schema API array to describe a table. The benefit is that it '.
00255                        'can be fully integrated with Tripal Views.  Tripal supports an extended '.
00256                        'array format to allow for descriptoin of foreign key relationships.'),
00257     '#collapsible' => 1,
00258     '#collapsed' => $schema_collapsed ,
00259   );
00260 
00261   $form['schema']['schema']= array(
00262     '#type'          => 'textarea',
00263     '#title'         => t('Schema Array'),
00264     '#description'   => t('Please enter the Drupal Schema API compatible array that defines the table.'),
00265     '#required'      => FALSE,
00266     '#default_value' => $default_schema,
00267     '#rows'          => 25,
00268   );
00269 
00270   // add a fieldset for the Original Table Description fields
00271   $form['traditional'] = array(
00272     '#type' => 'fieldset',
00273     '#title' => 'Legacy MViews Setup',
00274     '#description' => t('Traditionally MViews were created by specifying PostgreSQL style '.
00275                        'column types.  This method can be used but is deprecated in favor of the '.
00276                        'newer Drupal schema API method provided above. In rare cases where the Drupal Schema API ' .
00277                        'does not support a desired data type the Legacy Mviews should be used'),
00278     '#collapsible' => 1,
00279     '#collapsed' => $traditional_collapsed,
00280   );
00281 
00282   $form['traditional']['mv_table']= array(
00283     '#type'          => 'textfield',
00284     '#title'         => t('Table Name'),
00285     '#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'),
00286     '#required'      => FALSE,
00287     '#default_value' => $default_mv_table,
00288   );
00289 
00290   $form['traditional']['mv_specs']= array(
00291     '#type'          => 'textarea',
00292     '#title'         => t('Table Definition'),
00293     '#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.'),
00294     '#required'      => FALSE,
00295     '#default_value' => $default_mv_specs,
00296   );
00297 
00298   $form['traditional']['indexed']= array(
00299     '#type'          => 'textarea',
00300     '#title'         => t('Indexed Fields'),
00301     '#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.'),
00302     '#required'      => FALSE,
00303     '#default_value' => $default_indexed,
00304   );
00305 
00316   $form['mvquery']= array(
00317     '#type'          => 'textarea',
00318     '#title'         => t('Query'),
00319     '#description'   => t('Please enter the SQL statement used to populate the table.'),
00320     '#required'      => TRUE,
00321     '#default_value' => $default_mvquery,
00322     '#rows'          => 25,
00323   );
00324 
00325   if ($action == 'Edit') {
00326     $value = 'Save';
00327   }
00328   if ($action == 'Add') {
00329     $value = 'Add';
00330   }
00331   $form['submit'] = array(
00332     '#type'         => 'submit',
00333     '#value'        => t($value),
00334     '#weight'       => 9,
00335     '#executes_submit_callback' => TRUE,
00336   );
00337   $form['#redirect'] = 'admin/tripal/mviews';
00338 
00339   return $form;
00340 }
00341 
00348 function tripal_mviews_form_validate($form, &$form_state) {
00349   $action = $form_state['values']['action'];
00350   $mview_id = $form_state['values']['mview_id'];
00351   $name = $form_state['values']['name'];
00352   $mv_table = $form_state['values']['mv_table'];
00353   $mv_specs = $form_state['values']['mv_specs'];
00354   $indexed = $form_state['values']['indexed'];
00355   $query = $form_state['values']['mvquery'];
00356   $special_index = $form_state['values']['special_index'];
00357   $comment = $form_state['values']['comment'];
00358   $schema = $form_state['values']['schema'];
00359 
00360   if ($schema and ($mv_table or $mv_specs or $indexed or $special_index)) {
00361     form_set_error($form_state['values']['schema'],
00362       t('You can create an MView using the Drupal Schema API method or the '.
00363         'traditional method but not both.'));
00364   }
00365   if (!$schema) {
00366     if (!$mv_specs) {
00367       form_set_error($form_state['values']['mv_specs'],
00368         t('The Table Definition field is required.'));
00369     }
00370     if (!$mv_table) {
00371       form_set_error($form_state['values']['mv_table'],
00372         t('The Table Name field is required.'));
00373     }
00374   }
00375 
00376   // make sure the array is valid
00377   if ($schema) {
00378     $success = eval("\$schema_array = $schema;");
00379     if ($success === FALSE) {
00380       $error = error_get_last();
00381       form_set_error($form_state['values']['schema'],
00382         t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
00383     }
00384     if (!array_key_exists('table', $schema_array)) {
00385       form_set_error($form_state['values']['schema'],
00386         t("The schema array must have key named 'table'"));
00387     }
00388 
00389     // TODO: add in more validation checks of the array to help the user
00390   }
00391 }
00392 
00399 function tripal_mviews_form_submit($form, &$form_state) {
00400 
00401   $ret = array();
00402   $action = $form_state['values']['action'];
00403   $mview_id = $form_state['values']['mview_id'];
00404   $name = $form_state['values']['name'];
00405   $mv_table = $form_state['values']['mv_table'];
00406   $mv_specs = $form_state['values']['mv_specs'];
00407   $indexed = $form_state['values']['indexed'];
00408   $query = $form_state['values']['mvquery'];
00409   $special_index = $form_state['values']['special_index'];
00410   $comment = $form_state['values']['comment'];
00411   $schema = $form_state['values']['schema'];
00412   $modulename = $form_state['values']['modulename'];
00413 
00414   if (strcmp($action, 'Edit') == 0) {
00415     tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
00416       $indexed, $query, $special_index, $comment, $schema);
00417   }
00418   elseif (strcmp($action, 'Add') == 0) {
00419     tripal_add_mview($name, $modulename, $mv_table, $mv_specs,
00420       $indexed, $query, $special_index, $comment, $schema);
00421   }
00422   else {
00423     drupal_set_message(t("No action performed."));
00424   }
00425 
00426   return '';
00427 }
 All Classes Files Functions Variables