Project

General

Profile

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

    
7
/**
8
 * Implements hook_node_view().
9
 *
10
 * TODO This should also be used for other cdm node types like name page?
11
 */
12
function cdm_dataportal_node_view($node, $view_mode = 'full') {
13

    
14

    
15
  if($view_mode == 'search_index'){
16
    // this view mode is used by _node_index_node()
17
    // allowing drupal search to index the cdm instance
18
    // nodes can be a big performance problem, to this
19
    // viewmode is ignored
20
    // If you need a fearch function, use the cdm serach facilities instead.
21
    return;
22
  }
23

    
24
  // See cdm_add_node_content.
25
  switch ($node->type) {
26
    case 'cdm_' . NODETYPE_TAXON:
27
    case 'cdm_' . NODETYPE_NAME:
28
    case 'cdm_' . NODETYPE_REFERENCE:
29
    case 'cdm_' . NODETYPE_MEDIA:
30
      if (!isset($node->cdm) && arg(0) == 'node') {
31
        // If a node page is loaded directly, e.g. node/%nid instead of
32
        // cdm_dataportal/taxon/%uuid, try to load the taxon page content
33
        // into $node->cdm.
34
        // only do this for node pages (where arg(0) = node),
35
        // not for pages like comment/reply/%nid.
36
        $cdmnode = db_query('SELECT uuid FROM {node_cdm} WHERE nid = :nid', array(
37
          ':nid' => $node->nid,
38
        ))->fetch();
39
        if (isset($cdmnode->uuid)) {
40
          cdm_dataportal_taxon_page_view($cdmnode->uuid);
41
        }
42
      }
43
      $node->content['cdm'] = isset($node->cdm) ? $node->cdm : '';
44
      break;
45
  }
46

    
47
}
48

    
49
/**
50
 * Implements hook_node_delete().
51
 */
52
function cdm_dataportal_node_delete($node) {
53
  if (array_key_exists($node->type, cdm_get_nodetypes())) {
54
    db_delete('node_cdm')->condition('nid', $node->nid)->execute();
55
  }
56
}
57

    
58
/**
59
 * Loads the node if one exist for $uuid, or else creates one.
60
 *
61
 * @param string $nodetype
62
 *   The node type.
63
 * @param string $uuid
64
 *   UUID for which to load the node.
65
 * @param string $title
66
 *   The node title to display for the node.
67
 *
68
 * @return mixed
69
 *   The node object for $uuid.
70
 */
