/[drupal]/contributions/modules/helpers/helpers_form.module
ViewVC logotype

Contents of /contributions/modules/helpers/helpers_form.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Wed Feb 21 10:45:29 2007 UTC (2 years, 9 months ago) by ber
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +31 -1 lines
File MIME type: text/x-php
#72025 by robertDouglass
Introducing another function, a form item to bind any setting to a 'vocabulary'
1 <?php
2 // $Id: helpers_form.module,v 1.1 2006/07/16 16:26:32 ber Exp $
3
4 /**
5 * @file
6 * This is a library of usefull form functions and methods.
7 * please insert the link if you have a patch for core to introduce a function from this lib to core.
8 */
9
10 /**
11 * Implementation of hook_help().
12 */
13 function helpers_form_help($section) {
14 switch ($section) {
15 case 'admin/modules#description':
16 return t('A development library for the database. Contains useful functions and methods for form building.');
17 }
18 }
19
20 /**
21 * Form functions
22 */
23 /**
24 * returns a form-structure filled with countries. usefull in a select.
25 * @param $title, the title of the form. Defaults to "coutries" (translated). If provided, it will not be translated
26 * @param $default_value, the selected country.
27 * @param $required, whether or not a "none" string should be included in the options.
28 * @param $description, optionally provide a description. Is not translated.
29 */
30 function countries_select_for_form($title = '', $default_value = NULL, $required = FALSE, $description = '') {
31 if ($required) {
32 $options = _helpers_data_countries_array();
33 }
34 else {
35 $options = add_none_to_options(_helpers_data_countries_array());
36 }
37
38 return array(
39 '#type' => 'select',
40 '#title' => $title ? $title : t('countries'),
41 '#default_value' => $default_value,
42 '#options' => $options,
43 '#description' => $description,
44 );
45 }
46
47 /**
48 * returns a form-structure filled with US states, usefull in a select.
49 * @param $title, the title of the form. Defaults to "coutries" (translated). If provided, it will not be translated
50 * @param $default_value, the selected state.
51 * @param $required, whether or not a "none" string should be included in the options.
52 * @param $description, optionally provide a description. Is not translated.
53 */
54 function states_select_for_form($title = '', $default_value = NULL, $required = FALSE, $description = '') {
55 if ($required) {
56 $options = _helpers_data_us_states_array();
57 }
58 else {
59 $options = add_none_to_options(_helpers_data_us_states_array());
60 }
61
62 return array(
63 '#type' => 'select',
64 '#title' => $title ? $title : t('states'),
65 '#default_value' => $default_value,
66 '#options' => $options,
67 '#description' => $description,
68 );
69 }
70
71 /**
72 * This would be a very nice function to have in core
73 *
74 * @param string $variable_name
75 * the name of the variable that is to be bound to a vocabulary
76 * @param bool $multiple
77 * whether or not multiple vocabularies should be bound to this vocabulary
78 * @return array
79 * a form array suitable for use in hook_settings
80 */
81 function taxonomy_vocabulary_select($variable_name, $multiple = FALSE) {
82 $form[$variable_name] = array(
83 '#title' => t('Vocabularies associated with %variable', array('%variable' => $variable_name)),
84 '#default_value' => variable_get($variable_name, array()),
85 '#description' => t('These vocabularies will be bound to the variable %variable. You can get this list of vocabulary ids later by using <code>variable_get(&quot;%variable&quot;, array());</code>.', array('%variable' => $variable_name, '%variable' => $variable_name)),
86 );
87
88 $form[$variable_name]['#type'] = $multiple ? 'checkboxes' : 'radios';
89
90
91 $options = array();
92 foreach(taxonomy_get_vocabularies() as $vid => $vocabulary) {
93 $options[$vid] = $vocabulary->name;
94 }
95
96 $form[$variable_name]['#options'] = $options;
97
98 return $form;
99 }
100
101 /**
102 * Global and general functions
103 * @param $options the options that you want to append "None" to.
104 * @param $none_string optionally provide a string that will be the "none". If you provide this, it will not be translated and not passed trough the theme layer.
105 */
106 function add_none_to_options($options, $none_string = NULL) {
107 if ($none_string) {
108 return array('' => $none_string) + $options;
109 }
110 else {
111 return array('' => theme('none_option')) + $options;
112 }
113 }
114
115
116 /**
117 * Helper helpers. Private functions that are used internally
118 */
119 /**
120 * Returns a list of countries.
121 * TODO make this a proper associative array with the official country code as key.
122 */
123 function _helpers_data_countries_array($translated = TRUE) {
124 $countries = array('Afghanistan' => 'Afghanistan', 'Albania' => 'Albania', 'Algeria' => 'Algeria', 'American Samoa' => 'American Samoa', 'Andorra' => 'Andorra', 'Angola' => 'Angola', 'Anguilla' => 'Anguilla', 'Antarctica' => 'Antarctica', 'Antigua And Barbuda' => 'Antigua And Barbuda', 'Argentina' => 'Argentina', 'Armenia' => 'Armenia', 'Aruba' => 'Aruba', 'Australia' => 'Australia', 'Austria' => 'Austria', 'Azerbaijan' => 'Azerbaijan', 'Bahamas' => 'Bahamas', 'Bahrain' => 'Bahrain', 'Bangladesh' => 'Bangladesh', 'Barbados' => 'Barbados', 'Belarus' => 'Belarus', 'Belgium' => 'Belgium', 'Belize' => 'Belize', 'Benin' => 'Benin', 'Bermuda' => 'Bermuda', 'Bhutan' => 'Bhutan', 'Bolivia' => 'Bolivia', 'Bosnia and Herzegowina' => 'Bosnia and Herzegowina', 'Botswana' => 'Botswana', 'Bouvet Island' => 'Bouvet Island', 'Brazil' => 'Brazil', 'British Indian Ocean Territory' => 'British Indian Ocean Territory', 'Brunei Darussalam' => 'Brunei Darussalam', 'Bulgaria' => 'Bulgaria', 'Burkina Faso' => 'Burkina Faso', 'Burma' => 'Burma', 'Burundi' => 'Burundi', 'Cambodia' => 'Cambodia', 'Cameroon' => 'Cameroon', 'Canada' => 'Canada', 'Cape Verde' => 'Cape Verde', 'Cayman Islands' => 'Cayman Islands', 'Central African Republic' => 'Central African Republic', 'Chad' => 'Chad', 'Chile' => 'Chile', 'China' => 'China', 'Christmas Island' => 'Christmas Island', 'Cocos (Keeling) Islands' => 'Cocos (Keeling) Islands', 'Colombia' => 'Colombia', 'Comoros' => 'Comoros', 'Congo' => 'Congo', 'Congo, the Democratic Republic of the', 'Cook Islands' => 'Cook Islands', 'Costa Rica' => 'Costa Rica', 'Cote d\'Ivoire' => 'Cote d\'Ivoire', 'Croatia' => 'Croatia', 'Cuba' => 'Cuba', 'Cyprus' => 'Cyprus', 'Czech Republic' => 'Czech Republic', 'Denmark' => 'Denmark', 'Djibouti' => 'Djibouti', 'Dominica' => 'Dominica', 'Dominican Republic' => 'Dominican Republic', 'East Timor' => 'East Timor', 'Ecuador' => 'Ecuador', 'Egypt' => 'Egypt', 'El Salvador' => 'El Salvador', 'England' => 'England', 'Equatorial Guinea' => 'Equatorial Guinea', 'Eritrea' => 'Eritrea', 'Espana' => 'Espana', 'Estonia' => 'Estonia', 'Ethiopia' => 'Ethiopia', 'Falkland Islands' => 'Falkland Islands', 'Faroe Islands' => 'Faroe Islands', 'Fiji' => 'Fiji', 'Finland' => 'Finland', 'France' => 'France', 'French Guiana' => 'French Guiana', 'French Polynesia' => 'French Polynesia', 'French Southern Territories' => 'French Southern Territories', 'Gabon' => 'Gabon', 'Gambia' => 'Gambia', 'Georgia' => 'Georgia', 'Germany' => 'Germany', 'Ghana' => 'Ghana', 'Gibraltar' => 'Gibraltar', 'Great Britain' => 'Great Britain', 'Greece' => 'Greece', 'Greenland' => 'Greenland', 'Grenada' => 'Grenada', 'Guadeloupe' => 'Guadeloupe', 'Guam' => 'Guam', 'Guatemala' => 'Guatemala', 'Guinea' => 'Guinea', 'Guinea-Bissau' => 'Guinea-Bissau', 'Guyana' => 'Guyana', 'Haiti' => 'Haiti', 'Heard and Mc Donald Islands' => 'Heard and Mc Donald Islands', 'Honduras' => 'Honduras', 'Hong Kong' => 'Hong Kong', 'Hungary' => 'Hungary', 'Iceland' => 'Iceland', 'India' => 'India', 'Indonesia' => 'Indonesia', 'Ireland' => 'Ireland', 'Israel' => 'Israel', 'Italy' => 'Italy', 'Iran' => 'Iran', 'Iraq' => 'Iraq', 'Jamaica' => 'Jamaica', 'Japan' => 'Japan', 'Jordan' => 'Jordan', 'Kazakhstan' => 'Kazakhstan', 'Kenya' => 'Kenya', 'Kiribati' => 'Kiribati', 'Korea, Republic of', 'Korea (South)' => 'Korea (South)', 'Kuwait' => 'Kuwait', 'Kyrgyzstan' => 'Kyrgyzstan', 'Lao People\'s Democratic Republic' => 'Lao People\'s Democratic Republic', 'Latvia' => 'Latvia', 'Lebanon' => 'Lebanon', 'Lesotho' => 'Lesotho', 'Liberia' => 'Liberia', 'Liechtenstein' => 'Liechtenstein', 'Lithuania' => 'Lithuania', 'Luxembourg' => 'Luxembourg', 'Macau' => 'Macau', 'Macedonia' => 'Macedonia', 'Madagascar' => 'Madagascar', 'Malawi' => 'Malawi', 'Malaysia' => 'Malaysia', 'Maldives' => 'Maldives', 'Mali' => 'Mali', 'Malta' => 'Malta', 'Marshall Islands' => 'Marshall Islands', 'Martinique' => 'Martinique', 'Mauritania' => 'Mauritania', 'Mauritius' => 'Mauritius', 'Mayotte' => 'Mayotte', 'Mexico' => 'Mexico', 'Micronesia, Federated States of', 'Moldova, Republic of', 'Monaco' => 'Monaco', 'Mongolia' => 'Mongolia', 'Montserrat' => 'Montserrat', 'Morocco' => 'Morocco', 'Mozambique' => 'Mozambique', 'Myanmar' => 'Myanmar', 'Namibia' => 'Namibia', 'Nauru' => 'Nauru', 'Nepal' => 'Nepal', 'Netherlands' => 'Netherlands', 'Netherlands Antilles' => 'Netherlands Antilles', 'New Caledonia' => 'New Caledonia', 'New Zealand' => 'New Zealand', 'Nicaragua' => 'Nicaragua', 'Niger' => 'Niger', 'Nigeria' => 'Nigeria', 'Niue' => 'Niue', 'Norfolk Island' => 'Norfolk Island', 'Northern Ireland' => 'Northern Ireland', 'Northern Mariana Islands' => 'Northern Mariana Islands', 'Norway' => 'Norway', 'Oman' => 'Oman', 'Pakistan' => 'Pakistan', 'Palau' => 'Palau', 'Panama' => 'Panama', 'Papua New Guinea' => 'Papua New Guinea', 'Paraguay' => 'Paraguay', 'Peru' => 'Peru', 'Philippines' => 'Philippines', 'Pitcairn' => 'Pitcairn', 'Poland' => 'Poland', 'Portugal' => 'Portugal', 'Puerto Rico' => 'Puerto Rico', 'Qatar' => 'Qatar', 'Reunion' => 'Reunion', 'Romania' => 'Romania', 'Russia' => 'Russia', 'Rwanda' => 'Rwanda', 'Saint Kitts and Nevis' => 'Saint Kitts and Nevis', 'Saint Lucia' => 'Saint Lucia', 'Saint Vincent and the Grenadines' => 'Saint Vincent and the Grenadines', 'Samoa (Independent)' => 'Samoa (Independent)', 'San Marino' => 'San Marino', 'Sao Tome and Principe' => 'Sao Tome and Principe', 'Saudi Arabia' => 'Saudi Arabia', 'Scotland' => 'Scotland', 'Senegal' => 'Senegal', 'Serbia and Montenegro' => 'Serbia and Montenegro', 'Seychelles' => 'Seychelles', 'Sierra Leone' => 'Sierra Leone', 'Singapore' => 'Singapore', 'Slovakia' => 'Slovakia', 'Slovenia' => 'Slovenia', 'Solomon Islands' => 'Solomon Islands', 'Somalia' => 'Somalia', 'South Africa' => 'South Africa', 'South Georgia and the South Sandwich Islands' => 'South Georgia and the South Sandwich Islands', 'South Korea' => 'South Korea', 'Spain' => 'Spain', 'Sri Lanka' => 'Sri Lanka', 'St. Helena' => 'St. Helena', 'St. Pierre and Miquelon' => 'St. Pierre and Miquelon', 'Suriname' => 'Suriname', 'Svalbard and Jan Mayen Islands' => 'Svalbard and Jan Mayen Islands', 'Swaziland' => 'Swaziland', 'Sweden' => 'Sweden', 'Switzerland' => 'Switzerland', 'Taiwan' => 'Taiwan', 'Tajikistan' => 'Tajikistan', 'Tanzania' => 'Tanzania', 'Thailand' => 'Thailand', 'Togo' => 'Togo', 'Tokelau' => 'Tokelau', 'Tonga' => 'Tonga', 'Trinidad' => 'Trinidad', 'Trinidad and Tobago' => 'Trinidad and Tobago', 'Tunisia' => 'Tunisia', 'Turkey' => 'Turkey', 'Turkmenistan' => 'Turkmenistan', 'Turks and Caicos Islands' => 'Turks and Caicos Islands', 'Tuvalu' => 'Tuvalu', 'Uganda' => 'Uganda', 'Ukraine' => 'Ukraine', 'United Arab Emirates' => 'United Arab Emirates', 'United Kingdom' => 'United Kingdom', 'United States' => 'United States', 'United States Minor Outlying Islands' => 'United States Minor Outlying Islands', 'Uruguay' => 'Uruguay', 'Uzbekistan' => 'Uzbekistan', 'Vanuatu' => 'Vanuatu', 'Vatican City State (Holy See)' => 'Vatican City State (Holy See)', 'Venezuela' => 'Venezuela', 'Viet Nam' => 'Viet Nam', 'Virgin Islands (British)' => 'Virgin Islands (British)', 'Virgin Islands (U.S.)' => 'Virgin Islands (U.S.)', 'Wales' => 'Wales', 'Wallis and Futuna Islands' => 'Wallis and Futuna Islands', 'Western Sahara' => 'Western Sahara', 'Yemen' => 'Yemen', 'Zambia' => 'Zambia', 'Zimbabwe' => 'Zimbabwe');
125
126 if ($translated) {
127 foreach ($countries as $key => $value) {
128 $countries[$key] = t($value);
129 }
130 }
131 natsort($countries);
132 return $countries;
133 }
134
135 function _helpers_data_us_states_array($translated = TRUE) {
136 $states = array(
137 'AL' => 'Alabama', 'AK' => 'Alaska', 'AZ' => 'Arizona', 'AR' => 'Arkansas', 'CA'=> 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DC' => 'D.C.', 'DE' => 'Delaware', 'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID'=> 'Idaho', 'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' =>'Kansas', 'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine', 'MD' =>'Maryland', 'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi', 'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada', 'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'NewMexico', 'NY' => 'New York', 'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma', 'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina', 'SD' => 'South Dakota', 'TN' =>'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah', 'VT' => 'Vermont', 'VA' =>'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia', 'WI' => 'Wisconsin', 'WY' => 'Wyoming',
138 );
139 if ($translated) {
140 foreach ($states as $key => $value) {
141 $states[$key] = t($value);
142 }
143 }
144 natsort($states);
145 return $states;
146 }

  ViewVC Help
Powered by ViewVC 1.1.2