Project

General

Profile

Download (2.74 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
function cdm_get_nodetypes(){
4

    
5
	$nodetypes = module_invoke_all('cdm_nodetypes');
6
	return $nodetypes;
7

    
8
}
9

    
10
function cdm_load_node($nodetype, $uuid, $title){
11

    
12
	// try to find node id
13
	$cdmnode = db_fetch_object(db_query('SELECT nid, cdmtype FROM {node_cdm} WHERE wsuri = \'%s\' AND cdmtype = \'%s\' AND uuid = \'%s\''
14
	, variable_get('cdm_webservice_url', NULL),  $nodetype, $uuid));
15

    
16
	if(is_numeric($cdmnode->nid)) {
17
		$node = node_load($cdmnode->nid);
18
	} else {
19

    
20
		// Create a new node
21
		$node->type = 'cdm_'.$nodetype;
22
		// use just the plain text of the HTML title
23
		$values['title'] = filter_xss($title, array());
24
		// limit length to the max length of the database field 128
25
		$values['title'] = substr($values['title'], 0, 128);
26
		$values['comment'] =  variable_get('comment_'. $nodetype, $comment_node_disabled);
27

    
28
		// preserve the current messages but before  saveing the node,
29
		$messages = drupal_set_message();
30

    
31
		$result = drupal_execute($node->type.'_node_form', $values, $node);
32
		// restore the messages
33
		$_SESSION['messages'] = $messages;
34

    
35
		if(!is_array($result)){
36
			// result should contain the path the newly created node; e.g.: node/32
37
			$pathelements = explode('/', $result);
38
			$nid = array_pop($pathelements);
39
			$node->nid = $nid;
40
      $hash = md5( variable_get('cdm_webservice_url', NULL) . $uuid ); // hash as a 32-character hexadecimal number.
41
			db_query('INSERT INTO {node_cdm} (nid, wsuri, hash, cdmtype, uuid) VALUES (%d, \'%s\', \'%s\', \'%s\', \'%s\');'
42
			, $nid, variable_get('cdm_webservice_url', NULL), $hash,  $nodetype, $uuid);
43

    
44
		} else {
45
			drupal_set_message(t('Could not create node for ' . $nodetype),'error');
46
		}
47
	}
48

    
49
	return $node;
50
}
51

    
52

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

    
68

    
69
/**
70
 *
71
 * @param $node
72
 * @param $content
73
 * @param $weight
74
 * @return unknown_type
75
 */
76
function cdm_add_node_content(&$node, &$content, $weight){
77

    
78
	$cdm_content = array(
79
		// wrap content in cdm_dataportal specific container
80
        '#value' => '<div id="cdm_dataportal.node">' . $content . '</div>',
81
        '#weight' => variable_get('cdm_content_weight', -1)
82
	);
83

    
84
	$node->content['cdm'] = $cdm_content;
85
}
86

    
87
function cdm_delete_all_cdm_nodes(){
88

    
89
	$result = db_query("SELECT * FROM {node} WHERE type like '%s';", 'cdm_%');
90
	while ($node = db_fetch_object($result)) {
91
		node_delete($node->nid);
92
	}
93
}
(5-5/9)