Project

General

Profile

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

    
3
/**
4
 * @file
5
 * Allows to export classifications into a flat csv file.
6
 *
7
 * @copyright
8
 *   (C) 2007-2012 EDIT
9
 *   European Distributed Institute of Taxonomy
10
 *   http://www.e-taxonomy.eu
11
 *
12
 *   The contents of this module are subject to the Mozilla
13
 *   Public License Version 1.1.
14
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
15
 *
16
 * @author
17
 * Oppermann, Alexander <a.oppermann@BGBM.org>
18
 *
19
 */
20

    
21
define('CDM_WS_TERM', 'term/$0');
22
define('CDM_CSV_FEATURETREE_UUID', 'cdm_csv_featuretree_uuid');
23

    
24

    
25

    
26
/**
27
 * 
28
 * @param unknown $path
29
 *  Which path of the site we're using to display help
30
 * @param unknown $arg
31
 *  Array that holds the current path as returned from arg() function
32
 * @return string
33
 */
34
function cdm_csv_export_help($path, $arg){
35

    
36
switch ($path) {
37
  case "admin/help#cdm_csv_export":
38
  return '<h1>'.t('CDM CSV Export Help'). '</h1>'. '<p>' . t("In order 
39
    to export the data with special export options, it is essential 
40
    to configure a feature tree with the desired features. Then set
41
    this feature tree as the default feature tree for this portal."
42
    ) . '</p>';
43
  break;
44
  }
45
}
46

    
47
/**
48
 * Implements hook_block_info() 
49
 * 
50
 * Prepares the content for the help menu
51
 * 
52
 * @return array
53
 */
54
function cdm_csv_export_block_info() {
55
  $blocks['cdm_csv_export'] = array(
56
    'info' => t('CDM CSV Export Module'), //The name that will appear in the block list.
57
    'cache' => DRUPAL_CACHE_PER_ROLE, //Default
58
  );
59
  return $blocks;
60
}
61

    
62
/**
63
 * Implements hook_block_view().
64
 * 
65
 * Prepares the contents of the block.
66
 *  
67
 * @param string $delta
68
 * @return array
69
 */
70
function cdm_csv_export_block_view($delta='') {
71
  $block['subject'] = t('Export into CSV');
72
  $block['content']= array(
73
      drupal_get_form('cdm_csv_export_my_form'),
74
  );
75
  return $block;
76
}
77

    
78
/**
79
 * Creates the drupal form and returns it
80
 * 
81
 * @param unknown $form_state
82
 * @return array
83
 * 
84
 */
85
function cdm_csv_export_my_form($form_state) {
86
  $form['combobox'] = array(
87
    '#type' => 'select',
88
    '#title' => t('Classification').':',
89
    '#default_value' => variable_get(CDM_TAXONOMICTREE_UUID, FALSE),
90
    '#options' => cdm_get_taxontrees_as_options(),
91
    '#attributes' => array(
92
         'name' => 'classification',
93
          'onchange' => 'return validateForm()'),
94
  );
95

    
96
  $form['redListField'] = array(
97
      '#type' => 'fieldset',
98
      '#title'	=>t('Redlist Attributes'),
99
      '#collapsible' => TRUE,
100
      '#collapsed' => TRUE,
101
    );
102
  
103
  $form['redListField']['csvExportOptions'] = array(
104
      '#type'=>'checkboxes',
105
      '#options' => array(),
106
  );
107
    
108
   // ---- LAYOUT PER FEATURE ---- //
109
$feature_tree = get_csv_featureTree();
110
  if (isset($feature_tree->root->children)) {
111
    foreach ($feature_tree->root->children as $featureNode) {
112
      if (isset($featureNode->feature)) {
113

    
114
       // Must not exceed 45 characters !!!
115
        $subform_id =  $featureNode->feature->uuid; //LAYOUT_SETTING_PREFIX .
116
        $settings = mixed_variable_get($subform_id, FEATURE_TREE_LAYOUT_DEFAULTS);
117
        $systemDefaults = unserialize(FEATURE_TREE_LAYOUT_DEFAULTS);
118
        $form['redListField']['csvExportOptions'][$subform_id] = array(
119
          '#type' => 'checkbox',
120
          '#title' => $featureNode->feature->representation_L10n,
121
          '#default_value' => $featureNode->feature->uuid,//$settings,
122
          '#attributes' => array('value' => $featureNode->feature->uuid,
123
                                  'name' => 'features'
124
            ),
125
        );
126
      }
127
    }
128
  }
129
  
130
  $form['downloadTokenValueId'] = array(
131
    '#type' => 'hidden',
132
    '#attributes' => array('name' => 'downloadTokenValueId',
133
                           'id' => 'downloadTokenValueId',                     
134
      ),
135
    );
136
  
137
  $form['dialogMessage'] = array(
138
    '#type' => 'hidden',
139
    '#attributes' => array(
140
      'name' => 'dialogMessage',
141
      'id' => 'dialogMessage',
142
    ),
143
  );
144
  
145
  $country = variable_get('area_filter_country');
146
  $state = variable_get('area_filter_state');
147
if($country != null || $state != null){
148
  $form['regionFilter'] = array(
149
    '#type' => 'fieldset',
150
    '#title' => t('Geographical filter'),
151
    '#collapsible' => TRUE,
152
    '#collapsed' => TRUE,
153
  );
154
 }
155

    
156
  $term = cdm_ws_get(CDM_WS_TERM, $country);
157
  $cdmRepresentationTitleCache = cdm_term_representation($term);
158
  if($country != null){
159
  $form['regionFilter']['country'] = array (
160
    '#type' => 'checkbox',
161
    '#title' => t($cdmRepresentationTitleCache),
162
    '#attributes' => array('name' => 'area',
163
                            'id' => 'checkall',
164
                            'value' =>  $country
165
     ),
166
    );
167
  }
168
  
169
  //GermanFederalStates
170
  if($state != null){
171
  $nameRelationshipTypeOptions = cdm_Vocabulary_as_option($state);
172
  asort($nameRelationshipTypeOptions);
173
  $form['regionFilter']['area'] = array(
174
    '#type' => 'checkboxes',
175
    '#description' =>t('Select an area to filter the list of taxa'),
176
    '#options' => $nameRelationshipTypeOptions,
177
    '#attributes' => array('name' => 'area',
178
                            'id' => 'area',
179
      ),
180
    );
181
  }
182
  
183
  $form['button'] = array(
184
    '#type'  => 'submit',
185
    '#value' => 'Export',
186
    );
187
  
188
  $form['#action'] = url(variable_get('cdm_csv_export_webservice_url'));
189
  $form['#attributes'] = array(
190
                 'name' => 'exportForm',
191
                 'onsubmit' => 'return blockUIForDownload()');
192
 return $form;
193
}
194

    
195

    
196

    
197
/**
198
 * Implements Admin configuration hook_menu().
199
 * 
200
 * @return array
201
 */
