Project

General

Profile

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

    
177
  cdm_add_node_content($node, $content);
178
  return node_show($node);
179
}
180

    
181
/**
182
 * Sets the $content given a paramater to the $node object
183
 *
184
 * The $content can either be a string or an array.
185
 *
186
 * see:
187
 *  - element_children()
188
 *  - drupal_render()
189
 *  - http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_render/7#comment-6644
190
 *
191
 * TODO see notes near bottom of function
192
 *
193
 * @param object $node
194
 *   A $node object
195
 * @param string|array $content
196
 *   The content to set for the $node
197
 *
198
 */
199
function cdm_add_node_content(&$node, $content) {
200

    
201
  if(is_array($content)) {
202
    // $content seems to be a render array suitable for drupal_render()
203
    $cdm_content = array(
204
        // Wrap content in cdm_dataportal specific container.
205
        '#prefix' => '<div id="cdm_dataportal.node">',
206
        '#suffix' => '</div>',
207
        // the key of child elements can be chosen arbitrarily it only must not start with a '#'
208
        'content' => $content,
209
        '#weight' => variable_get('cdm_content_weight', -1),
210
    );
211
  } else {
212
    // Wrap content in cdm_dataportal specific container.
213
    $cdm_content = markup_to_render_array( $content, variable_get('cdm_content_weight', -1));
214
  }
215

    
216
  // Comment @WA: for some reason $node->content is lost or recreated in
217
  //   node_show($node) in D7, so we attach to $node->cdm here and re-attach to
218
  //   $node->content in hook_node_view.
219
  //
220
  // Followup by @AK:
221
  //   $node->content is removed in node_build_content() we need to
222
  //   implement the 'view' hook in order to set the  $node->content
223
  //   properly in the drupal way. => TODO
224
  //
225
  $node->cdm = $cdm_content;
226
}
227

    
228
/**
229
 * Deletes all cdm nodes, used when module is uninstalled
230
 */
231
function cdm_delete_all_cdm_nodes() {
232
  $result = db_query('SELECT n.nid FROM {node} n WHERE n.type like :type', array(':type' => 'cdm_%'));
233
  foreach ($result as $node) {
234
    node_delete($node->nid);
235
  }
236
}
(5-5/9)