Webman-framework

Lightweight, Component-based, and Database-oriented Web Application Framework

About | Overview | Documentation

 

Documentation > Modules and APIs > webman_db_item_view

webman_db_item_view

 

Description:

Component-type module that provides basic and simple database table items view/listing.

 

Dependencies:

Webman-framework's Core Modules:

  • DBI_HTML_Map (Composition)

Webman-framework's Component-type Modules:
  • CGI_Component::webman_CGI_component (Inheritance)
  • webman_link_path_generator (Composition)

 

1. View Template

The DYNAMIC_CONTENT template-element (line 4) named link_path will be processed by webman_link_path_generator module inside process_DYNAMIC hook function. It's used as place-holder to render current application's link path when webman_db_item_view module is called.

The main part of the view template where database table items will be rendered is the unnamed DBHTML template-element (line 16-21) contains word patterns $db_field_name_1_ till $db_field_name_n_. The form of word patterns used are following the requirements imposed by DBI_HTML_Map core module for the purpose of mapping database table field values into the template-element. This unnamed DBHTML template-element will be passed to process_DBHTML hook function before manipulated DBI_HTML_Map module.

  1 <html>                                                    
  2 <body>                                                    
  3 <!-- start_view_ //-->                                    
  4 <!-- dynamic_content_ name=link_path //--> &gt; List Title
  5 <p />                                                     
  6                                                           
  7 <table border="1">                                        
  8   <tr>                                                    
  9     <th>Num.</th>                                         
 10     <th>field_caption_1</th>                              
 11     <th>field_caption_2</th>                              
 12     <th>field_caption_n</th>                              
 13   </tr>                                                   
 14                                                           
 15   <!-- start_dbhtml_ //-->                                
 16   <tr>                                                    
 17     <td align="right">$num_.</td>                         
 18     <td>$db_field_name_1_</td>                            
 19     ...                                                   
 20     <td>$db_field_name_n_</td>                            
 21   </tr>                                                   
 22   <!-- end_dbhtml_ //-->                                  
 23 </table>                                                  
 24                                                           
 25 <!-- end_view_ //-->                                      
 26 </body>                                                   
 27 </html>                                                   


 
2. Instantiation and Basic Parameter Setting

Database table items to be viewed/listed are retrieved via standard SQL's select string command (line 9).

  1 my $component = new webman_db_item_view;                        
  2                                                                 
  3 $component->set_CGI($cgi);                                      
  4 $component->set_DBI_Conn($db_conn);                             
  5                                                                 
  6 ### Option to debug SQL satement generated by the module.       
  7 #$component->set_SQL_Debug(1);                                  
  8                                                                 
  9 $component->set_SQL("select * from ... where ... order by ...");
 10                                                                 
 11 $component->set_Template_Default($template_file);               
 12                                                                 


 
3. Component-type Generic Function Calls

 13 if ($component->authenticate) {       
 14     $component->run_Task;             
 15     $component->process_Content;      
 16     $component->end_Task;             
 17 }                                     
 18                                       
 19 my $content = $component->get_Content;


 
4. Child Module for Customization

The possible customization tasks are to manipulate the DBHTML template-element content (lines 64-65) before it being used and processed as place-holder for database items list (line 70) and programmatically modify the SQL's select string command (line 83) to support more complex database query logic.

  1 package child_module_name;                               
  2                                                                            
  3 use webman_db_item_view;                                                   
  4                                                                            
  5 @ISA=("webman_db_item_view");                                              
  6                                                                            
  7 sub new {                                                                  
  8     my $class = shift;                                                     
  9                                                                            
 10     my $this = $class->SUPER::new();                                       
 11                                                                            
 12     #$this->set_Debug_Mode(1, 1);                                          
 13                                                                            
 14     bless $this, $class;                                                   
 15                                                                            
 16     return $this;                                                          
 17 }                                                                          
 18                                                                            
 19 sub get_Name {                                                             
 20     my $this = shift @_;                                                   
 21                                                                            
 22     return __PACKAGE__;                                                    
 23 }                                                                          
 24                                                                            
 25 sub get_Name_Full {                                                        
 26     my $this = shift @_;                                                   
 27                                                                            
 28     return $this->SUPER::get_Name_Full . "::" . __PACKAGE__;               
 29 }                                                                          
 30                                                                            
 31 sub run_Task {                                                             
 32     my $this = shift @_;                                                   
 33                                                                            
 34     my $cgi = $this->get_CGI;                                              
 35     my $dbu = $this->get_DBU;                                              
 36     my $db_conn = $this->get_DB_Conn;                                      
 37                                                                            
 38     my $login_name = $this->get_User_Login;                                
 39     my @groups = $this->get_User_Groups;                                   
 40                                                                            
 41     my $match_group = $this->match_Group($group_name_, @groups);           
 42                                                                            
 43     $this->SUPER::run_Task;                                                
 44 }                                                                          
 45                                                                            
 46 sub process_DBHTML {                                                       
 47     my $this = shift @_;                                                   
 48     my $te = shift @_;                                                     
 49                                                                            
 50     my $cgi = $this->get_CGI;                                              
 51     my $dbu = $this->get_DBU;                                              
 52     my $db_conn = $this->get_DB_Conn;                                      
 53                                                                            
 54     my $login_name = $this->get_User_Login;                                
 55     my @groups = $this->get_User_Groups;                                   
 56                                                                            
 57     my $match_group = $this->match_Group($group_name_, @groups);           
 58                                                                            
 59     my $te_content = $te->get_Content;                                     
 60     my $te_type_num = $te->get_Type_Num;                                   
 61                                                                            
 62     ### It's possible to first manipulate DBHTML template-element content  
 63     ### before it being processed by DBI_HTML_Map core module.             
 64     #$te_content = ...;                                                    
 65     #$te->set_Content = $te_content;                                       
 66                                                                            
 67     ### Call back the overriden base class function that will do the real  
 68     ### implementations of manipulating DBHTML template-element content    
 69     ### using DBI_HTML_Map core module.                                    
 70     $this->SUPER::process_DBHTML($te);                                     
 71 }                                                                          
 72                                                                            
 73 sub customize_SQL {                                                        
 74     my $this = shift @_;                                                   
 75                                                                            
 76     my $cgi = $this->get_CGI;                                              
 77     my $dbu = $this->get_DBU;                                              
 78     my $db_conn = $this->get_DB_Conn;                                      
 79                                                                            
 80     my $sql = $this->{sql};                                                
 81                                                                            
 82     ### Next is to customize the $sql string.                              
 83     #$sql = ...;                                                           
 84                                                                            
 85     return $sql;                                                           
 86 }                                                                          
 87                                                                            
 88 1;