summaryrefslogtreecommitdiffstats
path: root/nodereference.module
blob: 15f31d819f9d8a59d4a8c70d57432b8c15b87c2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<?php
// $Id$

/**
 * @file
 * Defines a field type for referencing one node from another.
 */


/**
 * Implementation of hook_menu().
 */
function nodereference_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array('path' => 'nodereference/autocomplete', 'title' => t('node reference autocomplete'),
      'callback' => 'nodereference_autocomplete', 'access' => user_access('access content'), 'type' => MENU_CALLBACK);
  }

  return $items;
}

/**
 * Implementation of hook_field_info().
 */
function nodereference_field_info() {
  return array(
    'nodereference' => array('label' => t('Node Reference')),
  );
}

/**
 * Implementation of hook_field_settings().
 */
function nodereference_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
      $form['referenceable_types'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Content types that can be referenced'),
        '#multiple' => TRUE,
        '#default_value' => isset($field['referenceable_types']) ? $field['referenceable_types'] : array(),
        '#options' => array_map('check_plain', node_get_types('names')),
      );
      if (module_exists('views')) {
        $views = array('--' => '--');
        $result = db_query("SELECT name FROM {view_view} ORDER BY name");
        while ($view = db_fetch_array($result)) {
          $views[t('Existing Views')][$view['name']] = $view['name'];
        }
        views_load_cache();
        $default_views = _views_get_default_views();
        foreach ($default_views as $view) {
          $views[t('Default Views')][$view->name] = $view->name;
        }
        if (count($views) > 1) {
          $form['advanced'] = array(
             '#type' => 'fieldset',
             '#title' => t('Advanced - Nodes that can be referenced (View)'),
             '#collapsible' => TRUE,
             '#collapsed' => !isset($field['advanced_view']) || $field['advanced_view'] == '--',
           );
          $form['advanced']['advanced_view'] = array(
            '#type' => 'select',
            '#title' => t('View'),
            '#options' => $views,
            '#default_value' => isset($field['advanced_view']) ? $field['advanced_view'] : '--',
            '#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>'),
          );
          $form['advanced']['advanced_view_args'] = array(
            '#type' => 'textfield',
            '#title' => t('View arguments'),
            '#default_value' => isset($field['advanced_view_args']) ? $field['advanced_view_args'] : '',
            '#required' => FALSE,
            '#description' => t('Provide a comma separated list of arguments to pass to the view.'),
          );
        }
      }
      return $form;

    case 'save':
      $settings = array('referenceable_types');
      if (module_exists('views')) {
        $settings[] = 'advanced_view';
        $settings[] = 'advanced_view_args';
      }
      return $settings;

    case 'database columns':
      $columns = array(
        'nid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),
      );
      return $columns;

    case 'filters':
      return array(
        'default' => array(
          'list' => '_nodereference_filter_handler',
          'list-type' => 'list',
          'operator' => 'views_handler_operator_or',
          'value-type' => 'array',
          'extra' => array('field' => $field),
        ),
      );
  }
}

/**
 * Implementation of hook_field().
 */
function nodereference_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      $refs = _nodereference_potential_references($field, TRUE);
      foreach ($items as $delta => $item) {
        $error_field = isset($item['error_field']) ? $item['error_field'] : '';
        unset($item['error_field']);
        if (!empty($item['nid'])) {
          if (!in_array($item['nid'], array_keys($refs))) {
            form_set_error($error_field, t('%name : This post can\'t be referenced.', array('%name' => t($field['widget']['label']))));
          }
        }
      }
      return;
  }
}

/**
 * Implementation of hook_field_formatter_info().
 */
function nodereference_field_formatter_info() {
  return array(
    'default' => array(
      'label' => t('Title (link)'),
      'field types' => array('nodereference'),
    ),
    'plain' => array(
      'label' => t('Title (no link)'),
      'field types' => array('nodereference'),
    ),
    'full' => array(
      'label' => t('Full node'),
      'field types' => array('nodereference'),
    ),
    'teaser' => array(
      'label' => t('Teaser'),
      'field types' => array('nodereference'),
    ),
  );
}

/**
 * Implementation of hook_field_formatter().
 */
