Project

General

Profile

Download (7.95 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
      if (!isset($node->cdm) && arg(0) == 'node') {
28
        // If a node page is loaded directly, e.g. node/%nid instead of
29
        // cdm_dataportal/taxon/%uuid, try to load the taxon page content
30
        // into $node->cdm.
31
        // only do this for node pages (where arg(0) = node),
32
        // not for pages like comment/reply/%nid.
33
        $cdmnode = db_query('SELECT uuid FROM {node_cdm} WHERE nid = :nid', array(
34
          ':nid' => $node->nid,
35
        ))->fetch();
36
        if (isset($cdmnode->uuid)) {
37
          cdm_dataportal_taxon_page_view($cdmnode->uuid);
38
        }
39
      }
40
      $node->content['cdm'] = isset($node->cdm) ? $node->cdm : '';
41
      break;
42
  }
43

    
44
}
45

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

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

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

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

    
93
    // Set some default values for:
94
    // 'status', 'promote', 'sticky', 'uid' and 'created'.
95
    node_object_prepare($node);
96

    
97
    // Use just the plain text of the HTML title.
98
    $title = filter_xss($title, array());
99

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

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

    
108
    // 2 = comments on, 1 = comments off.
109
    $node->comment = variable_get('comment_' . $node->type);
110

    
111
    // Preserve the current messages but before saving the node.
112
    $messages = drupal_set_message();
113

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

    
125
    // Restore the messages.
126
    $_SESSION['messages'] = $messages;
127

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

    
141
    // Hash as a 32-character hexadecimal number.
142
    $hash = md5(variable_get('cdm_webservice_url') . $uuid);
143

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

    
153
  return $node;
154
}
155

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

    
188
  cdm_add_node_content($node, $content);
189
  return node_show($node);
190
}
191

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

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

    
227
  // Comment @WA: for some reason $node->content is lost or recreated in
228
  //   node_show($node) in D7, so we attach to $node->cdm here and re-attach to
229
  //   $node->content in hook_node_view.
230
  //
231
  // Followup by @AK:
232
  //   $node->content is removed in node_build_content() we need to
233
  //   implement the 'view' hook in order to set the  $node->content
234
  //   properly in the drupal way. => TODO
235
  //
236
  $node->cdm = $cdm_content;
237
}
238

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