Project

General

Profile

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

    
44

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

    
60
 
61
/**
62
 * 
63
 * @param $node
64
 * @param $content
65
 * @param $weight
66
 * @return unknown_type
67
 */
68
function cdm_add_node_content(&$node, &$content, $weight){
69
    $cdm_content = array(
70
        '#value' => $content, 
71
        '#weight' => variable_get('cdm_content_weight', -1)
72
    );
73
    $node->content['cdm'] = $cdm_content;
74
}
(5-5/9)