Project

General

Profile

« Previous | Next » 

Revision 8ff851c6

Added by Andreas Kohlbecker over 14 years ago

Framework to tread and use cdm entities as real Drupal nodes. Example implementation for taxon.

View differences:

.gitattributes
8 8
modules/cdm_dataportal/cdm_api/cdm_api.info -text
9 9
modules/cdm_dataportal/cdm_api/cdm_api.install -text
10 10
modules/cdm_dataportal/cdm_api/cdm_api.module -text
11
modules/cdm_dataportal/cdm_api/cdm_node.php -text
11 12
modules/cdm_dataportal/cdm_api/commons.php -text
12 13
modules/cdm_dataportal/cdm_api/json/JSON.php -text
13 14
modules/cdm_dataportal/cdm_api/json/LICENSE -text
......
190 191
modules/cdm_dataportal/js/jquery.imagetool.min.js -text
191 192
modules/cdm_dataportal/js/thickbox/cdm_thickbox.css -text
192 193
modules/cdm_dataportal/js/thickbox/thickbox.js -text
194
modules/cdm_dataportal/node_types.php -text
193 195
modules/cdm_dataportal/translations/cdm_dataportal.pot.template -text
194 196
modules/countrypresence/country-earth.dbf -text
195 197
modules/countrypresence/country-earth.prj -text
modules/cdm_dataportal/cdm_api/cdm_api.install
16 16
    INDEX expire (expire)
17 17
    ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
18 18
  
19
  db_query("
20
    CREATE TABLE {node_cdm} (
21
      `nid` int(11) NOT NULL,
22
      `wsuri` varchar(255) default NULL,
23
      `cdmtype` varchar(255) default NULL,
24
      `uuid` varchar(255) default NULL,
25
      PRIMARY KEY  (`uuid`)
26
    ) /*!40100 DEFAULT CHARACTER SET UTF8 */  
27
    ");
28
  
19 29
}
20 30

  
21 31
/**
modules/cdm_dataportal/cdm_api/cdm_api.module
18 18
require_once ('xml2json.php');
19 19
require_once ('commons.php');
20 20
require_once ('uuids.php');
21
require_once ('cdm_node.php');
21 22

  
22 23
define(DEFAULT_TAXONTREE_RANKLIMIT, '');//TODO Genus UUID
23 24

  
......
33 34

  
34 35
  if( function_exists('curl_init') ){
35 36
    $requirements['cdm_api']['description'] = ''; // description below title is not jet in use
36
    $requirements['cdm_api']['value'] =  'CURL php extension is available.';
37
    $requirements['cdm_api']['value'] =  'CURL php extension is available and will be used by the cdm api. HTTP requests thus will be up to 20x faster';
37 38
  } else {
38
    $requirements['cdm_api']['value'] =  'CURL php extension is missing.';
39
    $requirements['cdm_api']['value'] =  'CURL php extension is missing. If CURL lib is installed HTTP requests will be up to 20x faster';
39 40
  }
40 41

  
41 42
  //FIXME: once _get_content_fsockopen is implemented change  severity to  REQUIREMENT_WARNING,
42
  $requirements['cdm_api']['severity'] =  (function_exists('curl_init') ? REQUIREMENT_OK : REQUIREMENT_ERROR);
43
  $requirements['cdm_api']['severity'] =  (function_exists('curl_init') ? REQUIREMENT_OK : REQUIREMENT_INFO);
43 44

  
44 45
  return $requirements;
45 46
}
......
168 169
      );
169 170
}
170 171

  
171

  
172 172
// ----------------------------------------------------------- //
173 173

  
174 174

  
......
769 769

  
770 770
  if(!$header){
771 771
    $header = array();
772
    $header[] = 'Accept: '.(variable_get('cdm_webservice_type', 'json') == 'json' ? 'application/json' : 'text/xml');
773
    $header[] = 'Accept-Language: '.$locale;
774
    $header[] = 'Accept-Charset: UTF-8';
772
    $header['Accept'] = (variable_get('cdm_webservice_type', 'json') == 'json' ? 'application/json' : 'text/xml');
773
    $header['Accept-Language'] = $locale;
774
    $header['Accept-Charset'] = 'UTF-8';
775 775
  }
776 776

  
777 777
  if(function_exists('curl_init')){
778

  
779 778
    // use the CURL lib if installed it is supposed to be 20x faster
780 779
    return _http_request_using_curl($uri, $header, $method, $parameters);
781 780
  } else {
......
784 783
}
785 784

  
786 785
function _http_request_using_fsockopen($uri, $header = array(), $method = "GET"){
787
  //FIXME implement get_content_fsockopen($uri);
788
  watchdog('CDM_API', '_http_request_fsockopen - UNIMPLEMENTED', WATCHDOG_ERROR);
789
  return false;
786
 $response = drupal_http_request($uri, $header, $method);
787
 return $response->data;
790 788
}
791 789

  
792 790

  
......
798 796
 *
799 797
 * @author Luiz Miguel Axcar (lmaxcar@yahoo.com.br)
800 798
 */
