Project

General

Profile

Download (8.91 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
// $Id$
3

    
4
/*
5
 * @file
6
 * cdm_dataportal
7
 * 
8
 * Copyright (C) 2007 EDIT
9
 * European Distributed Institute of Taxonomy 
10
 * http://www.e-taxonomy.eu
11
 * 
12
 * The contents of this file are subject to the Mozilla Public License Version 1.1
13
 * See LICENSE.TXT at the top of this package for the full license terms.
14
 */
15

    
16
require_once('cdm_dataportal.theme.php');
17

    
18
/* ====================== hook implementations ====================== */ 
19

    
20
/**
21
 * Implementation of hook_help()
22
 * 
23
 * Display help and module information
24
 * @param section which section of the site we're displaying help
25
 * @return help text for section
26
 */
27
function cdm_dataportal_help($section='') {
28

    
29
    $out = '';
30
    switch ($section) {
31
        case "admin/modules#description":
32
            $out = t("The dataportal publishes CDM data hosted in a CommunityStore on the web.");
33
            break;
34
    }
35
    return $out;
36
}
37

    
38

    
39
/**
40
 * Implementation of hook_perm()
41
 * 
42
 * Valid permissions for this module
43
 * @return array An array of valid permissions for the portfolio module
44
 */
45
function cdm_dataportal_perm() {
46
    return array(
47
    	'administer cdm_dataportal',
48
        'cdm_dataportal view notes',
49
        //TODO which else permission are required? -> check the WP6 requirements document
50
	);
51
}
52

    
53

    
54
/**
55
 * Implementation of hook_menu()
56
 */
57
function cdm_dataportal_menu($may_cache) {
58
  $items = array();
59
  if ($may_cache) {
60
    
61
    $items[] = array(
62
      'path' => 'admin/settings/cdm_dataportal',
63
      'title' => t('CDM Dataportal'),
64
      'description' => t('Setting for the CDM Dataportal'),
65
      'access' => user_access('administer cdm_dataportal'),
66
      'callback' => 'drupal_get_form',
67
      'callback arguments' => 'cdm_dataportal_settings',
68
      'type' => MENU_NORMAL_ITEM,
69
    );
70
    
71
    $items[] = array(
72
	    'path' => 'cdm_dataportal/names',
73
	    'callback' => 'cdm_dataportal_view_names',
74
	    'access' => true,
75
	    'type' => MENU_CALLBACK, 
76
	    );
77
	    // optional callback arguments: page 
78
	    
79
	$items[] = array(
80
	    'path' => 'cdm_dataportal/taxon',
81
	    'callback' => 'cdm_dataportal_view_taxon',
82
	    'access' => true,
83
	    'type' => MENU_CALLBACK, 
84
	    // expected callback arguments: name_uuid
85
	    );
86
	$items[] = array(
87
      'path' => 'cdm/xml2json',
88
      'callback' => 'cdm_view_xml2json',
89
      'access' => true,
90
      'type' => MENU_CALLBACK, 
91
      );
92
	    
93
  }
94
  return $items;
95
}
96

    
97
/**
98
 * Implementation of hook_block()
99
 * 
100
 * Provides the following blocks:
101
 *  0: list of links useful during development
102
 *
103
 * @param String $op
104
 * @param int $delta
105
 */
106
function cdm_dataportal_block($op='list', $delta=0) {
107
  // listing of blocks, such as on the admin/block page
108
  if ($op == "list") {
109
    $block[0]["info"] = t("CDM DataPortal DevLinks");
110
    return $block;
111
  }
112
  else if ($op == 'view') {
113
    switch($delta){
114
      case 0:
115
        $block['subject'] = t('CDM DataPortal DevLinks');
116
        $block['content'] = '<ul>
117
        <li>'.l('A Taxon Page', cdm_dataportal_taxon_path('5000001-stub')).'</li>
118
        <li>'.l('Name List - page1 of A', 'cdm_dataportal/names/A/1').'</li>
119
        <li>'.l('xml2json', 'cdm/xml2json/namelist;startwith=A.xml').'</li>
120
        </ul>';
121
        return $block;
122
    }
123
  }
124
}
125

    
126

    
127
/**
128
 * Implementation of hook_validate()
129
 *
130
 * @param $element
131
 */
132
function cdm_dataportal_settings_validate($form_id, $form_values){
133
  if (!str_endsWith($form_values['cdm_webservice_url'], '/')) {
134
      form_set_error('cdm_webservice_url', t("The URL to the CDM Web Service must end with a slash: '/'."));
135
    }
136
}
137

    
138

    
139
/* ====================== menu callback functions ====================== */
140

    
141
/**
142
 * Generate main administration form.
143
 *
144
 * @return
145
 *   An array containing form items to place on the module settings page.
146
 */
147
function cdm_dataportal_settings(){
148

    
149
   //TODO: settings are still incomplete, compare with trunk/dataportal/inc/config_default.php.inc
150
    $form['cdm_webservice'] = array(
151
      '#type' => 'fieldset',
152
      '#title' => t('CDM Web Servic'),
153
      '#collapsible' => FALSE,
154
      '#collapsed' => TRUE,
155
    );
156
  
157
   $form['cdm_webservice']['cdm_webservice_url'] =  array(
158
    '#type' => 'textfield',
159
    '#title'         => t('CDM Web Service URL'),
160
    '#description'   => t('The URL of CDM Webservice which delivers the data to be published.'),
161
    '#default_value' => variable_get('cdm_webservice_url', 'http://dev.e-taxonomy.eu/svn/trunk/drupal/modules/cdm_dataportal/cdm_api/ws_stub/'),
162
    );
163
    
164
    $form['cdm_webservice']['cdm_webservice_isStub'] =  array(
165
    '#type' => 'checkbox',
166
    '#title'         => t('Use Web Service Stub'),
167
    '#default_value' => variable_get('cdm_webservice_isStub', 1),
168
    '#description'   => t('Use a static web service stub. Only for development. For further information please refer to the ')
169
                      .l('ws_stub/README.txt', 'http://dev.e-taxonomy.eu/svn/trunk/drupal/modules/cdm_dataportal/cdm/ws_stub/README.txt', array('target'=>'_blank')),
170
    );
171
    
172
    $form['cdm_webservice']['cdm_webservice_type'] =  array(
173
    '#type' => 'select',
174
    '#title'         => t('Web Service Type'),
175
    '#default_value' => variable_get('cdm_webservice_type', 'json'),
176
    '#options' => array(
177
        'xml' => t('XML'),
178
        'json' => t('JSON'),
179
      ),
180
    '#description'   => t('The response data type of the web service.'),
181
     );
182

    
183
   return system_settings_form($form);
184
}
185

    
186

    
187
/**
188
 * Displays a list of the known taxonomic names. Long lists are split up into multiple pages
189
 *
190
 * TODO: parameters are still preliminar
191
 * @param String $page page number to diplay defaults to page 1
192
 * @param boolean $hide_unaccepted whether to hide nams which are not accepted by the current view
193
 */
194
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = false ){
195
  
196
    $taxonSTOs = cdm_ws_name_list($beginsWith, $page, $onlyAccepted, $_SESSION['cdm']['namelist_pagesize']);
197
    /*   
198
     * FIXME the filter for accepted names will be form element, thus this widget 
199
     * should be generated via form api!
200
     */
201
    //$out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
202
    //$out .= theme('cdm_dataportal_widget_names_list', $names, $page);
203
    $out .= theme('cdm_dataportal_names_list', $taxonSTOs);
204
    return $out;
205
}
206

    
207

    
208
/**
209
 * The taxon page gives detailed information on a taxon, it shows:
210
 *  - Taxon name
211
 *  - Full list of synonyms homotypic synonyms on top, followed by the 
212
 *    heterotypic and finally followed by misapplied names. 
213
 *    The list is ordered historically.
214
 *  - All facts associated with the very taxon concept and taxon name. 
215
 *
216
 * @param String $uuid the UUID of the taxon  
217
 */
