forked from ding2/ding_base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ding_base.module
302 lines (275 loc) · 9.32 KB
/
ding_base.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
<?php
/**
* @file
* Base setup and functions for Ding!.
*/
/**
* Implements hook_menu().
*
* Provides the Configuration page block for all Ding modules.
*/
function ding_base_menu() {
$items['admin/config/ding'] = array(
'title' => 'Ding',
'description' => 'Manage Ding.',
'position' => 'right',
'weight' => 20,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
return $items;
}
/**
* Implements hook_theme().
*/
function ding_base_theme() {
return array(
'tableselect_form' => array(
'render element' => 'element',
),
'body_snippet' => array(
'variables' => array('element' => NULL),
),
);
}
/**
* Implements s a theme function for body_snippet.
*/
function theme_body_snippet($element) {
$element_string = strip_tags($element['element']['value']);
$element_string = preg_replace('/\[\[.*\]\]/', '', $element_string);
$element_string = html_entity_decode($element_string, ENT_NOQUOTES, 'utf-8');
$element_string = drupal_substr($element_string, 0, $element['display']['settings']['body_snippet_length']);
return $element_string;
}
/**
* Implements hook_field_formatter_info().
*/
function ding_base_field_formatter_info() {
return array(
'body_snippet' => array(
'label' => t('Show plaintext part of string'),
'field types' => array('text', 'text_long'),
'settings' => array('body_snippet_length' => 100),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function ding_base_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$elements = array();
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#markup' => theme('body_snippet', array('element' => $item, 'field' => $instance, 'display' => $display)),
);
}
return $elements;
}
function ding_base_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'body_snippet') {
$element['body_snippet_length'] = array(
'#title' => t('Trim length'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $settings['body_snippet_length'],
'#element_validate' => array('_element_validate_integer_positive'),
'#required' => TRUE,
);
}
return $element;
}
function ding_base_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
if ($display['type'] == 'body_snippet') {
$summary = t('Trim length') . ': ' . $settings['body_snippet_length'];
}
return $summary;
}
/**
* Implements hook_date_format_types().
*/
function ding_base_date_format_types() {
return array(
'date_only' => t('Date only'),
);
}
/**
* Implements hook_element_info().
*/
function ding_base_element_info() {
$types['tableselect_form'] = array(
'#input' => TRUE,
'#js_select' => TRUE,
'#multiple' => TRUE,
'#process' => array('ding_base_element_process_tableselect_form'),
'#options' => array(),
'#empty' => '',
'#theme' => 'tableselect_form',
);
return $types;
}
/**
* Process function for our element.
*/
function ding_base_element_process_tableselect_form($element) {
if ($element['#multiple']) {
$value = is_array($element['#value']) ? $element['#value'] : array();
}
else {
// Advanced selection behaviour make no sense for radios.
$element['#js_select'] = FALSE;
}
$element['#tree'] = TRUE;
if (count($element['#options']) > 0) {
if (!isset($element['#default_value']) || $element['#default_value'] === 0) {
$element['#default_value'] = array();
}
// Create a checkbox or radio for each item in #options in such a way that
// the value of the tableselect element behaves as if it had been of type
// checkboxes or radios.
foreach ($element['#options'] as $key => $choice) {
// Do not overwrite manually created children.
if (!isset($element[$key])) {
if ($element['#multiple']) {
$title = '';
if (!empty($element['#options'][$key]['title']['data']['#title'])) {
$title = t('Update @title', array(
'@title' => $element['#options'][$key]['title']['data']['#title'],
));
}
$element[$key] = array(
'#type' => 'checkbox',
'#title' => $title,
'#title_display' => 'invisible',
'#return_value' => $key,
'#default_value' => isset($value[$key]) ? $key : NULL,
'#attributes' => $element['#attributes'],
);
}
else {
// Generate the parents as the autogenerator does, so we will have a
// unique id for each radio button.
$parents_for_id = array_merge($element['#parents'], array($key));
$element[$key] = array(
'#type' => 'radio',
'#title' => '',
'#return_value' => $key,
'#default_value' => ($element['#default_value'] == $key) ? $key : NULL,
'#attributes' => $element['#attributes'],
'#parents' => $element['#parents'],
'#id' => drupal_html_id('edit-' . implode('-', $parents_for_id)),
'#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
);
}
if (isset($element['#options'][$key]['#weight'])) {
$element[$key]['#weight'] = $element['#options'][$key]['#weight'];
}
// Add a reference to cell content in a hidden child of this
// element. This means that form API will see them, and do the
// appropriate processing.
foreach ($element['#header'] as $fieldname => $title) {
if (isset($element['#options'][$key][$fieldname]['data']) && is_array($element['#options'][$key][$fieldname]['data'])) {
$element['_form_options'][$key][$fieldname] =& $element['#options'][$key][$fieldname]['data'];
}
elseif (!isset($element['#options'][$key][$fieldname]['data']) && is_array($element['#options'][$key][$fieldname])) {
$element['_form_options'][$key][$fieldname] =& $element['#options'][$key][$fieldname];
}
}
}
}
}
else {
$element['#value'] = array();
}
return $element;
}
/**
* Theme function for our custom element.
*/
function theme_tableselect_form($variables) {
$element = $variables['element'];
$rows = array();
$header = $element['#header'];
if (!empty($element['#options'])) {
// Generate a table row for each selectable item in #options.
foreach (element_children($element) as $key) {
if ($key == '_form_options') {
// Skip our hidden render sub-array.
continue;
}
$row = array();
$row['data'] = array();
if (isset($element['#options'][$key]['#attributes'])) {
$row += $element['#options'][$key]['#attributes'];
}
// Render the checkbox / radio element.
$row['data'][] = drupal_render($element[$key]);
// As theme_table only maps header and row columns by order, create the
// correct order by iterating over the header fields.
foreach ($element['#header'] as $fieldname => $title) {
$row['data'][] = $element['#options'][$key][$fieldname];
}
$rows[] = $row;
}
// Add an empty header or a "Select all" checkbox to provide room for the
// checkboxes/radios in the first table column.
if ($element['#js_select']) {
// Add a "Select all" checkbox.
drupal_add_js('misc/tableselect.js');
array_unshift($header, array('class' => array('select-all')));
}
else {
// Add an empty header when radio buttons are displayed or a "Select all"
// checkbox is not desired.
array_unshift($header, '');
}
}
return theme('table', array('header' => $header, 'rows' => $rows, 'empty' => $element['#empty'], 'attributes' => $element['#attributes']));
}
/**
* Validate an email.
*
* This check uses Drupals valid_email_addess(), and makes sure the domain used
* is a valid top level domain.
*
* @param string $email
* The email to validate.
*
* @return bool
* TRUE if the address is in a valid format.
*/
function ding_base_validate_email($email) {
// If < and > are used, set the email to the data encapsulated by these
// characters.
if (preg_match('/<([^>]*)>/', $email, $matches)) {
$email = $matches[1];
}
if (!valid_email_address($email)) {
return FALSE;
}
$tld_list = array();
if ($cache = cache_get('ding_base:tld_list')) {
$tld_list = $cache->data;
}
else {
// We assume iana will keep the list of tlds in the same format and on the
// same URL. It's been the same URL and format since 2004.
// If the response fails the $tld_list will be empty and we'll simply skip
// the domain check.
$response = drupal_http_request('http://data.iana.org/TLD/tlds-alpha-by-domain.txt');
if ($response->code == 200) {
$tld_list = explode("\n", $response->data);
cache_set('ding_base:tld_list', $tld_list);
}
}
$domain = strtoupper(substr($email, strrpos($email, '.') + 1));
return empty($tld_list) || in_array($domain, $tld_list);
}