Project

General

Profile

Download (10.9 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_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 taxon_selection_update($form, &$form_state) {
77
  unset($form_state['input']['taxon_select'], $form_state['values']['taxon_select']);
78
  $tree = $form_state['input']['taxonNode'];
79
  $form['taxon_select']['#autocomplete_path'] = 'cdm_dataportal/taxon/autosuggest/'.$tree.'///';
80
  $form['taxon_select']['#value'] = '';
81
  return form_builder($form['#id'], $form['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['combobox'] = 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' => 'taxonNode',
100
            'onchange' => 'return validateForm()'
101
        ),
102
        '#ajax' => array(
103
          'callback' => 'taxon_selection_update',
104
          'wrapper' => 'taxon-selection',
105
          'progress' => array(
106
            'message' => '',
107
            'type' => 'throbber',
108
          ),
109
        )
110
    );
111
    $form['taxon_select'] = array(
112
        '#title' => t('Taxon'),
113
        '#type' => 'textfield',
114
        '#prefix' => '<div id="taxon-selection">',
115
        '#suffix' => '</div>'
116
    );
117
    if(variable_get('cdm_dataportal_taxon_auto_suggest')){
118
        $form['taxon_select']['#autocomplete_path'] = 'cdm_dataportal/taxon/autosuggest////';
119
    }
120

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

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

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

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

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

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

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

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

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

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

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

    
220

    
221

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

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

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

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

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

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

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

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

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

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

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

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

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

    
336
 if($csv_featureTree == NULL) {
337
  $csv_featureTree = cdm_ws_get(
338
    CDM_WS_FEATURETREE,
339
    variable_get(CDM_CSV_FEATURETREE_UUID, UUID_DEFAULT_FEATURETREE)
340
  );
341
  if (!$csv_featureTree) {
342
   $csv_featureTree = cdm_ws_get(CDM_WS_FEATURETREE, UUID_DEFAULT_FEATURETREE);
343
  }
344
 }
345
 return $csv_featureTree;
346
}
(2-2/2)