218
function cdm_dataportal_view_taxon($uuid){
219
 
220
  $taxon = cdm_ws_get_taxon($uuid);
221
  
222
  drupal_set_title($taxon->name->fullname.' '.theme('cdm_nomenclaturalReferenceSTO', $taxon->nomenclaturalReference));
223
  //TODO retrieve complete synonymy and other data 
224
  
225
  $out = theme('cdm_taxon', $taxon); 
226
  return $out;
227

    
228
}
229

    
230

    
231
function cdm_view_xml2json(){
232
  $file = arg(2);
233
  $datastr = get_content(variable_get('cdm_webservice_url', '').$file);
234
  return  xml2json::transformXmlStringToJson($datastr);
235

    
236
}
237

    
238
/* ====================== other functions ====================== */
239

    
240
/**
241
 * Enter description here...
242
 *
243
 * @param String $uuid the UUID of the taxon
244
 * @return the URL
245
 */
246
function cdm_dataportal_taxon_path($uuid){
247
  return 'cdm_dataportal/taxon/'.$uuid;
248
}
249

    
250
/**
251
 * Creates a short taxonname by using the taggename field of NameSTO or NameTO instances.
252
 * If the taggename if empty the fullname will be returned.
253
 *
254
 * @param unknown_type $NameSTO a NameSTO or NameTO instance
255
 * @return string 
256
 */
257
function cdm_dataportal_shortname_of($NameSTO){
258
  
259
  $name = trim(cdm_taggedtext_value($NameSTO->taggedName, 'name'));
260
  if($name){
261
    if( $pos = stripos($name, ' ')){
262
      return substr($name, 0, 1).'. '.substr($name, $pos);
263
    } else {
264
      return $name;
265
    }
266
  } else {
267
    return $NameSTO->fullname;
268
  }
269
}
270

    
271
/**
272
 * @param $str the string to truncate
273
 * @param $len the maximun length
274
 * @param $appendix an optional appendix.
275
 *
276
 * @return the string truncated to the specified length or the original string as given as parameter.
277
 * if an appendix has been defined the resulting string
278
 * will have the specified length inculding the the appendix.
279
 */
280
function str_trunk(&$str, $len, $appendix=''){
281
    if(strlen($str) >= $len )
282
    return  substr($str, 0, $len - strlen($appendix)).$appendix;
283
    else
284
    return $str;
285
}
286

    
287
/**
288
 * @param string $str
289
 * @param string $sub
290
 * @return boolean
291
 */
292
function str_beginsWith( $str, $sub ) {
293
    return ( substr( $str, 0, strlen( $sub ) ) === $sub );
294
}
295

    
296
/**
297
 * 
298
 * @param string $str
299
 * @param string $sub
300
 * @return boolean
301
 */
302
function str_endsWith( $str, $sub ) {
303
    return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
304
}
(3-3/5)