PageRenderTime 71ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/helpers/deprecated/FormOptionsHelper.php

http://github.com/phpwax/phpwax
PHP | 273 lines | 230 code | 21 blank | 22 comment | 45 complexity | 211ac77a2e777ec717dc1bda38304d78 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package PHP-Wax
  5. * @author Ross Riley
  6. **/
  7. /**
  8. * All the countries included in the country_options output.
  9. */
  10. if(!array_key_exists('COUNTRIES',$GLOBALS)) {
  11. $GLOBALS['COUNTRIES'] =
  12. array("Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla",
  13. "Antarctica", "Antigua And Barbuda", "Argentina", "Armenia", "Aruba", "Australia",
  14. "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus",
  15. "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina",
  16. "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
  17. "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burma", "Burundi", "Cambodia",
  18. "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic",
  19. "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia",
  20. "Comoros", "Congo", "Congo, the Democratic Republic of the", "Cook Islands",
  21. "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark",
  22. "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt",
  23. "El Salvador", "England", "Equatorial Guinea", "Eritrea", "Espana", "Estonia",
  24. "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France",
  25. "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia",
  26. "Georgia", "Germany", "Ghana", "Gibraltar", "Great Britain", "Greece", "Greenland",
  27. "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana",
  28. "Haiti", "Heard and Mc Donald Islands", "Honduras", "Hong Kong", "Hungary", "Iceland",
  29. "India", "Indonesia", "Ireland", "Israel", "Italy", "Iran", "Iraq", "Jamaica", "Japan", "Jordan",
  30. "Kazakhstan", "Kenya", "Kiribati", "Korea, Republic of", "Korea (South)", "Kuwait",
  31. "Kyrgyzstan", "Lao People's Democratic Republic", "Latvia", "Lebanon", "Lesotho",
  32. "Liberia", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia",
  33. "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
  34. "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico",
  35. "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia",
  36. "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal",
  37. "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua",
  38. "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Ireland",
  39. "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama",
  40. "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland",
  41. "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russia", "Rwanda",
  42. "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines",
  43. "Samoa (Indep}ent)", "San Marino", "Sao Tome and Principe", "Saudi Arabia",
  44. "Scotland", "Senegal", "Serbia and Montenegro", "Seychelles", "Sierra Leone", "Singapore",
  45. "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa",
  46. "South Georgia and the South Sandwich Islands", "South Korea", "Spain", "Sri Lanka",
  47. "St. Helena", "St. Pierre and Miquelon", "Suriname", "Svalbard and Jan Mayen Islands",
  48. "Swaziland", "Sweden", "Switzerland", "Taiwan", "Tajikistan", "Tanzania", "Thailand",
  49. "Togo", "Tokelau", "Tonga", "Trinidad", "Trinidad and Tobago", "Tunisia", "Turkey",
  50. "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine",
  51. "United Arab Emirates", "United Kingdom", "United States",
  52. "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu",
  53. "Vatican City State (Holy See)", "Venezuela", "Viet Nam", "Virgin Islands (British)",
  54. "Virgin Islands (U.S.)", "Wales", "Wallis and Futuna Islands", "Western Sahara",
  55. "Yemen", "Zambia", "Zimbabwe");
  56. }
  57. /**
  58. * @todo Document this class
  59. */
  60. class FormOptionsHelper extends FormHelper {
  61. public function options_for_select($choices, $selected = null) {
  62. $options = array();
  63. if(is_array($choices)) {
  64. foreach($choices as $choice_value => $choice_text) {
  65. $choice_text = str_replace("&nbsp;", "^", $choice_text);
  66. if(!is_null($choice_value)) {
  67. $is_selected = ($choice_value == $selected)
  68. ? true : false;
  69. } else {
  70. $is_selected = ($choice_text == $selected)
  71. ? true : false;
  72. }
  73. if($is_selected) {
  74. $options[] = "<option value=\""
  75. . htmlspecialchars($choice_value)
  76. . "\" selected=\"selected\">"
  77. . str_replace("^","&nbsp;", htmlspecialchars($choice_text))."</option>";
  78. } else {
  79. $options[] = "<option value=\""
  80. . htmlspecialchars($choice_value)
  81. . "\">"
  82. . str_replace("^", "&nbsp;",htmlspecialchars($choice_text))."</option>";
  83. }
  84. }
  85. }
  86. return implode("\n", $options);
  87. }
  88. /**
  89. * @todo Document this method
  90. * @uses add_default_name_and_id_and_value()
  91. * @uses add_options()
  92. * @uses content_tag()
  93. * @uses value()
  94. */
  95. public function select($obj, $att, $choices, $options=array(), $with_label=true) {
  96. $this->initialise($obj, $att);
  97. if(is_array($choices)) {
  98. $content = $this->options_for_select($choices, $this->object->{$this->attribute_name});
  99. } else $content = $choices;
  100. unset($options['value']);
  101. if(!$options["class"]) $options["class"]="input_field select_field";
  102. $options['name'] = $this->object_name . "[" . $this->attribute_name . "]" ;
  103. $options['id'] = $this->object_name . "_" . $this->attribute_name;
  104. if($with_label) $html.= $this->make_label($with_label);
  105. $html.= $this->content_tag("select",$content,$options);
  106. return $html;
  107. }
  108. public function country_select($obj, $att, $options = array(), $with_label=true) {
  109. $this->initialise($obj, $att);
  110. if(!$options["class"]) $options["class"]="input_field select_field";
  111. $options['name'] = $this->object_name . "[" . $this->attribute_name . "]" ;
  112. $options['id'] = $this->object_name . "_" . $this->attribute_name;
  113. if($with_label) $html.= $this->make_label($with_label);
  114. $html.= $this->options_for_select($GLOBALS['COUNTRIES'], $options);
  115. return $html;
  116. }
  117. /***************************
  118. * added extra '$indent' param
  119. * - pads out front of string with the value passed
  120. ***************************/
  121. public function options_from_collection($collection, $attribute_value, $attribute_text, $blank=null, $indent=false) {
  122. if($collection instanceOf Iterator) {
  123. if($blank) $array[0]=$blank;
  124. foreach($collection as $object) {
  125. if($indent && ($object->get_level()>0) ){
  126. $tmp = str_pad("", $object->get_level(), "*", STR_PAD_LEFT);
  127. $tmp = str_replace("*", $indent, $tmp);
  128. }
  129. else $tmp = "";
  130. $array[$object->{$attribute_value}] = $tmp . $object->{$attribute_text};
  131. }
  132. }elseif(is_array($collection) || $collection instanceOf ArrayAccess) {
  133. if($blank) $array[0]=$blank;
  134. foreach($collection as $object) {
  135. if($indent && ($object->get_level()>0) ){
  136. $tmp = str_pad("", $object->get_level()-1, "*", STR_PAD_LEFT);
  137. $tmp = str_replace("*", $indent, $tmp);
  138. }
  139. else $tmp = "";
  140. $array[$object->{$attribute_value}] = $tmp . $object->{$attribute_text};
  141. }
  142. }
  143. return $array;
  144. }
  145. public function date_select($obj, $att, $options = array(), $with_label=true) {
  146. $this->initialise($obj, $att);
  147. $shared_id = $this->object_name."_".$this->attribute_name;
  148. if(strlen($this->object->{$this->attribute_name})>3 && $this->object->{$this->attribute_name}!="0000-00-00") {
  149. $selected_day = substr($this->object->{$this->attribute_name}, 8,2);
  150. $selected_month = substr($this->object->{$this->attribute_name}, 5,2);
  151. $selected_year = substr($this->object->{$this->attribute_name}, 0,4);
  152. $date_markup = $this->make_date_select($shared_id, $selected_day, $selected_month, $selected_year);
  153. } else {
  154. $date_markup = $this->make_date_select($shared_id);
  155. }
  156. $output .= content_tag("script", "function {$shared_id}_set_date() {
  157. document.getElementById('$shared_id').value = document.getElementById('{$shared_id}_year').value + '-' + document.getElementById('{$shared_id}_month').value + '-' + document.getElementById('{$shared_id}_day').value;
  158. }");
  159. if($with_label) $output .= $this->make_label($with_label);
  160. $output.= content_tag("span", $date_markup, array("class"=>"multiple_date_select"));
  161. $output .= $this->hidden_field($obj, $att);
  162. $output .= content_tag("script","{$shared_id}_set_date()");
  163. return $output;
  164. }
  165. public function time_select($obj, $att, $options = array(), $with_label=true) {
  166. $this->initialise($obj, $att);
  167. $shared_id = $this->object_name."_".$this->attribute_name;
  168. if(strlen($this->object->{$this->attribute_name})>3) {
  169. $selected_hour = substr($this->object->{$this->attribute_name}, 0,2);
  170. $selected_minute = substr($this->object->{$this->attribute_name}, 3,2);
  171. $time_markup = $this->make_time_select($shared_id, $selected_hour, $selected_minute);
  172. } else {
  173. $time_markup = $this->make_time_select($shared_id);
  174. }
  175. $output .= content_tag("script","function {$shared_id}_set_date() {
  176. document.getElementById('$shared_id').value = document.getElementById('{$shared_id}_hour').value + ':' + document.getElementById('{$shared_id}_minute').value + ':00';
  177. }");
  178. if($with_label) $output .= $this->make_label($with_label);
  179. $output .= $time_markup;
  180. $output.= content_tag("span", $time_markup, array("class"=>"multiple_time_select"));
  181. $output .= $this->hidden_field($obj, $att);
  182. $output .= content_tag("script","{$shared_id}_set_date()");
  183. return $output;
  184. }
  185. public function datetime_select($obj, $att, $options = array(), $with_label=true) {
  186. $this->initialise($obj, $att);
  187. $shared_id = $this->object_name."_".$this->attribute_name;
  188. if(strlen($this->object->{$this->attribute_name})>3 && $this->object->{$this->attribute_name}!="0000-00-00") {
  189. $selected_day = substr($this->object->{$this->attribute_name}, 8,2);
  190. $selected_month = substr($this->object->{$this->attribute_name}, 5,2);
  191. $selected_year = substr($this->object->{$this->attribute_name}, 0,4);
  192. $selected_hour = substr($this->object->{$this->attribute_name}, 11,2);
  193. $selected_minute = substr($this->object->{$this->attribute_name}, 14,2);
  194. $datetime_markup = $this->make_date_select($shared_id, $selected_day, $selected_month, $selected_year);
  195. $datetime_markup .= $this->make_time_select($shared_id, $selected_hour, $selected_minute);
  196. } else {
  197. $datetime_markup = $this->make_date_select($shared_id)."&nbsp;&nbsp;&nbsp;";
  198. $datetime_markup .= $this->make_time_select($shared_id);
  199. }
  200. $output .= content_tag("script","function {$shared_id}_set_date() {
  201. document.getElementById('$shared_id').value = document.getElementById('{$shared_id}_year').value +
  202. '-' + document.getElementById('{$shared_id}_month').value + '-' + document.getElementById('{$shared_id}_day').value
  203. + ' ' + document.getElementById('{$shared_id}_hour').value + ':' + document.getElementById('{$shared_id}_minute').value+':00';
  204. }");
  205. if($with_label) $output .= $this->make_label($with_label);
  206. $output.= content_tag("span", $datetime_markup, array("class"=>"multiple_datetime_select"));
  207. $output .= $this->hidden_field($obj, $att);
  208. $output .= content_tag("script","{$shared_id}_set_date()");
  209. return $output;
  210. }
  211. protected function make_date_select($shared_id, $selected_day=false, $selected_month=false, $selected_year=false) {
  212. for($i = 1; $i<=31; $i++) {
  213. $i = str_pad($i, 2, "0", STR_PAD_LEFT);
  214. $day[$i]=$i;
  215. }
  216. for($i = 1; $i<=12; $i++) {
  217. $i = str_pad($i, 2, "0", STR_PAD_LEFT);
  218. $month[$i]=$i;
  219. }
  220. for($i = 1900; $i<=(int)date('Y')+10; $i++) {
  221. $year[$i]=$i;
  222. }
  223. if(!$selected_day) $selected_day = date('d');
  224. if(!$selected_month) $selected_month = date('m');
  225. if(!$selected_year) $selected_year = date('Y');
  226. $day_options = FormOptionsHelper::options_for_select($day, $selected_day);
  227. $month_options = FormOptionsHelper::options_for_select($month, $selected_month);
  228. $year_options = FormOptionsHelper::options_for_select($year, $selected_year);
  229. $output .= $this->content_tag("select", $day_options, array("id"=>$shared_id."_day","name"=>$shared_id."_day", "onchange"=>"{$shared_id}_set_date();"));
  230. $output .= $this->content_tag("select", $month_options, array("id"=>$shared_id."_month","name"=>$shared_id."_month", "onchange"=>"{$shared_id}_set_date();"));
  231. $output .= $this->content_tag("select", $year_options, array("id"=>$shared_id."_year","name"=>$shared_id."_year", "onchange"=>"{$shared_id}_set_date();","class"=>"date_select_year"));
  232. return $output;
  233. }
  234. protected function make_time_select($shared_id, $selected_hour=false, $selected_minute=false) {
  235. for($i = 0; $i<=23; $i++) {
  236. $i = str_pad($i, 2, "0", STR_PAD_LEFT);
  237. $hour[$i]=$i;
  238. }
  239. for($i = 0; $i<=59; $i++) {
  240. $i = str_pad($i, 2, "0", STR_PAD_LEFT);
  241. $minute[$i]=$i;
  242. }
  243. if(!$selected_hour) $selected_hour = date('G');
  244. if(!$selected_minute) $selected_minute = date('i');
  245. $hour_options = FormOptionsHelper::options_for_select($hour, $selected_hour);
  246. $minute_options = FormOptionsHelper::options_for_select($minute, $selected_minute);
  247. $output .= $this->content_tag("select", $hour_options, array("id"=>$shared_id."_hour","name"=>$shared_id."_hour", "onchange"=>"{$shared_id}_set_date();"));
  248. $output .= $this->content_tag("select", $minute_options, array("id"=>$shared_id."_minute","name"=>$shared_id."_minute", "onchange"=>"{$shared_id}_set_date();"));
  249. return $output;
  250. }
  251. }
  252. ?>