/[drupal]/contributions/modules/composite/composite.node.inc
ViewVC logotype

Contents of /contributions/modules/composite/composite.node.inc

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


Revision 1.1 - (show annotations) (download) (as text)
Tue Oct 14 01:03:56 2008 UTC (13 months, 2 weeks ago) by bengtan
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-BETA3, DRUPAL-6--1-0-BETA2, DRUPAL-6--1-0-BETA1, HEAD
Branch point for: DRUPAL-6--1
File MIME type: text/x-php
Initial import.
1 <?php
2 // $Id$
3
4
5 function composite_composite_node_api(&$reference, $op, $a3 = NULL, $a4 = NULL) {
6 switch ($op) {
7 // Derive additional fields
8 case 'load':
9 // Note: can't do a node_load here, otherwise, we run the chance of an infinite loop.
10 list($unused, $reference['nid']) = explode('-', $reference['id'], 2);
11 break;
12
13 // Generate and insert an informative human-readable string into ['info']
14 case 'info':
15 $node = node_load($reference['nid']);
16 $reference['info'] = check_plain($node->title);
17 break;
18
19 // Return a rendering of the reference item
20 case 'view':
21 $node = node_load($reference['nid']);
22 if ($node->nid) {
23 switch ($reference['data']['format']) {
24 case 'title':
25 $output = theme('composite_node_title', check_plain($node->title));
26 break;
27
28 case 'title-link':
29 $output = theme('composite_node_title', l($node->title, 'node/' . $node->nid));
30 break;
31
32 case 'teaser':
33 $output = node_view($node, TRUE, FALSE);
34 break;
35
36 case 'full':
37 if (_composite_nodes_recursion_check($node)) {
38 drupal_set_message(t('You have created an infinite loop. One node is displaying a second node which in turn displays the first node.'), 'error');
39 $output = node_view($node, TRUE, FALSE);
40 }
41 else {
42 $output = node_view($node, FALSE, FALSE);
43 }
44 break;
45 }
46 return $output;
47 }
48 break;
49
50 // Return form elements for additional settings
51 case 'settings':
52 $form['format'] = array(
53 '#title' => t('Display as'),
54 '#type' => 'select',
55 '#options' => array(
56 'title' => t('Title'),
57 'title-link' => t('Title (link)'),
58 'teaser' => t('Teaser'),
59 'full' => t('Full node'),
60 ),
61 '#default_value' => $reference['data']['format'] ? $reference['data']['format'] : 'teaser',
62 );
63 return $form;
64 break;
65 }
66 }
67
68 function composite_composite_node_access($op, $node) {
69 // Only show nodes tab if referenceable types or a view has been configured
70 $types = variable_get('composite_referenceable_types_' . $node->type, array());
71 $view_name = variable_get('composite_advanced_view_' . $node->type, '--');
72
73 $access = FALSE;
74 if ($types || $view_name != '--')
75 $access = node_access($op, $node);
76 return $access;
77 }
78
79 /**
80 * Generates and returns a keyed array of potential composite references
81 * so composite.module can create a meaningful local task.
82 */
83 function composite_composite_node_potentials($node) {
84 $node_list = variable_get('composite_advanced_view_' . $node->type, '--') == '--' ? _composite_nodes_form_get_potentials_standard($node) : _composite_nodes_form_get_potentials_views($node);
85
86 foreach ($node_list as $nid => $v) {
87 $options['node-' . $nid] = $v->title;
88 }
89 return $options;
90 }
91
92 function _composite_nodes_form_get_potentials_standard($node) {
93 $types = variable_get('composite_referenceable_types_' . $node->type, array());
94 foreach ($types as $k => $type) {
95 $types[$k] = "'". $types[$k] ."'";
96 }
97
98 $list = array();
99 $result = db_query("SELECT nid, title FROM {node} WHERE type IN (". implode(', ', $types) . ") ORDER BY title, type, nid");
100 while ($object = db_fetch_object($result)) {
101 $list[$object->nid] = $object;
102 }
103 return $list;
104 }
105
106 function _composite_nodes_form_get_potentials_views($node) {
107 $view_name = variable_get('composite_advanced_view_' . $node->type, '--');
108
109 if (module_exists('views') && $view_name != '--' && $view = views_get_view($view_name)) {
110 $view_args = variable_get('composite_advanced_view_args_' . $node->type, '');
111 $view->set_arguments($view_args ? explode(',', $view_args) : array());
112 $result = $view->execute();
113 $results = $view->result;
114
115 // We rely on the view returning objects with ->nid and ->node_title.
116 // Can we safely presume this will always be true?
117 $list = array();
118 foreach ($results as $object) {
119 $object->title = $object->node_title;
120 $list[$object->nid] = $object;
121 }
122 return $list;
123 }
124 else {
125 return array();
126 }
127 }
128
129 // Returns true if we there is an infinite loop.
130 function _composite_nodes_recursion_check($node) {
131 static $nodes = array();
132
133 if (array_key_exists($node->nid, $nodes)) {
134 return true;
135 }
136 else {
137 $nodes[$node->nid] = $node->nid;
138 return false;
139 }
140 }

  ViewVC Help
Powered by ViewVC 1.1.2