/[drupal]/contributions/modules/cck/nodereference.module
ViewVC logotype

Contents of /contributions/modules/cck/nodereference.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.39.2.36 - (show annotations) (download) (as text)
Mon Apr 28 23:50:29 2008 UTC (19 months ago) by karens
Branch: DRUPAL-5
CVS Tags: DRUPAL-5--1-7
Changes since 1.39.2.35: +55 -1 lines
File MIME type: text/x-php
#97375 More Panels 2 code cleanup and move non-content module Panels integration to relevant module - fieldgroup, nodereference, and userreference.
1 <?php
2 // $Id: nodereference.module,v 1.39.2.35 2008/03/24 01:58:34 yched Exp $
3
4 /**
5 * @file
6 * Defines a field type for referencing one node from another.
7 */
8
9
10 /**
11 * Implementation of hook_menu().
12 */
13 function nodereference_menu($may_cache) {
14 $items = array();
15
16 if ($may_cache) {
17 $items[] = array('path' => 'nodereference/autocomplete', 'title' => t('node reference autocomplete'),
18 'callback' => 'nodereference_autocomplete', 'access' => user_access('access content'), 'type' => MENU_CALLBACK);
19 }
20
21 return $items;
22 }
23
24 /**
25 * Implementation of hook_field_info().
26 */
27 function nodereference_field_info() {
28 return array(
29 'nodereference' => array('label' => t('Node Reference')),
30 );
31 }
32
33 /**
34 * Implementation of hook_field_settings().
35 */
36 function nodereference_field_settings($op, $field) {
37 switch ($op) {
38 case 'form':
39 $form = array();
40 $form['referenceable_types'] = array(
41 '#type' => 'checkboxes',
42 '#title' => t('Content types that can be referenced'),
43 '#multiple' => TRUE,
44 '#default_value' => isset($field['referenceable_types']) ? $field['referenceable_types'] : array(),
45 '#options' => node_get_types('names'),
46 );
47 if (module_exists('views')) {
48 $views = array('--' => '--');
49 $result = db_query("SELECT name FROM {view_view} ORDER BY name");
50 while ($view = db_fetch_array($result)) {
51 $views[t('Existing Views')][$view['name']] = $view['name'];
52 }
53 views_load_cache();
54 $default_views = _views_get_default_views();
55 foreach ($default_views as $view) {
56 $views[t('Default Views')][$view->name] = $view->name;
57 }
58 if (count($views) > 1) {
59 $form['advanced'] = array(
60 '#type' => 'fieldset',
61 '#title' => t('Advanced - Nodes that can be referenced (View)'),
62 '#collapsible' => TRUE,
63 '#collapsed' => !isset($field['advanced_view']) || $field['advanced_view'] == '--',
64 );
65 $form['advanced']['advanced_view'] = array(
66 '#type' => 'select',
67 '#title' => t('View'),
68 '#options' => $views,
69 '#default_value' => isset($field['advanced_view']) ? $field['advanced_view'] : '--',
70 '#description' => t('Choose the "Views module" view that selects the nodes that can be referenced.<br>Note :<ul><li>This will discard the "Content types" settings above. Use the view\'s "filters" section instead.</li><li>Use the view\'s "fields" section to display additional informations about candidate nodes on node creation/edition form.</li><li>Use the view\'s "sort criteria" section to determine the order in which candidate nodes will be displayed.</li></ul>'),
71 );
72 $form['advanced']['advanced_view_args'] = array(
73 '#type' => 'textfield',
74 '#title' => t('View arguments'),
75 '#default_value' => isset($field['advanced_view_args']) ? $field['advanced_view_args'] : '',
76 '#required' => FALSE,
77 '#description' => t('Provide a comma separated list of arguments to pass to the view.'),
78 );
79 }
80 }
81 return $form;
82
83 case 'save':
84 $settings = array('referenceable_types');
85 if (module_exists('views')) {
86 $settings[] = 'advanced_view';
87 $settings[] = 'advanced_view_args';
88 }
89 return $settings;
90
91 case 'database columns':
92 $columns = array(
93 'nid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),
94 );
95 return $columns;
96
97 case 'filters':
98 return array(
99 'default' => array(
100 'list' => '_nodereference_filter_handler',
101 'list-type' => 'list',
102 'operator' => 'views_handler_operator_or',
103 'value-type' => 'array',
104 'extra' => array('field' => $field),
105 ),
106 );
107 }
108 }
109
110 /**
111 * Implementation of hook_field().
112 */
113 function nodereference_field($op, &$node, $field, &$items, $teaser, $page) {
114 switch ($op) {
115 case 'validate':
116 $refs = _nodereference_potential_references($field, TRUE);
117 foreach ($items as $delta => $item) {
118 $error_field = isset($item['error_field']) ? $item['error_field'] : '';
119 unset($item['error_field']);
120 if (!empty($item['nid'])) {
121 if (!in_array($item['nid'], array_keys($refs))) {
122 form_set_error($error_field, t('%name : This post can\'t be referenced.', array('%name' => t($field['widget']['label']))));
123 }
124 }
125 }
126 return;
127 }
128 }
129
130 /**
131 * Implementation of hook_field_formatter_info().
132 */
133 function nodereference_field_formatter_info() {
134 return array(
135 'default' => array(
136 'label' => t('Title (link)'),
137 'field types' => array('nodereference'),
138 ),
139 'plain' => array(
140 'label' => t('Title (no link)'),
141 'field types' => array('nodereference'),
142 ),
143 'full' => array(
144 'label' => t('Full node'),
145 'field types' => array('nodereference'),
146 ),
147 'teaser' => array(
148 'label' => t('Teaser'),
149 'field types' => array('nodereference'),
150 ),
151 );
152 }
153
154 /**
155 * Implementation of hook_field_formatter().
156 */
157 function nodereference_field_formatter($field, $item, $formatter, $node) {
158 static $titles = array();
159
160 // We store the rendered nids in order to prevent infinite recursion
161 // when using the 'full node' / 'teaser' formatters.
162 static $recursion_queue = array();
163
164 if (empty($item['nid']) || !is_numeric($item['nid'])) {
165 return '';
166 }
167
168 if ($formatter == 'full' || $formatter == 'teaser') {
169 // If no 'referencing node' is set, we are starting a new 'reference thread'
170 if (!isset($node->referencing_node)) {
171 $recursion_queue = array();
172 }
173 $recursion_queue[] = $node->nid;
174 if (in_array($item['nid'], $recursion_queue)) {
175 // Prevent infinite recursion caused by reference cycles :
176 // if the node has already been rendered earlier in this 'thread',
177 // we fall back to 'default' (node title) formatter.
178 $formatter = 'default';
179 }
180 elseif ($referenced_node = node_load($item['nid'])) {
181 $referenced_node->referencing_node = $node;
182 $referenced_node->referencing_field = $field;
183 $titles[$item['nid']] = $referenced_node->title;
184 }
185 }
186
187 if (!isset($titles[$item['nid']])) {
188 $title = db_result(db_query("SELECT title FROM {node} WHERE nid=%d", $item['nid']));
189 $titles[$item['nid']] = $title ? $title : '';
190 }
191
192 switch ($formatter) {
193 case 'full':
194 return $referenced_node ? node_view($referenced_node, FALSE) : '';
195
196 case 'teaser':
197 return $referenced_node ? node_view($referenced_node, TRUE) : '';
198
199 case 'plain':
200 return check_plain($titles[$item['nid']]);
201
202 default:
203 return $titles[$item['nid']] ? l($titles[$item['nid']], 'node/'. $item['nid']) : '';
204 }
205 }
206
207 /**
208 * Implementation of hook_widget_info().
209 */
210 function nodereference_widget_info() {
211 return array(
212 'nodereference_select' => array(
213 'label' => t('Select List'),
214 'field types' => array('nodereference'),
215 ),
216 'nodereference_autocomplete' => array(
217 'label' => t('Autocomplete Text Field'),
218 'field types' => array('nodereference'),
219 ),
220 );
221 }
222
223 /**
224 * Implementation of hook_widget().
225 */
226 function nodereference_widget($op, &$node, $field, &$items) {
227 if ($field['widget']['type'] == 'nodereference_select') {
228 switch ($op) {
229 case 'prepare form values':
230 $items_transposed = content_transpose_array_rows_cols($items);
231 $items['default nids'] = $items_transposed['nid'];
232 break;
233
234 case 'form':
235 $form = array();
236
237 $options = _nodereference_potential_references($field, TRUE);
238 foreach ($options as $key => $value) {
239 $options[$key] = _nodereference_item($field, $value, FALSE);
240 }
241 if (!$field['required']) {
242 $options = array(0 => t('<none>')) + $options;
243 }
244
245 $form[$field['field_name']] = array('#tree' => TRUE);
246 $form[$field['field_name']]['nids'] = array(
247 '#type' => 'select',
248 '#title' => t($field['widget']['label']),
249 '#default_value' => $items['default nids'],
250 '#multiple' => $field['multiple'],
251 '#size' => $field['multiple'] ? min(count($options), 6) : 0,
252 '#options' => $options,
253 '#required' => $field['required'],
254 '#description' => t($field['widget']['description']),
255 );
256
257 return $form;
258
259 case 'process form values':
260 if ($field['multiple']) {
261 // if nothing selected, make it 'none'
262 if (empty($items['nids'])) {
263 $items['nids'] = array(0 => '0');
264 }
265 // drop the 'none' options if other items were also selected
266 elseif (count($items['nids']) > 1) {
267 unset($items['nids'][0]);
268 }
269
270 $items = array_values(content_transpose_array_rows_cols(array('nid' => $items['nids'])));
271 }
272 else {
273 $items[0]['nid'] = $items['nids'];
274 }
275 // Remove the widget's data representation so it isn't saved.
276 unset($items['nids']);
277 foreach ($items as $delta => $item) {
278 $items[$delta]['error_field'] = $field['field_name'] .'][nids';
279 }
280 }
281 }
282 else {
283 switch ($op) {
284 case 'prepare form values':
285 foreach ($items as $delta => $item) {
286 if (!empty($items[$delta]['nid'])) {
287 $items[$delta]['default node_name'] = db_result(db_query(db_rewrite_sql('SELECT n.title FROM {node} n WHERE n.nid = %d'), $items[$delta]['nid']));
288 $items[$delta]['default node_name'] .= ' [nid:'. $items[$delta]['nid'] .']';
289 }
290 }
291 break;
292
293 case 'form':
294 $form = array();
295 $form[$field['field_name']] = array('#tree' => TRUE);
296
297 if ($field['multiple']) {
298 $form[$field['field_name']]['#type'] = 'fieldset';
299 $form[$field['field_name']]['#description'] = t($field['widget']['description']);
300 $delta = 0;
301 foreach ($items as $item) {
302 if ($item['nid']) {
303 $form[$field['field_name']][$delta]['node_name'] = array(
304 '#type' => 'textfield',
305 '#title' => ($delta == 0) ? t($field['widget']['label']) : '',
306 '#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'],
307 '#default_value' => $item['default node_name'],
308 '#required' => ($delta == 0) ? $field['required'] : FALSE,
309 );
310 $delta++;
311 }
312 }
313 foreach (range($delta, $delta + 2) as $delta) {
314 $form[$field['field_name']][$delta]['node_name'] = array(
315 '#type' => 'textfield',
316 '#title' => ($delta == 0) ? t($field['widget']['label']) : '',
317 '#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'],
318 '#default_value' => '',
319 '#required' => ($delta == 0) ? $field['required'] : FALSE,
320 );
321 }
322 }
323 else {
324 $form[$field['field_name']][0]['node_name'] = array(
325 '#type' => 'textfield',
326 '#title' => t($field['widget']['label']),
327 '#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'],
328 '#default_value' => $items[0]['default node_name'],
329 '#required' => $field['required'],
330 '#description' => t($field['widget']['description']),
331 );
332 }
333 return $form;
334
335 case 'validate':
336 foreach ($items as $delta => $item) {
337 $error_field = $field['field_name'] .']['. $delta .'][node_name';
338 if (!empty($item['node_name'])) {
339 preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches);
340 if (!empty($matches)) {
341 // explicit nid
342 list(, $title, $nid) = $matches;
343 if (!empty($title) && ($n = node_load($nid)) && $title != $n->title) {
344 form_set_error($error_field, t('%name : Title mismatch. Please check your selection.'), array('%name' => t($field['widget']['label'])));
345 }
346 }
347 }
348 }
349 return;
350
351 case 'process form values':
352 foreach ($items as $delta => $item) {
353 $nid = 0;
354 if (!empty($item['node_name'])) {
355 preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches);
356 if (!empty($matches)) {
357 // explicit nid
358 $nid = $matches[2];
359 }
360 else {
361 // no explicit nid
362 // TODO :
363 // the best thing would be to present the user with an additional form,
364 // allowing the user to choose between valid candidates with the same title
365 // ATM, we pick the first matching candidate...
366 $nids = _nodereference_potential_references($field, FALSE, $item['node_name'], TRUE);
367 $nid = (!empty($nids)) ? array_shift(array_keys($nids)) : 0;
368 }
369 }
370 // Remove the widget's data representation so it isn't saved.
371 unset($items[$delta]['node_name']);
372 if (!empty($nid)) {
373 $items[$delta]['nid'] = $nid;
374 $items[$delta]['error_field'] = $field['field_name'] .']['. $delta .'][node_name';
375 }
376 elseif ($delta > 0) {
377 // Don't save empty fields when they're not the first value (keep '0' otherwise)
378 unset($items[$delta]);
379 }
380 }
381 break;
382 }
383 }
384 }
385
386 /**
387 * Fetch an array of all candidate referenced nodes, for use in presenting the selection form to the user.
388 */
389 function _nodereference_potential_references($field, $return_full_nodes = FALSE, $string = '', $exact_string = false) {
390 if (module_exists('views') && isset($field['advanced_view']) && $field['advanced_view'] != '--' && ($view = views_get_view($field['advanced_view']))) {
391 // advanced field : referenceable nodes defined by a view
392 // let views.module build the query
393
394 // arguments for the view
395 $view_args = array();
396 if (isset($field['advanced_view_args'])) {
397 // TODO: Support Tokens using token.module ?
398 $view_args = array_map(trim, explode(',', $field['advanced_view_args']));
399 }
400
401 if (isset($string)) {
402 views_view_add_filter($view, 'node', 'title', $exact_string ? '=' : 'contains', $string, null);
403 }
404
405 // we do need title field, so add it if not present (unlikely, but...)
406 $has_title = array_reduce($view->field, create_function('$a, $b', 'return ($b["field"] == "title") || $a;'), false);
407 if (!$has_title) {
408 views_view_add_field($view, 'node', 'title', '');
409 }
410 views_load_cache();
411 views_sanitize_view($view);
412
413 // make sure the fields get included in the query
414 $view->page = true;
415 $view->page_type = 'list';
416
417 // make sure the query is not cached
418 unset($view->query); // Views 1.5-
419 $view->is_cacheable = FALSE; // Views 1.6+
420
421 $view_result = views_build_view('result', $view, $view_args);
422 $result = $view_result['result'];
423 }
424 else {
425 // standard field : referenceable nodes defined by content types
426 // build the appropriate query
427 $related_types = array();
428 $args = array();
429
430 if (isset($field['referenceable_types'])) {
431 foreach ($field['referenceable_types'] as $related_type) {
432 if ($related_type) {
433 $related_types[] = " n.type = '%s'";
434 $args[] = $related_type;
435 }
436 }
437 }
438
439 $related_clause = implode(' OR ', $related_types);
440
441 if (!count($related_types)) {
442 return array();
443 }
444
445 if (isset($string)) {
446 $string_clause = $exact_string ? " AND n.title = '%s'" : " AND n.title LIKE '%%%s%'";
447 $related_clause = "(". $related_clause .")". $string_clause;
448 $args[] = $string;
449 }
450
451 $result = db_query(db_rewrite_sql("SELECT n.nid, n.title AS node_title, n.type AS node_type FROM {node} n WHERE ". $related_clause ." ORDER BY n.title, n.type"), $args);
452 }
453
454 if (db_num_rows($result) == 0) {
455 return array();
456 }
457
458 $rows = array();
459
460 while ($node = db_fetch_object($result)) {
461 if ($return_full_nodes) {
462 $rows[$node->nid] = $node;
463 }
464 else {
465 $rows[$node->nid] = $node->node_title;
466 }
467 }
468
469 return $rows;
470 }
471
472 /**
473 * Retrieve a pipe delimited string of autocomplete suggestions
474 */
475 function nodereference_autocomplete($field_name, $string = '') {
476 $fields = content_fields();
477 $field = $fields[$field_name];
478 $matches = array();
479
480 foreach (_nodereference_potential_references($field, TRUE, $string) as $row) {
481 $matches[$row->node_title .' [nid:'. $row->nid .']'] = _nodereference_item($field, $row);
482 }
483 print drupal_to_js($matches);
484 exit();
485 }
486
487 function _nodereference_item($field, $item, $html = TRUE) {
488 if (module_exists('views') && isset($field['advanced_view']) && $field['advanced_view'] != '--' && ($view = views_get_view($field['advanced_view']))) {
489 $output = theme('nodereference_item_advanced', $item, $view);
490 if (!$html) {
491 // Views theming runs check_plain (htmlentities) on the values.
492 // We reverse that with html_entity_decode.
493 $output = html_entity_decode(strip_tags($output), ENT_QUOTES);
494 }
495 }
496 else {
497 $output = theme('nodereference_item_simple', $item);
498 $output = $html ? check_plain($output) : $output;
499 }
500 return $output;
501 }
502
503 function theme_nodereference_item_advanced($item, $view) {
504 $fields = _views_get_fields();
505 $item_fields = array();
506 foreach ($view->field as $field) {
507 $value = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $item, $view);
508 // remove link tags (ex : for node titles)
509 $value = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '$1', $value);
510 if (!empty($value)) {
511 $item_fields[] = "<span class='view-field view-data-$field[queryname]'>$value</span>";;
512 }
513 }
514 $output = implode(' - ', $item_fields);
515 $output = "<span class='view-item view-item-$view->name'>$output</span>";
516 return $output;
517 }
518
519 function theme_nodereference_item_simple($item) {
520 return $item->node_title;
521 }
522
523 /**
524 * Provide a list of users to filter on.
525 */
526 function _nodereference_filter_handler($op, $filterinfo) {
527 $options = array(0 => t('<empty>'));
528 $options = $options + _nodereference_potential_references($filterinfo['extra']['field']);
529 return $options;
530 }
531
532 /**
533 * Implementation of hook_panels_relationships().
534 */
535 function nodereference_panels_relationships() {
536 $args = array();
537 $args['node_from_noderef'] = array(
538 'title' => t('Node from reference'),
539 'keyword' => 'nodereference',
540 'description' => t('Adds a node from a node reference in a node context; if multiple nodes are referenced, this will get the first referenced node only.'),
541 'required context' => new panels_required_context(t('Node'), 'node'),
542 'context' => 'nodereference_node_from_noderef_context',
543 'settings form' => 'nodereference_node_from_noderef_settings_form',
544 'settings form validate' => 'nodereference_node_from_noderef_settings_form_validate',
545 );
546 return $args;
547 }
548
549 /**
550 * Return a new panels context based on an existing context
551 */
552 function nodereference_node_from_noderef_context($context = NULL, $conf) {
553 // If unset it wants a generic, unfilled context, which is just NULL
554 if (empty($context->data)) {
555 return panels_context_create_empty('node', NULL);
556 }
557 if (isset($context->data->{$conf['field_name']}[0]['nid']) && ($nid = $context->data->{$conf['field_name']}[0]['nid'])) {
558 if ($node = node_load($nid)) {
559 return panels_context_create('node', $node);
560 }
561 }
562 }
563
564 /**
565 * Settings form for the panels relationship.
566 */
567 function nodereference_node_from_noderef_settings_form($conf) {
568 $options = array();
569 foreach (content_fields() as $field) {
570 if ($field['type'] == 'nodereference') {
571 $options[$field['field_name']] = t($field['widget']['label']);
572 }
573 }
574 $form['field_name'] = array(
575 '#title' => t('Node reference field'),
576 '#type' => 'select',
577 '#options' => $options,
578 '#default_value' => $conf['field_name'],
579 '#prefix' => '<div class="clear-block">',
580 '#suffix' => '</div>',
581 );
582
583 return $form;
584 }

  ViewVC Help
Powered by ViewVC 1.1.2