Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

cdm-dataportal / 5.x / modules / expertsdb / expertsdb_cck / expertsdb_alias.module @ 36311992

History | View | Annotate | Download (17.4 KB)

1
<?php
2

    
3
/**
4
 * @file
5
 * Defines simple expertsdb_alias field types.
6
 */
7

    
8
/*
9
 * Definition of privacy levels
10
 */
11
define('PRIVACY_CONTACT_PRIVATE', 'ContactPrivate');
12
define('PRIVACY_PRIVATE', 'Private');
13
define('PRIVACY_PUBLIC', 'Public');
14

    
15
/**
16
 * Implementation of hook_perm()
17
 *
18
 * Valid permissions for this module
19
 * @return array An array of valid permissions for the portfolio module
20
 */
21
function expertsdb_alias_perm() {
22
	return array(
23
    	'view private expertsdb_alias fields',
24
	//TODO which else permission are required?
25
	);
26
}
27

    
28
/**
29
 * Implementation of hook_help().
30
 *
31
 * @param unknown_type $section
32
 * @return unknown
33
 */
34
function expertsdb_alias_help($section) {
35
	switch ($section) {
36
		case 'admin/modules#description':
37
			return t('Defines a text field type for multiple name aliases. Data will be integrated as serialized array into the according content type database. <em>Note: Requires content.module.</em>');
38
	}
39
}
40

    
41
/**
42
 * Implementation of hook_menu().
43
 */
44
function expertsdb_alias_menu($may_cache) {
45
	$items = array();
46
	if ($may_cache) {
47
		$items[] = array(
48
      'path' => 'expertsdb_alias/widget/js',
49
      'callback' => 'expertsdb_alias_widget_js',
50
      'access' => user_access('access content'),
51
      'type' => MENU_CALLBACK,
52
		);
53
	}
54
	return $items;
55
}
56

    
57
/**
58
 * Implementation of hook_field_info().
59
 */
60
function expertsdb_alias_field_info() {
61
	return array(
62
    'expertsdb_alias' => array('label' => 'Expertsdb Name Alias'),
63
	);
64
}
65

    
66
/**
67
 * Implementation of hook_field_settings().
68
 */
69
function expertsdb_alias_field_settings($op, $field) {
70
	switch ($op) {
71
		case 'form':
72
			$form = array(
73
        '#theme' => 'expertsdb_alias_field_settings',
74
			);
75

    
76
			// disable multiple and
77
			$form['multiple'] = array('#type' => 'hidden','#value' => '0');
78

    
79
			return $form;
80

    
81
		case 'validate':
82
			/*if ($field['title'] == 'value' && empty($field['title_value'])) {
83
				form_set_error('title_value', t('A default title must be provided if the title is a static value'));
84
				}*/
85
			break;
86

    
87
		case 'save':
88
			// return array('attributes', 'display', 'title', 'title_value', 'enable_tokens');
89
			break;
90

    
91
		case 'database columns':
92
			$columns = array(
93
        'expertsdb_alias' => array('type' => 'mediumtext', 'not null' => FALSE),
94
			);
95
			return $columns;
96

    
97
		case 'filters':
98
			return;
99
			/*			return array(
100
			 'default' => array(
101
			 'name' => t('URL'),
102
			 'operator' => 'views_handler_operator_like',
103
			 'handler' => 'views_handler_operator_like',
104
			 ),
105
			 'title' => array(
106
			 'name' => t('Title'),
107
			 'operator' => 'views_handler_operator_like',
108
			 'handler' => 'views_handler_operator_like',
109
			 ),
110
			 'protocol' => array(
111
			 'name' => t('Protocol'),
112
			 'list' => drupal_map_assoc(variable_get('filter_allowed_protocols', array('http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'mailto', 'irc', 'ssh', 'sftp', 'webcal'))),
113
			 'operator' => 'views_handler_operator_or',
114
			 'handler' => 'expertsdb_alias_views_protocol_filter_handler',
115
			 ),
116
			 );*/
117

    
118
		case 'arguments':
119
			return;
120
			/*      return array(
121
			 'content: '. $field['field_name'] .'_url' => array(
122
			 'name' => t('Link URL') .': '. t($field['widget']['label']) .' ('. $field['field_name'] .')',
123
			 'handler' => 'expertsdb_alias_views_argument_handler',
124
			 ),
125
			 'content: '. $field['field_name'] .'_title' => array(
126
			 'name' => t('Link Title') .': '. t($field['widget']['label']) .' ('. $field['field_name'] .')',
127
			 'handler' => 'expertsdb_alias_views_argument_handler',
128
			 ),
129
			 'content: '. $field['field_name'] .'_target' => array(
130
			 'name' => t('Link Target') .': '. t($field['widget']['label']) .' ('. $field['field_name'] .')',
131
			 'handler' => 'expertsdb_alias_views_argument_handler',
132

    
133
			 ),
134
			 );*/
135

    
136
		case 'callbacks'://pairs up with cck_fullname_field::view
137
			return array(
138
        'view' => CONTENT_CALLBACK_CUSTOM,
139
			);
140

    
141
	}
142
}
143

    
144
/**
145
 * Implemantation of hook_field()
146
 *
147
 * @param unknown_type $op
148
 * @param unknown_type $node
149
 * @param unknown_type $field
150
 * @param unknown_type $items
151
 * @param unknown_type $teaser
152
 * @param unknown_type $page
153
 * @return unknown
154
 */