71
function cdm_load_node($nodetype, $uuid, $title) {
72

    
73
  // Try to find node id.
74
  $cdmnode = db_query('SELECT nid, cdmtype FROM {node_cdm} WHERE wsuri = :wsuri AND cdmtype = :cdmtype AND uuid = :uuid', array(
75
    ':wsuri' => variable_get('cdm_webservice_url', NULL),
76
    ':cdmtype' => $nodetype,
77
    ':uuid' => $uuid,
78
  ))->fetch();
79

    
80
  // Nid should not be 0 , if it is, something is wrong with the record.
81
  if (isset($cdmnode->nid) && $cdmnode->nid == 0) {
82
    drupal_set_message(t('Something is wrong with the record for uuid=%uuid,
83
      please contact the helpdesk.', array('%uuid' => $uuid)), 'error');
84
    return;
85
  }
86
  if (isset($cdmnode->nid) && is_numeric($cdmnode->nid)) {
87
    $node = node_load($cdmnode->nid);
88
  }
89
  else {
90
    // Create a new node.
91
    $node = new stdClass();
92
    $node->type = 'cdm_' . $nodetype;
93
    // Comment @WA TODO set to e.g. 'en' if locale is enabled.
94
    $node->language = LANGUAGE_NONE;
95

    
96
    // Set some default values for:
97
    // 'status', 'promote', 'sticky', 'uid' and 'created'.
98
    node_object_prepare($node);
99

    
100
    // Use just the plain text of the HTML title.
101
    $title = filter_xss($title, array());
102

    
103
    // Limit length to the max length of the database field 128.
104
    $title = substr($title, 0, 128);
105
    $node->title = $title;
106

    
107
    // Comment @WA: this was used in the D5 module version.
108
    // Remove this to change it to the current user.
109
    $node->uid = 0;
110

    
111
    // 2 = comments on, 1 = comments off.
112
    $node->comment = variable_get('comment_' . $node->type);
113

    
114
    // Preserve the current messages but before saving the node.
115
    $messages = drupal_set_message();
116

    
117
    if ($node = node_submit($node)) {
118
      // Prepare node for saving by populating author and creation date.
119
      // Comment @WA: Note that node_save is using a helper function to save a
120
      // revision with the uid of the current user so the revision will not
121
      // have uid = 0 but the uid of the current user.
122
      // I guess that is not a problem so I leave it like this. Remedy would be
123
      // to alter that revision entry afterwards.
124
      // Will create a watchdog log entry if it fails to create the node.
125
      node_save($node);
126
    }
127

    
128
    // Restore the messages.
129
    $_SESSION['messages'] = $messages;
130

    
131
    // Comment @WA: I think http://dev.e-taxonomy.eu/trac/ticket/2964 is not
132
    // relevant here anymore, since node_save will roll_back if node cannot be
133
    // created.
134
    if (!isset($node->nid)) {
135
      $message = t('Could not create node for !nodetype (!title).', array(
136
        '!nodetype' => $nodetype,
137
        '!title' => $title,
138
      ));
139
      drupal_set_message($message, 'error');
140
      watchdog('content', $message, WATCHDOG_ERROR);
141
      return NULL;
142
    }
143

    
144
    // Hash as a 32-character hexadecimal number.
145
    $hash = md5(variable_get('cdm_webservice_url') . $uuid);
146

    
147
    $id = db_insert('node_cdm')->fields(array(
148
      'nid' => $node->nid,
149
      'wsuri' => variable_get('cdm_webservice_url'),
150
      'hash' => $hash,
151
      'cdmtype' => $nodetype,
152
      'uuid' => $uuid,
153
    ))->execute();
154
  }
155

    
156
  return $node;
157
}
158

    
159
/**
160
 * Wrapper function around node_show()
161
 *
162
 * Just like the drupal function node_show() this function will generate an
163
 * array which displays a node detail page. Prior calling node_show() this
164
 * function assures that the special cdm node types are undegone the nessecary
165
 * preprocessing.
166
 *
167
 * This function will be called by a cdm_dataportal_{CDM_NODE_TYPE}_view function.
168
 *
169
 *
170
 * @param String $cdm_node_type
171
 *     one of the cdm content type names as defined in
172
 *     the file node_types.php. Possible values are 'taxon', 'media', 'reference', 'name'.
173
 *     you may want to use the according contstants instred of the string: NODETYPE_TAXON,
174
 *     NODETYPE_MEDIA, NODETYPE_REFERENCE, NODETYPE_NAME.
175
 * @param String $uuid
176
 *     the UUID string of the cdm entitiy to be shown. The cdm type is of cource defined by
177
 *     the  $cdm_node_type value
178
 * @param String $title
179
 *     the Page title
180
 * @param String or render array? $content
181
 *
182
 * @return
183
 *     A $page element suitable for use by drupal_render().
184
 */
185
function cdm_node_show($cdm_node_type, $uuid, $title, $content) {
186
  // tell drupal code to load the node
187
  $node = cdm_load_node($cdm_node_type, $uuid, $title);
188
  // set the title coming supplied by a cdm_dataportal_{CDM_NODE_TYPE}_view function
189
  drupal_set_title($title, PASS_THROUGH);
190

    
191
  cdm_add_node_content($node, $content);
192
  return node_show($node);
193
}
194

    
195
/**
196
 * Sets the $content given a paramater to the $node object
197
 *
198
 * The $content can either be a string or an array.
199
 *
200
 * see:
201
 *  - element_children()
202
 *  - drupal_render()
203
 *  - http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_render/7#comment-6644
204
 *
205
 * TODO see notes near bottom of function
206
 *
207
 * @param object $node
208
 *   A $node object
209
 * @param string|array $content
210
 *   The content to set for the $node
211
 *
212
 */
213
function cdm_add_node_content(&$node, $content) {
214

    
215
  if(is_array($content)) {
216
    // $content seems to be a render array suitable for drupal_render()
217
    $cdm_content = array(
218
        // Wrap content in cdm_dataportal specific container.
219
        '#prefix' => '<div id="cdm_dataportal.node">',
220
        '#suffix' => '</div>',
221
        // the key of child elements can be chosen arbitrarily it only must not start with a '#'
222
        'content' => $content,
223
        '#weight' => variable_get('cdm_content_weight', -1),
224
    );
225
  } else {
226
    // Wrap content in cdm_dataportal specific container.
227
    $cdm_content = markup_to_render_array( $content, variable_get('cdm_content_weight', -1));
228
  }
229

    
230
  //  $node->content is cleared in node_build_content() therefore we need to
231
  //  implement the 'view' hook in order to set the  $node->content
232
  //  properly => cdm_dataportal_node_view()
233
  $node->cdm = $cdm_content;
234
}
235

    
236
/**
237
 * Deletes all cdm nodes, used when module is uninstalled
238
 */
239
function cdm_delete_all_cdm_nodes() {
240
  $result = db_query('SELECT n.nid FROM {node} n WHERE n.type like :type', array(':type' => 'cdm_%'));
241
  foreach ($result as $node) {
242
    node_delete($node->nid);
243
  }
244
}
(6-6/11)