Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.1 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
        '#name' => 'taxonName',
115
        '#prefix' => '<div id="taxon-selection">',
116
        '#suffix' => '</div>',
117
    );
118
    if(variable_get('cdm_dataportal_taxon_auto_suggest')){
119
        $form['csv_export_taxon_select']['#autocomplete_path'] = 'cdm_dataportal/taxon/autosuggest////';
120
    }
121

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

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

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

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

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

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

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

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

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

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

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

    
221

    
222

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

    
229
function cdm_csv_export_menu() {
230
 $items = array();
231

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

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

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

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

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

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

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

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

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

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

    
334
function get_csv_featureTree() {
335
 static $csv_featureTree;
336

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