Project

General

Profile

Download (10.2 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
/**
77
 * Creates the drupal form and returns it
78
 *
79
 * @param unknown $form_state
80
 * @return array
81
 *
82
 */
83
function cdm_csv_export_my_form($form_state) {
84
    $form['combobox'] = array(
85
        '#type' => 'select',
86
        '#title' => t('Classification').':',
87
        '#default_value' => variable_get(CDM_TAXONOMICTREE_UUID, FALSE),
88
        '#options' => cdm_get_taxontrees_as_options(),
89
        '#attributes' => array(
90
            'name' => 'taxonNode',
91
            'onchange' => 'return validateForm()'),
92
    );
93
    $form['taxon_select'] = array(
94
        '#title' => t('Taxon'),
95
        '#type' => 'textfield',
96
    );
97
    if(variable_get('cdm_dataportal_taxon_auto_suggest')){
98
        $form['taxon_select']['#autocomplete_path'] = 'cdm_dataportal/taxon/autosuggest';
99
    }
100

    
101
    $form['redListField'] = array(
102
        '#type' => 'fieldset',
103
        '#title'	=>t('Redlist Attributes'),
104
        '#collapsible' => TRUE,
105
        '#collapsed' => TRUE,
106
    );
107

    
108
    $form['redListField']['csvExportOptions'] = array(
109
        '#type'=>'checkboxes',
110
        '#options' => array(),
111
    );
112

    
113
    // ---- LAYOUT PER FEATURE ---- //
114
    $feature_tree = get_csv_featureTree();
115
    if (isset($feature_tree->root->childNodes)) {
116
        foreach ($feature_tree->root->childNodes as $featureNode) {
117
            if (isset($featureNode->feature)) {
118

    
119
                // Must not exceed 45 characters !!!
120
                $subform_id =  $featureNode->feature->uuid; //LAYOUT_SETTING_PREFIX .
121
                //        $settings = mixed_variable_get($subform_id, FEATURE_TREE_LAYOUT_DEFAULTS);
122
                //        $systemDefaults = unserialize(FEATURE_TREE_LAYOUT_DEFAULTS);
123
                $form['redListField']['csvExportOptions'][$subform_id] = array(
124
                    '#type' => 'checkbox',
125
                    '#title' => $featureNode->feature->representation_L10n,
126
                    '#default_value' => $featureNode->feature->uuid,//$settings,
127
                    '#attributes' => array('value' => $featureNode->feature->uuid,
128
                        'name' => 'features'
129
                    ),
130
                );
131
            }
132
        }
133
    }
134

    
135
    $form['downloadTokenValueId'] = array(
136
        '#type' => 'hidden',
137
        '#attributes' => array('name' => 'downloadTokenValueId',
138
            'id' => 'downloadTokenValueId',
139
        ),
140
    );
141

    
142
    $form['dialogMessage'] = array(
143
        '#type' => 'hidden',
144
        '#attributes' => array(
145
            'name' => 'dialogMessage',
146
            'id' => 'dialogMessage',
147
        ),
148
    );
149

    
150
    $country = variable_get('area_filter_country');
151
    $state = variable_get('area_filter_state');
152
    if($country != null || $state != null){
153
        $form['regionFilter'] = array(
154
            '#type' => 'fieldset',
155
            '#title' => t('Geographical filter'),
156
            '#collapsible' => TRUE,
157
            '#collapsed' => TRUE,
158
        );
159
    }
160

    
161
    $term = cdm_ws_get(CDM_WS_TERM, $country);
162
    $cdmRepresentationTitleCache = cdm_term_representation($term);
163
    if($country != null){
164
        $form['regionFilter']['country'] = array (
165
            '#type' => 'checkbox',
166
            '#title' => t($cdmRepresentationTitleCache),
167
            '#attributes' => array('name' => 'area',
168
                'id' => 'checkall',
169
                'value' =>  $country
170
            ),
171
        );
172
    }
173

    
174
    //GermanFederalStates
175
    if($state != null){
176
        $nameRelationshipTypeOptions = cdm_vocabulary_as_option($state);
177
        asort($nameRelationshipTypeOptions);
178
        $form['regionFilter']['area'] = array(
179
            '#type' => 'checkboxes',
180
            '#description' =>t('Select an area to filter the list of taxa'),
181
            '#options' => $nameRelationshipTypeOptions,
182
            '#attributes' => array('name' => 'area',
183
                'id' => 'area',
184
            ),
185
        );
186
    }
187

    
188
  $form['button'] = array(
189
        '#type'  => 'submit',
190
        '#value' => 'Export',
191
    );
192

    
193
    $form['#action'] = url(variable_get('cdm_csv_export_webservice_url'));
194
    $form['#attributes'] = array(
195
        'name' => 'exportForm',
196
        'onsubmit' => 'return blockUIForDownload()');
197
    return $form;
198
}
199

    
200

    
201

    
202
/**
203
 * Implements Admin configuration hook_menu().
204
 *
205
 * @return array
206
 */
207

    
208
function cdm_csv_export_menu() {
209
 $items = array();
210

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

    
222
/**
223
 * Generates the HTML form for the CSV Export Settings.
224
 *
225
 * @return array
226
 */
227
function cdm_csv_export_admin() {
228

    
229
 $form['cdm_csv_export_webservice'] = array(
230
   '#type' => 'fieldset',
231
   '#title' => t('CDM CSV Export web service'),
232
   '#collapsible' => TRUE,
233
   '#collapsed' => FALSE,
234
   '#description' => t('<em>CDM Server</em> makes the dialogue possible with
235
      <em>CDM Data Portal</em> thanks to its web services.'),
236
 );
237

    
238
 $form['cdm_csv_export_webservice']['cdm_csv_export_webservice_url'] = array(
239
   '#type' => 'textfield',
240
   '#title' => t('CSV Export web service URL') . ':',
241
   '#description' => t('This is the URL to the CDM-Server webservice exposing its controller
242
      e.g. <em>"http://localhost:8080/csv/exportRedlist"</em>'),
243
   '#default_value' => variable_get('cdm_csv_export_webservice_url', NULL),
244
 );
245

    
246
 // ---- Regional Filter ---- //
247
 $form['csv_export_area_filter'] = array(
248
   '#type' => 'fieldset',
249
   '#title' => t('CDM CSV Export Area Filter'),
250
   '#collapsible' => TRUE,
251
   '#collapsed' => FALSE,
252
   '#description' => t('The <em>Area Filter</em> can be freely configured to the need
253
       of the specific <em>CDM Data Portal</em> and its catchment area. Right now it will
254
       be only distinguished between two levels. If necessary it can be programmatically
255
       extended for a third level e.g. "continent level".'),
256
 );
257

    
258
 $form['csv_export_area_filter']['area_filter_country'] = array(
259
   '#type' => 'textfield',
260
   '#title' => t('Country Level') . ':',
261
   '#description' => t('This is the UUID for the country level, in order to be able to filter Germany,
262
      you should paste e.g. <em>"7b7c2db5-aa44-4302-bdec-6556fd74b0b9"</em> in the above text field'),
263
   '#default_value' => variable_get('area_filter_country', NULL),
264
 );
265

    
266
 $form['csv_export_area_filter']['area_filter_state'] = array(
267
   '#type' => 'textfield',
268
   '#title' => t('State Level') . ':',
269
   '#description' => t('This is the UUID for the state level, in order to be able to filter the german federal states,
270
      you should paste e.g. <em>"44a448f9-f4ca-49f6-b58c-d27d8a69efdb"</em> in the above text field'),
271
   '#default_value' => variable_get('area_filter_state', NULL),
272
 );
273

    
274
 // ---- FEATURE TREE ---- //
275
 $form['feature_trees'] = array(
276
   '#type' => 'fieldset',
277
   '#title' => t('Features'),
278
   '#collapsible' => TRUE,
279
   '#collapsed' => FALSE,
280
   '#description' => t("This section covers settings related to the taxon's
281
      <em>Feature Tree</em>. The <em>feature tree</em> are the taxon's
282
      features such as description, distribution, common names, etc. that Drupal
283
      will render at the taxon profile page."),
284
 );
285

    
286
 $featureTrees = cdm_get_featureTrees_as_options(TRUE);
287
 $form['feature_trees'][CDM_CSV_FEATURETREE_UUID] = array(
288
     '#type' => 'radios',
289
     '#title' => t('CSV Export Feature Tree sections') . ':',
290
  '#default_value' => variable_get(CDM_CSV_FEATURETREE_UUID, NULL),
291
   '#options' =>  $featureTrees['options'],
292
   '#pre_render' => array('form_pre_render_conditional_form_element', 'radios_prepare_options_suffix'),
293
   '#options_suffixes' => $featureTrees['treeRepresentations'],
294
  '#description' => t('Select the Feature Tree to be displayed at the taxon
295
      profile. Click "Show Details" to see the Feature Tree elements.'
296
   ),
297
 );
298
 return system_settings_form($form);
299
}
300

    
301
/**
302
 * Returns the chosen FeatureTree for the CSV Export Module.
303
 *
304
 * The returned CSV FeatureTree, has been set in the
305
 * CSV Export Module settings (CDM CSV EXPORT -> FEATURES).
306
 * If the chosen FeatureTree is not found in the database,
307
 * the standard feature tree (UUID_DEFAULT_FEATURETREE) will be returned.
308
 *
309
 * @return mixed
310
 *   A cdm FeatureTree object.
311
 */
312

    
313
function get_csv_featureTree() {
314
 static $csv_featureTree;
315

    
316
 if($csv_featureTree == NULL) {
317
  $csv_featureTree = cdm_ws_get(
318
    CDM_WS_FEATURETREE,
319
    variable_get(CDM_CSV_FEATURETREE_UUID, UUID_DEFAULT_FEATURETREE)
320
  );
321
  if (!$csv_featureTree) {
322
   $csv_featureTree = cdm_ws_get(CDM_WS_FEATURETREE, UUID_DEFAULT_FEATURETREE);
323
  }
324
 }
325
 return $csv_featureTree;
326
}
(2-2/2)