/[drupal]/contributions/modules/resource/resource.module
ViewVC logotype

Contents of /contributions/modules/resource/resource.module

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


Revision 1.1 - (show annotations) (download) (as text)
Fri Mar 14 10:17:44 2008 UTC (20 months, 1 week ago) by arto
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-php
Initial import of the Resource module.

Enables the creation of resource nodes which serve as references to remote
content identified by URIs and published in RDF format.
1 <?php
2 // $Id$
3 /**
4 * resource.module - Enables the use of remote content published in RDF format.
5 *
6 * @author Arto Bendiken <http://bendiken.net/>
7 * @copyright Copyright (c) 2007-2008 Arto Bendiken. All rights reserved.
8 * @license GPL <http://creativecommons.org/licenses/GPL/2.0/>
9 * @package resource.module
10 */
11
12 //////////////////////////////////////////////////////////////////////////////
13 // Module constants and settings
14
15 define('RESOURCE_GRAPH', 'http://drupal.org/project/resource');
16 define('RESOURCE_REPOSITORY', 'resource');
17 define('RESOURCE_TITLE', variable_get('resource_title', 'rdfs:label,dc:title,foaf:name'));
18 define('RESOURCE_DESCRIPTION', variable_get('resource_description', 'rdfs:comment,dc:description'));
19
20 //////////////////////////////////////////////////////////////////////////////
21 // Core API hooks
22
23 /**
24 * Implementation of hook_help().
25 */
26 function resource_help($path, $arg = NULL) {
27 switch ($path) {
28 case 'admin/content/node/import-resources':
29 return '<p>' . t('') . '</p>'; // TODO
30 }
31 }
32
33 /**
34 * Implementation of hook_perm().
35 */
36 function resource_perm() {
37 return array(
38 'access resources',
39 'create resources',
40 'edit any resources',
41 'edit own resources',
42 'delete any resources',
43 'delete own resources',
44 );
45 }
46
47 /**
48 * Implementation of hook_menu().
49 */
50 function resource_menu() {
51 return array(
52 // Administer >> Content management >> Content
53 'admin/content/node/import-resources' => array(
54 'title' => 'Import resources',
55 'type' => MENU_LOCAL_TASK,
56 'weight' => 20,
57 'access arguments' => array('administer nodes'),
58 'page callback' => 'drupal_get_form',
59 'page arguments' => array('resource_admin_import'),
60 'file' => 'resource.admin.inc',
61 ),
62 );
63 }
64
65 /**
66 * Implementation of hook_link().
67 */
68 function resource_link($type, $node = NULL, $teaser = FALSE) {
69 if ($type == 'node' && $node->type == 'resource' && !empty($node->resource_uri)) {
70 return array(
71 'resource_link' => array(
72 'title' => t('View original'),
73 'href' => $node->resource_uri,
74 'attributes' => array('title' => t(''), 'absolute' => TRUE),
75 ),
76 );
77 }
78 }
79
80 /**
81 * Implementation of hook_form_alter().
82 */
83 function resource_form_alter(&$form, $form_state, $form_id) {
84 switch ($form_id) {
85
86 // Administer >> Content management >> Content types >> Edit
87 case 'node_type_form':
88 if (@$form['orig_type']['#value'] == 'resource') {
89 $form['resource'] = array('#type' => 'fieldset', '#title' => t('Resource settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 5);
90 // TODO: Only display RDF properties which have known titles/descriptions
91 // TODO: RDF properties to use for titles
92 // TODO: RDF properties to use for descriptions
93 // TODO: Don't display RDF properties from the following vocabularies
94 // TODO: Enforce resource URI uniqueness constraint
95 }
96 break;
97 }
98 }
99
100 //////////////////////////////////////////////////////////////////////////////
101 // Node API hooks
102
103 /**
104 * Implementation of hook_node_info().
105 */
106 function resource_node_info() {
107 return array(
108 'resource' => array(
109 'module' => 'resource_node',
110 'name' => t('Resource'),
111 'description' => t('A <em>resource</em> is a reference to external content identified by a <acronym title="Uniform Resource Identifier">URI</a> and published in <acronym title="Resource Description Framework">RDF</a> format.'),
112 'title_label' => t('Title'),
113 'has_title' => TRUE,
114 'body_label' => t('Description'),
115 'has_body' => FALSE,
116 ),
117 );
118 }
119
120 /**
121 * Implementation of hook_access().
122 */
123 function resource_node_access($op, $node) {
124 switch ($op) {
125 case 'view':
126 return user_access('access resources');
127 case 'create':
128 return user_access('create resources');
129 case 'update':
130 global $user;
131 return user_access('edit any resources') || (user_access('edit own resources') && $user->uid == $node->uid);
132 case 'delete':
133 global $user;
134 return user_access('delete any resources') || (user_access('delete own resources') && $user->uid == $node->uid);
135 }
136 }
137
138 /**
139 * Implementation of hook_form().
140 */
141 function resource_node_form(&$node, $form_state) {
142 if (is_array($node)) {
143 return node_form($node, $form_state);
144 }
145
146 $form = array();
147 $type = node_get_types('type', $node);
148
149 $form['resource_uri'] = array('#type' => 'textfield', '#title' => t('URI'), '#default_value' => @$node->resource_uri, '#required' => TRUE, '#weight' => -10);
150
151 if ($type->has_title) {
152 $form['title'] = array('#type' => 'textfield', '#title' => check_plain($type->title_label), '#default_value' => $node->title, '#required' => FALSE, '#weight' => -5);
153 }
154
155 if ($type->has_body) {
156 $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
157 $form['body_field']['body']['#rows'] = 3;
158 }
159
160 return $form;
161 }
162
163 /**
164 * Implementation of hook_view().
165 */
166 function resource_node_view($node, $teaser = FALSE, $page = FALSE) {
167 $node = node_prepare($node, $teaser);
168 if (!$teaser) {
169 $node->content['resource_data'] = array('#value' => theme('rdf_property_table', $node->resource_data), '#weight' => 10);
170 }
171 return $node;
172 }
173
174 /**
175 * Implementation of hook_load().
176 */
177 function resource_node_load($node) {
178 $options = array('repository' => RESOURCE_REPOSITORY, 'graph' => RESOURCE_GRAPH);
179 $subject = url('node/' . $node->nid, array('absolute' => TRUE));
180 $uri = rdf_value($subject, rdfs::seeAlso, NULL, $options);
181 return (object)array(
182 'resource_uri' => $uri,
183 'resource_data' => rdf_normalize(rdf_query($uri, NULL, NULL)),
184 );
185 }
186
187 /**
188 * Implementation of hook_validate().
189 */
190 function resource_node_validate(&$node) {
191 if (!rdf_is_valid_uri($node->resource_uri)) {
192 form_set_error('resource_uri', t('URI is invalid'));
193 }
194 }
195
196 /**
197 * Implementation of hook_insert().
198 */
199 function resource_node_insert($node) {
200 resource_node_update($node);
201 }
202
203 /**
204 * Implementation of hook_update().
205 */
206 function resource_node_update($node) {
207 $options = array('repository' => RESOURCE_REPOSITORY, 'graph' => RESOURCE_GRAPH);
208 $subject = url('node/' . $node->nid, array('absolute' => TRUE));
209 rdf_delete($subject, rdfs::seeAlso, NULL, $options);
210 rdf_insert($subject, rdfs::seeAlso, rdf_uriref($node->resource_uri), $options);
211
212 $options = array('repository' => RESOURCE_REPOSITORY, 'graph' => $node->resource_uri);
213 rdf_delete(NULL, NULL, NULL, $options);
214 rdf_insert_all(module_invoke_all('rdf_extract', $node->resource_uri), $options);
215
216 if (empty($node->title)) {
217 $properties = array_filter(array_map('trim', explode(',', RESOURCE_TITLE)), 'strlen');
218 foreach ($properties as $property) {
219 if ($title = rdf_value($node->resource_uri, rdf_qname_to_uri($property), NULL, array('language' => NULL/*FIXME*/))) {
220 $node->title = is_object($title) ? $title->value : (string)$title;
221 db_query("UPDATE {node} SET title = '%s' WHERE nid = %d", $node->title, $node->nid);
222 db_query("UPDATE {node_revisions} SET title = '%s' WHERE vid = %d", $node->title, $node->vid);
223 cache_clear_all();
224 break;
225 }
226 }
227 }
228
229 if (empty($node->description)) {
230 $properties = array_filter(array_map('trim', explode(',', RESOURCE_DESCRIPTION)), 'strlen');
231 foreach ($properties as $property) {
232 if ($body = rdf_value($node->resource_uri, rdf_qname_to_uri($property), NULL, array('language' => NULL/*FIXME*/))) {
233 $node->body = is_object($body) ? $body->value : (string)$body;
234 $node = node_submit($node);
235 $node->teaser = empty($node->teaser) ? $node->body : $node->teaser;
236 db_query("UPDATE {node_revisions} SET body = '%s', teaser = '%s' WHERE vid = %d", $node->body, $node->teaser, $node->vid);
237 cache_clear_all();
238 break;
239 }
240 }
241 }
242 }
243
244 /**
245 * Implementation of hook_delete().
246 */
247 function resource_node_delete($node) {
248 $options = array('repository' => RESOURCE_REPOSITORY, 'graph' => RESOURCE_GRAPH);
249 $subject = url('node/' . $node->nid, array('absolute' => TRUE));
250 rdf_delete($subject, rdfs::seeAlso, NULL, $options);
251
252 $options = array('repository' => RESOURCE_REPOSITORY, 'graph' => $node->resource_uri);
253 rdf_delete(NULL, NULL, NULL, $options);
254 }
255
256 //////////////////////////////////////////////////////////////////////////////
257 // RDF API hooks
258
259 /**
260 * Implementation of hook_rdf_extract().
261 */
262 function resource_rdf_extract($uri) {
263 // TODO
264 }

  ViewVC Help
Powered by ViewVC 1.1.2