Project

General

Profile

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

    
7

    
8
define('CDM_DRUPAL_NODE_CREATION', 'cdm_drupal_node_creation');
9

    
10
function do_persist_drupal_nodes(){
11
  static $value = null;
12
  if($value === NULL){
13
    $value = variable_get(CDM_DRUPAL_NODE_CREATION, FALSE);
14
  }
15
  return $value;
16
}
17

    
18
/**
19
 * Implements hook_node_view().
20
 *
21
 * TODO This should also be used for other cdm node types like name page?
22
 */
23
function cdm_dataportal_node_view($node, $view_mode = 'full') {
24

    
25

    
26
  if($view_mode == 'search_index'){
27
    // this view mode is used by _node_index_node()
28
    // allowing drupal search to index the cdm instance
29
    // nodes can be a big performance problem, to this
30
    // viewmode is ignored
31
    // If you need a fearch function, use the cdm serach facilities instead.
32
    return;
33
  }
34

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

    
58
}
59

    
60
/**
61
 * Implements hook_node_delete().
62
 */
63
function cdm_dataportal_node_delete($node) {
64
  if (array_key_exists($node->type, cdm_get_nodetypes())) {
65
    db_delete('node_cdm')->condition('nid', $node->nid)->execute();
66
  }
67
}
68

    
69
/**
70
 * Loads the node if one exist for $uuid, or else creates one.
71
 *
72
 * @param string $nodetype
73
 *   The node type.
74
 * @param string $uuid
75
 *   UUID for which to load the node.
76
 * @param string $title
77
 *   The node title to display for the node.
78
 *
79
 * @return mixed
80
 *   The node object for $uuid.
81
 */
