Sample 2-6. Advanced DataGrid code.

    1. Master DataGrid in View Mode.
    2. Second Datagrid in all modes.
Show Plain Text »
  1. <?php
  2.  
  3. $act = isset($_REQUEST["act"]) ? $_REQUEST["act"] : "";
  4. $region_id = isset($_REQUEST["region_id"]) ? $_REQUEST["region_id"] : "";
  5. $regn_p = isset($_REQUEST["regn_p"]) ? $_REQUEST["regn_p"] : "";
  6. $regn_page_size = isset($_REQUEST["regn_page_size"]) ? $_REQUEST["regn_page_size"] : "";
  7.  
  8. ## +---------------------------------------------------------------------------+
  9. ## | 1. Creating & Calling:                                                    |
  10. ## +---------------------------------------------------------------------------+
  11. ##  *** define a relative (virtual) path to datagrid.class.php file
  12. ##  *** directory (relatively to the current file)
  13. ##  *** RELATIVE PATH ONLY ***
  14. define ("DATAGRID_DIR", "datagrid/");                    
  15. require_once(DATAGRID_DIR."datagrid.class.php");
  16.  
  17. ##  *** creating variables that we need for database connection    
  18. $DB_USER = "db_user";
  19. $DB_PASS = "db_password";      
  20. $DB_HOST = "db_host";      
  21. $DB_NAME = "db_name";      
  22.    
  23. ##  *** put a primary key on the first place
  24. $sql = "SELECT
  25.    demo_regions.id,
  26.    IF(demo_regions.id = ".(int)$region_id.", CONCAT('<span style=\"color:#ff0000\"><b>', demo_regions.name, '</b></span>'), demo_regions.name) as name,
  27.    'View Countries' as link_to_countries
  28. FROM demo_regions";
  29. ##  *** set needed options and create a new class instance
  30. $debug_mode = false;        /* display SQL statements while processing */    
  31. $messaging = true;          /* display system messages on a screen */
  32. $unique_prefix = "regn_";    /* prevent overlays - must be started with a letter */
  33. $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  34. ##  *** set data source with needed options
  35.  $default_order = array("id"=>"ASC");
  36.   $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  37.   $dgrid->isDemo = true;
  38.  
  39. ## +---------------------------------------------------------------------------+
  40. ## | 2. General Settings:                                                      |
  41. ## +---------------------------------------------------------------------------+
  42. ##  *** set layouts: "0" - tabular(horizontal) - default, "1" - columnar(vertical), "2" - customized
  43. $layouts = array("view"=>"0", "edit"=>"0", "details"=>"1", "filter"=>"2");
  44. $dgrid->setLayouts($layouts);
  45. ##  *** set modes for operations ("type" => "link|button|image")
  46. $modes = array(
  47.     "add"         =>array("view"=>false, "edit"=>false, "type"=>"link"),
  48.     "edit"        =>array("view"=>false, "edit"=>false,  "type"=>"link", "byFieldValue"=>""),
  49.     "details" =>array("view"=>false, "edit"=>false, "type"=>"link"),
  50.     "delete"  =>array("view"=>false, "edit"=>false,  "type"=>"image")
  51. );
  52. $dgrid->setModes($modes);
  53. ##  *** set DataGrid caption
  54. $dg_caption = "Regions (Master DataGrid)";
  55. $dgrid->setCaption($dg_caption);
  56.  
  57. ## +---------------------------------------------------------------------------+
  58. ## | 5. Filter Settings:                                                       |
  59. ## +---------------------------------------------------------------------------+
  60. ##  *** set filtering option: true or false(default)
  61. $filtering_option = true;
  62. $show_search_type = true;
  63. $dgrid->AllowFiltering($filtering_option, $show_search_type);
  64. ##  *** set additional filtering settings
  65. $filtering_fields = array(
  66.    "Region"=>array("type"=>"textbox", "table"=>"demo_regions", "field"=>"name", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"like%", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
  67. );
  68. $dgrid->SetFieldsFiltering($filtering_fields);
  69.  
  70. ## +---------------------------------------------------------------------------+
  71. ## | 6. View Mode Settings:                                                    |
  72. ## +---------------------------------------------------------------------------+
  73. ##  *** set columns in view mode
  74. $vm_columns = array(  
  75.    "id"                =>array("header"=>"ID",     "type"=>"label",   "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>true|false, "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  76.    "name"              =>array("header"=>"Name",   "type"=>"label", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>true|false, "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  77.    "link_to_countries" =>array("header"=>"Action", "type"=>"link", "align"=>"center", "width"=>"150px", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string|numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "field_key"=>"id", "field_data"=>"link_to_countries", "rel"=>"", "title"=>"", "target"=>"_self", "href"=>"javascript:".$unique_prefix."_doPostBack('paging','','&regn_sort_field=id&regn_sort_field_by=&regn_sort_field_type=&regn_sort_type=ASC&regn_page_size=1&regn_p=".$regn_p."&act=details&region_id={0}&regn_p=".$regn_p."&regn_page_size=".$regn_page_size."')")      
  78. );
  79. $dgrid->setColumnsInViewMode($vm_columns);
  80.  
  81. if($act == "details"){
  82.     ##  *** put a primary key on the first place
  83.    $sql=" SELECT  
  84.        demo_countries.id,
  85.        demo_countries.name,
  86.        demo_countries.description,
  87.        demo_countries.picture_url,
  88.        FORMAT(demo_countries.population, 0) as population,
  89.        CASE WHEN demo_countries.is_democracy = 1 THEN "Yes" ELSE "No" END as is_democracy
  90.    FROM demo_countries
  91.    WHERE region_id = ".(int)$region_id;        
  92.        
  93.     ##  *** set needed options and create a new class instance
  94.    $debug_mode = false;        /* display SQL statements while processing */    
  95.     $messaging = true;          /* display system messages on a screen */
  96.     $unique_prefix = "cnt_";    /* prevent overlays - must be started with a letter */
  97.     $dgrid1 = new DataGrid($debug_mode, $messaging, $unique_prefix);
  98.      
  99.     ##  *** set data source with needed options
  100.    $default_order = array("name"=>"ASC");
  101.     $dgrid1->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  102.     $dgrid1->isDemo = true;
  103.      
  104.     ##  *** set DataGrid caption
  105.    $dg_caption = "Countries (Detail DataGrid)";
  106.     $dgrid1->setCaption($dg_caption);
  107.    
  108.     ##  *** set variables that used to get access to the page (like: my_page.php?act=34&id=56 etc.)
  109.    $http_get_vars = array("act", "region_id");
  110.     $dgrid1->SetHttpGetVars($http_get_vars);
  111.  
  112.     $anotherDatagrids = array("regn_"=>array("view"=>true, "edit"=>true, "details"=>true));
  113.     $dgrid1->setAnotherDatagrids($anotherDatagrids);    
  114.  
  115.     ## +---------------------------------------------------------------------------+
  116.    ## | 6. View Mode Settings:                                                    |
  117.    ## +---------------------------------------------------------------------------+
  118.    ##  *** set view mode table properties
  119.    $vm_table_properties = array("width"=>"60%");
  120.     $dgrid1->setViewModeTableProperties($vm_table_properties);  
  121.     ##  *** set columns in view mode
  122.    $vm_columns = array(  
  123.        "name"       =>array("header"=>"Name",   "type"=>"label", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>true|false, "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  124.        "population" =>array("header"=>"Population", "type"=>"label", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>true|false, "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  125.     );
  126.     $dgrid1->setColumnsInViewMode($vm_columns);
  127.    
  128.     ## +---------------------------------------------------------------------------+
  129.    ## | 7. Add/Edit/Details Mode settings:                                        |
  130.    ## +---------------------------------------------------------------------------+
  131.    ##  ***  set settings for edit/details mode
  132.    $table_name = "demo_countries";
  133.     $primary_key = "id";
  134.     $condition = "";
  135.     $dgrid1->setTableEdit($table_name, $primary_key, $condition);
  136.  
  137.     $fill_from_array = array("10000"=>"10000", "250000"=>"250000", "5000000"=>"5000000", "25000000"=>"25000000", "100000000"=>"100000000");
  138.     $em_columns = array(
  139.       "region_id"        =>array("header"=>"Region",           "type"=>"textbox",  "width"=>"210px", "req_type"=>"rt", "title"=>"Region Name"),
  140.       "name"             =>array("header"=>"Country",          "type"=>"textbox",  "width"=>"210px", "req_type"=>"ry", "title"=>"Country Name", "unique"=>true),
  141.       "description"      =>array("header"=>"Short Descr.",     "type"=>"textarea", "width"=>"210px", "req_type"=>"rt", "title"=>"Short Description", "edit_type"=>"wysiwyg", "rows"=>"3", "cols"=>"50"),
  142.       "population"       =>array("header"=>"Peoples",          "type"=>"enum",     "source"=>$fill_from_array, "view_type"=>"dropdownlist",  "width"=>"139px", "req_type"=>"ri", "title"=>"Population (Peoples)"),
  143.       "picture_url"      =>array("header"=>"Image URL",        "type"=>"image",    "req_type"=>"st", "width"=>"210px", "title"=>"Picture", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "on_js_event"=>"", "target_path"=>"uploads/", "max_file_size"=>"100K", "image_width"=>"100px", "image_height"=>"100px", "file_name"=>"", "host"=>"local"),
  144.       "is_democracy"     =>array("header"=>"Is Democracy",     "type"=>"checkbox", "true_value"=>1, "false_value"=>0,  "width"=>"210px", "req_type"=>"sy", "title"=>"Is Democraty"),
  145.       "independent_date" =>array("header"=>"Independence Day", "type"=>"date",     "req_type"=>"rt", "width"=>"187px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating"),
  146.     );
  147.     $dgrid1->SetColumnsInEditMode($em_columns);
  148.     ##  *** set foreign keys for add/edit/details modes (if there are linked tables)
  149.    $foreign_keys = array(
  150.       "region_id"=>array("table"=>"demo_regions", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "order_by_field"=>"name", "order_type"=>"ASC"),
  151.     );
  152.     $dgrid1->SetForeignKeysEdit($foreign_keys);
  153. }
  154.  
  155. ## +---------------------------------------------------------------------------+
  156. ## | 8. Bind the DataGrid:                                                     |
  157. ## +---------------------------------------------------------------------------+
  158. ##  *** bind the DataGrid and draw it on the screen
  159.  
  160. ob_start();    
  161. $dgrid->Bind();            
  162.  
  163. if($act == "details"){
  164.     $dgrid1->Bind();                
  165. }
  166. ob_end_flush();    
  167.  
  168. ################################################################################
  169.  
  170.  
  171. ?>