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

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

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


Revision 1.23 - (show annotations) (download) (as text)
Thu Aug 27 17:36:57 2009 UTC (3 months ago) by deviantintegral
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-ALPHA1, HEAD
Changes since 1.22: +24 -19 lines
File MIME type: text/x-php
#544560: CCK text field support for tokens.
1 <?php
2
3 // $Id: skeleton_instance.inc,v 1.22 2009/08/27 17:35:41 deviantintegral Exp $
4
5 /**
6 * @file
7 * Functions for defining an instance of a book generated by the skeleton module
8 */
9
10 include_once('skeleton_common.inc');
11
12 /**
13 * Show the templates assigned to a skeleton and those still available.
14 *
15 * @param $skeleton
16 * An object from the {skeleton} table, indicating the outline to act upon.
17 * @return $output
18 * Themed HTML, including the outline form
19 */
20 function skeleton_define($skeleton) {
21 $result = db_query("SELECT COUNT(1) FROM {skeleton_template}");
22 if (db_result($result) == 0) {
23 $output .= t('<p>No skeleton templates are available to add to this outline. <a href="@template-add-url">Add a skeleton template</a>.</p>', array('@template-add-url' => url('admin/content/skeleton/template/add')));
24 return $output;
25 }
26 $templates = skeleton_get_tree($skeleton->skeleton_id);
27 $output .= drupal_get_form('skeleton_define_form', $skeleton, $templates);
28 return $output;
29 }
30
31 /**
32 * Callback to add a template to a skeleton
33 *
34 * @param $action
35 * Determines what to do with the template (add / remove).
36 * @param $skeleton
37 * An object from the {skeleton} table, indicating the outline to act upon
38 * @param $template
39 * An object from the {skeleton_template} table, indicating the template to act upon
40 */
41 function skeleton_assign($action, $skeleton, $template) {
42 $check = db_result(db_query("SELECT COUNT(*) FROM {skeleton_data} WHERE skeleton_id = %d AND template_id = %d", $skeleton->skeleton_id, $template->template_id));
43 if (!$check && $action == 'add') {
44 $weight = 0;
45 $parent = 0;
46 // TODO -- calculate the parent and weight correctly?
47 db_query("INSERT INTO {skeleton_data} (skeleton_id, template_id, parent, weight) VALUES (%d, %d, %d, %d)", $skeleton->skeleton_id, $template->template_id, $parent, $weight);
48 drupal_set_message(t('Template assigned'));
49 drupal_goto('admin/content/skeleton/skeleton/'. $skeleton->skeleton_id . '/edit');
50 }
51 elseif ($check && $action == 'remove') {
52 // update the children with the new parent, keep the original weight
53 if ($template->parent) {
54 $parent = $template->parent;
55 }
56 else {
57 $parent = 0;
58 }
59 db_query("UPDATE {skeleton_data} SET parent = %d WHERE skeleton_id = %d AND parent = %d", $parent, $skeleton->skeleton_id, $template->template_id);
60
61 // remove
62 db_query("DELETE FROM {skeleton_data} WHERE skeleton_id = %d AND template_id = %d", $skeleton->skeleton_id, $template->template_id);
63 drupal_set_message(t('Template removed'));
64 drupal_goto('admin/content/skeleton/skeleton/'. $skeleton->skeleton_id . '/edit');
65 }
66 else {
67 return 'failed';
68 }
69 }
70
71 /**
72 * FormsAPI for skeleton_define()
73 * @param $skeleton
74 * An object from the {skeleton} table, indicating the outline to act upon
75 * @param $templates
76 * The template tree for this outline, generated by skeleton_get_tree()
77 * @return $form
78 * A functional $form array.
79 */
80 function skeleton_define_form($form_state, $skeleton, $templates) {
81 $form = array();
82 $form['#tree'] = TRUE;
83 $form['skeleton_id'] = array(
84 '#type' => 'value',
85 '#value' => $skeleton->skeleton_id,
86 );
87 // show the remaining options
88 $remove = array();
89 foreach ($templates as $i => $template) {
90 $remove[] = $template->template_id;
91 }
92 if (!empty($remove)) {
93 $result = db_query("SELECT template_id, template, node_type, node_data FROM {skeleton_template} WHERE template_id NOT IN (". implode(', ', $remove) .") ORDER BY template_id");
94 }
95 else {
96 $result = db_query("SELECT template_id, template, node_type, node_data FROM {skeleton_template} ORDER BY template_id");
97 }
98 while ($template = db_fetch_object($result)) {
99 $form['available_templates'][$template->template_id] = array(
100 'template_id' => array(
101 '#type' => 'value',
102 '#value' => $template->template_id,
103 ),
104 'template' => array(
105 '#value' => check_plain($template->template),
106 ),
107 'node_type' => array(
108 '#value' => $template-> node_type,
109 ),
110 'view' => array(
111 '#value' => l(t('view template'), 'admin/content/skeleton/template/'. $template->template_id . '/view'),
112 ),
113 'modify' => array(
114 '#value' => l(t('edit template'), 'admin/content/skeleton/template/'. $template->template_id . '/edit', array('query' => drupal_get_destination())),
115 ),
116 'add' => array(
117 '#value' => l(t('add template to skeleton'), 'admin/content/skeleton/assign/add/'. $skeleton->skeleton_id .'/'. $template->template_id),
118 ),
119 );
120 if (module_exists('translation')) {
121 $form['multilang'] = array(
122 '#type' => 'value',
123 '#value' => TRUE,
124 );
125 $node = (object)unserialize($template->node_data);
126 $form['available_templates'][$template->template_id]['language'] = array(
127 '#value' => locale_language_name($node->language),
128 );
129 }
130 }
131 if (empty($form['available_templates'])) {
132 $form['available_templates'] = array(
133 '#prefix' => '<p>',
134 '#suffix' => '</p>',
135 '#value' => t('All available templates have been assigned to this skeleton outline.'),
136 );
137 }
138 $form['available_templates']['create'] = array(
139 '#value' => '<p>'. l(t('Create new template'), 'admin/content/skeleton/template/add') .'</p>',
140 );
141
142 if (count($templates) > 0) {
143 $form['current_templates']['help_text'] = array(
144 '#prefix' => '<p>',
145 '#suffix' => '</p>',
146 '#value' => t('Below are the current templates assigned to this skeleton outline.'),
147 );
148 $form['current_templates']['notes'] = array(
149 '#prefix' => '<div class="form-item"><p class="description">',
150 '#suffix' => '</p></div>',
151 '#value' => t('If you change a template parent, all the children of that parent will be reassigned as well.'),
152 );
153 }
154 else {
155 $form['current_templates'] = array(
156 '#prefix' => '<p>',
157 '#suffix' => '</p>',
158 '#value' => t('No templates are currently assigned to this skeleton outline.'),
159 );
160 }
161 foreach ($templates as $i => $template) {
162 $options = array(0 => '<none>');
163 foreach ($templates as $temp) {
164 if ($temp->template_id != $template->template_id && (empty($template->children) || !in_array($temp->template_id, $template->children))) {
165 $options[$temp->template_id] = $temp->template;
166 }
167 }
168 $form['current_templates'][$i]['template_id'] = array(
169 '#type' => 'value',
170 '#value' => $template->template_id,
171 );
172 $form['current_templates'][$i]['template'] = array(
173 '#value' => str_repeat('-', $template->depth) .' '. check_plain($template->template),
174 );
175 $form['current_templates'][$i]['node_type'] = array(
176 '#value' => check_plain($template->node_type),
177 );
178 if (module_exists('translation')) {
179 $form['multilang'] = array(
180 '#type' => 'value',
181 '#value' => TRUE,
182 );
183 $node = (object)unserialize($template->node_data);
184 $form['current_templates'][$i]['language'] = array(
185 '#value' => locale_language_name($node->language),
186 );
187 }
188 $form['current_templates'][$i]['parent'] = array(
189 '#type' => 'select',
190 '#default_value' => $template->parent,
191 '#options' => $options,
192 );
193 $form['current_templates'][$i]['weight'] = array(
194 '#type' => 'weight',
195 '#default_value' => $template->weight,
196 '#delta' => 15
197 );
198 $form['current_templates'][$i]['view'] = array(
199 '#value' => l(t('view template'), 'admin/content/skeleton/template/'. $template->template_id . '/view'),
200 );
201 $form['current_templates'][$i]['modify'] = array(
202 '#value' => l(t('edit template'), 'admin/content/skeleton/template/'. $template->template_id . '/edit', array('query' => drupal_get_destination())),
203 );
204 $form['current_templates'][$i]['delete'] = array(
205 '#value' => l(t('remove template from skeleton'), 'admin/content/skeleton/assign/remove/'. $skeleton->skeleton_id .'/'. $template->template_id)
206 );
207 }
208 $form['submit'] = array(
209 '#type' => 'submit',
210 '#value' => t('Save'),
211 '#weight' => 100,
212 );
213 $form['create'] = array(
214 '#type' => 'submit',
215 '#value' => t('Create instance'),
216 '#submit' => array('skeleton_create_redirect'),
217 '#access' => _skeleton_user_can_create_instance(),
218 '#weight' => 150,
219 );
220 $form['delete'] = array(
221 '#type' => 'submit',
222 '#value' => t('Delete'),
223 '#submit' => array('skeleton_delete_redirect'),
224 '#weight' => 200,
225 );
226 return $form;
227 }
228
229 /**
230 * FormsAPI for skeleton_define()
231 */
232 function skeleton_define_form_submit($form, &$form_state) {
233 foreach ($form_state['values']['current_templates'] as $key => $template) {
234 // only the numeric keys are templates, so process those
235 if (is_numeric($key)) {
236 db_query("UPDATE {skeleton_data} SET parent = %d, weight = %d WHERE skeleton_id = %d AND template_id = %d", $template['parent'], $template['weight'], $form_state['values']['skeleton_id'], $template['template_id']);
237 }
238 }
239 drupal_set_message(t('Skeleton changes saved.'));
240 }
241
242 /**
243 * FormsAPI for skeleton_define()
244 * Formats the form in a table.
245 */
246 function theme_skeleton_define_form($form) {
247 // Build current templates table.
248 $rows = array();
249 foreach (element_children($form['current_templates']) as $i) {
250 $template = &$form['current_templates'][$i];
251 if (isset($template['template_id'])) {
252 $row = array(
253 drupal_render($template['template']),
254 drupal_render($template['node_type']),
255 );
256 if (!empty($form['multilang'])) {
257 $row[] = drupal_render($template['language']);
258 }
259 $row[] = drupal_render($template['parent']);
260 $row[] = drupal_render($template['weight']);
261 $row[] = theme('item_list', array(drupal_render($template['view']), drupal_render($template['modify']), drupal_render($template['delete'])));
262 $rows[] = $row;
263 }
264 }
265 $output = '<h4>' . t('Current templates') . '</h4>';
266 if (!empty($rows)) {
267 // Finish table.
268 $header = array(t('Template'), t('Node type'));
269 if ($form['multilang']) {
270 $header[] = t('Language');
271 }
272 $header[] = t('Parent');
273 $header[] = t('Weight');
274 $header[] = array('data' => t('Operations'), 'colspan' => 2);
275 $output .= drupal_render($form['current_templates']['help_text']);
276 $output .= theme('table', $header, $rows, array('id' => 'skeleton'));
277 }
278
279 // Add anything under current_templates.
280 $output .= drupal_render($form['current_templates']);
281
282 // Build available templates table.
283 $rows = array();
284 foreach (element_children($form['available_templates']) as $i) {
285 $template = &$form['available_templates'][$i];
286 if (isset($template['template_id'])) {
287 $row = array(
288 drupal_render($template['template']),
289 drupal_render($template['node_type'])
290 );
291 if (!empty($form['multilang'])) {
292 $row[] = drupal_render($template['language']);
293 }
294 $row[] = theme('item_list', array(drupal_render($template['view']), drupal_render($template['modify']), drupal_render($template['add'])));
295 $rows[] = $row;
296 }
297 }
298 $output .= '<h4>' . t('Available templates') . '</h4>';
299 if (!empty($rows)) {
300 $header = array(t('Name'), t('Node type'));
301 if ($form['multilang']) {
302 $header[] = t('Language');
303 }
304 $header[] = t('Operations');
305 $output .= drupal_render($form['available_templates']['help_text']);
306 $output .= theme('table', $header, $rows);
307 }
308
309 // Append any additional form elements to the bottom of the form.
310 $output .= drupal_render($form);
311 return $output;
312 }
313
314 /**
315 * Redirect when clicking delete button on the skeleton form.
316 */
317 function skeleton_delete_redirect($form_id, &$form_state) {
318 $form_state['redirect'] = 'admin/content/skeleton/skeleton/' . $form_state['values']['skeleton_id'] . '/delete';
319 }
320
321 /**
322 * Redirect when clicking "Create instance" on the skeleton form.
323 */
324 function skeleton_create_redirect($form_id, &$form_state) {
325 $form_state['redirect'] = 'admin/content/skeleton/skeleton/'. $form_state['values']['skeleton_id'] . '/create';
326 }
327
328 /**
329 * Edit a skeleton definition
330 *
331 * @param $skeleton
332 * An object from the {skeleton} table, indicating the outline to act upon.
333 * @return
334 * Themed HTML, including the outline form
335 */
336 function skeleton_edit_instance($skeleton) {
337 $output = '';
338 drupal_set_title(check_plain($skeleton->skeleton));
339 $output .= '<h3>'. check_plain($skeleton->skeleton) .'</h3>';
340 include_once('skeleton_instance.inc');
341 return skeleton_define($skeleton);
342 }
343
344 /**
345 * Delete a skeleton
346 *
347 * @param $skeleton
348 * An object from the {skeleton} table, indicating the outline to act upon.
349 * @return
350 * Themed HTML, including the deletion form
351 */
352 function skeleton_delete_instance($skeleton) {
353 $output = '';
354 drupal_set_title(t('Skeleton: Delete'));
355 $output .= '<h3>'. check_plain($skeleton->skeleton) .'</h3>';
356 $output .= t('<p>Are you sure you want to delete this skeleton outline?</p>');
357 $output .= drupal_get_form('skeleton_delete_form', $skeleton->skeleton_id);
358 return $output;
359 }
360
361 /**
362 * FormsAPI for skeleton_delete_instance()
363 */
364 function skeleton_delete_form($form_state, $skeleton_id) {
365 $form = array();
366 $form['skeleton_id'] = array('#type' => 'value', '#value' => $skeleton_id);
367 $form['submit'] = array(
368 '#type' => 'submit',
369 '#value' => t('Delete skeleton'),
370 '#prefix' => l(t('Cancel action'), 'admin/content/skeleton') .'<br />'
371 );
372 return $form;
373 }
374
375 /**
376 * FormsAPI for skeleton_delete_form()
377 */
378 function skeleton_delete_form_submit($form, &$form_state) {
379 db_query("DELETE from {skeleton} WHERE skeleton_id = %d", $form_state['values']['skeleton_id']);
380 // We rebuild the menu system to update the links at node/add.
381 menu_rebuild();
382 // TODO add delete for legacy items
383 drupal_set_message(t('Skeleton deleted.'));
384 drupal_goto('admin/content/skeleton');
385 }
386
387 /**
388 * Create a new instance of a skeleton
389 * This function generates the actual book pages.
390 *
391 * @param $skeleton
392 * An object from the {skeleton} table, indicating the outline to act upon.
393 * @return
394 * Themed HTML, including the instance creation form
395 */
396 function skeleton_create_instance($skeleton) {
397 $output = '';
398 drupal_set_title(t('Skeleton: Create'));
399 $output .= '<h3>'. check_plain($skeleton->skeleton) .'</h3>';
400 $tree = skeleton_get_tree($skeleton->skeleton_id);
401 if (empty($tree)) {
402 $output .= '<p>' . t('You must first assign at least one template to this skeleton outline. <a href="@edit-url">Edit this skeleton</a> to add a template to it.', array('@edit-url' => url('admin/content/skeleton/skeleton/' . $skeleton->skeleton_id . '/edit'))) . '</p>';
403 return $output;
404 }
405 drupal_add_js(drupal_get_path('module', 'skeleton') . '/skeleton-admin.js');
406 $output .= drupal_get_form('skeleton_create_instance_form', $skeleton);
407 return $output;
408 }
409
410 /**
411 * FormsAPI for skeleton_create_instance()
412 */
413 function skeleton_create_instance_form(&$form_state, $skeleton) {
414 $form = array();
415 $form['instance'] = array(
416 '#type' => 'fieldset',
417 '#title' => t('Skeleton Information'),
418 '#collapsible' => TRUE
419 );
420
421 // Count the number of items with the parent set to 0. If this is more than
422 // one, we can't have a template as the book introduction.
423 $templates = skeleton_get_tree($skeleton->skeleton_id);
424 $parents_count = 0;
425 foreach($templates as $template) {
426 if ($template->parents[0] == 0) {
427 $parents_count++;
428 }
429 if ($parents_count > 1) {
430 $form['instance']['introduction_enabled'] = array(
431 '#type' => 'value',
432 '#value' => TRUE,
433 );
434 break;
435 }
436 }
437 $form['instance']['author'] = array(
438 '#type' => 'textfield',
439 '#title' => t('Book author'),
440 '#description' => t('Select an author for the created book, or leave blank to set yourself as the author.'),
441 '#access' => user_access('administer nodes'),
442 '#default_value' => $form_values['author'],
443 '#size' => 40,
444 '#autocomplete_path' => 'user/autocomplete',
445 '#weight' => -50,
446 );
447 if ($parents_count < 2 && (!$form_state['add_introduction'])) {
448 $form['instance']['add_introduction'] = array(
449 '#type' => 'submit',
450 '#value' => t('Toggle custom introduction for this book'),
451 '#description' => t('This will add an introduction page to this book, and all templates will be made a child of the introduction page.'),
452 '#ahah' => array(
453 'path' => 'skeleton/introduction',
454 'effect' => 'slide',
455 'wrapper' => 'introduction-wrapper',
456 ),
457 '#submit' => array('skeleton_add_introduction_submit'), // If no javascript action.
458 '#weight' => 100,
459 );
460 $form['instance']['introduction_wrapper'] = array(
461 '#value' => '<div id="introduction-wrapper" /></div>',
462 '#weight' => -25,
463 );
464 }
465 else {
466 $form['instance'] += skeleton_introduction_form($form_state);
467 }
468 $form['instance']['skeleton_id'] = array(
469 '#type' => 'value',
470 '#value' => $skeleton->skeleton_id,
471 );
472
473 // We need to determine if any skeleton tokens are used in this skeleton
474 // instance.
475 $skeleton_tokens = array();
476 $result = db_query("SELECT * FROM {skeleton_token}");
477 while ($token = db_fetch_object($result)) {
478 $token->description = check_plain($token->description);
479 $skeleton_tokens[] = $token;
480 }
481
482 // For each template, find any tokens used and put them in $required_tokens.
483 $required_tokens = array();
484 foreach ($templates as $template_data) {
485 $node = (object)unserialize($template_data->node_data);
486 foreach ($skeleton_tokens as $token) {
487 if (strstr($node->title, $token->token) || strstr($node->body, $token->token)) {
488 $required_tokens[] = $token;
489 }
490 }
491 }
492
493 // If there are required tokens, generate the form.
494 if (!empty($required_tokens)) {
495 $form['skeleton_tokens'] = array(
496 '#type' => 'fieldset',
497 '#title' => t('Skeleton tokens'),
498 '#description' => t('The following tokens are used in this skeleton outline. Please provide values for the following tokens.'),
499 '#collapsible' => TRUE,
500 '#tree' => TRUE,
501 );
502 foreach ($required_tokens as $token) {
503 $form['skeleton_tokens'][$token->token] = array(
504 '#type' => 'textfield',
505 '#title' => $token->token,
506 '#maxlength' => 384,
507 '#description' => $token->description,
508 '#default_value' => isset($form_state['values']['skeleton_tokens'][$token->token]) ? $form_state['values']['skeleton_tokens'][$token->token] : "",
509 );
510 }
511 }
512 $form['submit'] = array(
513 '#type' => 'submit',
514 '#value' => t('Create new book from skeleton'),
515 );
516 return $form;
517 }
518
519 /**
520 * Forms API validation function for the create instance form. We need this so
521 * we don't throw errors for required fields when changing the introduction
522 * status. A better way would be a way to intercept / override #required in
523 * specific cases, but the Forms API doesn't support that.
524 *
525 * @param $form
526 * The form being validated.
527 * @param $form_state
528 * The current state of the form.
529 */
530 function skeleton_create_instance_form_validate($form, &$form_state) {
531 if (!($form_state['clicked_button']['#value'] == t('Create new book from skeleton'))) {
532 return;
533 }
534 if (!empty($form_state['values']['author'])) {
535 if (!user_load(array('name' => $form_state['values']['author']))) {
536 form_set_error('author', t('The account %user you selected as the author does not exist.', array('%user' => $form_state['values']['author'])));
537 }
538 }
539 if (isset($form['instance']['title'])) {
540 if (empty($form_state['values']['title'])) {
541 form_set_error('title', t('You must set a title for the custom introduction.'));
542 }
543 if (empty($form_state['values']['body'])) {
544 form_set_error('body', t('You must set a body for the custom introduction.'));
545 }
546 }
547 // Now, do token validation.
548 foreach(element_children($form['skeleton_tokens']) as $token) {
549 if (empty($form_state['values']['skeleton_tokens'][$token])) {
550 form_set_error("skeleton_tokens][$token", t('You must set a value for the %token token.', array('%token' => $token)));
551 }
552 }
553 }
554
555 /**
556 * Return a Form API array of the introduction form.
557 *
558 * @param $form_state
559 * The current state of the form.
560 * @return
561 * The Form API array.
562 */
563 function skeleton_introduction_form($form_state) {
564 // Add a remove button for the JS-disabled case.
565 if ($form_state['add_introduction']) {
566 $form['remove_introduction'] = array(
567 '#type' => 'submit',
568 '#value' => t('Remove introduction to this book'),
569 '#submit' => array('skeleton_add_introduction_submit'), // If no javascript action.
570 '#weight' => -100,
571 );
572 }
573 $form['title'] = array(
574 '#type' => 'textfield',
575 '#size' => 40,
576 '#title' => t('Book title'),
577 '#description' => t('The title of the new book you wish to create'),
578 '#weight' => -10,
579 );
580 if (module_exists('translation')) {
581 // We are always creating a book page. This is modified from
582 // locale_form_alter().
583 if (variable_get('language_content_type_book', 0)) {
584 $form['language'] = array(
585 '#type' => 'select',
586 '#title' => t('Language'),
587 '#options' => array('' => t('Language neutral')) + locale_language_list('name'),
588 '#weight' => -5,
589 );
590 }
591 // Node type without language selector: assign the default for new nodes
592 else {
593 $default = language_default();
594 $form['language'] = array(
595 '#type' => 'value',
596 '#value' => $default->language
597 );
598 }
599 }
600 $form['body'] = array(
601 '#type' => 'textarea',
602 '#cols' => 40,
603 '#rows' => 10,
604 '#title' => t('Book introduction'),
605 '#description' => t('A description of the contents of this book.'),
606 '#suffix' => t('<p>After you create this instance, you will be able to edit the complete book page.</p>'),
607 '#weight' => 0,
608 );
609
610 return $form;
611 }
612
613 /**
614 * Submit handler to enable a custom introduction to a book. This handler is
615 * used when javascript is not available. It makes changes to the form state
616 * and the entire form is rebuilt during the page reload.
617 */
618 function skeleton_add_introduction_submit($form, &$form_state) {
619 // Set the form to rebuild and run submit handlers.
620 // Make the changes we want to the form state.
621 if ($form_state['values']['add_introduction']) {
622 $form_state['add_introduction'] = TRUE;
623 }
624 $form_state['rebuild'] = TRUE;
625 $form = form_builder('skeleton_create_instance_form', $form, $form_state);
626 }
627
628 /**
629 * Enable or disable a custom introduction form for an instantiated book.
630 */
631 function skeleton_introduction_js() {
632 // Build the new form
633 $form_state = array('submitted' => FALSE);
634 $form_build_id = $_POST['form_build_id'];
635 // Add the new element to the stored form. Without adding the element to the
636 // form, Drupal is not aware of this new elements existence and will not
637 // process it. We retreive the cached form, add the element, and resave.
638 if (!$form = form_get_cache($form_build_id, $form_state)) {
639 exit();
640 }
641 // Get the form element
642 if (!$form['instance']['introduction_added']) {
643 $form_element = skeleton_introduction_form($form_id);
644 // Let other modules alter it.
645 drupal_alter('form', $form_element, array(), 'skeleton_introduction_js');
646 $form['instance'] += $form_element;
647 $form['instance']['introduction_added'] = array(
648 '#type' => 'value',
649 '#value' => TRUE,
650 );
651 }
652 else {
653 unset($form['instance']['title']);
654 unset($form['instance']['body']);
655 unset($form['instance']['language']);
656 unset($form['instance']['introduction_added']);
657 }
658 form_set_cache($form_build_id, $form, $form_state);
659 $form += array(
660 '#post' => $_POST,
661 '#programmed' => FALSE,
662 );
663
664 // Rebuild the form.
665 $form = form_builder('skeleton_create_instance_form', $form, $form_state);
666
667 // Render the new output.
668 $introduction_form = $form['instance'];
669 unset($introduction_form['add_introduction']);
670 unset($introduction_form['author']);
671 unset($introduction_form['#type']);
672 $output = drupal_render($introduction_form);
673 drupal_json(array('status' => TRUE, 'data' => $output));
674 }
675
676 /**
677 * FormsAPI for skeleton_create_instance()
678 * Creates the nodes from the outline using drupal_execute().
679 *
680 * Note: The nid fetching sequence is sketchy and might need rewriting
681 *
682 */
683 function skeleton_create_instance_form_submit($form, &$form_state) {
684 module_load_include('inc', 'node', 'node.pages');
685 module_load_include('inc', 'skeleton', 'skeleton_token');
686
687 // Use the author as specified, otherwise default to the current user.
688 if (!empty($form_state['values']['author'])) {
689 $user = user_load(array('name' => $form_state['values']['author']));
690 }
691 else {
692 global $user;
693 }
694
695 $nodes = skeleton_get_tree($form_state['values']['skeleton_id']);
696 if (!empty($nodes)) {
697 $parents = array();
698 // If we are not using a custom introduction, generate the first page from
699 // the appropriate template.
700 if (!$form_state['values']['title']) {
701 // Get the first template, use it's data.
702 $node = array();
703 $node['values'] = unserialize($nodes[0]->node_data);
704
705 // Pathauto provides a 'bookpathalias' token which is very useful for
706 // generating inline URL's. However, the path isn't generated until the
707 // node is saved. Escape out the token if it exists, and replace it later
708 // after the node is saved for the first time.
709 if (module_exists('pathauto')) {
710 $node['values']['title'] = str_replace('[bookpathalias]', '[\skeleton-bookpathalias\]', $node['values']['title']);
711 $node['values']['body'] = str_replace('[bookpathalias]', '[\skeleton-bookpathalias\]', $node['values']['body']);
712 // Also do any CCK text fields.
713 $content_type = content_types($node['values']['type']);
714 $fields = $content_type['fields'];
715 foreach ($fields as $field) {
716 if ($field['type'] == 'text' && is_array($node['values'][$field['field_name']])) {
717 foreach (array_keys($node['values'][$field['field_name']]) as $delta) {
718 $node['values'][$field['field_name']][$delta]['value'] = str_replace('[bookpathalias]', '[\skeleton-bookpathalias\]', $node['values'][$field['field_name']][$delta]['value']);
719 }
720 }
721 }
722 }
723
724 // Replace custom tokens in the title and body fields. Replace skeleton
725 // tokens first so they may be used as node tokens such as [title].
726 if (!empty($form_state['values']['skeleton_tokens'])) {
727 $node['values'] = (array)_skeleton_token_replace_values((object)$node['values'], 'skeleton', $form_state['values']['skeleton_tokens']);
728 }
729
730 $node['values'] = (array)_skeleton_token_replace_values((object)$node['values'], 'node', (object)$node['values']);
731 $form_state['values']['title'] = $node['values']['title'];
732 $form_state['values']['body'] = $node['values']['body'];
733
734 $node['values']['tokens'] = $form_state['values']['skeleton_tokens'];
735 $form_state['values']['tokens'] = $form_state['values']['skeleton_tokens'];
736
737 $form_state['values']['language'] = $node['values']['language'];
738
739 $form_state['values']['template_id'] = $node['values']['template_id'];
740
741 // We've done this template, so remove it.
742 unset($nodes[0]);
743 }
744
745 $parent_node = array(
746 'values' => array(
747 'uid' => $user->uid,
748 'name' => $user->name,
749 'title' => $form_state['values']['title'],
750 'type' => 'book',
751 'body' => $form_state['values']['body'],
752 'tokens' => $form_state['values']['tokens'],
753 'skeleton_id' => $form_state['values']['skeleton_id'],
754 'template_id' => $form_state['values']['template_id'],
755 'op' => t('Save'),
756 ),
757 );
758
759 if (module_exists('translation')) {
760 $parent_node['values']['language'] = $form_state['values']['language'];
761 }
762
763 // Copy CCK fields.
764 if (module_exists('content')) {
765 $content_type = content_types('book');
766 $fields = $content_type['fields'];
767 if (is_array($fields)) {
768 foreach ($fields as $field) {
769 if (isset($node['values']->{$field['field_name']})) {
770 $parent_node['values'][$field['field_name']] = $node['values'][$field['field_name']];
771 }
772 }
773 }
774 }
775
776 $parent_node['values']['book']['bid'] = 'new';
777 $parent_node['values']['book']['options'] = array();
778 drupal_execute('book_node_form', $parent_node, $parent_node['values']);
779 // TODO: This will probably need to be removed if this call is integrated
780 // into the book module. See http://drupal.org/node/364529 for details.
781 menu_rebuild();
782 $parent_node = node_load($parent_node['nid'], NULL, TRUE);
783
784 // Only do this if pathauto exists, as it involves another save slowing
785 // things down.
786 if (module_exists('pathauto')) {
787 $parent_node->title = str_replace('[\skeleton-bookpathalias\]', drupal_get_path_alias($parent_node->path), $parent_node->title);
788 $parent_node->teaser = str_replace('[\skeleton-bookpathalias\]', drupal_get_path_alias($parent_node->path), $parent_node->teaser);
789 $parent_node->body = str_replace('[\skeleton-bookpathalias\]', drupal_get_path_alias($parent_node->path), $parent_node->body);
790 $content_type = content_types($parent_node->type);
791 $fields = $content_type['fields'];
792 foreach ($fields as $field) {
793 if ($field['type'] == 'text' && is_array($parent_node->{$field['field_name']})) {
794 foreach (array_keys($parent_node->{$field['field_name']}) as $delta) {
795 $parent_node->{$field['field_name']}[$delta]['value'] = str_replace('[\skeleton-bookpathalias\]', drupal_get_path_alias($parent_node->path), $parent_node->{$field['field_name']}[$delta]['value']);
796 }
797 }
798 }
799 $parent_node->skeleton_template->keep_connected = TRUE;
800 $parent_node = node_submit($parent_node);
801 node_save($parent_node);
802 }
803
804 // get the id we just created
805 $form_state['nids'][] = $parent_node->nid;
806 $book_id = $parent_node->nid;
807 $goto_id = $book_id;
808
809 // Iterate through the dummy nodes and create the book pages.
810 // The Book ID is the NID of the topmost book page.
811 foreach ($nodes as $data) {
812 $node = array();
813 $node['values'] = unserialize($data->node_data);
814 // if parents are assigned by the skeleton, use them
815 if ($data->parent > 0 && isset($parents[$data->parent])) {
816 $parent_node = node_load($parents[$data->parent], NULL, TRUE);
817 }
818 else {
819 $parent_node = node_load($book_id, NULL, TRUE);
820 }
821 $node['values']['book'] = array();
822 $node['values']['book']['bid'] = $book_id;
823 $node['values']['book']['plid'] = $parent_node->book['mlid'];
824
825 $node['values']['book']['weight'] = $data->weight;
826 $node['values']['book']['options'] = array();
827
828 // Replace custom tokens in the title and body fields. Replace skeleton
829 // tokens first so they may be used as node tokens such as [title].
830 if (!empty($form_state['values']['skeleton_tokens'])) {
831 $node['values'] = (array)_skeleton_token_replace_values((object)$node['values'], 'skeleton', $form_state['values']['skeleton_tokens']);
832 }
833
834 $node['values'] = (array)_skeleton_token_replace_values((object)$node['values'], 'node', (object)$node['values'], TRUE);
835
836 $node['values']['uid'] = $user->uid;
837 $node['values']['name'] = $user->name;
838
839 $node['values']['tokens'] = $form_state['values']['skeleton_tokens'];
840
841 // We have to add the skeleton ID here as we can't save it with the
842 // template as a template may have multiple skeletons.
843 $node['values']['skeleton_id'] = $form_state['values']['skeleton_id'];
844
845 $node['values']['op'] = t('Save');
846 drupal_execute($node['values']['type'] .'_node_form', $node, (object)$node['values']);
847 // TODO: This will probably need to be removed if this call is integrated
848 // into the book module. See http://drupal.org/node/364529 for details.
849 menu_rebuild();
850 $form_state['nids'][] = $node['nid'];
851 // Store all of the menu id's for each inserted template.
852 $parents[$data->template_id] = $node['nid'];
853 }
854 }
855 else {
856 form_set_error(t('This skeleton is empty.'), 'form');
857 }
858 if (!$form_state['values']['title']) {
859 $form_state['redirect'] = 'node/' . $goto_id;
860 }
861 else {
862 $form_state['redirect'] = 'node/' . $goto_id . '/edit';
863 }
864 }

  ViewVC Help
Powered by ViewVC 1.1.2