/[drupal]/contributions/modules/restapi/restapi_node.module
ViewVC logotype

Contents of /contributions/modules/restapi/restapi_node.module

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


Revision 1.1 - (show annotations) (download) (as text)
Thu Mar 20 22:16:50 2008 UTC (20 months, 1 week ago) by hanenkamp
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Added a new REST API Node module for creating, reading, updating, and deleting nodes.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Provides a module for giving REST API access to list, create, view, update,
7 * and delete nodes.
8 */
9
10 /**
11 * Implements hook_restapi_menu(). Adds the node callbacks.
12 */
13 function restapi_node_restapi_menu($may_cache) {
14 if ($may_cache) {
15 $items[] = array(
16 'method' => 'GET',
17 'path' => '=/node',
18 'callback' => 'restapi_node_content_types',
19 );
20 }
21
22 elseif (arg(0) == '=' && arg(1) == 'node') {
23 $types = array_keys(node_get_types('names'));
24 $types[] = '*';
25
26 if (in_array(arg(2), $types)) {
27 $items[] = array(
28 'method' => 'GET',
29 'path' => '=/node/'.arg(2),
30 'callback' => 'restapi_node_columns',
31 'callback arguments' => array(arg(2)),
32 );
33
34 if (arg(2) != '*') {
35 $items[] = array(
36 'method' => 'POST',
37 'path' => '=/node/'.arg(2),
38 'callback' => 'restapi_node_create',
39 'callback arguments' => array(arg(2)),
40 );
41 }
42
43 $columns = _restapi_node_columns(arg(2));
44
45 if (in_array(arg(3), $columns)) {
46 $items[] = array(
47 'method' => 'GET',
48 'path' => '=/node/'.arg(2).'/'.arg(3),
49 'callback' => 'restapi_node_list_column',
50 'callback arguments' => array(arg(2), arg(3)),
51 );
52
53 if (arg(4)) {
54 $rud_path = '=/node/'.implode('/', array(arg(2), arg(3), arg(4)));
55 $rud_args = array(arg(2), arg(3), arg(4));
56
57 $items[] = array(
58 'method' => 'GET',
59 'path' => $rud_path,
60 'callback' => 'restapi_node_read',
61 'callback arguments' => $rud_args,
62 );
63
64 $items[] = array(
65 'method' => 'PUT',
66 'path' => $rud_path,
67 'callback' => 'restapi_node_update',
68 'callback arguments' => $rud_args,
69 );
70
71 $items[] = array(
72 'method' => 'DELETE',
73 'path' => $rud_path,
74 'callback' => 'restapi_node_delete',
75 'callback arguments' => $rud_args,
76 );
77
78 if (in_array(arg(5), $columns)) {
79 $items[] = array(
80 'method' => 'GET',
81 'path' => $rud_path.'/'.arg(5),
82 'callback' => 'restapi_node_read_column',
83 'callback arguments' => array(arg(2), arg(3), arg(4), arg(5)),
84 );
85 }
86 }
87 }
88 }
89 }
90
91 return $items;
92 }
93
94 function _restapi_node_columns($type) {
95 // TODO There has to be a better way
96 return array(
97 'nid', 'vid', 'type', 'title', 'uid', 'status', 'created', 'changed',
98 'comment', 'promote', 'moderate', 'sticky', 'body', 'teaser', 'log',
99 'timestamp', 'format'
100 );
101 }
102
103 function restapi_node_restapi_help() {
104 $help['prototypes'][] = array(
105 'method' => 'GET',
106 'path' => '/=/node',
107 'description' => 'list all content types',
108 );
109 $help['prototypes'][] = array(
110 'method' => 'GET',
111 'path' => '/=/node/<type>',
112 'description' => 'list the fields available for the named type',
113 );
114 $help['prototypes'][] = array(
115 'method' => 'GET',
116 'path' => '/=/node/<type>/<column>',
117 'description' => 'list distinct values for the given column',
118 );
119 $help['prototypes'][] = array(
120 'method' => 'GET',
121 'path' => '/=/node/<type>/<column>/<key>',
122 'description' => 'display the first node where <column> = <key>',
123 );
124 $help['prototypes'][] = array(
125 'method' => 'GET',
126 'path' => '/=/node/<type>/<column>/<key>/<field>',
127 'description' => 'show the content of the field for the first node where <column> = <key>',
128 );
129 $help['prototypes'][] = array(
130 'method' => 'POST',
131 'path' => '/=/node/<type>',
132 'description' => 'create a node',
133 );
134 $help['prototypes'][] = array(
135 'method' => 'PUT',
136 'path' => '/=/node/<type>/<column>/<key>',
137 'description' => 'update the first node where <column> = <key>',
138 );
139 $help['prototypes'][] = array(
140 'method' => 'DELETE',
141 'path' => '/=/node/<type>/</column>/<key>',
142 'description' => 'delete the first node where <column> = <key>',
143 );
144
145 $help['suffix'] = 'You may use a <type> of "*" for nodes to specify any type is acceptable.';
146
147 return $help;
148 }
149
150 function restapi_node_content_types() {
151 $types = node_get_types();
152 print restapi_serialize($types);
153 }
154
155 function restapi_node_columns($data, $type) {
156 $columns = _restapi_node_columns($type);
157 print restapi_serialize($columns);
158 }
159
160 function restapi_node_create($data, $type) {
161 error_log(print_r($data, TRUE));
162 $node = (object) $data;
163 $node->type = $type;
164 node_save($node);
165 print restapi_serialize($node);
166 }
167
168 function restapi_node_list_column($data, $type, $column) {
169 $sql = "SELECT DISTINCT n.$column FROM {node} n";
170 $args = array();
171 if ($type != '*') {
172 $sql .= " WHERE type = '%s'";
173 $args[] = $type;
174 }
175 $result = db_query(db_rewrite_sql($sql), $args);
176
177 while ($node = db_fetch_object($result)) {
178 $values[] = $node->$column;
179 }
180
181 print restapi_serialize($values);
182 }
183
184 function _restapi_node_load($type, $column, $value) {
185 $args = array( $column => $value );
186 if ($type != '*') {
187 $args['type'] = $type;
188 }
189
190 $node = node_load($args);
191
192 return $node;
193 }
194
195 function restapi_node_read($data, $type, $column, $value) {
196 $node = _restapi_node_load($type, $column, $value);
197 print restapi_serialize($node);
198 }
199
200 function restapi_node_update($data, $type, $column, $value) {
201 $orig_node = _restapi_node_load($type, $column, $value);
202 $save_node = (object) $data;
203 $save_node->nid = $orig_node->nid;
204 node_save($save_node);
205 print restapi_serialize($save_node);
206 }
207
208 function restapi_node_delete($data, $type, $column, $value) {
209 $node = _restapi_node_load($type, $column, $value);
210 node_delete($node->nid);
211 print restapi_serialize(TRUE);
212 }
213
214 function restapi_node_read_column($data, $type, $column, $value, $field) {
215 $node = _restapi_node_load($type, $column, $value);
216 print restapi_serialize($node->$field);
217 }

  ViewVC Help
Powered by ViewVC 1.1.2