Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.2 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
    if(variable_get(SIMPLE_SEARCH_AUTO_SUGGEST)) {
80
        $form['csv_export_taxon_select']['#autocomplete_path'] = 'cdm_dataportal/taxon/autosuggest/' . $tree . '///';
81
    }
82
    $form['csv_export_taxon_select']['#value'] = '';
83
    return form_builder($form['#id'], $form['csv_export_taxon_select'], $form_state);
84
}
85

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

    
124
    $form['redListField'] = array(
125
        '#type' => 'fieldset',
126
        '#title'	=>t('Redlist Attributes'),
127
        '#collapsible' => TRUE,
128
        '#collapsed' => TRUE,
129
    );
130

    
131
    $form['redListField']['csvExportOptions'] = array(
132
        '#type'=>'checkboxes',
133
        '#options' => array(),
134
    );
135

    
136
    // ---- LAYOUT PER FEATURE ---- //
137
    $feature_tree = get_csv_featureTree();
138
    if (isset($feature_tree->root->childNodes)) {
139
        foreach ($feature_tree->root->childNodes as $featureNode) {
140
            if (isset($featureNode->feature)) {
141

    
142
                // Must not exceed 45 characters !!!
143
                $subform_id =  $featureNode->feature->uuid; //LAYOUT_SETTING_PREFIX .
144
                //        $settings = mixed_variable_get($subform_id, FEATURE_TREE_LAYOUT_DEFAULTS);
145
                //        $systemDefaults = unserialize(FEATURE_TREE_LAYOUT_DEFAULTS);
146
                $form['redListField']['csvExportOptions'][$subform_id] = array(
147
                    '#type' => 'checkbox',
148
                    '#title' => $featureNode->feature->representation_L10n,
149
                    '#default_value' => $featureNode->feature->uuid,//$settings,
150
                    '#attributes' => array('value' => $featureNode->feature->uuid,
151
                        'name' => 'features'
152
                    ),
153
                );
154
            }
155
        }
156
    }
157

    
158
    $form['downloadTokenValueId'] = array(
159
        '#type' => 'hidden',
160
        '#attributes' => array('name' => 'downloadTokenValueId',
161
            'id' => 'downloadTokenValueId',
162
        ),
163
    );
164

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

    
173
    $country = variable_get('area_filter_country');
174
    $state = variable_get('area_filter_state');
175
    if($country != null || $state != null){
176
        $form['regionFilter'] = array(
177
            '#type' => 'fieldset',
178
            '#title' => t('Geographical filter'),
179
            '#collapsible' => TRUE,
180
            '#collapsed' => TRUE,
181
        );
182
    }
183

    
184
    $term = cdm_ws_get(CDM_WS_TERM, $country);
185
  $representation_title_cache = cdm_term_representation($term);
186
    if($country != null){
187
        $form['regionFilter']['country'] = array (
188
            '#type' => 'checkbox',
189
    '#title' => t('@representation-title-cache', array('@representation-title-cache' => $representation_title_cache)),
190
            '#attributes' => array('name' => 'area',
191
                'id' => 'checkall',
192
                'value' =>  $country
193
            ),
194
        );
195
    }
196

    
197
    //GermanFederalStates
198
    if($state != null){
199
        $nameRelationshipTypeOptions = cdm_vocabulary_as_option($state);
200
        asort($nameRelationshipTypeOptions);
201
        $form['regionFilter']['area'] = array(
202
            '#type' => 'checkboxes',
203
            '#description' =>t('Select an area to filter the list of taxa'),
204
            '#options' => $nameRelationshipTypeOptions,
205
            '#attributes' => array('name' => 'area',
206
                'id' => 'area',
207
            ),
208
        );
209
    }
210

    
211
  $form['button'] = array(
212
        '#type'  => 'submit',
213
        '#value' => 'Export',
214
    );
215

    
216
    $form['#action'] = url(variable_get('cdm_csv_export_webservice_url'));
217
    $form['#attributes'] = array(
218
        'name' => 'exportForm',
219
        'onsubmit' => 'return blockUIForDownload()');
220
    return $form;
221
}
222

    
223

    
224

    
225
/**
226
 * Implements Admin configuration hook_menu().
227
 *
228
 * @return array
229
 */
