Project

General

Profile

Download (16.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3

    
4
function dwca_export_views_menu() {
5
	
6
	$items = array();	
7

    
8
	$items['admin/config/system/views_dwca_export'] = array(
9
		'title' => 'DarwinCore Archive (DwC-A) export mapping module',
10
		'description' => t('Allow a use to select views for DwC-A and map terms to those views to create the metadata file meta.xml for the DwC-A archive.'),
11
		'page callback' => 'drupal_get_form',
12
		//'page arguments' => array('dwca_export_views_config_form'),
13
		'page arguments' => array('dwca_export_views_wizard'),
14
		'access arguments' => array('access DwC-A export settings'),
15
		'type' => MENU_NORMAL_ITEM,
16
	//'file' => 'dwca_export.admin.inc'
17
	);	
18
	
19
	return $items;
20
}
21

    
22
/**
23
* Form function, called by drupal_get_form()
24
* in dwca_export_menu().
25
*/
26
function dwca_export_views_config_form($form, &$form_state) {
27

    
28
	global $base_url;
29

    
30
	$form['dwca_export_info'] = array(
31
		'#markup' => '<p>Select the views to map to dwca terms.'
32
.'</p>'
33
	);
34

    
35
	$form['dwca_export_view_mapping'] = dwca_export_views_select_view_form();
36
	//$form['dwca_export_view_mapping'] = views_get_all_views();
37

    
38
	$form['#submit'][] = 'dwca_export_views_config_form_submit';
39
	//lorna remove we don't want the save configuration button
40
	//return system_settings_form($form);	
41
	return $form;
42
}
43
function dwca_export_views_select_view_form() {
44

    
45
	$allviews = views_get_all_views();
46

    
47
	$views = array(
48
	    		'#type' => 'fieldset',
49
	    		'#title' => t('DwC-A views'),
50
	//'#tree' => TRUE,
51
	);
52

    
53
	foreach($allviews as $view){
54
		// select only the views that are prefixed with dwca_export for the form
55
		if(substr($view->name, 0, 11) == 'dwca_export' && substr($view->name, 0, 23) != 'dwca_export_description'){
56
				
57
			$dwca_filename = $view->name;
58
			$views[$dwca_filename] = array(
59
			//$form[$dwca_filename] = array(
60
								  	'#type' => 'checkbox',
61
								  	'#title' => t($dwca_filename),
62
								  	'#description' => t('select view for ' . $dwca_filename),
63
				
64
			);
65
				
66
		}
67
	}
68

    
69
	$savedvariables = variable_get('VIEWS_MAP');
70

    
71
	/*foreach ($savedvariables as $key => $value) {
72

    
73
		echo "VALUE saved" . $value;
74
		echo $savedvariables[$key];
75
		print_r(t('key ') . $key . t('key ') . $value);
76
	}*/
77
	//$meta_string = file_get_contents('http://www.google.com/');
78
	//drupal_set_message(t('The meta - is ') . $meta_string);
79
	//print_r($meta_string);
80
	return $views;
81
}
82

    
83
function _dwca_export_views_steps() {
84
  return array(
85
      1 => array(
86
        'form' => 'dwca_export_views_config_form',
87
      ),
88
      2 => array(
89
        'form' => 'dwca_export_views_fields_form',
90
      ),
91
    );
92
}
93
/**
94
* Returns form elements for the 'location info' page of the wizard. This is the
95
* second step of the wizard. This step asks for a textfield value: a City. This
96
* step also includes a validation declared later.
97
*
98
* @ingroup form_example
99
*/
100
function dwca_export_views_fields_form($form, &$form_state) {
101
	
102
	$form = array(
103
	    		'#type' => 'fieldset',
104
	    		'#title' => t('Field to DwC-A term mapping'),
105
	);
106
	
107
	$form['dwca_export_info'] = array(
108
			'#markup' => '<p>Click on a view name below to expand the form to map field names to terms in DwC-A. </p>'
109
	.'</p>'
110
	);
111
	
112
	//TODO create an array containing the terms for the core and each extension
113

    
114
	$views = _dwca_export_views_meta_xml_map();
115

    
116
	$count = 0;
117
	$savedvariables = variable_get('VIEWS_MAP');
118
	
119
	//lorna TODO create a collapsable form for each view
120
	// A FORM OF FORMS
121
	foreach ($savedvariables as $key => $value) {
122
	
123
		//print_r(t('key') . $key . t(' value') . $value);
124
		
125
		$view = views_get_view($value);
126
		//////////////////print_r(t('key') . $key . t(' value') . $value);
127
		
128
		$collapsed = 1;
129
		if($count == 0) {
130
			$collapsed = 0;
131
		}
132
		$form[$value] = array(
133
			    		'#type' => 'fieldset',
134
			    		'#title' => $value,
135
						//'#size' => 30,
136
						//'#collapsible' => $collapsed,
137
						//'#collapsed' => $collapsed,
138
						//'#tree' => $collapsed,
139
						'#collapsible' => 1,
140
						'#collapsed' => 1,
141
						'#tree' => 1,
142
		
143
		);
144
		// check whether there is a view named with this value
145
		//if($view) {
146
			//$fields = $view->display_handler->get_field_labels();
147
			$fields =$view->display['default']->display_options['fields'];
148
			
149
			$field_index = 0;
150
			foreach ($fields as $field_key => $field_label) {
151
				
152
				//display the field_keys as the names of each text box in this form
153
				
154
				//drupal_set_message(t('value') . $value . t('value and field_index ') . $field_index);
155
					
156
				$form[$value][$field_key] = array(
157
				    '#type' => 'textfield',
158
				    '#title' => $field_key,
159
				    '#description' => t('Enter the DwC-A term for the field - ' . $field_key),
160
					'#default_value' => $views[$value][$field_index],
161
					'#size' => 30,
162
					'#collapsible' => 1,
163
					'#collapsed' => 1,
164
					'#tree' => 1,
165
				);
166
																				
167
			$field_index++;	
168
		}		
169
	}
170
	$count++;	
171
	return $form;
172
}
173

    
174
function _dwca_export_views_meta_xml_update($field_term_map){
175
	
176
	//convert the field_term_map to an array of terms. The order of the terms corresponds to the 
177
	//order in the meta.xml. We don't need the keys (view field names)
178
	
179
	$term_array = array();
180
		
181
		foreach ($field_term_map as $key => $value) {
182
			
183
			$term_array[$key] = array();
184
			
185
			//drupal_set_message(t(' THE key... ') . $key);
186
			//if the value is an array with key names dwca_export_
187
			if ( (array) $value === $value ) {
188
				
189
				foreach ($value as $key2 => $value2) {
190
					
191
					/////////drupal_set_message(t(' THE key2... ') . $key2);
192
					//$term_array[] = $key;
193
					//////////////////////////////////////////////$term_array[$key] = array();
194
					if (substr($key, 0, 11) == 'dwca_export') {
195
						
196
						$term_array[$key][] = $value2;
197
					}
198
			}
199
		}		
200
		
201
}
202
	
203
	//check if the array size is the same as the number of fields in the meta.xml for either the
204
	//core or the particular extension
205
	
206
	//if the view has more fields than the default meta.xml we can add a node to the meta.xml
207
	//TODO: Need to work this out.
208
	
209
	//if the view has less fields we could either leave them blank e.g. <field index="21" term=""/>
210
	//or simply remove them
211

    
212
	$xml_file = drupal_get_path('module', 'dwca_export') . '/static/meta.xml';
213
	$meta_string = simplexml_load_file($xml_file);	
214
	
215
	$meta_string->core->files->location[0] = 'classification.txt';
216
	
217
	$core_string = $meta_string->core->files->location[0];
218
	$classification_view_name = "dwca_export_" . substr($core_string, 0, -4);
219
	
220
	//the first position in the dwca is always the id and this doesn't point to a term so use the empty string
221
	$views[$classification_view_name][0] = '';
222
	
223
	//set the core node of the meta.xml with dwca_export_classifcation values from the form
224
	//currently we're relying on the order of fields in the view to correspond to the order of fields in the meta.xml
225
	
226
	//in the meta.xml id index="0" fields start at index 1
227
	$index = 1;
228
	
229
	foreach($meta_string->core->field as $field)
230
	{
231
		$term = (string)$field[@term];
232
		
233
		if (isset($term_array['dwca_export_classification'][$index])) {
234
			$field[@term] = $term_array['dwca_export_classification'][$index];
235
		}
236
		
237
		$index++;
238
		//$views[$classification_view_name][] = $term;	
239
	}
240
	
241
	
242
	
243
	//only modify the extensions which have been modified in the form	
244
	foreach($meta_string->extension as $extension)
245
	{
246
		//echo $extension->files->location[0] . "\n";
247
		$extension_string = $extension->files->location[0] . "\n";	
248
		$extension_view_name = "dwca_export_" . substr($extension_string, 0, -5);
249
		//echo "The ext view name is:" . $classification_view_name . "\n";
250
		//drupal_set_message(t(' THE ext view name is ') . $extension_view_name);
251
				
252
		if ( array_key_exists($extension_view_name, $term_array)) {
253
		$views[$extension_view_name][0] = '';
254
		
255
		$index2 = 1;
256
	
257
		foreach($extension->field as $field2)
258
			{
259
				$term = (string)$field2[@term];
260
				//$views[$extension_view_name][] = $term;
261
				if (isset($term_array[(string)$extension_view_name][$index2])) {
262
					$field2[@term] = $term_array[(string)$extension_view_name][$index2];
263
				}
264
				$index2++;
265
			}
266
		}
267
	}
268

    
269
	// Saving the whole modified XML to a new filename
270
	$meta_string->asXml('updated_meta.xml');
271
	//$meta_string->asXml($xml_file);
272
	
273
	//drupal_set_message(t('VALUES: <pre>@result</pre>', array('@num' => 1, '@result' => print_r($term_array, TRUE))));
274
}
275

    
276
//TODO Lorna - could we modify this method with a boolean to indicate whether we want to create the map or whether we want to modify the xml with values in the map
277
function _dwca_export_views_meta_xml_map(){
278
	
279
	//TODO: Should we store the map in the database so we don't need to keep reading in the XML file?
280
	//static $file_map;
281
	//if(!isset($file_map)){
282
		//$file_map = variable_get(FILE_MAP);
283
	//}
284
	$views = array();
285

    
286
//create a 2D array so that each extension or view name points to an array of terms
287
	$meta_string = simplexml_load_file(drupal_get_path('module', 'dwca_export') . '/static/meta.xml');
288
	//simplexml_load_string()
289
	
290
	$core_string = $meta_string->core->files->location[0];
291
	$classification_view_name = "dwca_export_" . substr($core_string, 0, -4);
292
	echo $classification_view_name;
293

    
294
	//the first postion in the dwca is always the id and this doesn't point to a term so use the empty string
295
	$views[$classification_view_name][0] = '';
296
	
297
	foreach($meta_string->core->field as $field)
298
	{
299
		$term = (string)$field[@term];
300
		
301
		//$views[$classification_view_name][$term_array] = array($term);
302
		$views[$classification_view_name][] = $term;		
303
		
304
	}
305

    
306
	foreach($meta_string->extension as $extension)
307
	{
308
		//echo $extension->files->location[0] . "\n";
309
		$extension_string = $extension->files->location[0] . "\n";
310
		
311
		$extension_view_name = "dwca_export_" . substr($extension_string, 0, -5);
312
		//echo "The ext view name is:" . $extension_view_name . "\n";
313
		$views[$extension_view_name][0] = '';
314
		
315
		foreach($extension->field as $field)
316
		{
317
			$term = (string)$field[@term];
318
			$views[$extension_view_name][] = $term;
319
		}
320
	}
321

    
322
	foreach($meta_string->children() as $child)
323
	{
324
		// core or extension
325
		//foreach($child->children() as $child2)
326
		//{
327
		// core or extension
328
		//echo $child2->getName() . ": " . $child2 . "<br />";
329
		//}
330

    
331

    
332
	}
333
	//$result = $meta_string->xpath("//archive/core/files/location");
334
	//foreach ($result_array as )
335

    
336
	return $views;
337
}
338

    
339
function dwca_export_views_wizard($form, &$form_state) {
340
	
341
		// Initialize a description of the steps for the wizard.
342
		if (empty($form_state['step'])) {
343
			$form_state['step'] = 1;
344
	
345
			// This array contains the function to be called at each step to get the
346
			// relevant form elements. It will also store state information for each
347
			// step.
348
			$form_state['step_information'] = _dwca_export_views_steps();
349
		}
350
		$step = &$form_state['step'];
351
		drupal_set_title(t('DwC-A to views mapping: Step @step', array('@step' => $step)));
352
		//lorna
353
		//drupal_set_message(t('Extensible Wizard: Step @step', array('@step' => $step)));
354
	
355
		// Call the function named in $form_state['step_information'] to get the
356
		// form elements to display for this step.
357
		$form = $form_state['step_information'][$step]['form']($form, $form_state);
358
	
359
		// Show the 'previous' button if appropriate. Note that #submit is set to
360
		// a special submit handler, and that we use #limit_validation_errors to
361
		// skip all complaints about validation when using the back button. The
362
		// values entered will be discarded, but they will not be validated, which
363
		// would be annoying in a "back" button.
364
		if ($step > 1) {
365
			$form['prev'] = array(
366
	      '#type' => 'submit',
367
	      '#value' => t('Previous'),
368
	      '#name' => 'prev',
369
	      '#submit' => array('dwca_export_views_wizard_previous_submit'),
370
	      '#limit_validation_errors' => array(),
371
			);
372
		}
373
	
374
		// Show the Next button only if there are more steps defined.
375
		if ($step < count($form_state['step_information'])) {
376
			// The Next button should be included on every step
377
			$form['next'] = array(
378
	      '#type' => 'submit',
379
	      '#value' => t('Next'),
380
	      '#name' => 'next',
381
	      '#submit' => array('dwca_export_views_wizard_next_submit'),
382
			);
383
		}
384
		else {
385
			// Just in case there are no more steps, we use the default submit handler
386
			// of the form wizard. Call this button Finish, Submit, or whatever you
387
			// want to show. When this button is clicked, the
388
			// dwca_export_views_wizard_submit handler will be called.
389
			$form['finish'] = array(
390
	      '#type' => 'submit',
391
	      '#value' => t('Finish'),
392
			);
393
		}
394
	
395
		// Include each validation function defined for the different steps.
396
		if (function_exists($form_state['step_information'][$step]['form'] . '_validate')) {
397
			$form['next']['#validate'] = array($form_state['step_information'][$step]['form'] . '_validate');
398
		}
399
	
400
		return $form;
401
	}
402
function dwca_export_views_wizard_next_submit($form, &$form_state) {
403
	$current_step = &$form_state['step'];
404
	$form_state['step_information'][$current_step]['stored_values'] = $form_state['values'];
405

    
406
	if ($current_step < count($form_state['step_information'])) {
407
		
408
		// lorna save the values for the first step
409
		if ($current_step == 1) {
410
			_dwca_export_views_step1_submit($form, &$form_state);
411
		}
412
		$current_step++;
413
		if (!empty($form_state['step_information'][$current_step]['stored_values'])) {
414
			$form_state['values'] = $form_state['step_information'][$current_step]['stored_values'];
415
			
416
			
417
		}
418
		else {
419
			$form_state['values'] = array();
420
		}
421
		$form_state['rebuild'] = TRUE;  // Force rebuild with next step.
422
		return;
423
	}
424
}
425
function dwca_export_views_wizard_previous_submit($form, &$form_state) {
426
	$current_step = &$form_state['step'];
427
	$form_state['step_information'][$current_step]['stored_values'] = $form_state['values'];
428
	if ($current_step > 1) {
429
		$current_step--;
430
		$form_state['values'] = $form_state['step_information'][$current_step]['stored_values'];
431
	}
432
	$form_state['rebuild'] = TRUE;
433
}
434

    
435

    
436
function _dwca_export_views_step1_submit($form, &$form_state) {
437
	//function dwca_export_views_wizard_submit($form, &$form_state) {
438

    
439
	$variables = $form_state['input'];
440
	$save_variables = '';
441
	$views_map = variable_get('VIEWS_MAP');
442
	$views_to_map = array();	
443

    
444
	foreach ($variables as $key => $value) {
445

    
446
		if(substr($key, 0, 11) == 'dwca_export'){
447
			
448
			if ($value == 1) {
449
				$views_to_map[] = $key;
450
				//drupal_set_message(t('MAPPING VALUE ') . $value);
451
			}
452
					
453
		}
454
	}
455

    
456
	//save a map of the views the user want to change in the database
457
	variable_del('VIEWS_MAP');
458
	variable_set('VIEWS_MAP', $views_to_map);
459
	
460
	$savedvariables = variable_get('VIEWS_MAP');
461
	
462
	//foreach ($variables as $key => $value) {	
463
		
464
		//echo $key . ": " . $value . "...";
465
		//print_r(t('keys ') . $key . t('values ') . $value);
466
	//}
467
	//drupal_set_message(t('The classification view is ') . $save_variables);// . '<pre>' . print_r($form_state,true) . '</pre>');//$save_variables);
468
}
469

    
470
// And now comes the magic of the wizard, the function that should handle all the
471
// inputs from the user on each different step.
472
/**
473
 * Wizard form submit handler.
474
 * - Saves away $form_state['values']
475
 * - Process all the form values.
476
 *
477
 * This demonstration handler just do a drupal_set_message() with the information
478
 * collected on each different step of the wizard.
479
 *
480
 * @param $form
481
 * @param $form_state
482
 *
483
 * @ingroup form_example
484
 */
485
function dwca_export_views_wizard_submit($form, &$form_state) {
486
	$current_step = &$form_state['step'];
487
	$form_state['step_information'][$current_step]['stored_values'] = $form_state['values'];
488

    
489
	// In this case we've completed the final page of the wizard, so process the
490
	// submitted information.
491
	foreach ($form_state['step_information'] as $index => $value) {
492
		// Remove FAPI fields included in the values (form_token, form_id and form_build_id
493
		// This is not required, you may access the values using $value['stored_values']
494
		// but I'm removing them to make a more clear representation of the collected
495
		// information as the complete array will be passed through drupal_set_message().
496
		unset($value['stored_values']['form_id']);
497
		unset($value['stored_values']['form_build_id']);
498
		unset($value['stored_values']['form_token']);
499
		
500
		
501
		//drupal_set_message(t('INFO COLLECTED @result'), array('@result' => print_r($value['stored_values'], TRUE)));
502
		
503

    
504
		// Now show all the values.
505
		if ($index == 2) {
506
			_dwca_export_views_meta_xml_update($value['stored_values']);
507
		//drupal_set_message(t('Step @num collected the following values: <pre>@result</pre>', array('@num' => $index, '@result' => print_r($value['stored_values'], TRUE))));
508
	}
509
}
510
}
511

    
512

    
(2-2/2)