155
function expertsdb_alias_field($op, &$node, $field, &$items, $teaser, $page) {
156
	switch ($op) {
157

    
158
		case 'view':
159
			$context = $teaser ? 'teaser' : 'full';
160
			$formatter = isset($field['display_settings'][$context]['format']) ? $field['display_settings'][$context]['format'] : 'default';
161
			$items = content_format($field, $items, $formatter, $node);
162
			if(!empty($items) && is_array($items) && count($items) > 0){
163
				return theme('expertsdb_alias_container', $node, $field, $items, $teaser, $page);
164
			}
165
			return;
166
	}
167
}
168

    
169
/**
170
 * Implementation of hook_widget_info().
171
 */
172
function expertsdb_alias_widget_info() {
173
	return array(
174
    'expertsdb_alias' => array(
175
      'label' => 'Text Fields for Name Aliases',
176
      'field types' => array('expertsdb_alias'),
177
	),
178
	);
179
}
180

    
181
/**
182
 * Implementation of hook_widget().
183
 */
184
function expertsdb_alias_widget($op, &$node, $field, &$node_field) {
185
	switch ($op) {
186
		case 'prepare form values':
187
			// unserialize and prepare data
188
			_expertsdb_alias_widget_prepare($node_field);
189

    
190
			// get posted values in both node edit and profile edit mode
191
			if ($_POST[$field['field_name']] || $_POST[$field['type_name'].'_node_form'][$field['field_name']]) {
192
				$node_field = ($_POST['form_id'] == 'user_edit') ?  $_POST[$field['type_name'].'_node_form'][$field['field_name']] : $_POST[$field['field_name']];
193
				unset($node_field['count'], $node_field['more-url'], $node_field['more']);
194
			}
195
			return;
196

    
197
		case 'form':
198
			$form = array();
199
			$form[$field['field_name']] = array(
200
        '#tree' => TRUE,
201
        '#theme' => 'expertsdb_alias_widget_form',
202
        '#type' => 'fieldset',
203
        '#collapsible' => TRUE,
204
        '#collapsed' => FALSE,
205
        '#title' => t($field['widget']['label']),
206
        '#description' => t($field['widget']['description']),
207
			);
208

    
209
			drupal_add_js(drupal_get_path('module', 'expertsdb_alias') .'/expertsdb_alias.js');
210

    
211
			$delta = 0;
212
			// Render name alias fields for all the entered values
213
			foreach ($node_field as $data) {
214
				if (is_array($data) && $data['alias']) {
215
					_expertsdb_alias_widget_form($form[$field['field_name']][$delta], $field, $data, $delta);
216
					$delta++;
217
				}
218
			}
219
			// Render one additional new alias field
220
			_expertsdb_alias_widget_form($form[$field['field_name']][$delta], $field, array(), $delta);
221

    
222
			// Create a wrapper for additional fields
223
			$form[$field['field_name']]['wrapper'] = array(
224
          '#type' => 'markup',
225
          '#value' => '<div id="' . str_replace('_', '-', $field['field_name']) . '-wrapper" class="clear-block"></div>',
226
			);
227

    
228
			// Add 'More' Javascript Callback
229
			$form[$field['field_name']]['more-url'] = array(
230
          '#type' => 'hidden',
231
          '#value' => url('expertsdb_alias/widget/js/' . $field['type_name'] . '/' . $field['field_name'], NULL, NULL, TRUE),
232
          '#attributes' => array('class' => 'more-aliases'),
233
          '#id' => str_replace('_', '-', $field['field_name']) . '-more-url',
234
			);
235

    
236
			// Add Current Field Count
237
			$form[$field['field_name']]['count'] = array(
238
          '#type' => 'hidden',
239
          '#value' => $delta,
240
          '#id' => str_replace('_', '-', $field['field_name']) . '-count',
241
			);
242

    
243
			// Add More Button
244
			$form[$field['field_name']]['more'] = array(
245
        '#name' => 'more',
246
        '#id' => str_replace('_', '-', $field['field_name']) . '-more',
247
        '#weight' => 10,
248
				'#type' => 'views_imagebutton',
249
				'#title' => t('More Aliases'),
250
    		'#image' => drupal_get_path('module','expertsdb_email') . '/images/button_add_element.png',  // provide the path to your image here
251
    		'#default_value' => t('More emails'), // original value of button text
252
			);
253

    
254
			if (isset($tokens_form)) {
255
				$form[$field['field_name']]['tokens'] = $tokens_form;
256
			}
257

    
258
			return $form;
259

    
260
		case 'validate':
261
			_expertsdb_alias_cleanup($node_field);
262

    
263
			foreach($node_field as $delta => $value) {
264
				/*			if ($value['alias'] && !(isset($field['widget']['default_value'][$delta]['alias']) && $value['alias'] == $field['widget']['default_value'][$delta]['alias'] && !$field['required'])) {
265
					// Validate the link
266
					if (expertsdb_alias_validate_url(trim($value['alias'])) == FALSE) {
267
					form_set_error($field['field_name'] .']['. $delta. '][alias', t('Not a valid alias.'));
268
					}
269
					// Require a title for the link if necessary
270
					elseif ($field['title'] == 'required' && strlen(trim($value['title'])) == 0) {
271
					form_set_error($field['field_name'] .']['. $delta. '][title', t('Titles are required for all links.'));
272
					}
273
					}
274
					// Require a link if we have a title
275
					elseif (strlen($value['title']) > 0 && strlen(trim($value['alias'])) == 0) {
276
					form_set_error($field['field_name'] .']['. $delta. '][alias', t('You cannot enter a title without a link.'));
277
					}*/
278
			}
279
			return;
280

    
281
		case 'process form values':
282
			// run a cleanup cycle
283
			if(!strstr($node_field[0]['expertsdb_alias'],':{')){
284
				_expertsdb_alias_cleanup($node_field);
285
			}
286
			// apply some trimming
287
			_expertsdb_alias_widget_process($node_field);
288

    
289
			return;
290

    
291
		case 'submit':
292
			// do not save empty values and bring preferred email to the top
293
			_expertsdb_alias_cleanup($node_field);
294
			// apply some trimming
295
			_expertsdb_alias_widget_process($node_field);
296
			// serialize data
297
			_expertsdb_alias_serialize($node_field);
298
			return;
299
	}
300
}
301

    
302
/**
303
 * AHAH generation of additional form fields
304
 *
305
 * @param unknown_type $type_name
306
 * @param unknown_type $field_name
307
 */
