Project

General

Profile

Download (16.2 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
	//LORNA - DO I NEED THIS FIELDSET ARRAY!!!
48
	$views = array(
49
	    		'#type' => 'fieldset',
50
	    		'#title' => t('DwC-A views'),
51
	//'#tree' => TRUE,
52
	);
53

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

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

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

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

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

    
110
	$views = _dwca_export_views_meta_xml_map();
111

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

    
169
function _dwca_export_views_meta_xml_update($field_term_map){
170
	
171
	//convert the field_term_map to an array of terms. The order of the terms corresponds to the 
172
	//order in the meta.xml. We don't need the keys (view field names)
173
	
174
	$term_array = array();
175
	
176
	foreach ($field_term_map as $key => $value) {
177
		$term_array[] = $value;
178
	}
179
	
180
	//check if the array size is the same as the number of fields in the meta.xml for either the
181
	//core or the particular extension
182
	
183
	//if the view has more fields than the default meta.xml we can add a node to the meta.xml
184
	
185
	//if the view has less fields we could either leave them blank e.g. <field index="21" term=""/>
186
	//or simply remove them
187

    
188
	$xml_file = drupal_get_path('module', 'dwca_export') . '/static/meta.xml';
189
	$meta_string = simplexml_load_file($xml_file);	
190
	
191
	$meta_string->core->files->location[0] = 'classification.txt';
192
	
193
	$core_string = $meta_string->core->files->location[0];
194
	$classification_view_name = "dwca_export_" . substr($core_string, 0, -4);
195
	//echo $classification_view_name;
196
	echo "Theclassviewnameis:" . $classification_view_name . "\n";
197
	drupal_set_message(t(' THE class view name is ') . $classification_view_name);
198
	
199
	//the first postion in the dwca is always the id and this doesn't point to a term so use the empty string
200
	$views[$classification_view_name][0] = '';
201
	
202
	//set the core node of the meta.xml with dwca_export_classifcation values from the form
203
	//currently we're relying on the order of fields in the view corresponding to the order of fields in the meta.xml
204
	
205
	//in the meta.xml id index="0" fields start at index 1
206
	$index = 1;
207
	
208
	foreach($meta_string->core->field as $field)
209
	{
210
		$term = (string)$field[@term];
211
		drupal_set_message(t(' THE META STRING TERM IS ') . $term);
212
		$term = 'hello';
213
		//$field[@term] = 'goodbye';
214
		$field[@term] = $field_term_map['name_1'];
215
		$field[@term] = $term_array[$index];
216
		$index++;
217
		//$views[$classification_view_name][] = $term;	
218
	}
219
	
220
	foreach($meta_string->extension as $extension)
221
	{
222
		//echo $extension->files->location[0] . "\n";
223
		$extension_string = $extension->files->location[0] . "\n";
224
	
225
		$extension_view_name = "dwca_export_" . substr($extension_string, 0, -5);
226
		//echo "The ext view name is:" . $classification_view_name . "\n";
227
		drupal_set_message(t(' THE ext view name is ') . $extension_view_name);
228
		echo "Theclassviewnameis:" . $classification_view_name . "\n";
229
		$views[$extension_view_name][0] = '';
230
	
231
		foreach($extension->field as $field)
232
		{
233
			$term = (string)$field[@term];
234
			$views[$extension_view_name][] = $term;
235
		}
236
	}
237

    
238
	// Saving the whole modified XML to a new filename
239
	//$meta_string->asXml('updated.xml');
240
	$meta_string->asXml($xml_file);
241
	
242
	drupal_set_message(t('UPDATE THE META XML file WITH THE VALUES COLLECTED: <pre>@result</pre>', array('@num' => 1, '@result' => print_r($field_term_map, TRUE))));
243
}
244

    
245
//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
246
function _dwca_export_views_meta_xml_map(){
247
	
248
	//TODO: Should we store the map in the database so we don't need to keep reading in the XML file?
249
	//static $file_map;
250
	//if(!isset($file_map)){
251
		//$file_map = variable_get(FILE_MAP);
252
	//}
253
	$views = array();
254

    
255
//create a 2D array so that each extension or view name points to an array of terms
256
	$meta_string = simplexml_load_file(drupal_get_path('module', 'dwca_export') . '/static/meta.xml');
257
	//simplexml_load_string()
258
	
259
	$core_string = $meta_string->core->files->location[0];
260
	$classification_view_name = "dwca_export_" . substr($core_string, 0, -4);
261
	echo $classification_view_name;
262

    
263
	//the first postion in the dwca is always the id and this doesn't point to a term so use the empty string
264
	$views[$classification_view_name][0] = '';
265
	
266
	foreach($meta_string->core->field as $field)
267
	{
268
		$term = (string)$field[@term];
269
		
270
		//$views[$classification_view_name][$term_array] = array($term);
271
		$views[$classification_view_name][] = $term;		
272
		
273
	}
274

    
275
	foreach($meta_string->extension as $extension)
276
	{
277
		//echo $extension->files->location[0] . "\n";
278
		$extension_string = $extension->files->location[0] . "\n";
279
		
280
		$extension_view_name = "dwca_export_" . substr($extension_string, 0, -5);
281
		//echo "The ext view name is:" . $extension_view_name . "\n";
282
		$views[$extension_view_name][0] = '';
283
		
284
		foreach($extension->field as $field)
285
		{
286
			$term = (string)$field[@term];
287
			$views[$extension_view_name][] = $term;
288
		}
289
	}
290

    
291
	foreach($meta_string->children() as $child)
292
	{
293
		// core or extension
294
		//echo $child->getName() . ": " . $child . "<br />";
295

    
296
		//foreach($child->children() as $child2)
297
		//{
298
		// core or extension
299
		//echo $child2->getName() . ": " . $child2 . "<br />";
300
		//}
301

    
302

    
303
	}
304
	//$result = $meta_string->xpath("//archive/core/files/location");
305
	//foreach ($result_array as )
306

    
307
	return $views;
308
}
309

    
310
function dwca_export_views_wizard($form, &$form_state) {
311
	
312
		// Initialize a description of the steps for the wizard.
313
		if (empty($form_state['step'])) {
314
			$form_state['step'] = 1;
315
	
316
			// This array contains the function to be called at each step to get the
317
			// relevant form elements. It will also store state information for each
318
			// step.
319
			$form_state['step_information'] = _dwca_export_views_steps();
320
		}
321
		$step = &$form_state['step'];
322
		drupal_set_title(t('DwC-A to views mapping: Step @step', array('@step' => $step)));
323
		//lorna
324
		//drupal_set_message(t('Extensible Wizard: Step @step', array('@step' => $step)));
325
	
326
		// Call the function named in $form_state['step_information'] to get the
327
		// form elements to display for this step.
328
		$form = $form_state['step_information'][$step]['form']($form, $form_state);
329
	
330
		// Show the 'previous' button if appropriate. Note that #submit is set to
331
		// a special submit handler, and that we use #limit_validation_errors to
332
		// skip all complaints about validation when using the back button. The
333
		// values entered will be discarded, but they will not be validated, which
334
		// would be annoying in a "back" button.
335
		if ($step > 1) {
336
			$form['prev'] = array(
337
	      '#type' => 'submit',
338
	      '#value' => t('Previous'),
339
	      '#name' => 'prev',
340
	      '#submit' => array('dwca_export_views_wizard_previous_submit'),
341
	      '#limit_validation_errors' => array(),
342
			);
343
		}
344
	
345
		// Show the Next button only if there are more steps defined.
346
		if ($step < count($form_state['step_information'])) {
347
			// The Next button should be included on every step
348
			$form['next'] = array(
349
	      '#type' => 'submit',
350
	      '#value' => t('Next'),
351
	      '#name' => 'next',
352
	      '#submit' => array('dwca_export_views_wizard_next_submit'),
353
			);
354
		}
355
		else {
356
			// Just in case there are no more steps, we use the default submit handler
357
			// of the form wizard. Call this button Finish, Submit, or whatever you
358
			// want to show. When this button is clicked, the
359
			// dwca_export_views_wizard_submit handler will be called.
360
			$form['finish'] = array(
361
	      '#type' => 'submit',
362
	      '#value' => t('Finish'),
363
			);
364
		}
365
	
366
		// Include each validation function defined for the different steps.
367
		if (function_exists($form_state['step_information'][$step]['form'] . '_validate')) {
368
			$form['next']['#validate'] = array($form_state['step_information'][$step]['form'] . '_validate');
369
		}
370
	
371
		return $form;
372
	}
373
function dwca_export_views_wizard_next_submit($form, &$form_state) {
374
	$current_step = &$form_state['step'];
375
	$form_state['step_information'][$current_step]['stored_values'] = $form_state['values'];
376

    
377
	if ($current_step < count($form_state['step_information'])) {
378
		
379
		// lorna save the values for the first step
380
		if ($current_step == 1) {
381
			_dwca_export_views_step1_submit($form, &$form_state);
382
		}
383
		$current_step++;
384
		if (!empty($form_state['step_information'][$current_step]['stored_values'])) {
385
			$form_state['values'] = $form_state['step_information'][$current_step]['stored_values'];
386
			
387
			
388
		}
389
		else {
390
			$form_state['values'] = array();
391
		}
392
		$form_state['rebuild'] = TRUE;  // Force rebuild with next step.
393
		return;
394
	}
395
}
396
function dwca_export_views_wizard_previous_submit($form, &$form_state) {
397
	$current_step = &$form_state['step'];
398
	$form_state['step_information'][$current_step]['stored_values'] = $form_state['values'];
399
	if ($current_step > 1) {
400
		$current_step--;
401
		$form_state['values'] = $form_state['step_information'][$current_step]['stored_values'];
402
	}
403
	$form_state['rebuild'] = TRUE;
404
}
405

    
406

    
407
function _dwca_export_views_step1_submit($form, &$form_state) {
408
	//function dwca_export_views_wizard_submit($form, &$form_state) {
409

    
410
	$variables = $form_state['input'];
411
	$save_variables = '';
412
	$views_map = variable_get('VIEWS_MAP');
413
	$views_to_map = array();	
414

    
415
	foreach ($variables as $key => $value) {
416

    
417
		if(substr($key, 0, 11) == 'dwca_export'){
418
			
419
			if ($value == 1) {
420
				$views_to_map[] = $key;
421
				//drupal_set_message(t('MAPPING VALUE ') . $value);
422
			}
423
					
424
		}
425
	}
426

    
427
	//save a map of the views the user want to change in the database
428
	variable_del('VIEWS_MAP');
429
	variable_set('VIEWS_MAP', $views_to_map);
430
	
431
	$savedvariables = variable_get('VIEWS_MAP');
432
	
433
	//foreach ($variables as $key => $value) {	
434
		
435
		//echo $key . ": " . $value . "...";
436
		//print_r(t('keys ') . $key . t('values ') . $value);
437
	//}
438
	//drupal_set_message(t('The classification view is ') . $save_variables);// . '<pre>' . print_r($form_state,true) . '</pre>');//$save_variables);
439
}
440

    
441
// And now comes the magic of the wizard, the function that should handle all the
442
// inputs from the user on each different step.
443
/**
444
 * Wizard form submit handler.
445
 * - Saves away $form_state['values']
446
 * - Process all the form values.
447
 *
448
 * This demonstration handler just do a drupal_set_message() with the information
449
 * collected on each different step of the wizard.
450
 *
451
 * @param $form
452
 * @param $form_state
453
 *
454
 * @ingroup form_example
455
 */
456
function dwca_export_views_wizard_submit($form, &$form_state) {
457
	$current_step = &$form_state['step'];
458
	$form_state['step_information'][$current_step]['stored_values'] = $form_state['values'];
459

    
460
	// In this case we've completed the final page of the wizard, so process the
461
	// submitted information.
462
	drupal_set_message(t('This information was collected by this wizard:'));
463
	foreach ($form_state['step_information'] as $index => $value) {
464
		// Remove FAPI fields included in the values (form_token, form_id and form_build_id
465
		// This is not required, you may access the values using $value['stored_values']
466
		// but I'm removing them to make a more clear representation of the collected
467
		// information as the complete array will be passed through drupal_set_message().
468
		unset($value['stored_values']['form_id']);
469
		unset($value['stored_values']['form_build_id']);
470
		unset($value['stored_values']['form_token']);
471
		
472
		
473
		//drupal_set_message(t('INFO COLLECTED @result'), array('@result' => print_r($value['stored_values'], TRUE)));
474
		
475

    
476
		// Now show all the values.
477
		if ($index == 2) {
478
			_dwca_export_views_meta_xml_update($value['stored_values']);
479
		//drupal_set_message(t('Step @num collected the following values: <pre>@result</pre>', array('@num' => $index, '@result' => print_r($value['stored_values'], TRUE))));
480
	}
481
}
482
}
483

    
484

    
(2-2/2)