Project

General

Profile

Download (5.55 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * CDM Node functions.
5
 */
6

    
7
/**
8
 * Implements hook_node_view().
9
 *
10
 * Comment @WA should this also be used for other cdm node types like name page?
11
 */
12
function cdm_dataportal_node_view($node, $view_mode = 'full') {
13
  // See cdm_add_node_content.
14
  switch ($node->type) {
15
    case 'cdm_' . NODETYPE_TAXON:
16
      if (!isset($node->cdm) && arg(0) == 'node') {
17
        // If a node page is loaded directly, e.g. node/%nid instead of
18
        // cdm_dataportal/taxon/%uuid, try to load the taxon page content
19
        // into $node->cdm.
20
        // only do this for node pages (where arg(0) = node),
21
        // not for pages like comment/reply/%nid.
22
        $cdmnode = db_query('SELECT uuid FROM {node_cdm} WHERE nid = :nid', array(
23
          ':nid' => $node->nid,
24
        ))->fetch();
25
        if (isset($cdmnode->uuid)) {
26
          cdm_dataportal_taxon_page_view($cdmnode->uuid);
27
        }
28
      }
29
      $node->content['cdm'] = isset($node->cdm) ? $node->cdm : '';
30
      break;
31
  }
32

    
33
}
34

    
35
/**
36
 * Implements hook_node_delete().
37
 */
38
function cdm_dataportal_node_delete($node) {
39
  if (array_key_exists($node->type, cdm_get_nodetypes())) {
40
    db_delete('node_cdm')->condition('nid', $node->nid)->execute();
41
  }
42
}
43

    
44
/**
45
 * Loads the node if one exist for $uuid, or else creates one.
46
 *
47
 * @param string $nodetype
48
 *   The node type.
49
 * @param string $uuid
50
 *   UUID for which to load the node.
51
 * @param string $title
52
 *   The node title to display for the node.
53
 *
54
 * @return mixed
55
 *   The node object for $uuid.
56
 */
57
function cdm_load_node($nodetype, $uuid, $title) {
58

    
59
  // Try to find node id.
60
  $cdmnode = db_query('SELECT nid, cdmtype FROM {node_cdm} WHERE wsuri = :wsuri AND cdmtype = :cdmtype AND uuid = :uuid', array(
61
    ':wsuri' => variable_get('cdm_webservice_url', NULL),
62
    ':cdmtype' => $nodetype,
63
    ':uuid' => $uuid,
64
  ))->fetch();
65

    
66
  // Nid should not be 0 , if it is, something is wrong with the record.
67
  if (isset($cdmnode->nid) && $cdmnode->nid == 0) {
68
    drupal_set_message(t('Something is wrong with the record for uuid=%uuid,
69
      please contact the helpdesk.', array('%uuid' => $uuid)), 'error');
70
    return;
71
  }
72
  if (isset($cdmnode->nid) && is_numeric($cdmnode->nid)) {
73
    $node = node_load($cdmnode->nid);
74
  }
75
  else {
76
    // Create a new node.
77
    $node = new stdClass();
78
    $node->type = 'cdm_' . $nodetype;
79
    // Comment @WA TODO set to e.g. 'en' if locale is enabled.
80
    $node->language = LANGUAGE_NONE;
81

    
82
    // Set some default values for:
83
    // 'status', 'promote', 'sticky', 'uid' and 'created'.
84
    node_object_prepare($node);
85

    
86
    // Use just the plain text of the HTML title.
87
    $title = filter_xss($title, array());
88

    
89
    // Limit length to the max length of the database field 128.
90
    $title = substr($title, 0, 128);
91
    $node->title = $title;
92

    
93
    // Comment @WA: this was used in the D5 module version.
94
    // Remove this to change it to the current user.
95
    $node->uid = 0;
96

    
97
    // 2 = comments on, 1 = comments off.
98
    $node->comment = variable_get('comment_' . $node->type);
99

    
100
    // Preserve the current messages but before saving the node.
101
    $messages = drupal_set_message();
102

    
103
    if ($node = node_submit($node)) {
104
      // Prepare node for saving by populating author and creation date.
105
      // Comment @WA: Note that node_save is using a helper function to save a
106
      // revision with the uid of the current user so the revision will not
107
      // have uid = 0 but the uid of the current user.
108
      // I guess that is not a problem so I leave it like this. Remedy would be
109
      // to alter that revision entry afterwards.
110
      // Will create a watchdog log entry if it fails to create the node.
111
      node_save($node);
112
    }
113

    
114
    // Restore the messages.
115
    $_SESSION['messages'] = $messages;
116

    
117
    // Comment @WA: I think http://dev.e-taxonomy.eu/trac/ticket/2964 is not
118
    // relevant here anymore, since node_save will roll_back if node cannot be
119
    // created.
120
    if (!isset($node->nid)) {
121
      $message = t('Could not create node for !nodetype (!title).', array(
122
        '!nodetype' => $nodetype,
123
        '!title' => $title,
124
      ));
125
      drupal_set_message($message, 'error');
126
      watchdog('content', $message, WATCHDOG_ERROR);
127
      return NULL;
128
    }
129

    
130
    // Hash as a 32-character hexadecimal number.
131
    $hash = md5(variable_get('cdm_webservice_url') . $uuid);
132

    
133
    $id = db_insert('node_cdm')->fields(array(
134
      'nid' => $node->nid,
135
      'wsuri' => variable_get('cdm_webservice_url'),
136
      'hash' => $hash,
137
      'cdmtype' => $nodetype,
138
      'uuid' => $uuid,
139
    ))->execute();
140
  }
141

    
142
  return $node;
143
}
144

    
145
/**
146
 * @todo document this function.
147
 */
148
function cdm_node_show($cdm_node_type, $uuid, $title, $content) {
149
  $node = cdm_load_node($cdm_node_type, $uuid, $title);
150
  drupal_set_title($title, PASS_THROUGH);
151
  cdm_add_node_content($node, $content);
152
  return node_show($node);
153
}
154

    
155
/**
156
 * @todo document this function.
157
 */
158
function cdm_add_node_content(&$node, $content) {
159
  $cdm_content = array(
160
    // Wrap content in cdm_dataportal specific container.
161
    '#markup' => '<div id="cdm_dataportal.node">' . $content . '</div>',
162
    '#weight' => variable_get('cdm_content_weight', -1),
163
  );
164

    
165
  // Comment @WA: for some reason $node->content is lost or recreated in
166
  // node_show($node) in D7, so we attach to $node->cdm here and re-attach to
167
  // $node->content in hook_node_view.
168
  $node->cdm = $cdm_content;
169
}
170

    
171
/**
172
 * Deletes all cdm nodes, used when module is uninstalled
173
 */
174
function cdm_delete_all_cdm_nodes() {
175
  $result = db_query('SELECT n.nid FROM {node} n WHERE n.type like :type', array(':type' => 'cdm_%'));
176
  foreach ($result as $node) {
177
    node_delete($node->nid);
178
  }
179
}
(5-5/9)