Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

cdm-dataportal / modules / cdm_dataportal / cdm_csv_export / cdm_csv_export.module @ e8074ec5

History | View | Annotate | Download (11.4 KB)

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_CSV_FEATURETREE_UUID', 'cdm_csv_featuretree_uuid');
22

    
23

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

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

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

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

    
76
function update_classification_selected($form, &$form_state) {
77
  unset($form_state['input']['csv_export_taxon_select'], $form_state['values']['csv_export_taxon_select']);
78
  $tree = $form_state['input']['classificationUuid'];
79
  $form['csv_export_taxon_select']['#autocomplete_path'] = 'cdm_dataportal/taxon/autosuggest/'.$tree.'///';
80
  $form['csv_export_taxon_select']['#value'] = '';
81
    return form_builder($form['#id'], $form['csv_export_taxon_select'], $form_state);
82
}
83

    
84
/**
85
 * Creates the drupal form and returns it
86
 *
87
 * @param unknown $form_state
88
 * @return array
89
 *
90
 */
91
function cdm_csv_export_my_form($form_state) {
92
    $tree = variable_get(CDM_TAXONOMICTREE_UUID, FALSE);
93
    $form['csv_export_classification_select'] = array(
94
        '#type' => 'select',
95
        '#title' => t('Classification').':',
96
        '#default_value' => $tree,
97
        '#options' => cdm_get_taxontrees_as_options(FALSE, TRUE),
98
        '#attributes' => array(
99
            'name' => 'classificationUuid',
100
            'onchange' => 'return validateForm()'
101
        ),
102
        '#ajax' => array(
103
          'callback' => 'update_classification_selected',
104
          'wrapper' => 'taxon-selection',
105
          'progress' => array(
106
            'message' => '',
107
            'type' => 'throbber',
108
          ),
109
        )
110
    );
111
    $form['csv_export_taxon_select'] = array(
112
        '#title' => t('Taxon'),
113
        '#type' => 'textfield',
114
        '#prefix' => '<div id="taxon-selection">',
115
        '#suffix' => '</div>',
116
        '#attributes' => array(
117
            'onchange' => 'return validateForm()'
118
        ),
119
    );
120
    if(variable_get('cdm_dataportal_taxon_auto_suggest')){
121
        $form['csv_export_taxon_select']['#autocomplete_path'] = 'cdm_dataportal/taxon/autosuggest////';
122
    }
123
    $form['taxonUuidStore'] = array(
124
        '#type' => 'hidden',
125
        '#value' => '',
126
        '#name' => taxonUuid,
127
        '#attributes' => array(
128
            'id' => 'taxonUuid',
129
        )
130
    );
131

    
132
    $form['redListField'] = array(
133
        '#type' => 'fieldset',
134
        '#title'	=>t('Redlist Attributes'),
135
        '#collapsible' => TRUE,
136
        '#collapsed' => TRUE,
137
    );
138

    
139
    $form['redListField']['csvExportOptions'] = array(
140
        '#type'=>'checkboxes',
141
        '#options' => array(),
142
    );
143

    
144
    // ---- LAYOUT PER FEATURE ---- //
145
    $feature_tree = get_csv_featureTree();
146
    if (isset($feature_tree->root->childNodes)) {
147
        foreach ($feature_tree->root->childNodes as $featureNode) {
148
            if (isset($featureNode->feature)) {
149

    
150
                // Must not exceed 45 characters !!!
151
                $subform_id =  $featureNode->feature->uuid; //LAYOUT_SETTING_PREFIX .
152
                //        $settings = mixed_variable_get($subform_id, FEATURE_TREE_LAYOUT_DEFAULTS);
153
                //        $systemDefaults = unserialize(FEATURE_TREE_LAYOUT_DEFAULTS);
154
                $form['redListField']['csvExportOptions'][$subform_id] = array(
155
                    '#type' => 'checkbox',
156
                    '#title' => $featureNode->feature->representation_L10n,
157
                    '#default_value' => $featureNode->feature->uuid,//$settings,
158
                    '#attributes' => array('value' => $featureNode->feature->uuid,
159
                        'name' => 'features'
160
                    ),
161
                );
162
            }
163
        }
164
    }
165

    
166
    $form['downloadTokenValueId'] = array(
167
        '#type' => 'hidden',
168
        '#attributes' => array('name' => 'downloadTokenValueId',
169
            'id' => 'downloadTokenValueId',
170
        ),
171
    );
172

    
173
    $form['dialogMessage'] = array(
174
        '#type' => 'hidden',
175
        '#attributes' => array(
176
            'name' => 'dialogMessage',
177
            'id' => 'dialogMessage',
178
        ),
179
    );
180

    
181
    $country = variable_get('area_filter_country');
182
    $state = variable_get('area_filter_state');
183
    if($country != null || $state != null){
184
        $form['regionFilter'] = array(
185
            '#type' => 'fieldset',
186
            '#title' => t('Geographical filter'),
187
            '#collapsible' => TRUE,
188
            '#collapsed' => TRUE,
189
        );
190
    }
191

    
192
    $term = cdm_ws_get(CDM_WS_TERM, $country);
193
    $cdmRepresentationTitleCache = cdm_term_representation($term);
194
    if($country != null){
195
        $form['regionFilter']['country'] = array (
196
            '#type' => 'checkbox',
197
            '#title' => t($cdmRepresentationTitleCache),
198
            '#attributes' => array('name' => 'area',
199
                'id' => 'checkall',
200
                'value' =>  $country
201
            ),
202
        );
203
    }
204

    
205
    //GermanFederalStates
206
    if($state != null){
207
        $nameRelationshipTypeOptions = cdm_vocabulary_as_option($state);
208
        asort($nameRelationshipTypeOptions);
209
        $form['regionFilter']['area'] = array(
210
            '#type' => 'checkboxes',
211
            '#description' =>t('Select an area to filter the list of taxa'),
212
            '#options' => $nameRelationshipTypeOptions,
213
            '#attributes' => array('name' => 'area',
214
                'id' => 'area',
215
            ),
216
        );
217
    }
218

    
219
  $form['button'] = array(
220
        '#type'  => 'submit',
221
        '#value' => 'Export',
222
    );
223

    
224
    $form['#action'] = url(variable_get('cdm_csv_export_webservice_url'));
225
    $form['#attributes'] = array(
226
        'name' => 'exportForm',
227
        'onsubmit' => 'return blockUIForDownload()');
228
    return $form;
229
}
230

    
231

    
232

    
233
/**
234
 * Implements Admin configuration hook_menu().
235
 *
236
 * @return array
237
 */
