| 1 | jonen | 1.1 | <?php | 
| 2 |  |  |  | 
| 3 |  |  | /** | 
| 4 |  |  | * This example illustrates the use of the master slave element | 
| 5 |  |  | * | 
| 6 |  |  | * | 
| 7 |  |  | * $Id: form4.php,v 1.2 2004/04/01 17:23:24 hemna Exp $ | 
| 8 |  |  | * | 
| 9 |  |  | * @author Sumedh Thakar <sumedh_thakar@yahoo.com> | 
| 10 |  |  | * @package phpHtmlLib | 
| 11 |  |  | * @subpackage form-examples | 
| 12 |  |  | * @version 2.2.0 | 
| 13 |  |  | * | 
| 14 |  |  | */ | 
| 15 |  |  |  | 
| 16 |  |  | /** | 
| 17 |  |  | * Include the phphtmllib libraries | 
| 18 |  |  | * | 
| 19 |  |  | */ | 
| 20 |  |  | include("includes.inc"); | 
| 21 |  |  |  | 
| 22 |  |  | /** | 
| 23 |  |  | * Include the Form Processing objects | 
| 24 |  |  | * | 
| 25 |  |  | */ | 
| 26 |  |  | include_once($phphtmllib."/form/includes.inc"); | 
| 27 |  |  |  | 
| 28 |  |  |  | 
| 29 |  |  | //use the class we defined from | 
| 30 |  |  |  | 
| 31 |  |  | include_once("MyLayoutPage.inc"); | 
| 32 |  |  |  | 
| 33 |  |  | /** | 
| 34 |  |  | * A simple class to be the master. It contains a list of countries and the acceptable states for each county. | 
| 35 |  |  | * | 
| 36 |  |  | * @author Sumedh Thakar <sumedh_thakar@yahoo.com> | 
| 37 |  |  | * @package phpHtmlLib | 
| 38 |  |  | * @subpackage form-examples | 
| 39 |  |  | */ | 
| 40 |  |  |  | 
| 41 |  |  | class FECountryListMaster extends FEListBoxMaster { | 
| 42 |  |  | /** | 
| 43 |  |  | * The states array format "United States" => array ("code" => "US", | 
| 44 |  |  | *                                                   "slave" => Array("California" => "CA") | 
| 45 |  |  | *                                                  ) | 
| 46 |  |  | * if a seperate "code" value is not given then the array key (country name) is used | 
| 47 |  |  | * | 
| 48 |  |  | * @var array | 
| 49 |  |  | */ | 
| 50 |  |  | var $_countries = array("Select Country", | 
| 51 |  |  | "Hungary" , | 
| 52 |  |  | "Iceland" , | 
| 53 |  |  | "India" => array( | 
| 54 |  |  | "slave" => array("Select State" => "", | 
| 55 |  |  | "Andhra Pradesh" => "Andhra Pradesh", | 
| 56 |  |  | "Andaman and Nicobar Islands" => "Andaman and Nicobar Islands", | 
| 57 |  |  | "Maharashtra" => "Maharashtra", | 
| 58 |  |  | "Manipur" => "Manipur", | 
| 59 |  |  | "Uttar Pradesh" => "Uttar Pradesh", | 
| 60 |  |  | "Uttaranchal" => "Uttaranchal", | 
| 61 |  |  | "West Bengal" => "West Bengal", | 
| 62 |  |  | ) | 
| 63 |  |  | ), | 
| 64 |  |  | "Italy" , | 
| 65 |  |  | "Jamaica" , | 
| 66 |  |  | "Japan" , | 
| 67 |  |  | "Jersey, C.I." , | 
| 68 |  |  | "Jordan" , | 
| 69 |  |  | "Kazakhstan" , | 
| 70 |  |  | "Kenya" , | 
| 71 |  |  | "United Kingdom" , | 
| 72 |  |  | "United States of America" => array( | 
| 73 |  |  | "slave" => array("Select State" => "", | 
| 74 |  |  | "Alabama" => "Alabama", | 
| 75 |  |  | "California" => "California", | 
| 76 |  |  | "Colorado" => "Colorado", | 
| 77 |  |  | "Guam" => "Guam", | 
| 78 |  |  | "American Samoa" => "American Samoa", | 
| 79 |  |  | "Palau" => "Palau", | 
| 80 |  |  | ) | 
| 81 |  |  | ), | 
| 82 |  |  | "Uruguay" , | 
| 83 |  |  | "Uzbekistan" , | 
| 84 |  |  | "Zimbabwe" | 
| 85 |  |  | ); | 
| 86 |  |  |  | 
| 87 |  |  |  | 
| 88 |  |  | /** | 
| 89 |  |  | * The constructor | 
| 90 |  |  | * | 
| 91 |  |  | * @param string text label for the element | 
| 92 |  |  | * @param boolean is this a required element? | 
| 93 |  |  | * @param int element width in characters, pixels (px), percentage (%) or elements (em) | 
| 94 |  |  | * @param int element height in px | 
| 95 |  |  | * | 
| 96 |  |  | */ | 
| 97 |  |  | function FECountryListMaster($label, $required = TRUE, $width = NULL, $height = NULL) { | 
| 98 |  |  |  | 
| 99 |  |  | //Get the data and put it in the format that FEListBoxMaster wants it. | 
| 100 |  |  | $tmp = array(); | 
| 101 |  |  | foreach($this->_countries as $key => $country){ | 
| 102 |  |  | if(!is_array($country)){ | 
| 103 |  |  | $tmp[$country] = array("code"=>$country); | 
| 104 |  |  | }else{ | 
| 105 |  |  | $tmp[$key] = $country; | 
| 106 |  |  | $tmp[$key]["code"] = $key; | 
| 107 |  |  | } | 
| 108 |  |  | } | 
| 109 |  |  | $tmp['Select Country']["code"] = ""; | 
| 110 |  |  |  | 
| 111 |  |  | //Create the master. | 
| 112 |  |  | $this->FEListBoxMaster($label, $required, $width, $height); | 
| 113 |  |  | //Set the master-slave array | 
| 114 |  |  | $this->set_list_data($tmp); | 
| 115 |  |  | } | 
| 116 |  |  |  | 
| 117 |  |  | } | 
| 118 |  |  | /** | 
| 119 |  |  | * This demonstrates the power of the master-slave relationship between two list elements | 
| 120 |  |  | * The control produces the javascript required to change the value of the slave when the | 
| 121 |  |  | * master changes. It also automatically does the verification to check if the given value | 
| 122 |  |  | * of the slave is in the list of values for the given master. This way your validation is | 
| 123 |  |  | * taken care of | 
| 124 |  |  | * | 
| 125 |  |  | * @author Sumedh Thakar <sumedh_thakar@yahoo.com> | 
| 126 |  |  | * @package phpHtmlLib | 
| 127 |  |  | * @subpackage form-examples | 
| 128 |  |  | */ | 
| 129 |  |  | class Form4Page extends MyLayoutPage { | 
| 130 |  |  |  | 
| 131 |  |  | function content_block() { | 
| 132 |  |  | //build the FormProcessor, and add the | 
| 133 |  |  | //Form content object that the FormProcessor | 
| 134 |  |  | //will use.  Make the width of the form 600 | 
| 135 |  |  | return new FormProcessor( new MasterSlaveForm(600)); | 
| 136 |  |  | } | 
| 137 |  |  | } | 
| 138 |  |  |  | 
| 139 |  |  |  | 
| 140 |  |  | /** | 
| 141 |  |  | * This is the Class that handles the building | 
| 142 |  |  | * of the Form itself.  It creates the Form Elements | 
| 143 |  |  | * inside the form_init_elements() method. | 
| 144 |  |  | * | 
| 145 |  |  | * @author Sumedh Thakar <sumedh_thakar@yahoo.com> | 
| 146 |  |  | * @package phpHtmlLib | 
| 147 |  |  | * @subpackage form-examples | 
| 148 |  |  | */ | 
| 149 |  |  | class MasterSlaveForm extends FormContent { | 
| 150 |  |  |  | 
| 151 |  |  | /** | 
| 152 |  |  | * This method gets called EVERY time the object is | 
| 153 |  |  | * created.  It is used to build all of the | 
| 154 |  |  | * FormElement objects used in this Form. | 
| 155 |  |  | * | 
| 156 |  |  | */ | 
| 157 |  |  | function form_init_elements() { | 
| 158 |  |  | //we do not want any confirmation page for this form. | 
| 159 |  |  | $this->set_confirm(false); | 
| 160 |  |  |  | 
| 161 |  |  | //Create the slave element, a simple dropdown | 
| 162 |  |  | $state = new FEListBox("State",true); | 
| 163 |  |  |  | 
| 164 |  |  | $this->add_element($state); | 
| 165 |  |  |  | 
| 166 |  |  | //Create the master element | 
| 167 |  |  | $country = new FECountryListMaster("Country"); | 
| 168 |  |  |  | 
| 169 |  |  | //Set the state as the slave of this. remember to pass an array of references | 
| 170 |  |  | $country->set_slave_elements(array(&$state)); | 
| 171 |  |  |  | 
| 172 |  |  | $this->add_element($country); | 
| 173 |  |  |  | 
| 174 |  |  | //Create the slave element, a simple dropdown | 
| 175 |  |  | $state1 = new FEListBox("State1",true); | 
| 176 |  |  |  | 
| 177 |  |  | $this->add_element($state1); | 
| 178 |  |  |  | 
| 179 |  |  | //Create the master element | 
| 180 |  |  | $country1 = new FECountryListMaster("Country1"); | 
| 181 |  |  |  | 
| 182 |  |  | //Set the state as the slave of this. remember to pass an array of references | 
| 183 |  |  | $country1->set_slave_elements(array(&$state1)); | 
| 184 |  |  |  | 
| 185 |  |  | $this->add_element($country1); | 
| 186 |  |  |  | 
| 187 |  |  | } | 
| 188 |  |  |  | 
| 189 |  |  | /** | 
| 190 |  |  | * This method is called only the first time the form | 
| 191 |  |  | * page is hit.  This enables u to query a DB and | 
| 192 |  |  | * pre populate the FormElement objects with data. | 
| 193 |  |  | * | 
| 194 |  |  | */ | 
| 195 |  |  | function form_init_data() { | 
| 196 |  |  |  | 
| 197 |  |  | //In this example we just hard code some | 
| 198 |  |  | //initial values | 
| 199 |  |  | $this->set_element_value("Country", "India"); | 
| 200 |  |  |  | 
| 201 |  |  | $this->set_element_value("State", "Maharashtra"); | 
| 202 |  |  |  | 
| 203 |  |  | $this->set_element_value("Country1", "United States of America"); | 
| 204 |  |  |  | 
| 205 |  |  | $this->set_element_value("State1", "California"); | 
| 206 |  |  |  | 
| 207 |  |  |  | 
| 208 |  |  | } | 
| 209 |  |  |  | 
| 210 |  |  |  | 
| 211 |  |  | /** | 
| 212 |  |  | * This is the method that builds the layout of where the | 
| 213 |  |  | * FormElements will live.  You can lay it out any way | 
| 214 |  |  | * you like. | 
| 215 |  |  | * | 
| 216 |  |  | */ | 
| 217 |  |  | function form() { | 
| 218 |  |  | $table = &html_table($this->_width,0,4); | 
| 219 |  |  | $table->set_style("border: 1px solid"); | 
| 220 |  |  |  | 
| 221 |  |  | $table->add_row($this->element_label("Country"), | 
| 222 |  |  | $this->element_form("Country")); | 
| 223 |  |  |  | 
| 224 |  |  | $table->add_row($this->element_label("State"), | 
| 225 |  |  | $this->element_form("State")); | 
| 226 |  |  |  | 
| 227 |  |  | $table->add_row($this->element_label("Country1"), | 
| 228 |  |  | $this->element_form("Country1")); | 
| 229 |  |  |  | 
| 230 |  |  | $table->add_row($this->element_label("State1"), | 
| 231 |  |  | $this->element_form("State1")); | 
| 232 |  |  |  | 
| 233 |  |  |  | 
| 234 |  |  | $table->add_row(_HTML_SPACE, $this->add_action("Submit")); | 
| 235 |  |  |  | 
| 236 |  |  | return $table; | 
| 237 |  |  | } | 
| 238 |  |  |  | 
| 239 |  |  |  | 
| 240 |  |  | /** | 
| 241 |  |  | * This method is called ONLY after ALL validation has | 
| 242 |  |  | * passed.  This is the method that allows you to | 
| 243 |  |  | * do something with the data, say insert/update records | 
| 244 |  |  | * in the DB. | 
| 245 |  |  | */ | 
| 246 |  |  | function form_action() { | 
| 247 |  |  |  | 
| 248 |  |  | $this->set_action_message("Country :".$this->get_element_value("Country")." State ".$this->get_element_value("State")."Country1 :".$this->get_element_value("Country1")." State1 ".$this->get_element_value("State1")); | 
| 249 |  |  |  | 
| 250 |  |  | return TRUE; | 
| 251 |  |  | } | 
| 252 |  |  | } | 
| 253 |  |  |  | 
| 254 |  |  |  | 
| 255 |  |  | $page = new Form4Page("Master-Slave list example"); | 
| 256 |  |  | print $page->render(); | 
| 257 |  |  | ?> |