function nodereference_field_formatter($field, $item, $formatter, $node) {
  static $titles = array();

  // We store the rendered nids in order to prevent infinite recursion
  // when using the 'full node' / 'teaser' formatters.
  static $recursion_queue = array();

  if (empty($item['nid']) || !is_numeric($item['nid'])) {
    return '';
  }

  if ($formatter == 'full' || $formatter == 'teaser') {
    // If no 'referencing node' is set, we are starting a new 'reference thread'
    if (!isset($node->referencing_node)) {
      $recursion_queue = array();
    }
    $recursion_queue[] = $node->nid;
    if (in_array($item['nid'], $recursion_queue)) {
      // Prevent infinite recursion caused by reference cycles :
      // if the node has already been rendered earlier in this 'thread',
      // we fall back to 'default' (node title) formatter.
      $formatter = 'default';
    }
    elseif ($referenced_node = node_load($item['nid'])) {
      $referenced_node->referencing_node = $node;
      $referenced_node->referencing_field = $field;
      $titles[$item['nid']] = $referenced_node->title;
    }
  }

  if (!isset($titles[$item['nid']])) {
    $title = db_result(db_query("SELECT title FROM {node} WHERE nid=%d", $item['nid']));
    $titles[$item['nid']] = $title ? $title : '';
  }

  switch ($formatter) {
    case 'full':
      return $referenced_node ? node_view($referenced_node, FALSE) : '';

    case 'teaser':
      return $referenced_node ? node_view($referenced_node, TRUE) : '';

    case 'plain':
      return check_plain($titles[$item['nid']]);

    default:
      return $titles[$item['nid']] ? l($titles[$item['nid']], 'node/'. $item['nid']) : '';
  }
}

/**
 * Implementation of hook_widget_info().
 */
function nodereference_widget_info() {
  return array(
    'nodereference_select' => array(
      'label' => t('Select List'),
      'field types' => array('nodereference'),
    ),
    'nodereference_autocomplete' => array(
      'label' => t('Autocomplete Text Field'),
      'field types' => array('nodereference'),
    ),
  );
}

/**
 * Implementation of hook_widget().
 */