82
function cdm_load_node($nodetype, $uuid, $title) {
83

    
84
  // Try to find node id.
85
  $cdmnode = db_query('SELECT nid, cdmtype FROM {node_cdm} WHERE wsuri = :wsuri AND cdmtype = :cdmtype AND uuid = :uuid', array(
86
    ':wsuri' => variable_get('cdm_webservice_url', NULL),
87
    ':cdmtype' => $nodetype,
88
    ':uuid' => $uuid,
89
  ))->fetch();
90

    
91
  // Nid should not be 0 , if it is, something is wrong with the record.
92
  if (isset($cdmnode->nid) && $cdmnode->nid == 0) {
93
    drupal_set_message(t('Something is wrong with the record for uuid=%uuid,
94
      please contact the helpdesk.', array('%uuid' => $uuid)), 'error');
95
    return;
96
  }
97
  if (isset($cdmnode->nid) && is_numeric($cdmnode->nid)) {
98
    $node = node_load($cdmnode->nid);
99
  }
100
  else {
101
    // Create a new node.
102
    $node = new stdClass();
103
    $node->type = 'cdm_' . $nodetype;
104
    // Comment @WA TODO set to e.g. 'en' if locale is enabled.
105
    $node->language = LANGUAGE_NONE;
106

    
107
    // Set some default values for:
108
    // 'status', 'promote', 'sticky', 'uid' and 'created'.
109
    node_object_prepare($node);
110

    
111
    // Use just the plain text of the HTML title.
112
    $title = filter_xss($title, array());
113

    
114
    // Limit length to the max length of the database field 128.
115
    $title = substr($title, 0, 128);
116
    $node->title = $title;
117

    
118
    if(do_persist_drupal_nodes()){
119

    
120
      // using the system admin user for all new nodes
121
      $node->uid = 0;
122

    
123
      // 2 = comments on, 1 = comments off.
124
      $node->comment = variable_get('comment_' . $node->type);
125

    
126
      // Preserve the current messages but before saving the node.
127
      $messages = drupal_set_message();
128

    
129
      if ($node = node_submit($node)) {
130
        // Prepare node for saving by populating author and creation date.
131
        // Comment @WA: Note that node_save is using a helper function to save a
132
        // revision with the uid of the current user so the revision will not
133
        // have uid = 0 but the uid of the current user.
134
        // I guess that is not a problem so I leave it like this. Remedy would be
135
        // to alter that revision entry afterwards.
136
        // Will create a watchdog log entry if it fails to create the node.
137
        node_save($node);
138
      }
139

    
140
      // Restore the messages.
141
      $_SESSION['messages'] = $messages;
142

    
143
      // Comment @WA: I think http://dev.e-taxonomy.eu/trac/ticket/2964 is not
144
      // relevant here anymore, since node_save will roll_back if node cannot be
145
      // created.
146
      if (!isset($node->nid)) {
147
        $message = t('Could not create node for !nodetype (!title).', array(
148
          '!nodetype' => $nodetype,
149
          '!title' => $title,
150
        ));
151
        drupal_set_message($message, 'error');
152
        watchdog('content', '@message', array('@message' => $message), WATCHDOG_ERROR);
153
        return NULL;
154
      }
155

    
156
      // Hash as a 32-character hexadecimal number.
157
      $hash = md5(variable_get('cdm_webservice_url') . $uuid);
158

    
159
      $id = db_insert('node_cdm')->fields(array(
160
        'nid' => $node->nid,
161
        'wsuri' => variable_get('cdm_webservice_url'),
162
        'hash' => $hash,
163
        'cdmtype' => $nodetype,
164
        'uuid' => $uuid,
165
      ))->execute();
166
    } else {
167
      // Drupal node is not persisted
168
      // need to create fake nid
169
      $node->nid = 0;
170
    }
171
  }
172

    
173
  return $node;
174
}
175

    
176
/**
177
 * Wrapper function around node_show()
178
 *
179
 * Just like the drupal function node_show() this function will generate an
180
 * array which displays a node detail page. Prior calling node_show() this
181
 * function assures that the special cdm node types are undegone the nessecary
182
 * preprocessing.
183
 *
184
 * This function will be called by a cdm_dataportal_{CDM_NODE_TYPE}_view function.
185
 *
186
 *
187
 * @param String $cdm_node_type
188
 *     one of the cdm content type names as defined in
189
 *     the file node_types.php. Possible values are 'taxon', 'media', 'reference', 'name'.
190
 *     you may want to use the according contstants instred of the string: NODETYPE_TAXON,
191
 *     NODETYPE_MEDIA, NODETYPE_REFERENCE, NODETYPE_NAME.
192
 * @param String $uuid
193
 *     the UUID string of the cdm entitiy to be shown. The cdm type is of cource defined by
194
 *     the  $cdm_node_type value
195
 * @param String $title
196
 *     the Page title
197
 * @param String or render array? $content
198
 *
199
 * @return
200
 *     A $page element suitable for use by drupal_render().
201
 */
202
function cdm_node_show($cdm_node_type, $uuid, $title, $content) {
203

    
204
  if(!user_access('access cdm content')){
205
    drupal_access_denied();
206
  }
207

    
208
  // tell drupal code to load the node
209
  $node = cdm_load_node($cdm_node_type, $uuid, $title);
210
  // set the title coming supplied by a cdm_dataportal_{CDM_NODE_TYPE}_view function
211
  drupal_set_title($title, PASS_THROUGH);
212

    
213
  cdm_add_node_content($node, $content);
214

    
215
  if(do_persist_drupal_nodes()){
216
    // use the full node_show()
217
    $nodes = node_show($node);
218
  } else {
219
    // only use a part of the methods called in the node_show() method
220
    $nodes = node_view_multiple(array($node->nid => $node), 'full');
221
  }
222
  return $nodes;
223
}
224

    
225
/**
226
 * Simulates the influence on the page layout by cdm_node_show().
227
 *
228
 * Adds a div as first element for the general part, which usually is empty in dataporal pages.
229
 *
230
 * @param $content
231
 */
232
function cdm_node_show_simulate($content)
233
{
234
  array_unshift($content, array("general-part" => markup_to_render_array("<div id=\"general\" class=\"page-part\"></div>")));
235
  return drupal_render($content);
236
}
237

    
238

    
239
/**
240
 * Sets the $content given a paramater to the $node object
241
 *
242
 * The $content can either be a string or an array.
243
 *
244
 * see:
245
 *  - element_children()
246
 *  - drupal_render()
247
 *  - http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_render/7#comment-6644
248
 *
249
 * TODO see notes near bottom of function
250
 *
251
 * @param object $node
252
 *   A $node object
253
 * @param string|array $content
254
 *   The content to set for the $node
255
 *
256
 */
257
function cdm_add_node_content(&$node, $content) {
258

    
259
  if(is_array($content)) {
260
    // $content seems to be a render array suitable for drupal_render()
261
    $cdm_content = array(
262
        // Wrap content in cdm_dataportal specific container.
263
        '#prefix' => '<div id="cdm_dataportal.node">',
264
        '#suffix' => '</div>',
265
        // the key of child elements can be chosen arbitrarily it only must not start with a '#'
266
        'content' => $content,
267
        '#weight' => variable_get('cdm_content_weight', -1),
268
    );
269
  } else {
270
    // Wrap content in cdm_dataportal specific container.
271
    $cdm_content = markup_to_render_array( $content, variable_get('cdm_content_weight', -1));
272
  }
273

    
274
  //  $node->content is cleared in node_build_content() therefore we need to
275
  //  implement the 'view' hook in order to set the  $node->content
276
  //  properly => cdm_dataportal_node_view()
277
  $node->cdm = $cdm_content;
278
}
279

    
280
/**
281
 * Deletes all cdm nodes, used when module is uninstalled
282
 */
283
function cdm_delete_all_cdm_nodes() {
284
  $result = db_query('SELECT n.nid FROM {node} n WHERE n.type like :type', array(':type' => 'cdm_%'));
285
  foreach ($result as $node) {
286
    node_delete($node->nid);
287
  }
288
}
(6-6/11)