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

Contents of /contributions/modules/skeleton/skeleton.module

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


Revision 1.20 - (show annotations) (download) (as text)
Thu Aug 27 19:17:08 2009 UTC (3 months ago) by deviantintegral
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-ALPHA1, HEAD
Changes since 1.19: +2 -2 lines
File MIME type: text/x-php
#357493: Skeleton table doesn't check user permissions for template links.
1 <?php
2
3 // $Id: skeleton.module,v 1.19 2009/07/16 18:12:35 deviantintegral Exp $
4
5 /**
6 * @file
7 * Skeleton module creates predefined nodes in a book outline format.
8 * This file is mostly a router stub for hook_menu(). See the include files
9 * for actual functions.
10 */
11
12 /**
13 * Implements hook_help()
14 */
15 function skeleton_help($path, $arg) {
16 switch ($path) {
17 case 'admin/content/skeleton':
18 $output = '<p>' . t('Skeletons are book outlines that contain stored content and settings. To begin, create a new template. Then, create a skeleton outline for your templates. Create instances of skeleton outlines to publish a copy of their contents.' ) . '</p>';
19 if (module_exists('translation')) {
20 $output .= '<p>' . t('It is recommended that all templates within a skeleton have the same language, or be set to Language Neutral.') . '</p>';
21 }
22 return $output;
23 break;
24 }
25 }
26
27 /**
28 * Implements hook_perm()
29 */
30 function skeleton_perm() {
31 return array('configure skeleton outlines', 'create new instances');
32 }
33
34 /**
35 * Implements hook_menu()
36 */
37 function skeleton_menu() {
38
39 $items = array();
40
41 // Default configuration elements.
42 $items['admin/content/skeleton'] = array(
43 'page callback' => 'skeleton_admin',
44 'description' => 'Create reusable book outlines.',
45 'access callback' => '_skeleton_user_can_view',
46 'title' => 'Skeleton outlines',
47 'file' => 'skeleton_admin.inc',
48 );
49 $items['admin/content/skeleton/skeleton'] = array(
50 'page callback' => 'skeleton_admin',
51 'access arguments' => array('configure skeleton outlines'),
52 'type' => MENU_DEFAULT_LOCAL_TASK,
53 'weight' => -10,
54 'title' => 'Skeletons',
55 'file' => 'skeleton_admin.inc',
56 );
57 $items['admin/content/skeleton/skeleton/view'] = array(
58 'page callback' => 'skeleton_add',
59 'access arguments' => array('configure skeleton outlines'),
60 'type' => MENU_DEFAULT_LOCAL_TASK,
61 'weight' => -10,
62 'title' => 'List',
63 'file' => 'skeleton_admin.inc',
64 );
65 $items['admin/content/skeleton/skeleton/add'] = array(
66 'page callback' => 'skeleton_add',
67 'access arguments' => array('configure skeleton outlines'),
68 'type' => MENU_LOCAL_TASK,
69 'weight' => -8,
70 'title' => 'Add skeleton',
71 'file' => 'skeleton_admin.inc',
72 );
73 $items['admin/content/skeleton/sync'] = array(
74 'page callback' => 'skeleton_sync_page',
75 'access arguments' => array('configure skeleton outlines'),
76 'type' => MENU_LOCAL_TASK,
77 'title' => 'Synchronize',
78 'file' => 'skeleton_sync.inc',
79 );
80 $items['admin/content/skeleton/token'] = array(
81 'page callback' => 'drupal_get_form',
82 'page arguments' => array('skeleton_token_form'),
83 'access arguments' => array('configure skeleton outlines'),
84 'type' => MENU_LOCAL_TASK,
85 'weight' => 10,
86 'title' => 'Tokens',
87 'file' => 'skeleton_token.inc',
88 );
89 $items['admin/content/skeleton/token/view'] = array(
90 'page callback' => 'drupal_get_form',
91 'page arguments' => array('skeleton_token_form'),
92 'access arguments' => array('configure skeleton outlines'),
93 'type' => MENU_DEFAULT_LOCAL_TASK,
94 'weight' => -10,
95 'title' => 'List',
96 'file' => 'skeleton_token.inc',
97 );
98 $items['admin/content/skeleton/token/add'] = array(
99 'page callback' => 'drupal_get_form',
100 'page arguments' => array('skeleton_add_token_form'),
101 'access arguments' => array('configure skeleton outlines'),
102 'type' => MENU_LOCAL_TASK,
103 'title' => 'Add token',
104 'file' => 'skeleton_token.inc',
105 );
106 $items['admin/content/skeleton/token/%skeleton_token/edit'] = array(
107 'page callback' => 'drupal_get_form',
108 'page arguments' => array('skeleton_add_token_form', 4),
109 'access arguments' => array('configure skeleton outlines'),
110 'type' => MENU_CALLBACK,
111 'title' => 'Edit token',
112 'file' => 'skeleton_token.inc',
113 );
114 $items['admin/content/skeleton/token/%skeleton_token/delete'] = array(
115 'page callback' => 'drupal_get_form',
116 'page arguments' => array('skeleton_delete_token_form', 4),
117 'access arguments' => array('configure skeleton outlines'),
118 'type' => MENU_CALLBACK,
119 'title' => 'Delete token',
120 'file' => 'skeleton_token.inc',
121 );
122 // Template items.
123 $items['admin/content/skeleton/template'] = array(
124 'page callback' => 'skeleton_list_template',
125 'page arguments' => array('0'),
126 'access arguments' => array('configure skeleton outlines'),
127 'type' => MENU_LOCAL_TASK,
128 'weight' => 8,
129 'title' => 'Templates',
130 'file' => 'skeleton_template.inc',
131 );
132 $items['admin/content/skeleton/template/view'] = array(
133 'page callback' => 'skeleton_view_template',
134 'page arguments' => array('0'),
135 'access arguments' => array('configure skeleton outlines'),
136 'type' => MENU_DEFAULT_LOCAL_TASK,
137 'weight' => -10,
138 'title' => 'List',
139 'file' => 'skeleton_template.inc',
140 );
141 $items['admin/content/skeleton/template/add'] = array(
142 'page callback' => 'skeleton_add_template',
143 'page arguments' => array('0'),
144 'access arguments' => array('configure skeleton outlines'),
145 'type' => MENU_LOCAL_TASK,
146 'weight' => -8,
147 'title' => 'Add template',
148 'file' => 'skeleton_template.inc',
149 );
150 $items['admin/content/skeleton/assign/%/%/%'] = array(
151 'page callback' => 'skeleton_assign_template',
152 'page arguments' => array(4, 5, 6),
153 'access arguments' => array('configure skeleton outlines'),
154 'type' => MENU_CALLBACK,
155 'file' => 'skeleton_template.inc',
156 );
157 $items['admin/content/skeleton/template/%skeleton_template/view'] = array(
158 'page callback' => 'skeleton_view_template',
159 'page arguments' => array(4),
160 'access callback' => '_skeleton_user_can_view',
161 'type' => MENU_LOCAL_TASK,
162 'weight' => -5,
163 'title' => 'View template',
164 'file' => 'skeleton_template.inc',
165 );
166 $items['admin/content/skeleton/template/%skeleton_template/edit'] = array(
167 'page callback' => 'skeleton_edit_template',
168 'page arguments' => array(4),
169 'access arguments' => array('configure skeleton outlines'),
170 'type' => MENU_LOCAL_TASK,
171 'weight' => 10,
172 'title' => 'Edit template',
173 'file' => 'skeleton_template.inc',
174 );
175 $items['admin/content/skeleton/template/%skeleton_template/delete'] = array(
176 'page callback' => 'drupal_get_form',
177 'page arguments' => array('skeleton_delete_template', 4),
178 'access arguments' => array('configure skeleton outlines'),
179 'type' => MENU_CALLBACK,
180 'weight' => 10,
181 'file' => 'skeleton_template.inc',
182 );
183 // Skeleton items.
184 $items['admin/content/skeleton/skeleton/%skeleton/create'] = array(
185 'page callback' => 'skeleton_create_instance',
186 'page arguments' => array(4),
187 'access callback' => '_skeleton_user_can_create_instance',
188 'type' => MENU_LOCAL_TASK,
189 'weight' => 6,
190 'title' => 'Create skeleton instance',
191 'file' => 'skeleton_instance.inc',
192 );
193 $items['admin/content/skeleton/skeleton/%skeleton/edit'] = array(
194 'page callback' => 'skeleton_edit_instance',
195 'page arguments' => array(4),
196 'access arguments' => array('configure skeleton outlines'),
197 'type' => MENU_LOCAL_TASK,
198 'weight' => 8,
199 'title' => 'Edit skeleton',
200 'file' => 'skeleton_instance.inc',
201 );
202 $items['admin/content/skeleton/skeleton/%skeleton/delete'] = array(
203 'page callback' => 'skeleton_delete_instance',
204 'page arguments' => array(4),
205 'access arguments' => array('configure skeleton outlines'),
206 'type' => MENU_CALLBACK,
207 'weight' => 10,
208 'file' => 'skeleton_instance.inc',
209 );
210 $items['skeleton/introduction'] = array(
211 'page callback' => 'skeleton_introduction_js',
212 'access arguments' => array('create new instances'),
213 'type' => MENU_CALLBACK,
214 'file' => 'skeleton_instance.inc',
215 );
216
217 $result = db_query("SELECT skeleton_id, skeleton FROM {skeleton} ORDER BY skeleton_id");
218 while ($skeleton = db_fetch_object($result)) {
219 $items['node/add/skeleton/' . $skeleton->skeleton_id] = array(
220 'title' => '!skeleton skeleton',
221 'title arguments' => array('!skeleton' => $skeleton->skeleton),
222 'access callback' => '_skeleton_user_can_create_instance',
223 'description' => 'Create a new skeleton instance.',
224 'page callback' => 'drupal_goto',
225 'page arguments' => array('admin/content/skeleton/skeleton/' . $skeleton->skeleton_id . '/create'),
226 );
227 }
228
229 // Add an "Edit source template" tab for instantiated nodes.
230 $items['node/%node/edit-skeleton-template'] = array(
231 'title' => 'Edit source template',
232 'page callback' => '_skeleton_goto_edit_template',
233 'page arguments' => array(1),
234 'access callback' => '_skeleton_template_connected',
235 'access arguments' => array(1),
236 'type' => MENU_LOCAL_TASK,
237 );
238 return $items;
239 }
240
241 /**
242 * Return if the user should be given permission to view Skeleton Outlines.
243 *
244 * @return boolean
245 * TRUE if the user has permission to view outlines, FALSE otherwise.
246 */
247 function _skeleton_user_can_view() {
248 return user_access('configure skeleton outlines') || user_access('create new instances');
249 }
250
251 /**
252 * Return if the user is able to create a new skeleton instance.
253 *
254 * @return boolean
255 * TRUE if the user has permission to create instances, books, and book
256 * pages, FALSE otherwise.
257 */
258 function _skeleton_user_can_create_instance() {
259 return user_access('create new instances') && user_access('create new books') && user_access('add content to books');
260 }
261
262 /**
263 * Implementation of hook_theme()
264 */
265 function skeleton_theme() {
266 return array(
267 'skeleton_define_form' => array(
268 'arguments' => array('form' => NULL),
269 ),
270 'skeleton_token_form' => array(
271 'arguments' => array('form' => NULL),
272 ),
273 'skeleton_token_help' => array(
274 'arguments' => array('tokens' => NULL),
275 ),
276 'skeleton_other_token_help' => array(
277 'arguments' => array('tokens' => NULL),
278 ),
279 'skeleton_na_form_element' => array(
280 'arguments' => array('form' => NULL),
281 ),
282 );
283 }
284
285 /**
286 * Get the data for a skeleton
287 *
288 * @param $skeleton_id
289 * The id of the outline to act upon
290 * @return $result
291 * An object containing elements from the {skeleton} table
292 */
293 function skeleton_load($skeleton_id) {
294 $result = db_fetch_object(db_query("SELECT * FROM {skeleton} WHERE skeleton_id = %d", $skeleton_id));
295 return empty($result) ? FALSE : $result;
296 }
297
298 /**
299 * Get the data for a skeleton template
300 *
301 * @param $template_id
302 * The id of the template to act upon
303 * @return $result
304 * An object containing elements from the {skeleton_template} table
305 */
306 function skeleton_template_load($template_id) {
307 $result = db_fetch_object(db_query("SELECT * FROM {skeleton_template} WHERE template_id = %d", $template_id));
308 if ($result->node_data) {
309 // node data is a complex serialized array
310 $result->node_data = unserialize($result->node_data);
311 }
312 return $result;
313 }
314
315 /**
316 * Load a skeleton token from the database.
317 *
318 * @param $token_id
319 * The id of the token to load.
320 * @return
321 * The fully loaded skeleton token object. This object contains three
322 * properties: token_id, token, and description.
323 */
324 function skeleton_token_load($token_id) {
325 $result = db_fetch_object(db_query("SELECT * FROM {skeleton_token} WHERE token_id = %d", $token_id));
326 return empty($result) ? FALSE : $result;
327 }
328
329 /**
330 * Implementation of hook_nodeapi()
331 */
332 function skeleton_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
333 switch ($op) {
334 case 'load':
335 if ($template = db_fetch_object(db_query("SELECT skeleton_id, template_id, tokens FROM {skeleton_template_node} WHERE nid = %d AND template_status != 'overridden'", $node->nid))) {
336 $node->skeleton_template = new stdClass();
337 $node->skeleton_template->skeleton_id = $template->skeleton_id;
338 $node->skeleton_template->template_id = $template->template_id;
339 $node->skeleton_template->tokens = unserialize($template->tokens);
340 }
341 break;
342 case 'prepare':
343 if ($node->skeleton_template->template_id) {
344 drupal_set_message(t('This node has not been changed since it was created from a template. Saving this node will override the default content.'));
345 }
346 break;
347 case 'insert':
348 if (isset($node->skeleton_id) && isset($node->template_id)) {
349 db_query("INSERT INTO {skeleton_template_node} (nid, skeleton_id, template_id, template_status, tokens) VALUES (%d, %d, %d, 'synced', '%s')", $node->nid, $node->skeleton_id, $node->template_id, serialize($node->tokens));
350 }
351 break;
352 case 'update':
353 if (!$node->skeleton_template->keep_connected) {
354 db_query("UPDATE {skeleton_template_node} SET template_status = '%s' WHERE nid = %d", 'overridden', $node->nid);
355 }
356 break;
357 case 'delete':
358 db_query("DELETE FROM {skeleton_template_node} WHERE nid = %d", $node->nid);
359 break;
360 }
361 }
362
363 /**
364 * Implements hook_form_alter()
365 *
366 * This function lets us use the default node type form to create a template.
367 * There may be some issues with its handling of array data, like checkboxes
368 * radios, and other odd CCK field types.
369 *
370 * Note that it does not support file uploads.
371 */
372 function skeleton_form_alter(&$form, $form_state, $form_id) {
373 module_load_include('inc', 'skeleton', 'skeleton_token');
374 // only alter forms handled through the skeleton interface
375 $form_node = isset($form['type']['#value']) ? $form['type']['#value'] . '_node_form' : NULL;
376 if ($form_node == $form_id && arg(0) == 'admin' && arg(2) == 'skeleton' && arg(3) == 'template' && arg(5) == 'edit') {
377 module_load_include('inc', 'skeleton', 'skeleton_template');
378 skeleton_alter_node_form($form, $form_state, $form_id);
379 }
380 else if ($form_node == $form_id && (arg(0) == 'admin' && arg(2) == 'skeleton' && arg(3) == 'skeleton' && arg(5) == 'create')
381 || (arg(0) == 'admin' && arg(2) == 'skeleton' && arg(3) == 'sync')) {
382 // We need skeleton_id to be defined so forms validate properly. This value
383 // should always be provided by the function instantiating the skeleton.
384 $form['skeleton_id'] = array('#type' => 'hidden', '#value' => $form_state['values']['skeleton_id']);
385 $form['template_id'] = array('#type' => 'hidden', '#value' => $form_state['values']['template_id']);
386 $form['tokens'] = array('#type' => 'hidden', '#value' => $form_state['values']['tokens']);
387 }
388 }
389
390 /**
391 * Determine if a node has an associated template and if the current user has
392 * permission to determine such information.
393 *
394 * @param $node
395 * The node to search for.
396 * @return
397 * TRUE if the node has an associated template and the user can access it,
398 * FALSE otherwise.
399 */
400 function _skeleton_template_connected($node) {
401 return user_access('configure skeleton outlines') && db_result(db_query("SELECT COUNT(1) FROM {skeleton_template_node} WHERE nid = %d AND template_status != 'overridden'", $node->nid));
402 }
403
404 /**
405 * Given a node, go to its associated template of it exists.
406 * @param $node
407 * The node which was created from a template.
408 */
409 function _skeleton_goto_edit_template($node) {
410 if ($template_id = db_result(db_query("SELECT template_id FROM {skeleton_template_node} WHERE nid = %d", $node->nid))) {
411 drupal_goto('admin/content/skeleton/template/' . $template_id . '/edit');
412 }
413 }

  ViewVC Help
Powered by ViewVC 1.1.2