Sample 2-9. Advanced PHP DG code. All In One (all types of fields).

    1. All types of fields.
Show Plain Text »
  1. <?php
  2. ## +---------------------------------------------------------------------------+
  3. ## | 1. Creating & Calling:                                                    |
  4. ## +---------------------------------------------------------------------------+
  5. ##  *** define a relative (virtual) path to datagrid.class.php file
  6. ##  *** directory (relatively to the current file)
  7. ##  *** RELATIVE PATH ONLY ***
  8. define ("DATAGRID_DIR", "datagrid/");                    
  9. require_once(DATAGRID_DIR."datagrid.class.php");
  10.  
  11. ##  *** creating variables that we need for database connection    
  12. $DB_USER = "db_user";
  13. $DB_PASS = "db_password";      
  14. $DB_HOST = "db_host";      
  15. $DB_NAME = "db_name";      
  16.  
  17. ##  *** set needed options and create a new class instance
  18. $debug_mode = false;        /* display SQL statements while processing */    
  19. $messaging = true;          /* display system messages on a screen */
  20. $unique_prefix = "aio_";    /* prevent overlays - must be started with a letter */
  21. $mode = isset($_REQUEST[$unique_prefix."mode"]) ? strip_tags($_REQUEST[$unique_prefix."mode"]) : "";
  22. $rid = isset($_REQUEST[$unique_prefix."rid"]) ? (int)$_REQUEST[$unique_prefix."rid"] : "";
  23.  
  24. ##  *** set data source with required settings
  25. $sql = "SELECT
  26.          oio.id,
  27.          oio.field_textbox,
  28.          oio.field_textarea,
  29.          oio.field_date,
  30.          oio.field_money,
  31.          oio.field_enum,
  32.          oio.field_checkbox,
  33.          oio.field_color,
  34.          oio.field_percent,
  35.          dc.name as field_foreign_key_name
  36.        FROM demo_all_in_one oio
  37.          LEFT OUTER JOIN demo_countries dc ON oio.field_foreign_key = dc.id";
  38. $default_order = array("id"=>"ASC");   /* Ex.: array("field_1"=>"ASC", "field_2"=>"DESC") */
  39. $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);
  40. $dgrid->isDemo = true;
  41.  
  42. ## +---------------------------------------------------------------------------+
  43. ## | 2. General Settings:                                                      |
  44. ## +---------------------------------------------------------------------------+
  45. ## +-- PostBack Submission Method ---------------------------------------------+
  46. ##  *** defines postback submission method for DataGrid: AJAX, POST(default) or GET
  47. $postback_method = "post";
  48. $dgrid->SetPostBackMethod($postback_method);
  49. $dg_caption = "All In One Example";
  50. $dgrid->SetCaption($dg_caption);
  51. ## +-- Multirow Operations ----------------------------------------------------+
  52. ##  *** allow multirow operations
  53. $multirow_option = true;
  54. $dgrid->AllowMultirowOperations($multirow_option);
  55. $multirow_operations = array(
  56.    "edit"    => array("view"=>true),
  57.    "details" => array("view"=>true),
  58.    "clone"   => array("view"=>false),
  59.    "delete"  => array("view"=>true),
  60. );
  61. $dgrid->SetMultirowOperations($multirow_operations);  
  62.  
  63. ## +---------------------------------------------------------------------------+
  64. ## | 4. Sorting & Paging Settings:                                             |
  65. ## +---------------------------------------------------------------------------+
  66. ##  *** set paging option: true(default) or false
  67. $paging_option = true;
  68. $rows_numeration = false;
  69. $numeration_sign = "N #";
  70. $dropdown_paging = false;
  71. $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign, $dropdown_paging);
  72. ##  *** set paging settings
  73. $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
  74. $top_paging = array();
  75. $pages_array = array("10"=>"10", "12"=>"12", "25"=>"25", "50"=>"50", "100"=>"100", "250"=>"250", "500"=>"500", "1000"=>"1000");
  76. $default_page_size = 12;
  77. $paging_arrows = array("first"=>"|&lt;&lt;", "previous"=>"&lt;&lt;", "next"=>"&gt;&gt;", "last"=>"&gt;&gt;|");
  78. $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size, $paging_arrows);
  79.  
  80. ## +---------------------------------------------------------------------------+
  81. ## | 5. Filter Settings:                                                       |
  82. ## +---------------------------------------------------------------------------+
  83. ##  *** set filtering option: true or false(default)
  84. $filtering_option = true;
  85. $show_search_type = true;
  86. $dgrid->AllowFiltering($filtering_option, $show_search_type);
  87. ##  *** set additional filtering settings
  88. $filtering_fields = array(
  89.   "Textbox"     => array("type"=>"textbox",  "table"=>"demo_all_in_one", "table_alias"=>"oio", "field"=>"field_textbox", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"120px", "on_js_event"=>"", "default"=>""),
  90.   "Date"        => array("type"=>"calendar", "table"=>"demo_all_in_one", "table_alias"=>"oio", "field"=>"field_date", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"120px", "on_js_event"=>"", "default"=>"", "calendar_type"=>"popup|floating", "date_format"=>"date", "field_type"=>""),
  91.   "Foreign Key" => array("type"=>"enum",     "table"=>"demo_countries", "table_alias"=>"dc", "field"=>"id", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false", "comparison_type"=>"string|numeric|binary", "width"=>"", "on_js_event"=>"", "default"=>"", "source"=>"self", "field_view"=>"name", "order_by_field"=>"name", "order_type"=>"ASC", "condition"=>"", "show_count"=>false, "multiple"=>"false", "multiple_size"=>"4"),
  92. );
  93. $dgrid->SetFieldsFiltering($filtering_fields);
  94.  
  95. ## +---------------------------------------------------------------------------+
  96. ## | 6. View Mode Settings:                                                    |
  97. ## +---------------------------------------------------------------------------+
  98. ##  *** set columns in view mode
  99. $fill_from_array = array("0"=>"No", "1"=>"Yes", "2"=>"Don't know", "3"=>"My be");
  100. $vm_columns = array(
  101.   "field_textbox"  => array("header"=>"Textbox",  "type"=>"label", "align"=>"left", "width"=>"", "wrap"=>"wrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  102.   "field_date"     => array("header"=>"Date",     "type"=>"label", "align"=>"center", "width"=>"90px", "wrap"=>"wrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  103.   "field_foreign_key_name" => array("header"=>"Foreign Key",  "type"=>"label", "align"=>"center", "width"=>"100px", "wrap"=>"wrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  104.   "field_enum"     => array("header"=>"Enum",     "type"=>"enum",  "align"=>"center", "width"=>"90px", "wrap"=>"wrap|nowrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>$fill_from_array),    
  105.   "field_checkbox" => array("header"=>"Checkbox", "type"=>"checkbox", "align"=>"center", "width"=>"80px", "wrap"=>"wrap|nowrap", "sort_type"=>"numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "true_value"=>1, "false_value"=>0),
  106.   "field_color"    => array("header"=>"Color",    "type"=>"color",    "align"=>"center", "width"=>"90px", "wrap"=>"wrap|nowrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"floating|simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "view_type"=>"image"),
  107.   "field_money"    => array("header"=>"Money",    "type"=>"money", "align"=>"right", "width"=>"80px", "wrap"=>"wrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"true", "summarize_sign"=>"SUM$=", "sort_type"=>"numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "sign"=>"$", "sign_place"=>"before", "decimal_places"=>"2", "dec_separator"=>".", "thousands_separator"=>","),
  108.   "field_percent"  => array("header"=>"Percent",  "type"=>"percent",  "align"=>"right", "width"=>"90px", "wrap"=>"wrap|nowrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"true", "summarize_sign"=>"AVG%=", "summarize_function"=>"AVG", "sort_type"=>"numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "decimal_places"=>"2", "dec_separator"=>"."),
  109. );
  110. $dgrid->SetColumnsInViewMode($vm_columns);
  111.  
  112. ## +---------------------------------------------------------------------------+
  113. ## | 7. Add/Edit/Details Mode Settings:                                        |
  114. ## +---------------------------------------------------------------------------+
  115. $table_name  = "demo_all_in_one";
  116. $primary_key = "id";
  117. $condition   = "";
  118. $dgrid->SetTableEdit($table_name, $primary_key, $condition);
  119. ##  *** set columns in edit mode  
  120. $em_columns = array(
  121.   "id"             => array("header"=>"Label (ID)", "type"=>"label",  "title"=>"", "default"=>"", "visible"=>"true", "on_js_event"=>""),
  122.   "field_textbox"  => array("header"=>"Textbox",  "type"=>"textbox",  "req_type"=>"ry", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"255", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  123.   "field_textarea" => array("header"=>"Textarea", "type"=>"textarea", "req_type"=>"sy", "width"=>"410px", "title"=>"", "readonly"=>"false", "maxlength"=>"1024", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "edit_type"=>"simple", "resizable"=>"true", "upload_images"=>"false", "rows"=>"3", "cols"=>"50"),
  124.   "field_date"     => array("header"=>"Date",     "type"=>"date",     "req_type"=>"st", "width"=>"187px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"dropdownlist"),
  125.   "field_datetime" => array("header"=>"DateTime", "type"=>"datetime", "req_type"=>"st", "width"=>"127px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating", "show_seconds"=>"true"),
  126.   "field_time"     => array("header"=>"Time",     "type"=>"time",     "req_type"=>"st", "width"=>"90px",  "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"dropdownlist", "show_seconds"=>"true"),
  127.   "field_image"    => array("header"=>"Image",    "type"=>"image",    "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "target_path"=>"uploads/", "allow_image_updating"=>"false", "max_file_size"=>"100K", "image_width"=>"120px", "image_height"=>"90px", "resize_image"=>"false", "resize_width"=>"", "resize_height"=>"", "magnify"=>"true", "magnify_type"=>"magnifier", "magnify_power"=>"2", "file_name"=>"", "host"=>"local", "allow_downloading"=>"false", "allowed_extensions"=>""),
  128.   "field_file"     => array("header"=>"File",     "type"=>"file",     "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "target_path"=>"uploads/", "max_file_size"=>"100K", "file_name"=>"", "host"=>"local", "allow_downloading"=>"false", "allowed_extensions"=>""),
  129.   "field_money"    => array("header"=>"Money",    "type"=>"money",    "req_type"=>"sn", "width"=>"80px",  "title"=>"", "readonly"=>"false", "maxlength"=>"8",  "default"=>"0", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "sign"=>"$", "sign_place"=>"before", "decimal_places"=>"2", "dec_separator"=>".", "thousands_separator"=>","),
  130.   "delimiter_1"    => array("inner_html"=>"&#8226;&#8226;&#8226; Delimiter "),
  131.   "field_foreign_key" =>array("header"=>"Foreign Key", "type"=>"foreign_key","req_type"=>"ri", "width"=>"210px", "title"=>"", "readonly"=>"false", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true"),
  132.   "field_enum"     => array("header"=>"Enum",     "type"=>"enum",     "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>$fill_from_array, "view_type"=>"radiobutton", "elements_alignment"=>"horizontal|vertical", "multiple"=>"false", "multiple_size"=>"4"),
  133.   "field_enum_multiple" => array("header"=>"Multiple Select", "type"=>"enum", "req_type"=>"sr", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>array("Vice"=>"Vice", "Current"=>"Current", "Candidate"=>"Candidate"), "view_type"=>"checkbox", "elements_alignment"=>"vertical", "multiple"=>"true", "multiple_size"=>"4"),
  134.   "field_password" => array("header"=>"Password", "type"=>"password", "req_type"=>"sp", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"20", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "hide"=>"false", "generate"=>"true", "cryptography"=>"true", "cryptography_type"=>"aes", "aes_password"=>"aes_password"),
  135.   "validator"      => array("header"=>"Password Validator", "type"=>"validator", "req_type"=>"sv", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "visible"=>(($mode == "add") ? "true" : "false"), "on_js_event"=>"", "for_field"=>"field_password", "validation_type"=>"password"),
  136.   "field_checkbox" => array("header"=>"Checkbox", "type"=>"checkbox", "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "true_value"=>1, "false_value"=>0),
  137.   "field_color"    => array("header"=>"Color",    "type"=>"color",    "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "view_type"=>"picker", "save_format"=>"hexcodes"),
  138.   "field_percent"  => array("header"=>"Percent",  "type"=>"percent",  "req_type"=>"rt", "width"=>"70px", "title"=>"", "readonly"=>"false", "maxlength"=>"4", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "decimal_places"=>"2", "dec_separator"=>"."),
  139.   "field_hidden"   => array("header"=>"Hidden",   "type"=>"hidden",   "req_type"=>"st", "default"=>@date("Y-m-d"), "value"=>"", "unique"=>"false", "visible"=>(($mode == "add") ? "false" : "true")),
  140.   "field_link"     => array("header"=>"Link (add/edit modes)",     "type"=>"link",     "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"http://", "unique"=>"false", "unique_condition"=>"", "visible"=>true, "on_js_event"=>""),
  141. );
  142.  
  143. if($mode == "details"){
  144.   $em_columns["field_link"] = array("header"=>"Link (details mode)",     "type"=>"link",     "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>true, "on_js_event"=>"", "field_key"=>"field_link", "field_data"=>"field_link", "rel"=>"", "title"=>"", "target"=>"_new", "href"=>"{0}");        
  145. }
  146. $dgrid->SetColumnsInEditMode($em_columns);
  147.  
  148. ## +---------------------------------------------------------------------------+
  149. ## | 8. Foreign Keys Settings:                                                 |
  150. ## +---------------------------------------------------------------------------+
  151. ##  *** set foreign keys for add/edit/details modes (if there are linked tables)
  152. $foreign_keys = array(
  153.   "field_foreign_key" => array("table"=>"demo_countries", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "elements_alignment"=>"horizontal|vertical", "condition"=>"", "order_by_field"=>"name", "order_type"=>"ASC", "show_count"=>"", "on_js_event"=>""),
  154. );
  155. $dgrid->SetForeignKeysEdit($foreign_keys);
  156.  
  157. ## +---------------------------------------------------------------------------+
  158. ## | 9. Bind the DataGrid:                                                     |
  159. ## +---------------------------------------------------------------------------+
  160. ##  *** bind the DataGrid and draw it on the screen
  161. $dgrid->Bind();        
  162.  
  163. ?>