Tripal v1.0 (6.x-1.0)
tripal_views_integration.inc
Go to the documentation of this file.
00001 <?php
00002 
00016 function tripal_views_description_page() {
00017 
00018   $text .= '<h3>Tripal Views Quick Links:</h3>';
00019   $text .= "<ul>
00020              <li><a href=\"" . url("admin/tripal/views/integration/list")  .  "\">List of integrated tables</a></li>
00021              <li><a href=\"" . url("admin/tripal/views/integration/new") .  "\">Integrate a new table</a></li>
00022              <li><a href=\"" . url("admin/tripal/views/integration/import") .  "\">Import a previously exported table integration.</a></li>
00023            </ul>";
00024 
00025   $text .= '<h3>Views Integration Description:</h3>';
00026   $text .= '<p>Tripal Views provides an interface for integrating <a href="http://drupal.org/project/views">Drupal Views</a>
00027             with Tripal materialized views.  This will allow site administrators to create custom queries for the materialized views
00028             and in turn provide custom content pages, custom blocks and custom search forms. The forms allow a site administrator
00029             to select a materialized view and associate other Chado tables on which the view can join.  Usage of this module requires
00030             a good understanding of foreign-key relationships in Chado.
00031             </p>';
00032 
00033   $text .= '<h3>Setup Instructions:</h3>';
00034   $text .= '<p>After installation of the Tripal core module.  The following tasks should be performed
00035             <ol>
00036               <li><b>Set Permissions</b>: To allow access to site administrators for this module, simply
00037                <a href="' . url('admin/user/permissions') . '">assign permissions</a> to the appropriate user roles for the
00038                permission type "manage tripal_views_integration". </li>
00039             </ol>
00040             </p>';
00041   $text .= '<h3>Usage Instructions:</h3>';
00042   $text .= "<p>To use Tripal Views integration follow these steps:
00043             <ol>
00044                <li><b>Identify or create a materialized view:</b> Using the <a href=\"" . url("admin/tripal/views/mviews") . "\">
00045                  Tripal materialized View</a> interface, identify the view you would like to integrate or create a new one.</li>
00046                <li><b>Setup the Views Integration</b>: Navigate to the <a href=\"" . url("admin/tripal/views/integration/mviews/new") . "\">
00047                  Tripal views integration setup page</a> to integrate the selected materialized view.  Provide a  user friendly name
00048                  and description to help you remember the purpose for integrating the view.  Next, select the view you want to integrate
00049                  from the provided select box.  If your materialized view has fields that can join with other Chado tables, you may
00050                  provide those relationships in the provided form.  Finally, if your fields require a special handler for display, you
00051                  may select it from the drop down provided</li>
00052                <li><b>Create custom pages/block/search form</b>:  After saving setup information from step 2 above, you will be redirected to the
00053                  Drupal Views interface</a> where you can create a custom page, block or search form.</li>
00054                <li><b>Review your integrated views</b>:  A page providing a
00055                  <a href=\"" . url("admin/tripal/views/integration/mviews/list") . "\">list of all integrated views</a> is provided. You may
00056                  view this page to see all integrated views, but also to remove any unwanted integrations.</li>
00057             </ol>
00058 
00059             </p>";
00060   return $text;
00061 }
00062 
00073 function tripal_views_integration_setup_list() {
00074   $output = '';
00075 
00076   $output .=  l(t('Add a New Entry'), "admin/tripal/views/integration/new") ;
00077 
00078   $output .= '<p>' . t('The following tables are available for integration with Drupal Views. If '
00079     . 'a table is integrated more than once, then the setup with the lightest '
00080     . 'priority will be used. For example, if you have created a custom setup with a priority of -5 then '
00081     . 'that will be used instead of the default setup with priority 10. '
00082     . 'Priorities range from -10 to +10  where a setup with -10 has '
00083     . 'greater precedent than any other and +10 has the least.') . '</p>';
00084 
00085 
00086   // Start with materialized views
00087   $output .= '<br /><h3>Legacy Materialized Views</h3>';
00088   $header = array('', 'Drupal Views Type Name', 'Table Name', 'Is Legacy?', 'Priority', 'Comment');
00089   $rows = array();
00090 
00091   // get the list of materialized views
00092   $tviews = db_query('SELECT tv.setup_id, tv.name, tv.table_name, tc.table_id, tv.priority, tv.comment '
00093                      .'FROM {tripal_views} tv '
00094                      .'LEFT JOIN {tripal_custom_tables} tc ON tc.table_name=tv.table_name '
00095                      .'WHERE tv.mview_id IS NOT NULL '
00096                      .'ORDER BY tv.table_name ASC, tv.priority ASC');
00097   while ($tview = db_fetch_object($tviews)) {
00098     $rows[] = array(
00099       l(t('Edit'), "admin/tripal/views/integration/edit/" . $tview->setup_id) . "<br />"
00100         . l(t('Export'), "admin/tripal/views/integration/export/" . $tview->setup_id) . "<br />"
00101         . l(t('Delete'), "admin/tripal/views/integration/delete/" . $tview->setup_id),
00102       $tview->name,
00103       $tview->table_name,
00104       ($tview->table_id) ? 'No' : 'Yes',
00105       $tview->priority,
00106       $tview->comment,
00107     );
00108   }
00109 
00110   if ($rows) {
00111     $output .= theme('table', $header, $rows);
00112   }
00113   else {
00114     $output .= '<p>There are currently no Materialized Views defined. ';
00115     $output .=  l(t('Add a New Entry'), "admin/tripal/views/integration/new") . '</p>';
00116 
00117   }
00118 
00119   // Now list non-mview custom tables
00120   $output .= '<br /><h3>Custom Tables & Non-Legacy Materialized Views</h3>';
00121   $header = array('', 'Drupal Views Type Name', 'Table Name', 'Priority', 'Comment');
00122   $rows = array();
00123 
00124   // get the list of chado tables
00125   $tviews = db_query('SELECT tv.setup_id, tv.name, tv.table_name, tv.priority, tv.comment '
00126                      .'FROM {tripal_views} tv '
00127                      .'LEFT JOIN {tripal_custom_tables} tc ON tc.table_name=tv.table_name '
00128                      .'WHERE mview_id IS NULL AND tc.table_id IS NOT NULL '
00129                      .'ORDER BY table_name ASC, priority ASC');
00130   while ($tview = db_fetch_object($tviews)) {
00131     $rows[] = array(
00132       l(t('Edit'), "admin/tripal/views/integration/edit/" . $tview->setup_id) . "<br />"
00133         . l(t('Export'), "admin/tripal/views/integration/export/" . $tview->setup_id) . "<br />"
00134         . l(t('Delete'), "admin/tripal/views/integration/delete/" . $tview->setup_id),
00135       $tview->name,
00136       $tview->table_name,
00137       $tview->priority,
00138       $tview->comment,
00139     );
00140   }
00141 
00142   if ($rows) {
00143     $output .= theme('table', $header, $rows);
00144   }
00145   else {
00146     $output .= '<p>There are currently no non-Materialized View Custom Tables defined.</p>';
00147   }
00148 
00149   // Now list chado tables
00150   $output .= '<br /><h3>Chado Tables</h3>';
00151   $header = array('', 'Drupal Views Type Name', 'Table Name', 'Priority', 'Comment');
00152   $rows = array();
00153 
00154   // get the list of chado tables
00155   $tviews = db_query('SELECT tv.setup_id, tv.name, tv.table_name, tv.priority, tv.comment '
00156                      .'FROM {tripal_views} tv '
00157                      .'LEFT JOIN {tripal_custom_tables} tc ON tc.table_name=tv.table_name '
00158                      .'WHERE mview_id IS NULL AND tc.table_id IS NULL '
00159                      .'ORDER BY table_name ASC, priority ASC');
00160   while ($tview = db_fetch_object($tviews)) {
00161     $rows[] = array(
00162       l(t('Edit'), "admin/tripal/views/integration/edit/" . $tview->setup_id) . "<br />"
00163         . l(t('Export'), "admin/tripal/views/integration/export/" . $tview->setup_id) . "<br />"
00164         . l(t('Delete'), "admin/tripal/views/integration/delete/" . $tview->setup_id),
00165       $tview->name,
00166       $tview->table_name,
00167       $tview->priority,
00168       $tview->comment,
00169     );
00170   }
00171 
00172   $output .= theme('table', $header, $rows);
00173   return $output;
00174 }
00175 
00186 function tripal_views_integration_delete($setup_id) {
00187 
00188   tripal_views_integration_remove_entry_by_setup_id($setup_id);
00189   drupal_set_message(t("Record Deleted"));
00190   drupal_goto('admin/tripal/views/integration');
00191 
00192 }
00193 
00211 function tripal_views_integration_form(&$form_state, $setup_id = NULL) {
00212 
00213   $form = array();
00214   $data = array();
00215   $form['#cache'] = TRUE;
00216 
00217   // initialize Tripal AHAH
00218   tripal_core_ahah_init_form();
00219 
00220   // if a setup_id is provided then we want to get the form defaults
00221   $setup_obj = array();
00222   if (isset($setup_id)) {
00223 
00224     // get the deafult setup values
00225     $sql = "SELECT * FROM {tripal_views} WHERE setup_id = %d";
00226     $setup_obj = db_fetch_object(db_query($sql, $setup_id));
00227     $mview_id = $setup_obj->mview_id;
00228     $table_name = $setup_obj->table_name;
00229     $form_state['values']['mview_id'] = $mview_id;
00230     $form_state['values']['table_name'] = $table_name;
00231 
00232     // get the default field name/description
00233     $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=%d";
00234     $query = db_query($sql, $setup_id);
00235     $default_fields = array();
00236     while ($field = db_fetch_object($query)) {
00237       $default_fields[$field->column_name]['name'] = $field->name;
00238       $default_fields[$field->column_name]['description'] = $field->description;
00239     }
00240 
00241     // get the default join settings and handlers
00242     $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
00243     $query = db_query($sql, $setup_id);
00244     $default_joins = array();
00245     while ($join = db_fetch_object($query)) {
00246       $default_joins[$join->base_field]['left_table'] = $join->left_table;
00247       $default_joins[$join->base_field]['left_field'] = $join->left_field;
00248     }
00249 
00250     // get the default handlers
00251     $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = %d";
00252     $query = db_query($sql, $setup_id);
00253     $default_handlers = array();
00254     while ($handler = db_fetch_object($query)) {
00255       $default_handlers[$handler->column_name][$handler->handler_type]['handler_name'] = $handler->handler_name;
00256       $default_handlers[$handler->column_name][$handler->handler_type]['arguments'] = $handler->arguments;
00257     }
00258 
00259     // get the default join handlers
00260     $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
00261     $query = db_query($sql, $setup_id);
00262     while ($handler = db_fetch_object($query)) {
00263       $default_handlers[$handler->base_field]['join']['handler_name'] = $handler->handler;
00264       //$default_handlers[$handler->base_field]['join']['arguments'] = $handler->arguments;
00265     }
00266 
00267     // add in the setup_id for the form so we know this is an update not an insert
00268     $form['setup_id'] = array(
00269        '#type' => 'hidden',
00270        '#value' => $setup_id,
00271     );
00272   }
00273 
00274   // add a fieldset for the MView & Chado table selectors
00275   $form['base_table_type'] = array(
00276     '#type' => 'fieldset',
00277     '#title' => 'Base Table',
00278     '#description' => 'Please select either a materialized view or a Chado table for integration with '.
00279                        'Drupal Views.  In Drupal Views terminology, the selected table becomes the "base table". '.
00280                        'After you select a table from either list, the fields from that table will appear below '.
00281                        'and you can specify other tables to join with and handlers.',
00282   );
00283 
00284   // build the form element for the Chado tables
00285   $chado_tables = tripal_core_get_chado_tables(TRUE);
00286   $chado_tables = array_merge(array('Select'), $chado_tables);
00287   $form['base_table_type']['table_name'] = array(
00288     '#title' => t('Chado/Custom Table'),
00289     '#type' => 'select',
00290     '#options' => $chado_tables,
00291     '#description' => t('Tables from Chado, custom tables and materialized view tables (non-legacy MViews) can be selected for integration.'),
00292     '#default_value' => (!$setup_obj->mview_id) ? $setup_obj->table_name : '',
00293     '#ahah' => array(
00294        'path' => 'tripal/views/integration/ajax/view_setup_table',
00295        'wrapper' => 'table-rows-div',
00296        'effect' => 'fade',
00297        'event' => 'change',
00298        'method' => 'replace',
00299     ),
00300   );
00301 
00302 
00303   // build the form element that lists the materialized views
00304   $query = db_query("SELECT mview_id, name FROM {tripal_mviews} WHERE mv_schema is NULL or mv_schema = '' ORDER BY name");
00305   $mview_tables = array();
00306   $mview_tables['0'] = 'Select';
00307   while ($mview = db_fetch_object($query)) {
00308     $mview_tables[$mview->mview_id] = $mview->name;
00309   }
00310   $form['base_table_type']['mview_id'] = array(
00311     '#title' => t('Legacy Materialized View'),
00312     '#type' => 'select',
00313     '#options' => $mview_tables,
00314     '#description' => 'Which materialized view to use.',
00315     '#default_value' => $setup_obj->mview_id,
00316     '#ahah' => array(
00317        'path' => 'tripal/views/integration/ajax/view_setup_table',
00318        'wrapper' => 'table-rows-div',
00319        'effect' => 'fade',
00320        'event' => 'change',
00321        'method' => 'replace',
00322     ),
00323   );
00324 
00325   $form['views_type'] = array(
00326     '#type' => 'fieldset',
00327     '#title' => 'View Type',
00328     '#description' => 'Here you can provide the "type" of View you want to create.',
00329   );
00330 
00331   // field for the name of the
00332   $form['views_type']['row_name'] = array(
00333     '#title' => t('View Type Name'),
00334     '#type' => 'textfield',
00335     '#default_value' => $setup_obj->name,
00336     '#size' => 60,
00337     '#maxlength' => 128,
00338     '#description' => 'Provide the view type name.  This is the name that will appear in '.
00339                       'the Drupal Views interface when adding a new view.  The view type name '.
00340                       'must be unique.',
00341     '#required' => TRUE,
00342   );
00343 
00344   if (isset($setup_id)) {
00345     $form['row_name']['#attributes'] = array('readonly' => 'readonly');
00346   }
00347 
00348   $priorities = array();
00349   foreach (range(-10, 10) as $v) {
00350     $priorities[$v] = (string) $v;
00351   }
00352   $form['views_type']['row_priority'] = array(
00353     '#type' => 'select',
00354     '#title' => t('Priority'),
00355     '#description' => t('The level of priority your Views integration has in relation to the '
00356       .'default core and module definitions. The views integration definition with the '
00357       .'lightest priority will be used. For example, if there is a definition created by '
00358       .'core with a priority of 10 and another by a custom module of 5 and yours is -1 then '
00359       .'you definition will be used for that table because -1 is lighter then both 5 and 10.'),
00360     '#options' => $priorities,
00361     '#default_value' => (isset($setup_obj->priority)) ? $setup_obj->priority : -1,
00362   );
00363   if ($setup_obj->priority >= 9) {
00364     drupal_set_message('You are editing a default views integration. To ensure your changes
00365     are used, change the priority to -10.', 'warning');
00366   }
00367 
00368   $form['views_type']['new_integration'] = array(
00369     '#type' => 'checkbox',
00370     '#title' => t('Create a New Tripal Views Integration Record'),
00371     '#description' => t('If this checkbox is checked then a new tripal views integration '
00372       .'will be created rather then overriding the current one with your changes. This '
00373       .'especially important if you are editing one of the default views integrations '
00374       .'(ie: if the original priority was either 10 or 9).'),
00375     '#default_value' => ($setup_obj->priority >= 9) ? true : false,
00376   );
00377 
00378   $form['views_type']['base_table'] = array(
00379     '#type' => 'checkbox',
00380     '#title' => t('Base Table?'),
00381     '#description' => t('If you want this table to show up as one of the options in the '
00382       . 'add view page, then check this checkbox. It allows you to create listings '
00383       . 'primarily from this table'),
00384     '#default_value' => (isset($setup_obj->base_table)) ? $setup_obj->base_table : 1,
00385   );
00386 
00387   $form['views_type']['row_description'] = array(
00388     '#title' => t('Comment'),
00389     '#type' => 'textarea',
00390     '#description' => '(Optional). Provide any details regarding this setup you would like. This '.
00391                       'description will appear when selecting a type for a new Drupal View',
00392     '#required' => FALSE,
00393     '#default_value' => (isset($setup_obj->comment)) ? $setup_obj->comment : '',
00394   );
00395 
00396   // we need a div block where the table fields will get put when the
00397   // AHAH callback is made
00398   $form['view_setup_table'] = array(
00399     '#type' => 'item',
00400     '#prefix' => '<div id="table-rows-div">',
00401     '#suffix' => '</div>',
00402   );
00403 
00404 
00405   // add the fieldset for the table fields, but only if the $mview_id or $table_name
00406   // is set. The only times these values are set is if we're editing an existing
00407   // record or if the AHAH callback is being made.
00408   if ($form_state['values']['mview_id'] or $form_state['values']['table_name']) {
00409     $mview_id = $form_state['values']['mview_id'];
00410     $table_name = $form_state['values']['table_name'];
00411     $form['view_setup_table'] = array(
00412       '#type' => 'fieldset',
00413       '#title' => 'Join Selection',
00414       '#prefix' => '<div id="fieldset-table-rows-wrapper">',
00415       '#suffix' => '</div>',
00416     );
00417 
00418     // get the columns in this materialized view.  They are separated by commas
00419     // where the first word is the column name and the rest is the type
00420     $columns = array();
00421     if ($mview_id) {
00422       $sql = "SELECT mv_specs FROM {tripal_mviews} WHERE mview_id = %d";
00423       $mview = db_fetch_object(db_query($sql, $mview_id));
00424       $columns = explode(",", $mview->mv_specs);
00425     }
00426     else {
00427       $table_desc = tripal_core_get_chado_table_schema($table_name);
00428       if ($table_desc) {
00429         $fields = $table_desc['fields'];
00430         // iterate through the columns and build the format
00431         // compatible with the code below. The column name is first followed
00432         // by the type with a separating space
00433         foreach ($fields as $column => $attrs) {
00434           $columns[] = "$column " . $attrs['type'];
00435         }
00436       }
00437     }
00438 
00439     $i=1;
00440     $form['view_setup_table']["instructions"] = array(
00441          '#type' => 'markup',
00442          '#value' => filter_xss("Select an optional table to which the fields of the ".
00443                      "materialized view can join.  If a field does not need to ".
00444                      "join you may leave the selection blank."),
00445     );
00446     $data['field_types'] = array();
00447 
00448     // get the list of chado tables to join on
00449     $chado_join_tables = tripal_core_get_chado_tables(TRUE);
00450     $chado_join_tables = array_merge(array('Select a Join Table'), $chado_join_tables);
00451 
00452     // get list of all handlers
00453     $all_handlers = tripal_views_integration_discover_handlers();
00454     $handlers_fields = array(0 => "Select a field handler");
00455     $handlers_filters = array(0 => "Select a filter handler");
00456     $handlers_sort = array(0 => "Select a sort handler");
00457     $handlers_argument = array(0 => "Select an argument handler");
00458     $handlers_join = array(0 => "Select a join handler");
00459     $handlers_rel = array(0 => "Select a relationship handler");
00460     foreach ($all_handlers as $handler) {
00461       if (preg_match("/views_handler_field/", $handler)) {
00462         $handlers_fields[$handler] = $handler;
00463       }
00464       if (preg_match("/views_handler_filter/", $handler)) {
00465         $handlers_filters[$handler] = $handler;
00466       }
00467       if (preg_match("/views_handler_sort/", $handler)) {
00468         $handlers_sort[$handler] = $handler;
00469       }
00470       if (preg_match("/views_handler_argument/", $handler)) {
00471         $handlers_argument[$handler] = $handler;
00472       }
00473       if (preg_match("/_join/", $handler)) {
00474         $handlers_join[$handler] = $handler;
00475       }
00476       if (preg_match("/views_handler_relationship/", $handler)) {
00477         $handlers_rel[$handler] = $handler;
00478       }
00479     }
00480 
00481     // generate a unique $table_id for keeping track of the table
00482     if ($mview_id) {
00483       $table_id = $mview_id;
00484     }
00485     else {
00486       $table_id = $table_name;
00487     }
00488 
00489     // Per Row (Fields) --------------
00490     // now iterate through the columns of the materialized view or
00491     // chado table and generate the join and handler fields
00492     foreach ($columns as $column) {
00493       $column = trim($column);  // trim trailing and leading spaces
00494       preg_match("/^(.*?)\ (.*?)$/", $column, $matches);
00495       $column_name = $matches[1];
00496       $column_type = $matches[2];
00497 
00498       $form['view_setup_table']["$table_id-$i"] = array(
00499            '#type' => 'markup',
00500            '#prefix' => "<div class=\"fields-new-row\">",
00501            '#suffix' => "</div>",
00502            '#value' => filter_xss('')
00503       );
00504 
00505       // COLUMN I
00506       $form['view_setup_table']["$table_id-$i"]["fields_name_$table_id-$i"] = array(
00507         '#type' => 'markup',
00508         '#prefix' => "<div class=\"column-one\">",
00509         '#value' => "<span class=\"column-name\">" . filter_xss($column_name) . "</span>".
00510                     "<br /><span class=\"column-type\">" . filter_xss($column_type) . "</span>",
00511         '#suffix' => "</div>",
00512       );
00513       $data['field_types'][$column_name] = $column_type;
00514 
00515       // COLUMN II
00516       $form['view_setup_table']["$table_id-$i"]['column-2'] = array(
00517            '#type' => 'markup',
00518            '#prefix' => "<div class=\"column-two\">",
00519            '#suffix' => "</div>",
00520            '#value' => filter_xss('')
00521       );
00522 
00523       // set the default values for the human-readable name and description
00524       $default_name = '';
00525       $default_descrip = '';
00526       if (isset($setup_id) && !isset($form_state['values']["fields_readable_name_$table_id-$i"])) {
00527         $default_name = $default_fields[$column_name]['name'];
00528         $default_descrip = $default_fields[$column_name]['description'];
00529       }
00530       else {
00531         $default_name = $form_state['values']["fields_readable_name_$table_id-$i"];
00532         $default_descrip = $form_state['values']["fields_description_$table_id-$i"];
00533       }
00534       $form['view_setup_table']["$table_id-$i"]['column-2']["fields_readable_name_$table_id-$i"] = array(
00535         '#type' => 'textfield',
00536         '#title' => 'Human-Readable Name',
00537         '#description' => 'This is the name of the field in the Views UI',
00538         '#required' => TRUE,
00539         '#default_value' => $default_name,
00540         '#size' => 42,
00541       );
00542 
00543       $form['view_setup_table']["$table_id-$i"]['column-2']["fields_description_$table_id-$i"] = array(
00544         '#type' => 'textarea',
00545         '#title' => 'Short Description',
00546         '#description' => 'This is the field help in the Views UI',
00547         '#required' => TRUE,
00548         '#cols' => 42,
00549         '#rows' => 3,
00550         '#default_value' => $default_descrip,
00551       );
00552 
00553       // COLUMN III
00554       $form['view_setup_table']["$table_id-$i"]['column-3'] = array(
00555            '#type' => 'markup',
00556            '#prefix' => "<div class=\"column-three\">",
00557            '#suffix' => "</div>",
00558            '#value' => filter_xss('')
00559       );
00560 
00561       // set the default values for the join table and columns
00562       $default_join_table = 0;
00563       $default_join_field = 0;
00564       if (isset($setup_id) && !isset($form_state['values']["fields_join_$table_id-$i"])) {
00565         $default_join_table = $default_joins[$column_name]['left_table'];
00566         $default_join_field = $default_joins[$column_name]['left_field'];
00567         $form_state['values']["fields_join_$table_id-$i"] = $default_join_table;
00568         $form_state['values']["fields_join_column_$table_id-$i"] = $default_join_field;
00569       }
00570       else{
00571         $default_join_table = $form_state['values']["fields_join_$table_id-$i"];
00572         $default_join_field = $form_state['values']["fields_join_column_$table_id-$i"];
00573       }
00574 
00575       $form['view_setup_table']["$table_id-$i"]['column-3']["fields_join_$table_id-$i"] = array(
00576         '#type' => 'select',
00577         '#prefix' => "<div class=\"fields-column-join\">",
00578         '#suffix' => "</div>",
00579         '#options' => $chado_join_tables,
00580         '#required' => FALSE,
00581         '#default_value' => $default_join_table,
00582         '#ahah' => array(
00583            'path' => "tripal/views/integration/ajax/join_field/$table_id-$i/fields_join_column_$table_id-$i",
00584            'wrapper' => "fields-column-join-column-$table_id-$i",
00585            'effect' => 'fade',
00586            'event' => 'change',
00587            'method' => 'replace',
00588         ),
00589       );
00590 
00591       $columns = array();
00592       if ($default_join_table) {
00593         // get the table description
00594         $table_desc = tripal_core_get_chado_table_schema($default_join_table);
00595         foreach ($table_desc['fields'] as $column => $def) {
00596           $columns[$column] = $column;
00597         }
00598       }
00599       else {
00600         $columns = array('Select Join Column');
00601       }
00602       $form['view_setup_table']["$table_id-$i"]['column-3']["fields_join_column_$table_id-$i"] = array(
00603         '#type' => 'select',
00604         '#prefix' => "  <div id=\"fields-column-join-column-$table_id-$i\" class=\"fields-column-join-column\">",
00605         '#suffix' => "</div>",
00606         '#options' => $columns,
00607         '#required' => FALSE,
00608         '#default_value' => $default_join_field
00609       );
00610 
00611       $default_join_handler = 0;
00612       if (isset($setup_id) && !isset($form_state['values']["fields_join_handler_$table_id-$i"])) {
00613         $default_join_handler = $default_handlers[$column_name]['join']['handler_name'];
00614         $form_state['values']["fields_join_handler_$table_id-$i"] = $default_join_handler;
00615       }
00616       else {
00617         $default_join_handler = $form_state['values']["fields_join_handler_$table_id-$i"];
00618       }
00619       $form['view_setup_table']["$table_id-$i"]['column-3']["fields_join_handler_$table_id-$i"] = array(
00620         '#type' => 'select',
00621         '#prefix' => "<div class=\"fields-join-handler\">",
00622         '#suffix' => "</div>",
00623         '#options' => $handlers_join,
00624         '#required' => FALSE,
00625         '#default_value' => $default_join_handler,
00626       );
00627 
00628       // COLUMN 4
00629       $form['view_setup_table']["$table_id-$i"]['column-4'] = array(
00630         '#type' => 'markup',
00631         '#prefix' => "<div class=\"column-four\">",
00632         '#suffix' => "</div>",
00633         '#value' => filter_xss('')
00634       );
00635 
00636       // create the handler fields
00637       $default_field_handler = 0;
00638       if (isset($setup_id) && !isset($form_state['values']["fields_field_handler_$table_id-$i"])) {
00639         $default_field_handler = $default_handlers[$column_name]['field']['handler_name'];
00640         $form_state['values']["fields_field_handler_$table_id-$i"] = $default_field_handler;
00641       }
00642       else {
00643         $default_field_handler = $form_state['values']["fields_field_handler_$table_id-$i"];
00644         if (!$default_field_handler) {
00645           if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
00646             $default_field_handler = 'chado_views_handler_field_numeric';
00647           }
00648           elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
00649             $default_field_handler = 'chado_views_handler_field';
00650           }
00651           elseif ($column_type == 'boolean') {
00652             $default_field_handler = 'chado_views_handler_field_boolean';
00653           }
00654           elseif ($column_type == 'float') {
00655             $default_field_handler = 'chado_views_handler_field_numeric';
00656           }
00657           elseif ($column_type == 'datetime') {
00658             $default_field_handler = 'chado_views_handler_field_date';
00659           }
00660         }
00661       }
00662 
00663       $form['view_setup_table']["$table_id-$i"]['column-4']["fields_field_handler_$table_id-$i"] = array(
00664          '#type' => 'select',
00665          '#prefix' => "<div class=\"fields-field-handler\">",
00666          '#suffix' => "</div>",
00667          '#options' => $handlers_fields,
00668          '#required' => FALSE,
00669          '#default_value' => $default_field_handler,
00670       );
00671 
00672       $default_filter_handler = 0;
00673       if (isset($setup_id) && !isset($form_state['values']["fields_filter_handler_$table_id-$i"])) {
00674         $default_filter_handler = $default_handlers[$column_name]['filter']['handler_name'];
00675         $form_state['values']["fields_filter_handler_$table_id-$i"]= $default_filter_handler;
00676       }
00677       else {
00678         $default_filter_handler = $form_state['values']["fields_filter_handler_$table_id-$i"];
00679         if (!$default_filter_handler) {
00680           if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
00681             $default_filter_handler = 'chado_views_handler_filter_numeric';
00682           }
00683           elseif (preg_match("/^character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
00684             $default_filter_handler = 'chado_views_handler_filter_string';
00685           }
00686           elseif ($column_type == 'boolean') {
00687             $default_filter_handler = 'chado_views_handler_filter_boolean';
00688           }
00689           elseif ($column_type == 'float') {
00690             $default_filter_handler = 'chado_views_handler_filter_float';
00691           }
00692           elseif ($column_type == 'datetime') {
00693             $default_filter_handler = 'chado_views_handler_filter_date';
00694           }
00695         }
00696       }
00697       $form['view_setup_table']["$table_id-$i"]['column-4']["fields_filter_handler_$table_id-$i"] = array(
00698         '#type' => 'select',
00699         '#prefix' => "<div class=\"fields-filter-handler\">",
00700         '#suffix' => "</div>",
00701         '#options' => $handlers_filters,
00702         '#required' => FALSE,
00703         '#default_value' => $default_filter_handler,
00704       );
00705 
00706       $default_sort_handler = 0;
00707       if (isset($setup_id) && !isset($form_state['values']["fields_sort_handler_$table_id-$i"])) {
00708         $default_sort_handler = $default_handlers[$column_name]['sort']['handler_name'];
00709         $form_state['values']["fields_sort_handler_$table_id-$i"] = $default_sort_handler;
00710       }
00711       else {
00712         $default_sort_handler = $form_state['values']["fields_sort_handler_$table_id-$i"];
00713         if (!$default_sort_handler) {
00714           if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
00715             $default_sort_handler = 'chado_views_handler_sort';
00716           }
00717           elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
00718             $default_sort_handler = 'chado_views_handler_sort';
00719           }
00720           elseif ($column_type == 'boolean') {
00721             $default_sort_handler = 'chado_views_handler_sort';
00722           }
00723           elseif ($column_type == 'float') {
00724             $default_sort_handler = 'chado_views_handler_sort';
00725           }
00726           elseif ($column_type == 'datetime') {
00727             $default_sort_handler = 'chado_views_handler_sort_date';
00728           }
00729         }
00730       }
00731       $form['view_setup_table']["$table_id-$i"]['column-4']["fields_sort_handler_$table_id-$i"] = array(
00732         '#type' => 'select',
00733         '#prefix' => "<div class=\"fields-sort-handler\">",
00734         '#suffix' => "</div>",
00735         '#options' => $handlers_sort,
00736         '#required' => FALSE,
00737         '#default_value' => $default_sort_handler,
00738       );
00739 
00740       $default_argument_handler = 0;
00741       if (isset($setup_id) && !isset($form_state['values']["fields_argument_handler_$table_id-$i"])) {
00742         $default_argument_handler = $default_handlers[$column_name]['argument']['handler_name'];
00743         $form_state['values']["fields_argument_handler_$table_id-$i"]=$default_argument_handler ;
00744       }
00745       else {
00746         $default_argument_handler = $form_state['values']["fields_argument_handler_$table_id-$i"];
00747         if (!$default_argument_handler) {
00748           if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
00749             $default_argument_handler = 'views_handler_argument_numeric';
00750           }
00751           elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
00752             $default_argument_handler = 'views_handler_argument_string';
00753           }
00754           elseif ($column_type == 'boolean') {
00755             $default_argument_handler = 'views_handler_argument_numeric';
00756           }
00757           elseif ($column_type == 'float') {
00758             $default_argument_handler = 'views_handler_argument_numeric';
00759           }
00760           elseif ($column_type == 'datetime') {
00761             $default_argument_handler = 'views_handler_argument_date';
00762           }
00763         }
00764       }
00765       $form['view_setup_table']["$table_id-$i"]['column-4']["fields_argument_handler_$table_id-$i"] = array(
00766         '#type' => 'select',
00767         '#prefix' => "<div class=\"fields-argument-handler\">",
00768         '#suffix' => "</div>",
00769         '#options' => $handlers_argument,
00770         '#required' => FALSE,
00771         '#default_value' => $default_argument_handler,
00772       );
00773 
00774       $default_relationship_handler = 0;
00775       if (isset($setup_id) && !isset($form_state['values']["fields_relationship_handler_$table_id-$i"])) {
00776         $default_relationship_handler = $default_handlers[$column_name]['relationship']['handler_name'];
00777         $form_state['values']["fields_relationship_handler_$table_id-$i"]=$default_relationship_handler;
00778       }
00779       else {
00780         $default_relationship_handler = $form_state['values']["fields_relationship_handler_$table_id-$i"];
00781         if (!$default_relationship_handler) {
00782           if ($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial') {
00783             $default_relationship_handler = 'views_handler_relationship';
00784           }
00785           elseif (preg_match("/character varying/", $column_type) or $column_type == 'char' or $column_type == 'text' or $column_type == 'varchar') {
00786             $default_relationship_handler = 'views_handler_relationship';
00787           }
00788           elseif ($column_type == 'boolean') {
00789             $default_relationship_handler = 'views_handler_relationship';
00790           }
00791           elseif ($column_type == 'float') {
00792             $default_relationship_handler = 'views_handler_relationship';
00793           }
00794           elseif ($column_type == 'datetime') {
00795             $default_relationship_handler = 'views_handler_relationship';
00796           }
00797         }
00798       }
00799       $form['view_setup_table']["$table_id-$i"]['column-4']["fields_relationship_handler_$table_id-$i"] = array(
00800         '#type' => 'select',
00801         '#prefix' => "<div class=\"fields-relationship-handler\">",
00802         '#suffix' => "</div>",
00803         '#options' => $handlers_rel,
00804         '#required' => FALSE,
00805         '#default_value' => $default_relationship_handler,
00806       );
00807 
00808       $i++;
00809     }
00810     $form['view_setup_table']['save'] = array(
00811       '#type'  => 'submit',
00812       '#value' => t('Save'),
00813     );
00814 
00815     $data['row_count'] = $i - 1;
00816   }
00817 
00818   //use this to put values into $form_state['values']
00819   $form['data'] = array();
00820 
00821   // Ensure that we don't store an array
00822   // since we will get a check_plain:htmlspecial_characters error if we do
00823   foreach ($data as $key => $value) {
00824     if (is_array($value)) {
00825       $form['data'][$key] = array(
00826         '#type' => 'hidden',
00827         '#value' => serialize($value),
00828       );
00829     }
00830     else {
00831       $form['data'][$key] = array(
00832         '#type' => 'hidden',
00833         '#value' => $value,
00834       );
00835     }
00836   }
00837 
00838   $form['#redirect'] = 'admin/tripal/views/integration/list';
00839 
00840   return $form;
00841 }
00842 
00843 
00855 function tripal_views_integration_form_validate($form, &$form_state) {
00856   $name_array = explode(" ", $form_state['values']['row_name']);
00857   $mview_id = $form_state['values']['mview_id'];
00858   $table_name = $form_state['values']['table_name'];
00859 
00860 //  if (count($name_array) > 1) {
00861 //    form_set_error($form_state['values']['row_name'], 'The View type name must be a single word only.');
00862 //  }
00863   if ($mview_id and $table_name) {
00864     form_set_error('mview_id', 'Please select either a materialized view or a Chado table but not both');
00865   }
00866   if (!$mview_id and !$table_name) {
00867     form_set_error('mview_id', 'Please select either a materialized view or a Chado table');
00868   }
00869 
00870   // Ensure that users don't override the default integrations
00871   if ($form_state['values']['row_priority'] >= 9) {
00872     form_set_error('row_priority', 'A priority of 10 or 9 is reserved for default tripal '
00873       .'views integrations created by core modules. Please set the priority between '
00874       .'0 and -10 to ensure your changes are used rather over the defaults.');
00875   }
00876   // TODO: do we need to require that a handler be set for each field and each type of handler?
00877 }
00878 
00891 function tripal_views_integration_form_submit($form, &$form_state) {
00892   $name = $form_state['values']['row_name'];
00893   $mview_id = $form_state['values']['mview_id'];
00894   $table_name = $form_state['values']['table_name'];
00895   $setup_id = $form_state['values']['setup_id'];
00896   $priority = $form_state['values']['row_priority'];
00897   $comment = $form_state['values']['row_description'];
00898 
00899   // get details about this mview
00900   if ($mview_id) {
00901     $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d";
00902     $mview = db_fetch_object(db_query($sql, $mview_id));
00903     $table_name = $mview->mv_table;
00904     $table_id = $mview_id;
00905     $type = 'mview';
00906   }
00907   else {
00908     $type = 'chado';
00909     $table_id = $table_name;
00910   }
00911 
00912   // If this is for a materialized view then we want to add/update that record
00913   $tripal_views_record = array();
00914   if ($mview_id) {
00915     // get details about this mview
00916     $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d";
00917     $mview = db_fetch_object(db_query($sql, $mview_id));
00918 
00919     // build the record for insert/update
00920     $tripal_views_record = array(
00921      'mview_id' => $mview_id,
00922      'table_name' => $mview->mv_table,
00923      'name' => $name,
00924      'priority' => $priority,
00925      'comment' => $comment,
00926      'base_table' => $form_state['values']['base_table']
00927     );
00928   }
00929   // if a chado table then...
00930   else {
00931     // build the record for insert/update
00932     $tripal_views_record = array(
00933      'table_name' => $table_name,
00934      'name' => $name,
00935      'priority' => $priority,
00936      'comment' => $comment,
00937      'base_table' => $form_state['values']['base_table']
00938     );
00939   }
00940 
00941   // perform the insert or update
00942   if (!$setup_id) {  // this is an insert
00943     if (!drupal_write_record('tripal_views', $tripal_views_record)) {
00944       drupal_set_message(t("Failed to add record."), 'error');
00945       return;
00946     }
00947   }
00948   else {  // this is an update
00949     // check to see if it was specified to create a new integration
00950     if ($form_state['values']['new_integration']) {
00951       if (!drupal_write_record('tripal_views', $tripal_views_record)) {
00952         drupal_set_message(t("Failed to add record."), 'error');
00953         return;
00954       }
00955     }
00956     else {
00957       $tripal_views_record['setup_id'] = $setup_id;
00958       if (!drupal_write_record('tripal_views', $tripal_views_record, array('setup_id'))) {
00959         drupal_set_message(t("Failed to update record."), 'error');
00960         return;
00961       }
00962     }
00963   }
00964 
00965   // if this is an update then clean out the existing joins and handlers so we can add new ones
00966   if ($setup_id) {
00967     db_query("DELETE FROM {tripal_views_field} WHERE setup_id = %d", $setup_id);
00968     db_query("DELETE FROM {tripal_views_join} WHERE setup_id = %d", $setup_id);
00969     db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id = %d", $setup_id);
00970   }
00971 
00972   // iterate through the columns of the form and add
00973   // the joins if provided, and the handlers
00974   $i = 1;
00975   foreach (unserialize($form_state['values']['field_types']) as $key => $value) {
00976 
00977     // add the field definition
00978     $view_field_record = array(
00979       'setup_id' => $tripal_views_record['setup_id'],
00980       'column_name' => $key,
00981       'name' => $form_state['values']["fields_readable_name_$table_id-$i"],
00982       'description' => $form_state['values']["fields_description_$table_id-$i"],
00983       'type' => $value,
00984     );
00985     drupal_write_record('tripal_views_field', $view_field_record);
00986 
00987     // first add the join if it exists
00988     $left_table = $form_state['values']["fields_join_$table_id-$i"];
00989     $left_column = $form_state['values']["fields_join_column_$table_id-$i"];
00990 
00991     if ($left_column) {
00992       if ($mview_id) {
00993         $base_table = $mview->mv_table;
00994       }
00995       else {
00996         $base_table = $table_name;
00997       }
00998       $view_join_record = array(
00999         'setup_id' => $tripal_views_record['setup_id'],
01000         'base_table' => $base_table,
01001         'base_field' => $key,
01002         'left_table' => $left_table,
01003         'left_field' => $left_column,
01004         'handler' => $form_state['values']["fields_join_handler_$table_id-$i"],
01005       );
01006 
01007       // write the new joins to the database
01008       drupal_write_record('tripal_views_join', $view_join_record);
01009     }
01010 
01011     // add the hanlders
01012     $handlers = array('filter', 'field', 'sort', 'argument', 'join', 'relationship');
01013 
01014     foreach ($handlers as $handler) {
01015       $handler_name = $form_state['values']["fields_" . $handler . "_handler_$table_id-$i"];
01016       if ($handler_name) {
01017         $handler_record = array(
01018           'setup_id' => $tripal_views_record['setup_id'],
01019           'column_name' => $key,
01020           'handler_type' => $handler,
01021           'handler_name' => $handler_name,
01022         );
01023         drupal_write_record('tripal_views_handlers', $handler_record);
01024       }
01025     }
01026     $i++;
01027   }
01028 
01029   if ($setup_id) {
01030     drupal_set_message(t('Record Updated'));
01031   }
01032   else {
01033     drupal_set_message(t('Record Added'));
01034   }
01035 
01036   // now clear all the caches so that Drupal views picks up our chages
01037   views_invalidate_cache();
01038 }
01039 
01050 function tripal_views_integration_discover_handlers() {
01051 
01052   $handlers = array();
01053 
01054   // Get handlers from all modules.
01055   foreach (module_implements('views_handlers') as $module) {
01056     $function = $module . '_views_handlers';
01057     $result = $function();
01058     if (!is_array($result)) {
01059       continue;
01060     }
01061     foreach ($result['handlers'] as $handler => $parent) {
01062       $handlers[] = $handler;
01063     }
01064   }
01065 
01066   // these handlers are hard coded because I could not
01067   // get the views_handlers() function to be called
01068   // in the code above.  However, we will be creating
01069   // Chado wrappers for many of these and once that work
01070   // is done these will no longer be needed.
01071 
01072   // argument handlers
01073   $handlers[] = 'views_handler_argument';
01074   $handlers[] = 'views_handler_argument_numeric';
01075   $handlers[] = 'views_handler_argument_formula';
01076   $handlers[] = 'views_handler_argument_date';
01077   $handlers[] = 'views_handler_argument_string';
01078   $handlers[] = 'views_handler_argument_many_to_one';
01079   $handlers[] = 'views_handler_argument_null';
01080 
01081   // field handlers
01082   $handlers[] = 'views_handler_field';
01083   $handlers[] = 'views_handler_field_date';
01084   $handlers[] = 'views_handler_field_boolean';
01085   $handlers[] = 'views_handler_field_markup';
01086   $handlers[] = 'views_handler_field_xss';
01087   $handlers[] = 'views_handler_field_url';
01088   $handlers[] = 'views_handler_field_file_size';
01089   $handlers[] = 'views_handler_field_prerender_list';
01090   $handlers[] = 'views_handler_field_numeric';
01091   $handlers[] = 'views_handler_field_custom';
01092   $handlers[] = 'views_handler_field_counter';
01093 
01094   // filter handlers
01095   $handlers[] = 'views_handler_filter';
01096   $handlers[] = 'views_handler_filter_equality';
01097   $handlers[] = 'views_handler_filter_string';
01098   $handlers[] = 'views_handler_filter_boolean_operator';
01099   $handlers[] = 'views_handler_filter_boolean_operator_string';
01100   $handlers[] = 'views_handler_filter_in_operator';
01101   $handlers[] = 'views_handler_filter_numeric';
01102   $handlers[] = 'views_handler_filter_float';
01103   $handlers[] = 'views_handler_filter_date';
01104   $handlers[] = 'views_handler_filter_many_to_one';
01105 
01106   // relationship handlers
01107   $handlers[] = 'views_handler_relationship';
01108 
01109   // sort handlers
01110   $handlers[] = 'views_handler_sort';
01111   $handlers[] = 'views_handler_sort_formula';
01112   $handlers[] = 'views_handler_sort_date';
01113   $handlers[] = 'views_handler_sort_menu_hierarchy';
01114   $handlers[] = 'views_handler_sort_random';
01115 
01116   // join handler
01117   $handlers[] = 'views_join';
01118 
01119   return $handlers;
01120 }
01121 
01122 /*
01123 *
01124 * */
01125 function tripal_views_integration_ajax_view_setup_table() {
01126   // we only want the table row setup fields
01127   $form = tripal_core_ahah_prepare_form();
01128   $form = $form['view_setup_table'];
01129   $data = drupal_render($form);
01130 
01131   // bind javascript events to the new objects that will be returned
01132   // so that AHAH enabled elements will work.
01133   $settings = tripal_core_ahah_bind_events();
01134 
01135   // return the updated JSON
01136   drupal_json(
01137     array(
01138       'status'   => TRUE,
01139       'data'     => $data,
01140       'settings' => $settings,
01141     )
01142   );
01143 }
01144 /*
01145  *
01146  */
01147 function tripal_views_integration_ajax_join_field($field, $join_field) {
01148   // prepare and render the form
01149   $form = tripal_core_ahah_prepare_form();
01150   $form = $form['view_setup_table'][$field]['column-3'][$join_field];
01151   $data = drupal_render($form);
01152 
01153   // bind javascript events to the new objects that will be returned
01154   // so that AHAH enabled elements will work.
01155   $settings = tripal_core_ahah_bind_events();
01156 
01157   // return the updated JSON
01158   drupal_json(
01159     array(
01160       'status'   => TRUE,
01161       'data'     => $data,
01162       'settings' => $settings,
01163     )
01164   );
01165 }
 All Classes Files Functions Variables