308
function expertsdb_alias_widget_js($type_name, $field_name) {
309
	$field = content_fields($field_name, $type_name);
310
	$type = content_types($type_name);
311
	// get the correct delta and field_name depending on being in node_form or user_edit
312
	// $delta = ($_POST['form_id'] == 'user_edit') ? $_POST[$type['type'].'_node_form'][$field_name]['count']: $_POST[$field_name]['count'];
313
	$delta = $_POST[$field_name]['count'];
314
	$form = array();
315

    
316
	_expertsdb_alias_widget_form($form, $field, $node_field, $delta);
317

    
318
	// Assign parents matching the original form
319
	foreach (element_children($form) as $key) {
320
		$form[$key]['#parents'] = array($field_name, $delta, $key);
321
	}
322

    
323
	// Add names, ids, and other form properties
324
	foreach (module_implements('form_alter') as $module) {
325
		$function = $module .'_form_alter';
326
		$function('expertsdb_alias_widget_js', $form);
327
	}
328
	$form = form_builder('expertsdb_alias_widget_js', $form);
329

    
330
	// if form is rendered as part of the user profile page, we need to alter name and id of newly generated form elements
331
/*	if($_POST['form_id'] == 'user_edit'){
332
		foreach (element_children($form) as $key) {
333
			$element_name = explode('[',$form[$key]['#name']);
334
			array_walk($element_name,'_expertsdb_alias_widget_alter_elements');
335
			$form[$key]['#name'] = $type['type'] . '_node_form' . '[' . implode('][',$element_name).']';
336
		}
337
	}*/
338

    
339
	$output = drupal_render($form);
340

    
341
	print drupal_to_js(array('status' => TRUE, 'data' => $output));
342
	exit;
343
}
344

    
345

    
346
/**
347
 * Helper function renders the expertsdb_email widget only for multiple values.
348
 *
349
 * @param unknown_type $form_item
350
 * @param unknown_type $field
351
 * @param unknown_type $node_field
352
 * @param unknown_type $delta
353
 */
