/[drupal]/contributions/modules/skeleton/skeleton_template.inc
ViewVC logotype

Contents of /contributions/modules/skeleton/skeleton_template.inc

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


Revision 1.20 - (show annotations) (download) (as text)
Thu Aug 27 18:51:01 2009 UTC (3 months ago) by deviantintegral
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-ALPHA1, HEAD
Changes since 1.19: +2 -4 lines
File MIME type: text/x-php
#357500: Add Template breaks if no node types are available.
1 <?php
2
3 // $Id: skeleton_template.inc,v 1.19 2009/08/27 17:33:43 deviantintegral Exp $
4
5 /**
6 * @file
7 * Template creation functions for the skeleton module
8 */
9
10 include_once('skeleton_common.inc');
11
12 /**
13 * List all templates
14 *
15 * @param $template_id
16 * Unused, sent by the router function
17 * @return
18 * A table of current templates or an error message.
19 */
20 function skeleton_list_template($template_id) {
21 drupal_set_title(t('Skeleton templates'));
22 $result = db_query("SELECT * FROM {skeleton_template} ORDER BY template_id");
23 $header = array(t('Name'), t('Node type'), t('Associated skeletons'));
24 if (module_exists('translation')) {
25 $header[] = t('Language');
26 }
27 $header[] = t('Operations');
28 while ($template = db_fetch_object($result)) {
29 $skeleton_result = db_query("SELECT s.skeleton_id, s.skeleton FROM {skeleton} s
30 INNER JOIN {skeleton_data} sd ON s.skeleton_id = sd.skeleton_id
31 INNER JOIN {skeleton_template} st ON st.template_id = sd.template_id
32 WHERE st.template_id = %d
33 ORDER BY s.skeleton_id", $template->template_id);
34 $skeletons = array();
35 while ($skeleton = db_fetch_object($skeleton_result)) {
36 $skeletons[] = l(check_plain($skeleton->skeleton), 'admin/content/skeleton/skeleton/' . $skeleton->skeleton_id . '/edit');
37 }
38 $extra = l(t('view template'), 'admin/content/skeleton/template/' . $template->template_id . '/view');
39 $extra2 = l(t('edit template'), 'admin/content/skeleton/template/'. $template->template_id . '/edit');
40 $extra3 = l(t('delete template'), 'admin/content/skeleton/template/'. $template->template_id . '/delete');
41 $operations = theme('item_list', array($extra, $extra2, $extra3));
42 $row = array(
43 check_plain($template->template),
44 $template->node_type,
45 theme('item_list', $skeletons),
46 );
47 if (module_exists('translation')) {
48 $node = (object)unserialize($template->node_data);
49 $row[] = locale_language_name($node->language);
50 }
51 $row[] = $operations;
52 $rows[] = $row;
53 }
54 if (!empty($rows)) {
55 return theme('table', $header, $rows);
56 }
57 else {
58 return t('<p>No skeleton templates have been created.</p>');
59 }
60 }
61
62 /**
63 * View a store template.
64 * @param $template_id
65 * The ID of the template to view.
66 * @return
67 * The fully rendered node HTML.
68 */
69 function skeleton_view_template($template) {
70 if (empty($template->node_data)) {
71 drupal_set_title(check_plain($template->template));
72 return '<p>' . t('This template has no content set. Please <a href="@edit-template">add some content</a> to the template.', array('@edit-template' => url('admin/content/skeleton/template/' . $template->template_id . '/edit'))) . '</p>';
73 }
74 $node = (object)$template->node_data;
75 drupal_set_title($node->title);
76 return node_view($node, FALSE, TRUE);
77 }
78
79 /**
80 * Add new template
81 *
82 * @param $template_id
83 * The id of the template to act upon
84 * @return
85 * A themed HTML page, including the form
86 */
87 function skeleton_add_template($template_id) {
88 drupal_set_title(t('Skeleton templates: Add'));
89 return drupal_get_form('skeleton_add_template_form', $template_id);
90 }
91
92 /**
93 * FormsAPI for skeleton_add_template()
94 */
95 function skeleton_add_template_form($form_state) {
96 $form = array();
97 $form['skeleton_template'] = array(
98 '#type' => 'fieldset',
99 '#title' => t('Template information'),
100 '#collapsible' => TRUE
101 );
102 $form['skeleton_template']['template'] = array(
103 '#type' => 'textfield',
104 '#title' => t('Template name'),
105 '#description' => t('Enter a unique name to identify this template.'),
106 '#required' => TRUE
107 );
108 $types = node_get_types();
109 foreach ($types as $type) {
110 $options[$type->type] = $type->name;
111 }
112 $form['skeleton_template']['node_type'] = array(
113 '#type' => 'select',
114 '#title' => t('Node type'),
115 '#options' => $options,
116 '#description' => t('Select the node type for this template.'),
117 '#required' => TRUE
118 );
119 $form['submit'] = array(
120 '#type' => 'submit',
121 '#value' => t('Create template')
122 );
123 // TODO merge this form with the edit form?
124 return $form;
125 }
126
127 /**
128 * FormsAPI for skeleton_add_template()
129 */
130 function skeleton_add_template_form_validate($form, &$form_state) {
131 $check = db_result(db_query("SELECT COUNT(template_id) FROM {skeleton_template} WHERE template = '%s'", $form_state['values']['template']));
132 if ($check > 0) {
133 form_set_error('template', t('The name of the template must be unique.'));
134 }
135 }
136
137 /**
138 * FormsAPI for skeleton_add_template()
139 */
140 function skeleton_add_template_form_submit($form, &$form_state) {
141 db_query("INSERT INTO {skeleton_template} (template, node_type) VALUES ('%s', '%s')", $form_state['values']['template'], $form_state['values']['node_type']);
142 drupal_set_message(t('Template added.'));
143 $template = skeleton_get_template_by_name($form_state['values']['template']);
144 drupal_goto('admin/content/skeleton/template/'. $template->template_id . '/edit');
145 }
146
147 /**
148 * Delete template
149 *
150 * @param $template
151 * The template to act upon
152 * @return
153 * A themed HTML page, including the form
154 */
155 function skeleton_delete_template(&$form_state, $template) {
156 $form['template_id'] = array(
157 '#type' => 'value',
158 '#value' => $template->template_id,
159 );
160 return confirm_form($form,
161 t('Are you sure you want to delete the template %title?', array('%title' => $template->template)),
162 'admin/content/skeleton/template',
163 t('This action cannot be undone.'),
164 t('Delete template'),
165 t('Cancel')
166 );
167 }
168
169 /**
170 * Redirect when clicking delete button on template form.
171 */
172 function skeleton_template_delete_redirect($form_id, &$form_state) {
173 $form_state['redirect'] = 'admin/content/skeleton/template/' . $form_state['values']['template_id'] . '/delete';
174 }
175
176 /**
177 * FormsAPI for skeleton_delete_template()
178 */
179 function skeleton_delete_template_submit($form, &$form_state) {
180 db_query("DELETE from {skeleton_template} WHERE template_id = %d", $form_state['values']['template_id']);
181 db_query("UPDATE {skeleton_data} SET parent = 0 WHERE parent = %d", $form_state['values']['template_id']);
182 db_query("DELETE from {skeleton_data} WHERE template_id = %d", $form_state['values']['template_id']);
183 drupal_set_message(t('Template deleted.'));
184 drupal_goto('admin/content/skeleton/template');
185 }
186
187 /**
188 * Edit template
189 *
190 * @param $template
191 * The template to act upon
192 * @return
193 * A themed HTML page, including the form
194 */
195 function skeleton_edit_template($template) {
196 include_once drupal_get_path('module', 'node') . '/node.pages.inc';
197 drupal_set_title($template->node_data['title']);
198 $check = FALSE;
199 if (!empty($template->node_data)) {
200 $node = $template->node_data;
201 $node['type'] = $template->node_type;
202 $check = TRUE;
203 }
204 else {
205 $node = array('uid' => $user->uid, 'name' => $user->name, 'type' => $template->node_type);
206 }
207 $node_form = $template->node_type .'_node_form';
208 return drupal_get_form($node_form, $node);
209 }
210
211 /**
212 * Alter the node edit form to make it suitable for a skeleton template.
213 *
214 * @param $form
215 * The form array to alter.
216 * @param $form_state
217 * The state of the current form.
218 * @param $form_id
219 * The ID of the current form, usually node-type_node_form.
220 */
221 function skeleton_alter_node_form(&$form, $form_state, $form_id) {
222 $template = skeleton_template_load(arg(4));
223 if (!empty($template->node_data)) {
224 $node = $template->node_data;
225 }
226 // handles some form elements (radios mostly) that don't get set correctly
227 foreach ($form as $key => $value) {
228 if (isset($node[$key]['key']) && isset($form[$key]['key']['#default_value'])) {
229 $form[$key]['key']['#default_value'] = $node[$key]['key'];
230 }
231 }
232 // publishing options are handled oddly -- this may affect all checkbox types
233 foreach ($form['options'] as $key => $value) {
234 if (isset($node[$key])) {
235 $form['options'][$key]['#default_value'] = $node[$key];
236 }
237 }
238 // handle taxonomies -- this may apply to all select elements
239 if (!empty($form['taxonomy'])) {
240 foreach ($form['taxonomy'] as $key => $value) {
241 if (is_numeric($key)) {
242 $form['taxonomy'][$key]['#default_value'] = $node['taxonomy'][$key];
243 }
244 }
245 }
246 $form['template_id'] = array('#type' => 'value', '#value' => $template->template_id);
247 $form['type'] = array('#type' => 'value', '#value' => $template->node_type);
248 $form['skeleton_template'] = array(
249 '#type' => 'fieldset',
250 '#title' => t('Template information'),
251 '#collapsible' => TRUE,
252 '#weight' => -100
253 );
254 $form['skeleton_template']['template'] = array(
255 '#type' => 'textfield',
256 '#title' => t('Template name'),
257 '#default_value' => $template->template,
258 '#description' => t('The unique name to identify this template.'),
259 '#required' => TRUE
260 );
261 $options = node_get_types('names');
262 $form['skeleton_template']['node_type'] = array(
263 '#type' => 'select',
264 '#title' => t('Node type'),
265 '#value' => $template->node_type,
266 '#options' => $options,
267 '#description' => t('The node type for this template. <em>This value cannot be edited.</em>'),
268 '#disabled' => TRUE
269 );
270 $form['skeleton_template']['template_id'] = array('#type' => 'value', '#value' => $template->template_id);
271
272 // Add token help information below the skeleton fieldset.
273 $form['skeleton_tokens'] = array(
274 '#type' => 'fieldset',
275 '#title' => t('Skeleton replacement tokens'),
276 '#description' => t('These tokens will be replaced in the title and the body when instantiating a skeleton outline'),
277 '#collapsible' => TRUE,
278 '#collapsed' => TRUE,
279 '#weight' => -99,
280 );
281 $form['skeleton_tokens']['tokens'] = array(
282 '#value' => _skeleton_build_token_help('skeleton'),
283 );
284 $form['node_tokens'] = array(
285 '#type' => 'fieldset',
286 '#title' => t('Node replacement tokens'),
287 '#description' => t('These tokens will be replaced in the title and the body when instantiating a skeleton outline'),
288 '#collapsible' => TRUE,
289 '#collapsed' => TRUE,
290 '#weight' => -98,
291 );
292 $form['node_tokens']['tokens'] = array(
293 '#value' => _skeleton_build_token_help('node'),
294 );
295
296 // Unset the values we don't want or cannot save.
297 $unset = array('parent', 'weight', 'menu', 'book', 'path', 'attachments', 'revision_information', 'log', 'buttons', '#submit');
298
299 // Saving Filefield (or Imagefield) fields as is really breaks things, so
300 // find any such fields and remove them.
301 $content_type = content_types($form['#node']->type);
302 foreach ($content_type['fields'] as $data) {
303 if ($data['type'] == 'filefield') {
304 $unset[] = $data['field_name'];
305 }
306 }
307 foreach ($unset as $item) {
308 unset($form[$item]);
309 }
310
311 if (module_exists('nodeaccess') && user_access('grant node permissions')) {
312 $form['skeleton_template_grants'] = array(
313 '#type' => 'fieldset',
314 '#title' => t('Node access rules'),
315 '#collapsible' => TRUE,
316 '#collapsed' => TRUE,
317 '#weight' => 100,
318 '#theme' => 'skeleton_na_form_element'
319 );
320 // get roles and set default permissions
321 $roles = user_roles();
322 if (isset($node['rid'])) {
323 // set the gid into the array correctly
324 $perms = $node['rid'];
325 foreach ($roles as $key => $value) {
326 $perms[$key] = $node['rid'][$key];
327 $perms[$key]['gid'] = $key;
328 $perms[$key]['name'] = $roles[$key];
329 }
330 }
331 else {
332 $perms = variable_get('nodeaccess_'. $template->node_type, array());
333 }
334 foreach ($perms as $perm) {
335 $name = $roles[$perm['gid']];
336 $roles[$perm['gid']] = $perm;
337 $roles[$perm['gid']]['name'] = $name;
338 }
339 $naform = array();
340 if (is_array($roles)) {
341 $naform['rid'] = array('#tree' => TRUE);
342 $allowed = variable_get('nodeaccess-roles', array());
343 foreach ($roles as $key => $field) {
344 if ($allowed[$key]) {
345 $naform['rid'][$key]['name'] = array('#type' => 'hidden', '#value' => $field['name']);
346 $naform['rid'][$key]['grant_view'] = array('#type' => 'checkbox', '#default_value' => $field['grant_view']);
347 $naform['rid'][$key]['grant_update'] = array('#type' => 'checkbox', '#default_value' => $field['grant_update']);
348 $naform['rid'][$key]['grant_delete'] = array('#type' => 'checkbox', '#default_value' => $field['grant_delete']);
349 }
350 }
351 }
352 if (count($naform['rid']) > 1) {
353 $form['skeleton_template_grants']['na'] = $naform;
354 }
355 else {
356 unset($form['skeleton_template_grants']);
357 }
358 }
359 $form['buttons']['delete'] = array(
360 '#type' => 'submit',
361 '#value' => t('Delete template'),
362 '#weight' => 200,
363 '#submit' => array('skeleton_template_delete_redirect'),
364 );
365 $form['#submit'][] = 'skeleton_edit_template_form_submit';
366 $form['#validate'][] = 'skeleton_edit_template_form_validate';
367 $form['buttons']['submit'] = array(
368 '#type' => 'submit',
369 '#value' => t('Update template'),
370 '#weight' => 100
371 );
372 }
373
374 /**
375 * Theme the nodeaccess part of the form
376 */
377 function theme_skeleton_na_form_element($form) {
378 // role table
379 $roles = element_children($form['na']['rid']);
380 if (count($roles) > 0) {
381 $header = array(t('Role'), t('View'), t('Edit'), t('Delete'));
382 foreach ($roles as $key) {
383 $row = array();
384 $row[] = $form['na']['rid'][$key]['name']['#value'] . drupal_render($form['rid'][$key]['name']);
385 $row[] = drupal_render($form['na']['rid'][$key]['grant_view']);
386 $row[] = drupal_render($form['na']['rid'][$key]['grant_update']);
387 $row[] = drupal_render($form['na']['rid'][$key]['grant_delete']);
388 $rows[] = $row;
389 }
390 $output .= theme('table', $header, $rows);
391 }
392 return $output;
393 }
394
395 /**
396 * FormsAPI
397 */
398 function skeleton_edit_template_form_validate($form, &$form_state) {
399 // stub function
400 }
401
402 /**
403 * FormsAPI for handling the template form
404 */
405 function skeleton_edit_template_form_submit($form, &$form_state) {
406 // these items are not stored node data
407 $node = $form_state['values'];
408 $unset = array('template', 'node_type', 'nid', 'vid', 'teaser_js', 'created', 'changed', 'date', 'op', 'submit', 'form_token', 'form_id', '#after_build');
409 foreach ($unset as $item) {
410 unset($node[$item]);
411 }
412 db_query("UPDATE {skeleton_template} SET node_data = '%s' WHERE template_id = %d", serialize($node), $form_state['values']['template_id']);
413 drupal_set_message(t('Template successfully updated.'));
414
415 // Now, mark any instantiated copies of this template as eligible for
416 // updating.
417 db_query("UPDATE {skeleton_template_node} SET template_status = 'updated' WHERE template_id = %d AND template_status != 'overridden'", $form_state['values']['template_id']);
418 }
419
420 /**
421 * Add a template to a skeleton
422 *
423 * @param $action
424 * Route the function to the correct action (add / remove)
425 * @param $skeleton_id
426 * The id of the template to act upon
427 * @param $template_id
428 * The id of the template to act upon
429 * @return
430 * Callback function, will return you to the current page after acting.
431 */
432 function skeleton_assign_template($action, $skeleton_id, $template_id) {
433 if (($action != 'add' && $action != 'remove') || intval($skeleton_id) == 0 || intval($template_id) == 0) {
434 return t('Illegal request');
435 }
436 include_once('skeleton_instance.inc');
437 $skeleton = skeleton_load($skeleton_id);
438 $template = skeleton_template_load($template_id);
439 return skeleton_assign($action, $skeleton, $template);
440 }

  ViewVC Help
Powered by ViewVC 1.1.2