202

    
203
function cdm_csv_export_menu() {
204
 $items = array();
205

    
206
 $items['admin/config/cdm_dataportal/csvexport'] = array(
207
   'title' => 'CDM CSV Export',
208
   'description' => 'Settings of CSV Export module.',
209
   'page callback' => 'drupal_get_form',
210
   'page arguments' => array('cdm_csv_export_admin'),
211
   'access arguments' => array('access administration pages'),
212
   'type' => MENU_NORMAL_ITEM,
213
 );
214
 return $items;
215
}
216

    
217
/**
218
 * Generates the HTML form for the CSV Export Settings.
219
 * 
220
 * @return array
221
 */
222
function cdm_csv_export_admin() {
223
 
224
 $form['cdm_csv_export_webservice'] = array(
225
   '#type' => 'fieldset',
226
   '#title' => t('CDM CSV Export web service'),
227
   '#collapsible' => TRUE,
228
   '#collapsed' => FALSE,
229
   '#description' => t('<em>CDM Server</em> makes the dialogue possible with
230
      <em>CDM Data Portal</em> thanks to its web services.'),
231
 );
232
 
233
 $form['cdm_csv_export_webservice']['cdm_csv_export_webservice_url'] = array(
234
   '#type' => 'textfield',
235
   '#title' => t('CSV Export web service URL') . ':',
236
   '#description' => t('This is the URL to the CDM-Server webservice exposing its controller
237
      e.g. <em>"http://localhost:8080/csv/exportRedlist"</em>'),
238
   '#default_value' => variable_get('cdm_csv_export_webservice_url', NULL),
239
 );
240
 
241
 // ---- Regional Filter ---- //
242
 $form['csv_export_area_filter'] = array(
243
   '#type' => 'fieldset',
244
   '#title' => t('CDM CSV Export Area Filter'),
245
   '#collapsible' => TRUE,
246
   '#collapsed' => FALSE,
247
   '#description' => t('The <em>Area Filter</em> can be freely configured to the need 
248
       of the specific <em>CDM Data Portal</em> and its catchment area. Right now it will 
249
       be only distinguished between two levels. If necessary it can be programmatically
250
       extended for a third level e.g. "continent level".'),
251
 );
252
 
253
 $form['csv_export_area_filter']['area_filter_country'] = array(
254
   '#type' => 'textfield',
255
   '#title' => t('Country Level') . ':',
256
   '#description' => t('This is the UUID for the country level, in order to be able to filter Germany,
257
      you should paste e.g. <em>"7b7c2db5-aa44-4302-bdec-6556fd74b0b9"</em> in the above text field'),
258
   '#default_value' => variable_get('area_filter_country', NULL),
259
 );
260
 
261
 $form['csv_export_area_filter']['area_filter_state'] = array(
262
   '#type' => 'textfield',
263
   '#title' => t('State Level') . ':',
264
   '#description' => t('This is the UUID for the state level, in order to be able to filter the german federal states,
265
      you should paste e.g. <em>"44a448f9-f4ca-49f6-b58c-d27d8a69efdb"</em> in the above text field'),
266
   '#default_value' => variable_get('area_filter_state', NULL),
267
 );
268
 
269
 // ---- FEATURE TREE ---- //
270
 $form['feature_trees'] = array(
271
   '#type' => 'fieldset',
272
   '#title' => t('Features'),
273
   '#collapsible' => TRUE,
274
   '#collapsed' => FALSE,
275
   '#description' => t("This section covers settings related to the taxon's
276
      <em>Feature Tree</em>. The <em>feature tree</em> are the taxon's
277
      features such as description, distribution, common names, etc. that Drupal
278
      will render at the taxon profile page."),
279
 );
280
 
281
 $featureTrees = get_csv_featureTree_as_options(TRUE);
282
 $form['feature_trees'][CDM_CSV_FEATURETREE_UUID] = array(
283
   '#type' => 'radios',
284
   '#title' => t('CSV Export Feature Tree sections') . ':',
285
   '#default_value' => variable_get(CDM_CSV_FEATURETREE_UUID, NULL),
286
   '#options' =>  $featureTrees['options'],
287
   '#field_suffix' => $featureTrees['treeRepresentations'],
288
 );
289
 return system_settings_form($form);
290
}
291

    
292
/**
293
 * Returns the chosen FeatureTree for the CSV Export Module.
294
 *
295
 * The returned CSV FeatureTree, has been set in the
296
 * CSV Export Module settings (CDM CSV EXPORT -> FEATURES).
297
 * If the chosen FeatureTree is not found in the database,
298
 * the standard feature tree (UUID_DEFAULT_FEATURETREE) will be returned.
299
 *
300
 * @return mixed
301
 *   A cdm FeatureTree object.
302
 */
303

    
304
function get_csv_featureTree() {
305
 static $csv_featureTree;
306

    
307
 if($csv_featureTree == NULL) {
308
  $csv_featureTree = cdm_ws_get(
309
    CDM_WS_FEATURETREE,
310
    variable_get(CDM_CSV_FEATURETREE_UUID, UUID_DEFAULT_FEATURETREE)
311
  );
312
  if (!$csv_featureTree) {
313
   $csv_featureTree = cdm_ws_get(CDM_WS_FEATURETREE, UUID_DEFAULT_FEATURETREE);
314
  }
315
 }
316
 return $csv_featureTree;
317
}
318

    
319
/**
320
 * Returns an array with all available Feature 
321
 * Trees and the representations of the selected
322
 * Feature Tree as a detail view.
323
 * 
324
 * @param boolean $add_default_feature_free
325
 * @return array 
326
 * associative array with following keys:
327
 * -options: Returns an array with all available Feature Trees
328
 * -treeRepresentations: Returns representations of the selected Feature Tree as a detail view
329
 * 
330
 */
331

    
332
function get_csv_featureTree_as_options($add_default_feature_tree = FALSE) {
333
 $feature_trees = array();
334
 
335
 // Set tree that contains all features.
336
 if ($add_default_feature_tree) {
337
  $feature_trees[UUID_DEFAULT_FEATURETREE] = t('Default Featuretree (contains all features)');
338
 }
339
 
340
 // Get features from database.
341
 $persisted_trees = cdm_ws_get(CDM_WS_FEATURETREES);
342
 $tree_representation = NULL;
343
 if (is_array($persisted_trees)) {
344

    
345
  foreach ($persisted_trees as $featureTree) {
346
   // Do not add the DEFAULT_FEATURETREE again,
347
   if ($featureTree->uuid == UUID_DEFAULT_FEATURETREE) {
348
    continue;
349
   }
350
   $feature_trees[$featureTree->uuid] = $featureTree->titleCache;
351
  }
352
 }
353
//Feature Tree Details
354
 $selected_feature_tree = get_csv_featureTree();
355
 //  $tree_representation = $selected_feature_tree->titleCache;
356
 $tree_representation = '';
357
 if (is_array($selected_feature_tree->root->children) && count($selected_feature_tree->root->children) > 0) {
358

    
359
  // Render the hierarchic tree structure.
360
  $treeDetails = '<div class="featuretree_structure">'
361
    . theme('FeatureTree_hierarchy', array('FeatureTreeUuid' => $selected_feature_tree->uuid))
362
    . '</div>';
363

    
364
  $form = array();
365
  $form['featureTree-' . $selected_feature_tree->uuid] = array(
366
    '#type' => 'fieldset',
367
    '#title' => 'Show details',
368
    '#attributes' => array('class' => array('collapsible collapsed')),
369
  );
370
  $form['featureTree-' . $selected_feature_tree->uuid]['details'] = array(
371
    '#markup' => $treeDetails,
372
  );
373
  $tree_representation .= drupal_render($form);
374
 }
375
 return array('options' => $feature_trees, 'treeRepresentations' => $tree_representation);
376
}
(2-2/2)