Project

General

Profile

Download (2.27 KB) Statistics
| Branch: | Tag: | Revision:
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
}
(5-5/9)