-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontactinfo.module
314 lines (287 loc) · 9.58 KB
/
contactinfo.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* @file
* Collects contact information and displays it in an hCard block.
*/
/**
* Implements hook_help().
*/
function contactinfo_help($path, $arg) {
switch ($path) {
case 'admin/help#contactinfo':
$output = '<p>' . t('Contact information that you provide will be displayed on the site in the <a href="http://microformats.org/wiki/hcard">hCard microformat</a>. An hCard is a small bundle of code that you want to put on your web site so that Google Maps (and other mapping services) can more easily index the site’s location information.') . '</p>';
return $output;
case 'admin/config/system/contactinfo':
return '<p>' . t("Enter your site’s contact information into the appropriate fields. All fields are optional.") . '</p>';
}
}
/**
* Implements hook_permission().
*/
function contactinfo_permission() {
return array(
'administer contactinfo' => array(
'title' => t('Administer contact information'),
'description' => t('Edit contact information for this site.'),
),
);
}
/**
* Implements hook_menu().
*/
function contactinfo_menu() {
$items['admin/config/system/contactinfo'] = array(
'title' => 'Contact information',
'description' => 'Configure contact information that is publicly displayed on this site.',
'page callback' => 'drupal_get_form',
'page arguments' => array('contactinfo_admin_settings'),
'access arguments' => array('administer contactinfo'),
'file' => 'contactinfo.form.inc',
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
return $items;
}
/**
* Implements hook_theme().
*/
function contactinfo_theme($existing, $type, $theme, $path) {
return array(
'contactinfo_admin_settings' => array(
'render element' => 'form',
),
'contactinfo' => array(
'variables' => array(
'contactinfo' => NULL,
'type' => 'personal',
'given_name' => NULL,
'family_name' => NULL,
'org' => NULL,
'tagline' => NULL,
'street_address' => NULL,
'extended_address' => NULL,
'locality' => NULL,
'region' => NULL,
'postal_code' => NULL,
'country' => NULL,
'longitude' => NULL,
'latitude' => NULL,
'phones' => array(),
'faxes' => array(),
'email' => NULL,
),
'template' => 'contactinfo',
),
);
}
/**
* Theme preprocess function for the contact information block.
*
* @param array $variables
* Template variables as defined by contactinfo_theme().
* $variable['contactinfo'] is equivalent to contactinfo_get_contactinfo().
*
* @see contactinfo_theme()
* @see contactinfo_get_contactinfo()
*/
function template_preprocess_contactinfo(array &$variables) {
drupal_add_css(drupal_get_path('module', 'contactinfo') . '/css/contactinfo.css');
// Build $variables from scratch.
$contactinfo = $variables['contactinfo'];
$variables['type'] = $contactinfo['type'];
$variables['given_name'] = check_plain($contactinfo['fn_n']['given-name']);
$variables['family_name'] = check_plain($contactinfo['fn_n']['family-name']);
$variables['org'] = $contactinfo['use_site_name'] ? check_plain(variable_get('site_name', '')) : check_plain($contactinfo['org']);
$variables['street_address'] = check_plain($contactinfo['adr']['street-address']);
$variables['extended_address'] = check_plain($contactinfo['adr']['extended-address']);
$variables['locality'] = check_plain($contactinfo['adr']['locality']);
$variables['region'] = check_plain($contactinfo['adr']['region']);
$variables['postal_code'] = check_plain($contactinfo['adr']['postal-code']);
$variables['country'] = check_plain($contactinfo['adr']['country-name']);
$variables['longitude'] = check_plain($contactinfo['location']['longitude']);
$variables['latitude'] = check_plain($contactinfo['location']['latitude']);
$variables['tagline'] = $contactinfo['use_site_slogan'] ? check_plain(variable_get('site_slogan', '')) : check_plain($contactinfo['tagline']);
// Generate formatted longitude and latitude.
$variables['longitude_formatted'] = contactinfo_coord_convert($variables['longitude'], 'longitude');
$variables['latitude_formatted'] = contactinfo_coord_convert($variables['latitude'], 'latitude');
// Generates the output for the 'phones' variable.
if ($contactinfo['phone']['voice']) {
$phone_text = check_plain($contactinfo['phone']['voice']);
$phones = explode(',', $phone_text);
$variables['phones'] = array_map('trim', $phones);
}
// Generates the output for the 'faxes' variable.
if ($contactinfo['phone']['fax']) {
$fax_text = check_plain($contactinfo['phone']['fax']);
$faxes = explode(',', $fax_text);
$variables['faxes'] = array_map('trim', $faxes);
}
// Generate the output for the 'email' variable.
if ($contactinfo['email']) {
$email = check_plain($contactinfo['email']);
// Use obfuscation provided by invisimail module.
if (function_exists('invisimail_encode_html')) {
$variables['email'] = invisimail_encode_html($email);
$variables['email_url'] = INVISIMAIL_MAILTO_ASCII . $variables['email'];
}
else {
$variables['email'] = $email;
$variables['email_url'] = 'mailto:' . $email;
}
}
// Generate ID.
$id = 'contactinfo';
if ($contactinfo['type'] == 'personal') {
$id .= !empty($contactinfo['fn_n']['given-name']) ? '-' . check_plain($contactinfo['fn_n']['given-name']) : '';
$id .= !empty($contactinfo['fn_n']['family-name']) ? '-' . check_plain($contactinfo['fn_n']['family-name']) : '';
}
else {
$id .= !empty($contactinfo['org']) ? '-' . check_plain($contactinfo['org']) : '';
}
$variables['id'] = drupal_html_id($id);
}
/**
* Validate an email address.
*/
function contactinfo_validate_email($element, &$form_state) {
if (!empty($element['#value']) && !valid_email_address($element['#value'])) {
form_error($element, t('You must enter a valid e-mail address.'));
}
}
/**
* Implements hook_block_info().
*/
function contactinfo_block_info() {
$blocks['hcard'] = array(
'info' => 'Contact information',
'weight' => 10,
'status' => 1,
'region' => 'footer',
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function contactinfo_block_view($delta) {
$block = array();
switch ($delta) {
case 'hcard':
$contactinfo = contactinfo_get_contactinfo();
if ($contactinfo) {
$block = array(
'subject' => '',
'content' => theme('contactinfo', array('contactinfo' => $contactinfo)),
);
}
return $block;
}
}
/**
* Theme function for the Contact Info settings form.
*
* It's just a wrapper so that themers can do what they want with this form.
*/
function theme_contactinfo_admin_settings($variables) {
$form = $variables['form'];
return drupal_render_children($form);
}
/**
* Implements hook_contextual_links_view_alter().
*/
function contactinfo_contextual_links_view_alter(array &$element, array $items) {
$block = isset($element['#element']['#block']) ? $element['#element']['#block'] : NULL;
if (is_object($block) && $block->module == 'contactinfo' && user_access('administer contactinfo')) {
$element['#links']['contactinfo-edit'] = array(
'title' => t('Edit information'),
'href' => 'admin/config/system/contactinfo',
'query' => drupal_get_destination(),
'attributes' => array(
'title' => t('Edit your contact information'),
),
);
}
}
/**
* Helper function to convert longitude or latitude points.
*
* Convert a decimal-degree longitude or latitude point into degrees and
* decimal minutes.
*
* @param float $decimal
* Decimal value for a longitude or latitude point.
* @param string $direction
* Strings 'longitude' or 'latitude' are the only acceptable inputs.
*
* @return string
* String containing a single character for N, S, E, or W, the degrees as
* whole number, and minutes as a decimal value.
*/
function contactinfo_coord_convert($decimal, $direction) {
$decimal = floatval($decimal);
if (!$decimal) {
return FALSE;
}
switch ($direction) {
case 'longitude':
$coord_direction = ($decimal < 0) ? 'W' : 'E';
break;
case 'latitude':
$coord_direction = ($decimal < 0) ? 'S' : 'N';
break;
default:
return FALSE;
}
$coord_degrees = intval($decimal);
$coord_minutes = abs(fmod($decimal, 1) * 60);
return $coord_direction . ' ' . $coord_degrees . '° ' . $coord_minutes . '"';
}
/**
* Helper function to return saved contact information.
*
* @return array
* All contact information saved from the settings form.
*/
function contactinfo_get_contactinfo() {
// Get variable defaults.
$default = contactinfo_defaults();
// Get saved contact information.
$contactinfo = variable_get('contactinfo', $default);
// Merge default values and saved data to ensure all array keys are present.
return array_merge($default, $contactinfo);
}
/**
* Returns the default values for the site contact information.
*
* @return array
* Default values for all contact info keys.
*/
function contactinfo_defaults() {
return array(
'type' => 'personal',
'fn_n' => array(
'given-name' => '',
'family-name' => '',
),
'org' => '',
'use_site_name' => 0,
'tagline' => '',
'use_site_slogan' => 0,
'adr' => array(
'street-address' => '',
'extended-address' => '',
'locality' => '',
'region' => '',
'postal-code' => '',
'country-name' => '',
),
'location' => array(
'longitude' => '',
'latitude' => '',
),
'phone' => array(
'voice' => '',
'fax' => '',
),
'email' => '',
);
}