354
function _expertsdb_alias_widget_form(&$form_item, $field, $node_field, $delta = 0) {
355

    
356
	$form_item = array(
357
    '#tree' => TRUE,
358
    '#theme' => 'expertsdb_alias_widget_form_row',
359
	);
360

    
361
	$default_alias = "";
362
	if (isset($field['widget']['default_value'][$delta]['alias'])) {
363
		$default_alias = $field['widget']['default_value'][$delta]['alias'];
364
	}
365

    
366
	$form_item['alias'] = array(
367
    '#type' => 'textfield',
368
    '#maxlength' => '255',
369
    '#title' => $delta === 0 ? t('Name Alias') : NULL,
370
    '#default_value' => ($node_field['alias']) ? $node_field['alias'] : $default_alias,
371
    '#required' => ($delta === 0) ? $field['required'] : FALSE,
372
	);
373
}
374

    
375
function _expertsdb_alias_widget_prepare(&$node_field) {
376
	// only prepare, if data is actually serialized
377
	if(strstr($node_field[0]['expertsdb_alias'],':{')){
378
		$node_field[0] = unserialize($node_field[0]['expertsdb_alias']);
379
		if(count($node_field) > 0){
380
			// return content of first array element
381
			$node_field = $node_field[0];
382
		}
383
	}
384
}
385

    
386
function _expertsdb_alias_serialize(&$node_field){
387
	$node_field = array(array('expertsdb_alias' => serialize($node_field)));
388
}
389

    
390
/**
391
 * Function to remove unwanted elements from the node_field array
392
 *
393
 * @param array $node_field
394
 */
395
function _expertsdb_alias_cleanup(&$node_field){
396

    
397
	$save_field = array();
398
	$registered_aliases =array();
399
	// Remove the JS helper fields
400
	unset($node_field['count'], $node_field['more-url'], $node_field['more']);
401
	foreach ($node_field as $delta => $value) {
402
		if (!empty($value['alias']) || $delta == 0) {
403
			// skip duplicates emails
404
			if(in_array($value['alias'], $registered_aliases)){
405
				continue;
406
			}
407
			// register email to avoid duplicates
408
			array_push($registered_aliases, $value['alias']);
409

    
410
			if (!empty($value['alias']) || $delta == 0) {
411
				$save_field[] = $node_field[$delta];
412
			}
413
		}
414
	}
415
	$node_field = $save_field;
416
}
417

    
418
function _expertsdb_alias_widget_process(&$node_field) {
419
	foreach($node_field as $delta => $value) {
420
		if (is_numeric($delta)) {
421
			// Trim whitespace from alias
422
			$node_field[$delta]['alias'] = check_plain(trim($node_field[$delta]['alias']));
423
		}
424
	}
425
}
426

    
427
function _expertsdb_alias_widget_alter_elements(&$element){
428
	$element = preg_replace('@\]@', '', $element);
429
}
430

    
431

    
432
/**
433
 * Theme the display of the entire link set
434
 */
435
function theme_expertsdb_alias_widget_form($element) {
436
	// pull in the stylesheet
437
	drupal_add_css(drupal_get_path('module', 'expertsdb_alias') .'/expertsdb_alias_form.css');
438
	$output = drupal_render($element);
439

    
440
	return $output;
441
}
442

    
443
/**
444
 * Theme the display of a single form row
445
 */
446
function theme_expertsdb_alias_widget_form_row($element) {
447
	$output = '';
448
	$output .= '<div class="expertsdb-alias-field-row clear-block"><div class="expertsdb-alias-field-subrow clear-block">';
449
	$output .= '<div class="expertsdb-alias-field-alias">' . drupal_render($element['alias']) . '</div>';
450
	$output .= '</div>';
451
	$output .= drupal_render($element);
452
	$output .= '</div>';
453
	return $output;
454
}
455

    
456
/**
457
 * Theme the settings form for the expertsdb_alias field.
458
 */