230

    
231
function cdm_csv_export_menu() {
232
 $items = array();
233

    
234
 $items['admin/config/cdm_dataportal/csvexport'] = array(
235
   'title' => 'CDM CSV Export',
236
   'description' => 'Settings of CSV Export module.',
237
   'page callback' => 'drupal_get_form',
238
   'page arguments' => array('cdm_csv_export_admin'),
239
   'access arguments' => array('access administration pages'),
240
   'type' => MENU_NORMAL_ITEM,
241
 );
242
 return $items;
243
}
244

    
245
/**
246
 * Generates the HTML form for the CSV Export Settings.
247
 *
248
 * @return array
249
 */
250
function cdm_csv_export_admin() {
251

    
252
 $form['cdm_csv_export_webservice'] = array(
253
   '#type' => 'fieldset',
254
   '#title' => t('CDM CSV Export web service'),
255
   '#collapsible' => TRUE,
256
   '#collapsed' => FALSE,
257
   '#description' => t('<em>CDM Server</em> makes the dialogue possible with
258
      <em>CDM Data Portal</em> thanks to its web services.'),
259
 );
260

    
261
 $form['cdm_csv_export_webservice']['cdm_csv_export_webservice_url'] = array(
262
   '#type' => 'textfield',
263
   '#title' => t('CSV Export web service URL') . ':',
264
   '#description' => t('This is the URL to the CDM-Server webservice exposing its controller
265
      e.g. <em>"http://localhost:8080/csv/exportRedlist"</em>'),
266
   '#default_value' => variable_get('cdm_csv_export_webservice_url', NULL),
267
 );
268

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

    
281
 $form['csv_export_area_filter']['area_filter_country'] = array(
282
   '#type' => 'textfield',
283
   '#title' => t('Country Level') . ':',
284
   '#description' => t('This is the UUID for the country level, in order to be able to filter Germany,
285
      you should paste e.g. <em>"7b7c2db5-aa44-4302-bdec-6556fd74b0b9"</em> in the above text field'),
286
   '#default_value' => variable_get('area_filter_country', NULL),
287
 );
288

    
289
 $form['csv_export_area_filter']['area_filter_state'] = array(
290
   '#type' => 'textfield',
291
   '#title' => t('State Level') . ':',
292
   '#description' => t('This is the UUID for the state level, in order to be able to filter the german federal states,
293
      you should paste e.g. <em>"44a448f9-f4ca-49f6-b58c-d27d8a69efdb"</em> in the above text field'),
294
   '#default_value' => variable_get('area_filter_state', NULL),
295
 );
296

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

    
309
 $featureTrees = cdm_get_featureTrees_as_options(TRUE);
310
 $form['feature_trees'][CDM_CSV_FEATURETREE_UUID] = array(
311
     '#type' => 'radios',
312
     '#title' => t('CSV Export Feature Tree sections') . ':',
313
  '#default_value' => variable_get(CDM_CSV_FEATURETREE_UUID, NULL),
314
   '#options' =>  $featureTrees['options'],
315
   '#pre_render' => array('form_pre_render_conditional_form_element', 'radios_prepare_options_suffix'),
316
   '#options_suffixes' => $featureTrees['treeRepresentations'],
317
  '#description' => t('Select the Feature Tree to be displayed at the taxon
318
      profile. Click "Show Details" to see the Feature Tree elements.'
319
   ),
320
 );
321
 return system_settings_form($form);
322
}
323

    
324
/**
325
 * Returns the chosen FeatureTree for the CSV Export Module.
326
 *
327
 * The returned CSV FeatureTree, has been set in the
328
 * CSV Export Module settings (CDM CSV EXPORT -> FEATURES).
329
 * If the chosen FeatureTree is not found in the database,
330
 * the standard feature tree (UUID_DEFAULT_FEATURETREE) will be returned.
331
 *
332
 * @return mixed
333
 *   A cdm FeatureTree object.
334
 */
335

    
336
function get_csv_featureTree() {
337
 static $csv_featureTree;
338

    
339
 if($csv_featureTree == NULL) {
340
  $csv_featureTree = cdm_ws_get(
341
    CDM_WS_FEATURETREE,
342
    variable_get(CDM_CSV_FEATURETREE_UUID, UUID_DEFAULT_FEATURETREE)
343
  );
344
  if (!$csv_featureTree) {
345
   $csv_featureTree = cdm_ws_get(CDM_WS_FEATURETREE, UUID_DEFAULT_FEATURETREE);
346
  }
347
 }
348
 return $csv_featureTree;
349
}
Add picture from clipboard (Maximum size: 40 MB)