238

    
239
function cdm_csv_export_menu() {
240
 $items = array();
241

    
242
 $items['admin/config/cdm_dataportal/csvexport'] = array(
243
   'title' => 'CDM CSV Export',
244
   'description' => 'Settings of CSV Export module.',
245
   'page callback' => 'drupal_get_form',
246
   'page arguments' => array('cdm_csv_export_admin'),
247
   'access arguments' => array('access administration pages'),
248
   'type' => MENU_NORMAL_ITEM,
249
 );
250
 return $items;
251
}
252

    
253
/**
254
 * Generates the HTML form for the CSV Export Settings.
255
 *
256
 * @return array
257
 */
258
function cdm_csv_export_admin() {
259

    
260
 $form['cdm_csv_export_webservice'] = array(
261
   '#type' => 'fieldset',
262
   '#title' => t('CDM CSV Export web service'),
263
   '#collapsible' => TRUE,
264
   '#collapsed' => FALSE,
265
   '#description' => t('<em>CDM Server</em> makes the dialogue possible with
266
      <em>CDM Data Portal</em> thanks to its web services.'),
267
 );
268

    
269
 $form['cdm_csv_export_webservice']['cdm_csv_export_webservice_url'] = array(
270
   '#type' => 'textfield',
271
   '#title' => t('CSV Export web service URL') . ':',
272
   '#description' => t('This is the URL to the CDM-Server webservice exposing its controller
273
      e.g. <em>"http://localhost:8080/csv/exportRedlist"</em>'),
274
   '#default_value' => variable_get('cdm_csv_export_webservice_url', NULL),
275
 );
276

    
277
 // ---- Regional Filter ---- //
278
 $form['csv_export_area_filter'] = array(
279
   '#type' => 'fieldset',
280
   '#title' => t('CDM CSV Export Area Filter'),
281
   '#collapsible' => TRUE,
282
   '#collapsed' => FALSE,
283
   '#description' => t('The <em>Area Filter</em> can be freely configured to the need
284
       of the specific <em>CDM Data Portal</em> and its catchment area. Right now it will
285
       be only distinguished between two levels. If necessary it can be programmatically
286
       extended for a third level e.g. "continent level".'),
287
 );
288

    
289
 $form['csv_export_area_filter']['area_filter_country'] = array(
290
   '#type' => 'textfield',
291
   '#title' => t('Country Level') . ':',
292
   '#description' => t('This is the UUID for the country level, in order to be able to filter Germany,
293
      you should paste e.g. <em>"7b7c2db5-aa44-4302-bdec-6556fd74b0b9"</em> in the above text field'),
294
   '#default_value' => variable_get('area_filter_country', NULL),
295
 );
296

    
297
 $form['csv_export_area_filter']['area_filter_state'] = array(
298
   '#type' => 'textfield',
299
   '#title' => t('State Level') . ':',
300
   '#description' => t('This is the UUID for the state level, in order to be able to filter the german federal states,
301
      you should paste e.g. <em>"44a448f9-f4ca-49f6-b58c-d27d8a69efdb"</em> in the above text field'),
302
   '#default_value' => variable_get('area_filter_state', NULL),
303
 );
304

    
305
 // ---- FEATURE TREE ---- //
306
 $form['feature_trees'] = array(
307
   '#type' => 'fieldset',
308
   '#title' => t('Features'),
309
   '#collapsible' => TRUE,
310
   '#collapsed' => FALSE,
311
   '#description' => t("This section covers settings related to the taxon's
312
      <em>Feature Tree</em>. The <em>feature tree</em> are the taxon's
313
      features such as description, distribution, common names, etc. that Drupal
314
      will render at the taxon profile page."),
315
 );
316

    
317
 $featureTrees = cdm_get_featureTrees_as_options(TRUE);
318
 $form['feature_trees'][CDM_CSV_FEATURETREE_UUID] = array(
319
     '#type' => 'radios',
320
     '#title' => t('CSV Export Feature Tree sections') . ':',
321
  '#default_value' => variable_get(CDM_CSV_FEATURETREE_UUID, NULL),
322
   '#options' =>  $featureTrees['options'],
323
   '#pre_render' => array('form_pre_render_conditional_form_element', 'radios_prepare_options_suffix'),
324
   '#options_suffixes' => $featureTrees['treeRepresentations'],
325
  '#description' => t('Select the Feature Tree to be displayed at the taxon
326
      profile. Click "Show Details" to see the Feature Tree elements.'
327
   ),
328
 );
329
 return system_settings_form($form);
330
}
331

    
332
/**
333
 * Returns the chosen FeatureTree for the CSV Export Module.
334
 *
335
 * The returned CSV FeatureTree, has been set in the
336
 * CSV Export Module settings (CDM CSV EXPORT -> FEATURES).
337
 * If the chosen FeatureTree is not found in the database,
338
 * the standard feature tree (UUID_DEFAULT_FEATURETREE) will be returned.
339
 *
340
 * @return mixed
341
 *   A cdm FeatureTree object.
342
 */
343

    
344
function get_csv_featureTree() {
345
 static $csv_featureTree;
346

    
347
 if($csv_featureTree == NULL) {
348
  $csv_featureTree = cdm_ws_get(
349
    CDM_WS_FEATURETREE,
350
    variable_get(CDM_CSV_FEATURETREE_UUID, UUID_DEFAULT_FEATURETREE)
351
  );
352
  if (!$csv_featureTree) {
353
   $csv_featureTree = cdm_ws_get(CDM_WS_FEATURETREE, UUID_DEFAULT_FEATURETREE);
354
  }
355
 }
356
 return $csv_featureTree;
357
}
Add picture from clipboard (Maximum size: 40 MB)