459
function theme_expertsdb_alias_field_settings($form) {
460
	$title_value = drupal_render($form['title_value']);
461
	$title_checkbox = drupal_render($form['title']['value']);
462

    
463
	// Set Static Title radio option to include the title_value textfield
464
	$form['title']['value'] = array('#value' => '<div class="container-inline">'. $title_checkbox . $title_value .'</div>');
465

    
466
	// Reprint the title radio options with the included textfield
467
	return drupal_render($form);
468
}
469

    
470
/**
471
 * Theme to display a complete container with all links in view mode
472
 *
473
 * @param unknown_type $node
474
 * @param unknown_type $field
475
 * @param unknown_type $items
476
 * @param unknown_type $teaser
477
 * @param unknown_type $page
478
 * @return unknown
479
 */
480
function theme_expertsdb_alias_container($node, $field, $items, $teaser, $page){
481
	// pull in the stylesheet
482
	drupal_add_css(drupal_get_path('module', 'expertsdb_alias') .'/expertsdb_alias_view.css');
483
	$output = '';
484
	$output .= '<div class="expertsdb-link-container">';
485
	$output .= theme('field', $node, $field, $items, $teaser, $page);
486
	$output .= '</div>';
487
	return $output;
488
}
489

    
490
/**
491
 * Implementation of hook_field_formatter_info().
492
 */
493
function expertsdb_alias_field_formatter_info() {
494
	return array(
495
    'default' => array(
496
      'label' => t('Default'),
497
      'field types' => array('expertsdb_alias'),
498
	),
499
    'comma separated list' => array(
500
      'label' => t('Comma separated list'),
501
      'field types' => array('expertsdb_alias'),
502
	),
503
    'comma separated list in brackets' => array(
504
      'label' => t('Comma separated list, in brackets'),
505
      'field types' => array('expertsdb_alias'),
506
	),
507
	);
508
}
509

    
510
/**
511
 * Implementation of hook_field_formatter().
512
 */
513
function expertsdb_alias_field_formatter($field, $item, $formatter, $node) {
514
	// item comes in either as serialized or unserialized value
515
	if (empty($item[0]['expertsdb_alias']) && empty($item[0]['alias'])) {
516
		return '';
517
	}
518
	if(strstr($item[0]['expertsdb_alias'],':{')){
519
		$items = unserialize($item[0]['expertsdb_alias']);
520
	}else{
521
		unset($item['count'], $item['more-url'], $item['more']);
522
		$items = $item;
523
	}
524

    
525
	/*
526
	 * React on privacy settings
527
	 */
528
	if($node->field_privacy){
529
		global $user;
530
		$term = taxonomy_get_term($node->field_privacy[0]['tid']);
531
		switch($term->name){
532

    
533
			case PRIVACY_CONTACT_PRIVATE:
534
				// allow everybody to view aliases as configured in the field formatter (display field)
535
				break;
536

    
537
			case PRIVACY_PRIVATE:
538
				// show information only to roles with access and node author
539
				if($user->uid != $node->uid && !user_access('view private expertsdb_alias fields',$user)){
540
					return;
541
				}
542
				break;
543

    
544
			case PRIVACY_PUBLIC:
545
				// allow everybody to view aliases as configured in the field formatter (display field)
546
				break;
547

    
548

    
549
		}
550
	}
551

    
552
	// check, if we do have any aliases
553
	$aliases_present = FALSE;
554
	foreach ($items as $delta => $item){
555
		if(!empty($item['alias'])){
556
			$aliases_present = TRUE;
557
		}
558
	}
559
	if (!$aliases_present) return;
560

    
561
	$tmp_items = array();
562
	foreach ($items as $delta => $item) {
563

    
564
		switch ($formatter) {
565
			case "comma separated list":
566
			case "comma separated list in brackets":
567
				$tmp_items[] = $item['alias'];
568
				break;
569

    
570
			default:
571
				$items[$delta]['view'] = '<span class="name-alias">' . $item['alias'] . '</span>';
572
		}
573
	}
574

    
575
	if(count($tmp_items) > 0) {
576
		// clear items
577
		$items = array();
578
		$items[0]['alias'] =  implode(", ",$tmp_items);
579
		$items[0]['view'] = ($formatter == "comma separated list") ? implode(", ",$tmp_items) : '<span class="name-alias">(' . implode(", ",$tmp_items) . ")</span>";
580
	}
581

    
582
	return $items;
583
}
Add picture from clipboard (Maximum size: 40 MB)