1 |
6657531f
|
Andreas Kohlbecker
|
<?php
|
2 |
|
|
/**
|
3 |
|
|
* @file
|
4 |
|
|
* CDM Node functions.
|
5 |
|
|
*/
|
6 |
|
|
|
7 |
06c2b3e2
|
Andreas Kohlbecker
|
|
8 |
|
|
define('CDM_DRUPAL_NODE_CREATION', 'cdm_drupal_node_creation');
|
9 |
|
|
|
10 |
86833725
|
Andreas Kohlbecker
|
function do_persist_drupal_nodes(){
|
11 |
06c2b3e2
|
Andreas Kohlbecker
|
static $value = null;
|
12 |
|
|
if($value === NULL){
|
13 |
|
|
$value = variable_get(CDM_DRUPAL_NODE_CREATION, FALSE);
|
14 |
|
|
}
|
15 |
|
|
return $value;
|
16 |
|
|
}
|
17 |
|
|
|
18 |
6657531f
|
Andreas Kohlbecker
|
/**
|
19 |
|
|
* Implements hook_node_view().
|
20 |
|
|
*
|
21 |
5e848c4b
|
Andreas Kohlbecker
|
* TODO This should also be used for other cdm node types like name page?
|
22 |
6657531f
|
Andreas Kohlbecker
|
*/
|
23 |
|
|
function cdm_dataportal_node_view($node, $view_mode = 'full') {
|
24 |
5e848c4b
|
Andreas Kohlbecker
|
|
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 |
6657531f
|
Andreas Kohlbecker
|
// See cdm_add_node_content.
|
36 |
|
|
switch ($node->type) {
|
37 |
|
|
case 'cdm_' . NODETYPE_TAXON:
|
38 |
b8d1f824
|
Andreas Kohlbecker
|
case 'cdm_' . NODETYPE_NAME:
|
39 |
|
|
case 'cdm_' . NODETYPE_REFERENCE:
|
40 |
|
|
case 'cdm_' . NODETYPE_MEDIA:
|
41 |
6657531f
|
Andreas Kohlbecker
|
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 |
86833725
|
Andreas Kohlbecker
|
if(do_persist_drupal_nodes()){
|
119 |
|
|
|
120 |
06c2b3e2
|
Andreas Kohlbecker
|
// 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 |
6657531f
|
Andreas Kohlbecker
|
|
140 |
06c2b3e2
|
Andreas Kohlbecker
|
// 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 |
7cc085da
|
Andreas Kohlbecker
|
watchdog('content', '@message', array('@message' => $message), WATCHDOG_ERROR);
|
153 |
06c2b3e2
|
Andreas Kohlbecker
|
return NULL;
|
154 |
|
|
}
|
155 |
6657531f
|
Andreas Kohlbecker
|
|
156 |
06c2b3e2
|
Andreas Kohlbecker
|
// Hash as a 32-character hexadecimal number.
|
157 |
|
|
$hash = md5(variable_get('cdm_webservice_url') . $uuid);
|
158 |
6657531f
|
Andreas Kohlbecker
|
|
159 |
06c2b3e2
|
Andreas Kohlbecker
|
$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 |
86833725
|
Andreas Kohlbecker
|
} else {
|
167 |
|
|
// Drupal node is not persisted
|
168 |
|
|
// need to create fake nid
|
169 |
|
|
$node->nid = 0;
|
170 |
06c2b3e2
|
Andreas Kohlbecker
|
}
|
171 |
6657531f
|
Andreas Kohlbecker
|
}
|
172 |
|
|
|
173 |
|
|
return $node;
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
/**
|
177 |
59f6da42
|
Andreas Kohlbecker
|
* 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 |
6657531f
|
Andreas Kohlbecker
|
*/
|
202 |
|
|
function cdm_node_show($cdm_node_type, $uuid, $title, $content) {
|
203 |
239c4040
|
Andreas Kohlbecker
|
|
204 |
|
|
if(!user_access('access cdm content')){
|
205 |
|
|
drupal_access_denied();
|
206 |
|
|
}
|
207 |
|
|
|
208 |
59f6da42
|
Andreas Kohlbecker
|
// tell drupal code to load the node
|
209 |
6657531f
|
Andreas Kohlbecker
|
$node = cdm_load_node($cdm_node_type, $uuid, $title);
|
210 |
59f6da42
|
Andreas Kohlbecker
|
// set the title coming supplied by a cdm_dataportal_{CDM_NODE_TYPE}_view function
|
211 |
6657531f
|
Andreas Kohlbecker
|
drupal_set_title($title, PASS_THROUGH);
|
212 |
59f6da42
|
Andreas Kohlbecker
|
|
213 |
6657531f
|
Andreas Kohlbecker
|
cdm_add_node_content($node, $content);
|
214 |
06c2b3e2
|
Andreas Kohlbecker
|
|
215 |
86833725
|
Andreas Kohlbecker
|
if(do_persist_drupal_nodes()){
|
216 |
06c2b3e2
|
Andreas Kohlbecker
|
// 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 |
6657531f
|
Andreas Kohlbecker
|
}
|
224 |
|
|
|
225 |
|
|
/**
|
226 |
59f6da42
|
Andreas Kohlbecker
|
* Sets the $content given a paramater to the $node object
|
227 |
|
|
*
|
228 |
|
|
* The $content can either be a string or an array.
|
229 |
|
|
*
|
230 |
|
|
* see:
|
231 |
|
|
* - element_children()
|
232 |
|
|
* - drupal_render()
|
233 |
|
|
* - http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_render/7#comment-6644
|
234 |
|
|
*
|
235 |
|
|
* TODO see notes near bottom of function
|
236 |
|
|
*
|
237 |
|
|
* @param object $node
|
238 |
|
|
* A $node object
|
239 |
|
|
* @param string|array $content
|
240 |
|
|
* The content to set for the $node
|
241 |
|
|
*
|
242 |
6657531f
|
Andreas Kohlbecker
|
*/
|
243 |
|
|
function cdm_add_node_content(&$node, $content) {
|
244 |
59f6da42
|
Andreas Kohlbecker
|
|
245 |
|
|
if(is_array($content)) {
|
246 |
|
|
// $content seems to be a render array suitable for drupal_render()
|
247 |
d2a09415
|
Andreas Kohlbecker
|
$cdm_content = array(
|
248 |
|
|
// Wrap content in cdm_dataportal specific container.
|
249 |
59f6da42
|
Andreas Kohlbecker
|
'#prefix' => '<div id="cdm_dataportal.node">',
|
250 |
|
|
'#suffix' => '</div>',
|
251 |
|
|
// the key of child elements can be chosen arbitrarily it only must not start with a '#'
|
252 |
d2a09415
|
Andreas Kohlbecker
|
'content' => $content,
|
253 |
|
|
'#weight' => variable_get('cdm_content_weight', -1),
|
254 |
59f6da42
|
Andreas Kohlbecker
|
);
|
255 |
|
|
} else {
|
256 |
0a1151a4
|
Andreas Kohlbecker
|
// Wrap content in cdm_dataportal specific container.
|
257 |
|
|
$cdm_content = markup_to_render_array( $content, variable_get('cdm_content_weight', -1));
|
258 |
59f6da42
|
Andreas Kohlbecker
|
}
|
259 |
6657531f
|
Andreas Kohlbecker
|
|
260 |
b8d1f824
|
Andreas Kohlbecker
|
// $node->content is cleared in node_build_content() therefore we need to
|
261 |
|
|
// implement the 'view' hook in order to set the $node->content
|
262 |
|
|
// properly => cdm_dataportal_node_view()
|
263 |
6657531f
|
Andreas Kohlbecker
|
$node->cdm = $cdm_content;
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
/**
|
267 |
|
|
* Deletes all cdm nodes, used when module is uninstalled
|
268 |
|
|
*/
|
269 |
|
|
function cdm_delete_all_cdm_nodes() {
|
270 |
|
|
$result = db_query('SELECT n.nid FROM {node} n WHERE n.type like :type', array(':type' => 'cdm_%'));
|
271 |
|
|
foreach ($result as $node) {
|
272 |
|
|
node_delete($node->nid);
|
273 |
|
|
}
|
274 |
|
|
}
|