801
function _http_request_using_curl($uri, $header = array(), $method = "GET", $parameters = array())
799
function _http_request_using_curl($uri, $headers = array(), $method = "GET", $parameters = array())
802 800
{
803 801
  $ch = curl_init();
804 802

  
......
811 809
      curl_setopt ($ch, CURLOPT_PROXYUSERPWD, variable_get('cdm_webservice_proxy_usr', '').':'.variable_get('cdm_webservice_proxy_pwd', ''));
812 810
    }
813 811
  }
814
  // set headers
815
  curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
812
  // modify headers array to be used by curl
813
  foreach($headers as $header_key=>$header_val){
814
    $curl_headers[] = $header_key.': '.$header_val;
815
  }
816
  curl_setopt ($ch, CURLOPT_HTTPHEADER, $curl_headers);
816 817
  // set method if not default
817 818
  if($method != "GET"){
818 819
    if($method == "POST"){
modules/cdm_dataportal/cdm_api/cdm_node.php
1
<?php
2

  
3
function cdm_load_node($nodetype, $uuid, $title){
4
  
5
  // try to find node id
6
  $cdmnode = db_fetch_object(db_query('SELECT nid, cdmtype FROM {node_cdm} WHERE wsuri = \'%s\' AND cdmtype = \'%s\' AND uuid = \'%s\''
7
      , variable_get('cdm_webservice_url', NULL),  $nodetype, $uuid));
8
    
9
  if(is_numeric($cdmnode->nid)) {
10
    $node = node_load($cdmnode->nid);
11
  } else {
12
    
13
    // Create a new node
14
    $node->type = 'cdm_'.$nodetype;
15
    // use just the plain text of the HTML title
16
    $values['title'] = filter_xss($title, array()); 
17
    // limit length to the max length of the database field 128  
18
    $values['title'] = substr($values['title'], 0, 128);
19

  
20
    // preserve the current messages but before  saveing the node, 
21
    $messages = drupal_set_message();
22

  
23
    $result = drupal_execute($node->type.'_node_form', $values, $node);
24
    
25
    // restore the messages
26
    if(isset($messages)){
27
      $_SESSION['messages'] = $messages;
28
    }
29
    
30
    if(!is_array($result)){
31
      // result should contain the path the newly created node; e.g.: node/32
32
      $pathelements = explode('/', $result);
33
      $nid = array_pop($pathelements); 
34
      $node->nid = $nid;
35
      
36
      db_query('INSERT INTO {node_cdm} (nid, wsuri, cdmtype, uuid) VALUES (%d, \'%s\', \'%s\', \'%s\');'
37
        , $nid, variable_get('cdm_webservice_url', NULL),  $nodetype, $uuid);
38
      
39
    } else {
40
      drupal_set_message(t('Could not create node for ' . $nodetype),'error');
41
    }
42
  }
43
  
44
  return $node;
45
}
46

  
47

  
48
/**
49
 * 
50
 * @param $cdm_node_type one of the NODETYPE_* constants 
51
 * @param $uuid
52
 * @param $content
53
 * @param $title
54
 * 
55
 */
56
function cdm_node_show($cdm_node_type, $uuid, $title, $content){
57
    $node = cdm_load_node($cdm_node_type, $uuid, $title);
58
    drupal_set_title($title);
59
    cdm_add_node_content($node, $content, variable_get('cdm_content_weight', -1));
60
    return node_show($node, null); //TODO is using null for cid OK?
61
}
62

  
63
 
64
/**
65
 * 
66
 * @param $node
67
 * @param $content
68
 * @param $weight
69
 * @return unknown_type
70
 */
71
function cdm_add_node_content(&$node, &$content, $weight){
72
    $cdm_content = array(
73
        '#value' => $content, 
74
        '#weight' => variable_get('cdm_content_weight', -1)
75
    );
76
    $node->content['cdm'] = $cdm_content;
77
}
modules/cdm_dataportal/cdm_dataportal.module
11 11
 *
12 12
 */
13 13

  
14
require_once('node_types.php');
14 15
require_once('cdm_dataportal.theme.php');
15 16

  
17

  
16 18
define(CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE, 25);
17 19

  
18 20
/* ====================== hook implementations ====================== */
......
89 91
   
90 92
    $items[] = array(
91 93
      'path' => 'cdm_dataportal/taxon',
92
      'callback' => 'cdm_dataportal_view_taxon',
94
      'callback' => 'cdm_dataportal_taxon_page_view',
93 95
      'access' => true,
94 96
      'type' => MENU_CALLBACK,
95 97
    // expected callback arguments: uuid
......
149 151
       $items[] = array(
150 152
      'path' => 'cdm_dataportal/taxon/'.arg(2),
151 153
      'title' => theme('cdm_taxonpage_tab', 'General'),
152
      'callback' => 'cdm_dataportal_view_taxon',
154
      'callback' => 'cdm_dataportal_taxon_page_view',
153 155
      'access' => true,
154 156
      'type' => MENU_CALLBACK,
155 157
      'weight' => 1,
......
160 162
      $items[] = array(
161 163
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/description',
162 164
      'title' => theme('cdm_taxonpage_tab', 'General'),
163
      'callback' => 'cdm_dataportal_view_taxon',
165
      'callback' => 'cdm_dataportal_taxon_page_view',
164 166
      'access' => true,
165 167
      'type' => MENU_DEFAULT_LOCAL_TASK,
166 168
      'weight' => 2,
......
171 173
      $items[] = array(
172 174
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/synonymy',
173 175
      'title' => theme('cdm_taxonpage_tab', 'Synonymy'),
174
      'callback' => 'cdm_dataportal_view_taxon',
176
      'callback' => 'cdm_dataportal_taxon_page_view',
175 177
      'access' => true,
176 178
      'type' => MENU_LOCAL_TASK,
177 179
      'weight' => 3,
......
182 184
      $items[] = array(
183 185
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/images',
184 186
      'title' => theme('cdm_taxonpage_tab', 'Images'),
185
      'callback' => 'cdm_dataportal_view_taxon',
187
      'callback' => 'cdm_dataportal_taxon_page_view',
186 188
      'access' => true,
187 189
      'type' => MENU_LOCAL_TASK,
188 190
      'weight' => 4,
......
677 679
  
678 680
  $form['cdm_dataportal']['layout']['findtaxa']['cdm_dataportal_findtaxa_show_synonym_thumbnails'] = array(
679 681
    '#type' => 'checkbox',
680
    '#title' => t('Show media thumbnails for syonyms'),
682
    '#title' => t('Show media thumbnails for synonyms'),
681 683
    '#default_value' => variable_get('cdm_dataportal_findtaxa_show_synonym_thumbnails', 0),
682 684
    '#description' => t('')
683 685
  );
......
874 876
 *  - Full list of synonyms homotypic synonyms on top, followed by the
875 877
 *    heterotypic and finally followed by misapplied names.
876 878
 *    The list is ordered historically.
877
 *  - All facts associated with the very taxon concept and taxon name.
879
 *  - All description associated with the taxon.
878 880
 *
879
 * @param String $uuid the UUID of the taxon
881
 * @param $uuid
882
 * @param $chapter name of the part to display,
883
 *         valid values are: 'description', 'images', 'synonymy', 'all'
884
 * @return unknown_type
880 885
 */
881
function cdm_dataportal_view_taxon($uuid, $chapter = null){
886
function cdm_dataportal_taxon_page_view($uuid, $chapter = 'all'){
882 887
  
883 888
    // display the page for the taxon defined by $uuid
889
    $taxonpage = cdm_dataportal_taxon_view($uuid, $chapter); 
890
    return cdm_node_show(NODETYPE_TAXON, $uuid, $taxonpage->title , $taxonpage->content);
891
}
892

  
893
/**
894
 * @param $uuid
895
 * @param $chapter name of the part to display,
896
 *         valid values are: 'description', 'images', 'synonymy', 'all'
897
 * @return unknown_type
898
 */
899
function cdm_dataportal_taxon_view($uuid, $chapter = 'all'){
900
  
884 901
    $taxon = cdm_ws_get(CDM_WS_TAXON, $uuid);
885 902
    if(!$taxon){
886 903
      drupal_set_title(t('Taxon does not exist'));
......
888 905
    }
889 906
    _cdm_dataportal_set_currentSecUuid($taxon->sec->uuid);
890 907
    
891
    drupal_set_title(theme('cdm_taxon_page_title', $taxon));
892
    $out = theme('cdm_taxon_page_general', $taxon, $chapter);
893
    
894
    return $out;
895
  /*}*/
908
    $taxonpage->title = theme('cdm_taxon_page_title', $taxon);
909
    $taxonpage->content = theme('cdm_taxon_page_general', $taxon, $chapter);
910

  
911
    return $taxonpage;
896 912
}
897 913

  
914

  
898 915
function cdm_dataportal_view_search_advanced(){
899 916
  
900 917
  drupal_set_title(t('Advanced Search'));
......
1362 1379

  
1363 1380
    //returned value
1364 1381
    return $metadata_caption;
1365

  
1366 1382
}
modules/cdm_dataportal/node_types.php
1
<?php
2

  
3
define(NODETYPE_TAXON, 'taxon');
4
define(NODETYPE_MEDIA, 'media');
5
define(NODETYPE_REFERENCE, 'reference');
6

  
7
/**
8
 * Implementation of hook_node_info().
9
 */
10
function cdm_dataportal_node_info() {
11
  
12
  static $nodetypes;
13
  if(!$nodetype){
14
    $nodetypes = array(NODETYPE_REFERENCE, NODETYPE_TAXON, NODETYPE_MEDIA); 
15
  }
16
  
17
  $nodeinfo = array();
18
  foreach( $nodetypes as $type){
19
    $nodeinfo['cdm_'.$type] = array(
20
      'name' => t(ucfirst($type)),
21
      'has_title' => TRUE,
22
      'module' => 'cdm_dataportal',
23
      'description' => t('Node type with reflects cdm entities of the type' . $type . '.')
24
    );
25
  }
26
  
27
  return $nodeinfo;
28
}
29

  
30
/**
31
 *  Implementation of hook_form()
32
 */
33
function cdm_dataportal_form(&$node) {
34
  
35
  $type = node_get_types('type', $node);
36
  
37
  if(is_numeric($node->nid)){
38
    $cdm_node_notice = t('In order to edit CDM content, please use the '). l(t('Taxonomic Editor'), 'http://dev.e-taxonomy.eu/trac/wiki/TaxonomicEditor', NULL, NULL, TRUE);   
39
  } else {
40
    $cdm_node_notice = t('You cannot manually create a node of type ').$type->name . '. '. t('This node type is only created internally');
41
  }
42
  $form['cdm'] = array(
43
    '#value' => '<div class="cdm_node_notice warning">' . $cdm_node_notice . '</div>',
44
    '#weight' => -5
45
  );
46
  // We need to define form elements for the node's title and body.
47
  $form['title'] = array(
48
    '#type' => 'textfield',
49
    '#title' => check_plain($type->title_label),
50
    '#required' => TRUE,
51
    '#disabled' => TRUE,
52
    '#default_value' => $node->title,
53
    '#weight' => -5
54
  );
55
  
56
  return $form;
57
}
58

  
59

  
60
/**
61
 * Implementation of hook_nodeapi().
62
 */
63
function cdm_dataportal_nodeapi(&$node, $op, $teaser, $page) {
64
  switch ($op) {
65
    case 'view':
66
      switch($node->type){
67
        case 'cdm_'.NODETYPE_TAXON : 
68
          if(arg(0) == 'node'){
69
            $cdmnode = db_fetch_object(db_query('SELECT * FROM {node_cdm} WHERE nid = \'%d\''
70
              , $node->nid));
71
            $taxonpage = cdm_dataportal_taxon_view($cdmnode->uuid);
72
            drupal_set_title($taxonpage->title);
73
            cdm_add_node_content($node, $taxonpage->content, variable_get('cdm_content_weight', -1));
74
          }
75
          break;
76
      }
77
      break;   
78
  }
79
}

Also available in: Unified diff