function nodereference_widget($op, &$node, $field, &$items) {
  if ($field['widget']['type'] == 'nodereference_select') {
    switch ($op) {
      case 'prepare form values':
        $items_transposed = content_transpose_array_rows_cols($items);
        $items['default nids'] = $items_transposed['nid'];
        break;

      case 'form':
        $form = array();

        $options = _nodereference_potential_references($field, TRUE);
        foreach ($options as $key => $value) {
          $options[$key] = _nodereference_item($field, $value, FALSE);
        }
        if (!$field['required']) {
          $options = array(0 => t('<none>')) + $options;
        }

        $form[$field['field_name']] = array('#tree' => TRUE);
        $form[$field['field_name']]['nids'] = array(
          '#type' => 'select',
          '#title' => t($field['widget']['label']),
          '#default_value' => $items['default nids'],
          '#multiple' => $field['multiple'],
          '#size' =>  $field['multiple'] ? min(count($options), 6) : 0,
          '#options' => $options,
          '#required' => $field['required'],
          '#description' => content_filter_xss(t($field['widget']['description'])),
        );

        return $form;

      case 'process form values':
        if ($field['multiple']) {
          // if nothing selected, make it 'none'
          if (empty($items['nids'])) {
            $items['nids'] = array(0 => '0');
          }
          // drop the 'none' options if other items were also selected
          elseif (count($items['nids']) > 1) {
            unset($items['nids'][0]);
          }

          $items = array_values(content_transpose_array_rows_cols(array('nid' => $items['nids'])));
        }
        else {
          $items[0]['nid'] = $items['nids'];
        }
        // Remove the widget's data representation so it isn't saved.
        unset($items['nids']);
        foreach ($items as $delta => $item) {
          $items[$delta]['error_field'] =  $field['field_name'] .'][nids';
        }
    }
  }
  else {
    switch ($op) {
      case 'prepare form values':
        foreach ($items as $delta => $item) {
          if (!empty($items[$delta]['nid'])) {
            $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']));
            $items[$delta]['default node_name'] .= ' [nid:'. $items[$delta]['nid'] .']';
          }
        }
        break;

      case 'form':
        $form = array();
        $form[$field['field_name']] = array('#tree' => TRUE);

        if ($field['multiple']) {
          $form[$field['field_name']]['#type'] = 'fieldset';
          $form[$field['field_name']]['#description'] = content_filter_xss(t($field['widget']['description']));
          $delta = 0;
          foreach ($items as $item) {
            if ($item['nid']) {
              $form[$field['field_name']][$delta]['node_name'] = array(
                '#type' => 'textfield',
                '#title' => ($delta == 0) ? t($field['widget']['label']) : '',
                '#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'],
                '#default_value' => $item['default node_name'],
                '#required' => ($delta == 0) ? $field['required'] : FALSE,
              );
              $delta++;
            }
          }
          foreach (range($delta, $delta + 2) as $delta) {
            $form[$field['field_name']][$delta]['node_name'] = array(
              '#type' => 'textfield',
              '#title' => ($delta == 0) ? t($field['widget']['label']) : '',
              '#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'],
              '#default_value' => '',
              '#required' => ($delta == 0) ? $field['required'] : FALSE,
            );
          }
        }
        else {
          $form[$field['field_name']][0]['node_name'] = array(
            '#type' => 'textfield',
            '#title' => t($field['widget']['label']),
            '#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'],
            '#default_value' => $items[0]['default node_name'],
            '#required' => $field['required'],
            '#description' => content_filter_xss( t( $field['widget']['description'] )),
          );
        }
        return $form;

      case 'validate':
        foreach ($items as $delta => $item) {
          $error_field = $field['field_name'] .']['. $delta .'][node_name';
          if (!empty($item['node_name'])) {
            preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches);
            if (!empty($matches)) {
              // explicit nid
              list(, $title, $nid) = $matches;
              if (!empty($title) && ($n = node_load($nid)) && $title != $n->title) {
                form_set_error($error_field, t('%name : Title mismatch. Please check your selection.', array('%name' => t($field['widget']['label']))));
              }
            }
            else {
              // no explicit nid
              $nids = _nodereference_potential_references($field, FALSE, $item['node_name'], TRUE);
              if (empty($nids)) {
                form_set_error($error_field, t('%name: Found no valid post with that title.', array('%name' => t($field['widget']['label']))));
              }
              else {
                // TODO:
                // the best thing would be to present the user with an additional form,
                // allowing the user to choose between valid candidates with the same title
                // ATM, we pick the first matching candidate...
                $nid = array_shift(array_keys($nids));
              }
            }
          }
        }
        return;

      case 'process form values':
        foreach ($items as $delta => $item) {
          $nid = 0;
          if (!empty($item['node_name'])) {
            preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches);
            if (!empty($matches)) {
              // explicit nid
              $nid = $matches[2];
            }
            else {
              // no explicit nid
              // TODO :
              // the best thing would be to present the user with an additional form,
              // allowing the user to choose between valid candidates with the same title
              // ATM, we pick the first matching candidate...
              $nids = _nodereference_potential_references($field, FALSE, $item['node_name'], TRUE);
              $nid = (!empty($nids)) ? array_shift(array_keys($nids)) : 0;
            }
          }
          // Remove the widget's data representation so it isn't saved.
          unset($items[$delta]['node_name']);
          if (!empty($nid)) {
            $items[$delta]['nid'] = $nid;
            $items[$delta]['error_field'] = $field['field_name'] .']['. $delta .'][node_name';
          }
          elseif ($delta > 0) {
            // Don't save empty fields when they're not the first value (keep '0' otherwise)
            unset($items[$delta]);
          }
        }
        break;
    }
  }
}

/**
 * Fetch an array of all candidate referenced nodes, for use in presenting the selection form to the user.
 */
function _nodereference_potential_references($field, $return_full_nodes = FALSE, $string = '', $exact_string = false) {
  if (module_exists('views') && isset($field['advanced_view']) && $field['advanced_view'] != '--' && ($view = views_get_view($field['advanced_view']))) {
    // advanced field : referenceable nodes defined by a view
    // let views.module build the query

    // arguments for the view
    $view_args = array();
    if (isset($field['advanced_view_args'])) {
      // TODO: Support Tokens using token.module ?
      $view_args = array_map(trim, explode(',', $field['advanced_view_args']));
    }

    if (isset($string)) {
      views_view_add_filter($view, 'node', 'title', $exact_string ? '=' : 'contains', $string, null);
    }

    // we do need title field, so add it if not present (unlikely, but...)
    $has_title = array_reduce($view->field, create_function('$a, $b', 'return ($b["field"] == "title") || $a;'), false);
    if (!$has_title) {
      views_view_add_field($view, 'node', 'title', '');
    }
    views_load_cache();
    views_sanitize_view($view);

    // make sure the fields get included in the query
    $view->page = true;
    $view->page_type = 'list';

    // make sure the query is not cached
    unset($view->query); // Views 1.5-
    $view->is_cacheable = FALSE; // Views 1.6+

    $view_result = views_build_view('result', $view, $view_args);
    $result = $view_result['result'];
  }
  else {
    // standard field : referenceable nodes defined by content types
    // build the appropriate query
    $related_types = array();
    $args = array();

    if (isset($field['referenceable_types'])) {
      foreach ($field['referenceable_types'] as $related_type) {
        if ($related_type) {
          $related_types[] = " n.type = '%s'";
          $args[] = $related_type;
        }
      }
    }

    $related_clause = implode(' OR ', $related_types);

    if (!count($related_types)) {
      return array();
    }

    if ($string !== '') {
      $string_clause = $exact_string ? " AND n.title = '%s'" : " AND n.title LIKE '%%%s%'";
      $related_clause = "(". $related_clause .")". $string_clause;
      $args[] = $string;
    }

    $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);
  }

  if (db_num_rows($result) == 0) {
    return array();
  }

  $rows = array();

  while ($node = db_fetch_object($result)) {
    if ($return_full_nodes) {
      $rows[$node->nid] = $node;
    }
    else {
      $rows[$node->nid] = $node->node_title;
    }
  }

  return $rows;
}

