Project

General

Profile

Download (8.36 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_create_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_create_drupal_nodes()){
119
      // using the system admin user for all new nodes
120
      $node->uid = 0;
121

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

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

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

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

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

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

    
158
      $id = db_insert('node_cdm')->fields(array(
159
        'nid' => $node->nid,
160
        'wsuri' => variable_get('cdm_webservice_url'),
161
        'hash' => $hash,
162
        'cdmtype' => $nodetype,
163
        'uuid' => $uuid,
164
      ))->execute();
165
    }
166
  }
167

    
168
  return $node;
169
}
170

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

    
203
  cdm_add_node_content($node, $content);
204

    
205
  if(do_create_drupal_nodes()){
206
    // use the full node_show()
207
    $nodes = node_show($node);
208
  } else {
209
    // only use a part of the methods called in the node_show() method
210
    $nodes = node_view_multiple(array($node->nid => $node), 'full');
211
  }
212
  return $nodes;
213
}
214

    
215
/**
216
 * Sets the $content given a paramater to the $node object
217
 *
218
 * The $content can either be a string or an array.
219
 *
220
 * see:
221
 *  - element_children()
222
 *  - drupal_render()
223
 *  - http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_render/7#comment-6644
224
 *
225
 * TODO see notes near bottom of function
226
 *
227
 * @param object $node
228
 *   A $node object
229
 * @param string|array $content
230
 *   The content to set for the $node
231
 *
232
 */
233
function cdm_add_node_content(&$node, $content) {
234

    
235
  if(is_array($content)) {
236
    // $content seems to be a render array suitable for drupal_render()
237
    $cdm_content = array(
238
        // Wrap content in cdm_dataportal specific container.
239
        '#prefix' => '<div id="cdm_dataportal.node">',
240
        '#suffix' => '</div>',
241
        // the key of child elements can be chosen arbitrarily it only must not start with a '#'
242
        'content' => $content,
243
        '#weight' => variable_get('cdm_content_weight', -1),
244
    );
245
  } else {
246
    // Wrap content in cdm_dataportal specific container.
247
    $cdm_content = markup_to_render_array( $content, variable_get('cdm_content_weight', -1));
248
  }
249

    
250
  //  $node->content is cleared in node_build_content() therefore we need to
251
  //  implement the 'view' hook in order to set the  $node->content
252
  //  properly => cdm_dataportal_node_view()
253
  $node->cdm = $cdm_content;
254
}
255

    
256
/**
257
 * Deletes all cdm nodes, used when module is uninstalled
258
 */
259
function cdm_delete_all_cdm_nodes() {
260
  $result = db_query('SELECT n.nid FROM {node} n WHERE n.type like :type', array(':type' => 'cdm_%'));
261
  foreach ($result as $node) {
262
    node_delete($node->nid);
263
  }
264
}
(6-6/11)