Tripal v1.0 (6.x-1.0)
tripal_stock.views.inc
Go to the documentation of this file.
00001 <?php
00002 
00030 function tripal_stock_views_data()  {
00031   $data = array();
00032 
00033   if (module_exists('tripal_views')) {
00034     $tablename = 'stock';
00035     $priority = 9;
00036 
00037     // check to see if the table is integrated. If it is then integrate it's
00038     // corresponding 'chado_[table]' table.
00039     if (!tripal_views_is_integrated($tablename, $priority)) {
00040       $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
00041 
00042       // Add in node relationships if chado is in the same db as drupal
00043       if (tripal_core_chado_schema_exists()) {
00044         $integrations = tripal_views_add_node_relationship_to_chado_table_integration($table_integration_array);
00045         foreach ($integrations as $integration) {
00046           tripal_views_integration_add_entry($integration);
00047         }
00048       }
00049       else {
00050         tripal_views_integration_add_entry($table_integration_array);
00051       }
00052     }
00053 
00054     $tablename = 'stockcollection';
00055     $priority = 9;
00056       if (!tripal_views_is_integrated($tablename, $priority)) {
00057       $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
00058       tripal_views_integration_add_entry($table_integration_array);
00059     }
00060 
00061     $tables = array(
00062       'stock_cvterm',
00063       'stock_dbxref',
00064       'stock_genotype',
00065       'stock_pub',
00066       'stock_relationship',
00067       'stock_relationship_pub',
00068       'stockcollection_stock',
00069       'stockcollectionprop',
00070       'stockprop',
00071       'stockprop_pub'
00072     );
00073     foreach ($tables as $tablename) {
00074       $priority = 9;
00075       if (!tripal_views_is_integrated($tablename, $priority)) {
00076         $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
00077         tripal_views_integration_add_entry($table_integration_array);
00078       }
00079     }
00080   }
00081 
00082   return $data;
00083 }
00084 
00097 function tripal_stock_views_handlers() {
00098   return array(
00099   'info' => array(
00100     'path' => drupal_get_path('module', 'tripal_stock') . '/views/handlers',
00101   ),
00102   'handlers' => array(
00103     'views_handler_field_computed_stock_nid' => array(
00104       'parent' => 'views_handler_field_numeric',
00105     ),
00106   'views_handler_field_stockprop_by_type' => array(
00107     'parent' => 'views_handler_field_prerender_list',
00108     ),
00109   'views_handler_field_stockprop_all' => array(
00110     'parent' => 'views_handler_field_prerender_list',
00111     ),
00112   'views_handler_field_stockrel_by_type' => array(
00113     'parent' => 'views_handler_field_prerender_list',
00114     ),
00115   'views_handler_field_stockrel_all' => array(
00116     'parent' => 'views_handler_field_prerender_list',
00117     ),
00118   'views_handler_field_stock_dbxref_by_type' => array(
00119     'parent' => 'views_handler_field_prerender_list',
00120     ),
00121   'views_handler_field_stock_dbxref_all' => array(
00122     'parent' => 'views_handler_field_prerender_list',
00123     ),
00124   'views_handler_filter_stockprop_id' => array(
00125     'parent' => 'views_handler_filter',
00126     ),
00127   'views_handler_filter_stock_dbxref_id' => array(
00128     'parent' => 'views_handler_filter',
00129     ),
00130   'views_handler_filter_stock_relationship_id' => array(
00131     'parent' => 'views_handler_filter',
00132     ),
00133   'views_handler_argument_stockprop_id' => array(
00134     'parent' => 'views_handler_argument_string',
00135     ),
00136   ),
00137   );
00138 }
00139 
00143 function tripal_stock_views_data_alter(&$data) {
00144 
00145   if ( !(is_array($db_url) and array_key_exists('chado', $db_url)) ) {
00146 
00147     // Add featuer relationship to node
00148     $data['node']['stock_chado_nid'] = array(
00149       'group' => 'Stock',
00150       'title' => 'Stock Node',
00151       'help' => 'Links Chado Stock Fields/Data to the Nodes in the current View.',
00152       'real field' => 'nid',
00153       'relationship' => array(
00154         'handler' => 'views_handler_relationship',
00155         'title' => t('Node => Chado'),
00156         'label' => t('Node => Chado'),
00157         'real field' => 'nid',
00158         'base' => 'chado_stock',
00159         'base field' => 'nid'
00160       ),
00161     );
00162   }
00163 
00164 }
00165 
00178 function tripal_stock_views_pre_render  (&$view) {
00179   if (preg_match('/stock/', $view->base_table)) {
00180 
00181     //-----Node IDs---------------------------------------------
00182     // @see tripal_core.views.inc, tripal_core_add_node_ids_to_view()
00183 
00184     // retrieve the stock_id for each record in the views current page
00185     $stock_ids = array();
00186     foreach ($view->result as $row_num => $row) {
00187       $stock_ids[$row_num] = $row->stock_id;
00188     }
00189 
00190     if (sizeof($stock_ids)) {
00191 
00192       //-----Properties------------------------------------------
00193       $field_names = array_keys($view->field);
00194       //if any property fields are in the current view
00195       $property_field_names = preg_grep('/properties/', $field_names);
00196       if (!empty($property_field_names)) {
00197         $sql = "SELECT stockprop.*, cvterm.name as type_name FROM {stockprop} "
00198           ."INNER JOIN cvterm cvterm ON stockprop.type_id=cvterm.cvterm_id "
00199           ."WHERE stockprop.stock_id IN (" . implode(',', $stock_ids) . ")";
00200         $resource = chado_query($sql);
00201 
00202         $view->result[$key]->properties = array();
00203         while ($r = db_fetch_object($resource)) {
00204           $key = array_search($r->stock_id, $stock_ids);
00205           $view->result[$key]->properties[] = $r;
00206         }
00207       }
00208 
00209       //-----Relationships----------------------------------------
00210       //if any relationship fields are in the current view
00211       $relationship_field_names = preg_grep('/relationships/', $field_names);
00212       if (!empty($relationship_field_names)) {
00213         $sql = "SELECT stock_relationship.*, cvterm.name as type_name, "
00214           ."subject_stock.name as subject_name, object_stock.name as object_name "
00215           ."FROM stock_relationship "
00216           ."LEFT JOIN stock subject_stock ON stock_relationship.subject_id=subject_stock.stock_id "
00217           ."LEFT JOIN stock object_stock ON stock_relationship.object_id=object_stock.stock_id "
00218           ."LEFT JOIN cvterm cvterm ON stock_relationship.type_id = cvterm.cvterm_id "
00219           ."WHERE stock_relationship.subject_id IN (" . implode(',', $stock_ids) . ") "
00220           ."OR stock_relationship.object_id IN (" . implode(',', $stock_ids) . ") ";
00221         $resource = chado_query($sql);
00222 
00223         while ($r = db_fetch_object($resource)) {
00224           if (in_array($r->subject_id, $stock_ids)) {
00225             $key = array_search($r->subject_id, $stock_ids);
00226             $r->stock_id = $r->subject_id;
00227             $view->result[$key]->relationships[] = clone $r;
00228           }
00229           if (in_array($r->object_id, $stock_ids)) {
00230             $key = array_search($r->object_id, $stock_ids);
00231             $r->stock_id = $r->object_id;
00232             $view->result[$key]->relationships[] = clone $r;
00233           }
00234         }
00235       }
00236 
00237       //-----DB References--------------------------------------------
00238       //if any dbxref fields are in the current view
00239       $dbxref_field_names = preg_grep('/dbxref/', $field_names);
00240       if (!empty($dbxref_field_names)) {
00241         $sql = "SELECT stock_dbxref.*, dbxref.db_id, db.name as db_name, db.urlprefix, "
00242           ."dbxref.accession, dbxref.version, dbxref.description "
00243           ."FROM stock_dbxref "
00244           ."LEFT JOIN dbxref dbxref ON stock_dbxref.dbxref_id=dbxref.dbxref_id "
00245           ."LEFT JOIN db db ON dbxref.db_id=db.db_id "
00246           ."WHERE stock_dbxref.stock_id IN (" . implode(',', $stock_ids) . ")";
00247         $resource = chado_query($sql);
00248 
00249         $view->result[$key]->dbxref = array();
00250         while ($r = db_fetch_object($resource)) {
00251           $key = array_search($r->stock_id, $stock_ids);
00252           $view->result[$key]->dbxref[] = $r;
00253         }
00254       }
00255 
00256     } //if there are stocks
00257   } //if we're dealing with a stock view
00258 }
00259 
00264 function tripal_stock_views_default_views() {
00265   $views = array();
00266 
00267   if (!module_exists('tripal_views')) {
00268     return $views;
00269   }
00270 
00271   // Main default view
00272   $view = new view;
00273   $view->name = 'stock_listing';
00274   $view->description = 'This view lists all stocks by default.';
00275   $view->tag = 'chado default';
00276   $view->base_table = 'stock';
00277   $view->core = 0;
00278   $view->api_version = '2';
00279   $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
00280   $handler = $view->new_display('default', 'Defaults', 'default');
00281   $handler->override_option('fields', array(
00282     'uniquename' => array(
00283       'label' => 'Unique Name',
00284       'alter' => array(
00285         'alter_text' => 0,
00286         'text' => '',
00287         'make_link' => 0,
00288         'path' => '',
00289         'link_class' => '',
00290         'alt' => '',
00291         'prefix' => '',
00292         'suffix' => '',
00293         'target' => '',
00294         'help' => '',
00295         'trim' => 0,
00296         'max_length' => '',
00297         'word_boundary' => 1,
00298         'ellipsis' => 1,
00299         'html' => 0,
00300         'strip_tags' => 0,
00301       ),
00302       'empty' => '',
00303       'hide_empty' => 0,
00304       'empty_zero' => 0,
00305       'link_to_node' => 1,
00306       'exclude' => 0,
00307       'id' => 'uniquename',
00308       'table' => 'stock',
00309       'field' => 'uniquename',
00310       'relationship' => 'none',
00311     ),
00312     'name_2' => array(
00313       'label' => 'Name',
00314       'alter' => array(
00315         'alter_text' => 0,
00316         'text' => '',
00317         'make_link' => 0,
00318         'path' => '',
00319         'link_class' => '',
00320         'alt' => '',
00321         'prefix' => '',
00322         'suffix' => '',
00323         'target' => '',
00324         'help' => '',
00325         'trim' => 0,
00326         'max_length' => '',
00327         'word_boundary' => 1,
00328         'ellipsis' => 1,
00329         'html' => 0,
00330         'strip_tags' => 0,
00331       ),
00332       'empty' => '',
00333       'hide_empty' => 0,
00334       'empty_zero' => 0,
00335       'link_to_node' => 1,
00336       'exclude' => 0,
00337       'id' => 'name_2',
00338       'table' => 'stock',
00339       'field' => 'name',
00340       'relationship' => 'none',
00341     ),
00342     'name' => array(
00343       'label' => 'Type',
00344       'alter' => array(
00345         'alter_text' => 0,
00346         'text' => '',
00347         'make_link' => 0,
00348         'path' => '',
00349         'link_class' => '',
00350         'alt' => '',
00351         'prefix' => '',
00352         'suffix' => '',
00353         'target' => '',
00354         'help' => '',
00355         'trim' => 0,
00356         'max_length' => '',
00357         'word_boundary' => 1,
00358         'ellipsis' => 1,
00359         'html' => 0,
00360         'strip_tags' => 0,
00361       ),
00362       'empty' => '',
00363       'hide_empty' => 0,
00364       'empty_zero' => 0,
00365       'exclude' => 0,
00366       'id' => 'name',
00367       'table' => 'cvterm',
00368       'field' => 'name',
00369       'relationship' => 'none',
00370     ),
00371     'common_name' => array(
00372       'label' => 'Organism',
00373       'alter' => array(
00374         'alter_text' => 0,
00375         'text' => '',
00376         'make_link' => 0,
00377         'path' => '',
00378         'link_class' => '',
00379         'alt' => '',
00380         'prefix' => '',
00381         'suffix' => '',
00382         'target' => '',
00383         'help' => '',
00384         'trim' => 0,
00385         'max_length' => '',
00386         'word_boundary' => 1,
00387         'ellipsis' => 1,
00388         'html' => 0,
00389         'strip_tags' => 0,
00390       ),
00391       'empty' => '',
00392       'hide_empty' => 0,
00393       'empty_zero' => 0,
00394       'link_to_node' => 1,
00395       'exclude' => 0,
00396       'id' => 'common_name',
00397       'table' => 'organism',
00398       'field' => 'common_name',
00399       'relationship' => 'none',
00400     ),
00401   ));
00402   $handler->override_option('sorts', array(
00403     'common_name' => array(
00404       'order' => 'ASC',
00405       'id' => 'common_name',
00406       'table' => 'organism',
00407       'field' => 'common_name',
00408       'relationship' => 'none',
00409     ),
00410     'uniquename' => array(
00411       'order' => 'ASC',
00412       'id' => 'uniquename',
00413       'table' => 'stock',
00414       'field' => 'uniquename',
00415       'relationship' => 'none',
00416     ),
00417   ));
00418   $handler->override_option('filters', array(
00419     'common_name' => array(
00420       'operator' => '=',
00421       'value' => array(),
00422       'group' => '0',
00423       'exposed' => TRUE,
00424       'expose' => array(
00425         'use_operator' => 0,
00426         'operator' => 'common_name_op',
00427         'identifier' => 'organism_common_name',
00428         'label' => 'Organism',
00429         'remember' => 0,
00430       ),
00431       'case' => 1,
00432       'id' => 'common_name',
00433       'table' => 'organism',
00434       'field' => 'common_name',
00435       'relationship' => 'none',
00436       'values_form_type' => 'select',
00437       'multiple' => 1,
00438       'optional' => 0,
00439       'agg' => array(
00440         'records_with' => 1,
00441         'aggregates_with' => 1,
00442       ),
00443     ),
00444     'search_results' => array(
00445       'operator' => '=',
00446       'value' => '',
00447       'group' => '0',
00448       'exposed' => FALSE,
00449       'expose' => array(
00450         'operator' => FALSE,
00451         'label' => '',
00452       ),
00453       'id' => 'search_results',
00454       'table' => 'views',
00455       'field' => 'search_results',
00456       'relationship' => 'none',
00457       'apply_button' => 'Show',
00458       'no_results_text' => 'Click "Show" to see a list of all stocks matching the entered criteria. If you leave a any of the criteria blank then the stocks will be not be filtered based on that field. Furthermore, if you leave all criteria blank then all stocks will be listed.',
00459     ),
00460     'type_id' => array(
00461       'operator' => '=',
00462       'value' => '',
00463       'group' => '0',
00464       'exposed' => TRUE,
00465       'expose' => array(
00466         'use_operator' => 0,
00467         'operator' => 'type_id_op',
00468         'identifier' => 'type_id',
00469         'label' => 'Type',
00470         'remember' => 0,
00471       ),
00472       'case' => 1,
00473       'id' => 'type_id',
00474       'table' => 'stock',
00475       'field' => 'type_id',
00476       'relationship' => 'none',
00477       'values_form_type' => 'select',
00478       'multiple' => 1,
00479       'optional' => 0,
00480       'show_all' => 0,
00481       'agg' => array(
00482         'records_with' => 1,
00483         'aggregates_with' => 1,
00484       ),
00485     ),
00486     'name' => array(
00487       'operator' => '~',
00488       'value' => '',
00489       'group' => '0',
00490       'exposed' => TRUE,
00491       'expose' => array(
00492         'use_operator' => 0,
00493         'operator' => 'name_op',
00494         'identifier' => 'name',
00495         'label' => 'Name Contains',
00496         'remember' => 0,
00497       ),
00498       'case' => 0,
00499       'id' => 'name',
00500       'table' => 'stock',
00501       'field' => 'name',
00502       'relationship' => 'none',
00503       'agg' => array(
00504         'records_with' => 1,
00505         'aggregates_with' => 1,
00506       ),
00507     ),
00508   ));
00509   $handler->override_option('access', array(
00510     'type' => 'perm',
00511     'perm' => 'access chado_stock content',
00512   ));
00513   $handler->override_option('cache', array(
00514     'type' => 'none',
00515   ));
00516   $handler->override_option('title', 'Stocks');
00517   $handler->override_option('header', 'Click "Show" to see a list of all stocks matching the entered criteria. If you leave a any of the criteria blank then the stocks will be not be filtered based on that field. Furthermore, if you leave all criteria blank then all stocks will be listed.');
00518   $handler->override_option('header_format', '2');
00519   $handler->override_option('header_empty', 0);
00520   $handler->override_option('empty', 'No stocks match the supplied criteria.');
00521   $handler->override_option('empty_format', '1');
00522   $handler->override_option('items_per_page', 50);
00523   $handler->override_option('use_pager', '1');
00524   $handler->override_option('style_plugin', 'table');
00525   $handler->override_option('style_options', array(
00526     'grouping' => '',
00527     'override' => 1,
00528     'sticky' => 0,
00529     'order' => 'asc',
00530     'columns' => array(
00531       'uniquename' => 'uniquename',
00532       'name_2' => 'name_2',
00533       'name' => 'name',
00534       'common_name' => 'common_name',
00535       'all_dbxref' => 'all_dbxref',
00536       'all_properties' => 'all_properties',
00537       'all_relationships' => 'all_relationships',
00538     ),
00539     'info' => array(
00540       'uniquename' => array(
00541         'sortable' => 1,
00542         'separator' => '',
00543       ),
00544       'name_2' => array(
00545         'sortable' => 1,
00546         'separator' => '',
00547       ),
00548       'name' => array(
00549         'sortable' => 1,
00550         'separator' => '',
00551       ),
00552       'common_name' => array(
00553         'sortable' => 1,
00554         'separator' => '',
00555       ),
00556       'all_dbxref' => array(
00557         'separator' => '',
00558       ),
00559       'all_properties' => array(
00560         'separator' => '',
00561       ),
00562       'all_relationships' => array(
00563         'separator' => '',
00564       ),
00565     ),
00566     'default' => '-1',
00567   ));
00568   $default_handler = $handler;
00569   $handler = $view->new_display('page', 'Page', 'page_1');
00570   $handler->override_option('path', 'chado/stocks');
00571   $handler->override_option('menu', array(
00572     'type' => 'normal',
00573     'title' => 'Stocks',
00574     'description' => 'A stock is the physical entities, either living or preserved, held by collections and can be globally identified by the combination of organism, uniquename and stock type.',
00575     'weight' => '10',
00576     'name' => 'navigation',
00577   ));
00578   $handler->override_option('tab_options', array(
00579     'type' => 'none',
00580     'title' => '',
00581     'description' => '',
00582     'weight' => 0,
00583     'name' => 'navigation',
00584   ));
00585   // Add code specific to a local chado installation
00586   // NOTE: Edit $handler above to $default_handler for the default display
00587   if (tripal_core_chado_schema_exists()) {
00588     // Add nid field
00589     $fields = $view->get_items('field', 'default');
00590     $new_fields = array(
00591       'nid' => array(
00592         'label' => 'Nid',
00593         'alter' => array(
00594           'alter_text' => 0,
00595           'text' => '',
00596           'make_link' => 0,
00597           'path' => '',
00598           'absolute' => 0,
00599           'link_class' => '',
00600           'alt' => '',
00601           'rel' => '',
00602           'prefix' => '',
00603           'suffix' => '',
00604           'target' => '',
00605           'help' => '',
00606           'trim' => 0,
00607           'max_length' => '',
00608           'word_boundary' => 1,
00609           'ellipsis' => 1,
00610           'html' => 0,
00611           'strip_tags' => 0,
00612         ),
00613         'empty' => '',
00614         'hide_empty' => 0,
00615         'empty_zero' => 0,
00616         'hide_alter_empty' => 1,
00617         'link_to_node' => 0,
00618         'exclude' => 1,
00619         'id' => 'nid',
00620         'table' => 'node',
00621         'field' => 'nid',
00622         'relationship' => 'none',
00623       )
00624     );
00625     $fields = $new_fields + $fields;
00626     // Change analysis.name to have a link to the node
00627     $fields['name_2']['alter']['link_to_node'] = 1;
00628     $default_handler->override_option('fields', $fields);
00629     // Adds stock => Node relationship
00630     $default_handler->override_option('relationships', array(
00631       'nid' => array(
00632         'label' => 'Stock to Node',
00633         'required' => 0,
00634         'id' => 'nid',
00635         'table' => 'chado_stock',
00636         'field' => 'nid',
00637         'relationship' => 'none',
00638       ),
00639     ));
00640     // Only show records with published nodes
00659   }
00660   $views[$view->name] = $view;
00661 
00662   return $views;
00663 }
 All Classes Files Functions Variables