/**
 * Retrieve a pipe delimited string of autocomplete suggestions
 */
function nodereference_autocomplete($field_name, $string = '') {
  $fields = content_fields();
  $field = $fields[$field_name];
  $matches = array();

  foreach (_nodereference_potential_references($field, TRUE, $string) as $row) {
    $matches[$row->node_title .' [nid:'. $row->nid .']'] = _nodereference_item($field, $row);
  }
  print drupal_to_js($matches);
  exit();
}

function _nodereference_item($field, $item, $html = TRUE) {
  if (module_exists('views') && isset($field['advanced_view']) && $field['advanced_view'] != '--' && ($view = views_get_view($field['advanced_view']))) {
    $output = theme('nodereference_item_advanced', $item, $view);
    if (!$html) {
      // Views theming runs check_plain (htmlentities) on the values.
      // We reverse that with html_entity_decode.
      $output = html_entity_decode(strip_tags($output), ENT_QUOTES);
    }
  }
  else {
    $output = theme('nodereference_item_simple', $item);
    $output = $html ? check_plain($output) : $output;
  }
  return $output;
}

function theme_nodereference_item_advanced($item, $view) {
  $fields = _views_get_fields();
  $item_fields = array();
  foreach ($view->field as $field) {
    $value = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $item, $view);
    // remove link tags (ex : for node titles)
    $value = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '$1', $value);
    if (!empty($value)) {
      $item_fields[] = "<span class='view-field view-data-$field[queryname]'>$value</span>";;
    }
  }
  $output = implode(' - ', $item_fields);
  $output = "<span class='view-item view-item-$view->name'>$output</span>";
  return $output;
}

function theme_nodereference_item_simple($item) {
  return $item->node_title;
}

/**
 * Provide a list of nodes to filter on.
 */
function _nodereference_filter_handler($op, $filterinfo) {
  $options = array(0 => t('<empty>'));
  $options = $options + _nodereference_potential_references($filterinfo['extra']['field']);
  return $options;
}

/**
 * Implementation of hook_panels_relationships().
 */
function nodereference_panels_relationships() {
  $args = array();
  $args['node_from_noderef'] = array(
    'title' => t('Node from reference'),
    'keyword' => 'nodereference',
    '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.'),
    'required context' => new panels_required_context(t('Node'), 'node'),
    'context' => 'nodereference_node_from_noderef_context',
    'settings form' => 'nodereference_node_from_noderef_settings_form',
    'settings form validate' => 'nodereference_node_from_noderef_settings_form_validate',
    );
  return $args;
}

/**
 * Return a new panels context based on an existing context
 */
function nodereference_node_from_noderef_context($context = NULL, $conf) {
  // If unset it wants a generic, unfilled context, which is just NULL
  if (empty($context->data)) {
    return panels_context_create_empty('node', NULL);
  }
  if (isset($context->data->{$conf['field_name']}[0]['nid']) && ($nid = $context->data->{$conf['field_name']}[0]['nid'])) {
    if ($node = node_load($nid)) {
      return panels_context_create('node', $node);
    }
  }
}

/**
 * Settings form for the panels relationship.
 */
function nodereference_node_from_noderef_settings_form($conf) {
  $options = array();
  foreach (content_fields() as $field) {
    if ($field['type'] == 'nodereference') {
      $options[$field['field_name']] = t($field['widget']['label']);
    }
  }
  $form['field_name'] = array(
    '#title' => t('Node reference field'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $conf['field_name'],
    '#prefix' => '<div class="clear-block">',
    '#suffix' => '</div>',
  );

  return $form;
}