2007-09-11 17:35:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2013-01-04 18:29:51 +00:00
|
|
|
* Administration page callbacks for the Book module.
|
2007-09-11 17:35:58 +00:00
|
|
|
*/
|
|
|
|
|
2013-03-10 19:05:24 +00:00
|
|
|
use Drupal\Core\Entity\EntityInterface;
|
2012-04-26 16:44:37 +00:00
|
|
|
|
2007-09-11 17:35:58 +00:00
|
|
|
/**
|
2011-12-13 03:13:23 +00:00
|
|
|
* Form constructor for administering a single book's hierarchy.
|
2007-09-11 17:35:58 +00:00
|
|
|
*
|
2013-03-10 19:05:24 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $node
|
2011-12-13 03:13:23 +00:00
|
|
|
* The node of the top-level page in the book.
|
|
|
|
*
|
|
|
|
* @see book_menu()
|
|
|
|
* @see book_admin_edit_validate()
|
|
|
|
* @see book_admin_edit_submit()
|
|
|
|
* @ingroup forms
|
2007-09-11 17:35:58 +00:00
|
|
|
*/
|
2013-03-10 19:05:24 +00:00
|
|
|
function book_admin_edit($form, $form_state, EntityInterface $node) {
|
2012-07-22 18:07:00 +00:00
|
|
|
drupal_set_title($node->label());
|
2007-10-15 19:33:27 +00:00
|
|
|
$form['#node'] = $node;
|
2008-07-05 06:00:51 +00:00
|
|
|
_book_admin_table($node, $form);
|
2007-10-15 19:33:27 +00:00
|
|
|
$form['save'] = array(
|
|
|
|
'#type' => 'submit',
|
|
|
|
'#value' => t('Save book pages'),
|
|
|
|
);
|
2008-05-15 21:19:24 +00:00
|
|
|
|
2007-09-11 17:35:58 +00:00
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
2008-07-05 06:00:51 +00:00
|
|
|
/**
|
2011-12-13 03:13:23 +00:00
|
|
|
* Form validation handler for book_admin_edit().
|
2008-07-05 06:00:51 +00:00
|
|
|
*
|
2011-12-13 03:13:23 +00:00
|
|
|
* Checks that the book has not been changed while using the form.
|
|
|
|
*
|
|
|
|
* @see book_admin_edit_submit()
|
2008-07-05 06:00:51 +00:00
|
|
|
*/
|
|
|
|
function book_admin_edit_validate($form, &$form_state) {
|
|
|
|
if ($form_state['values']['tree_hash'] != $form_state['values']['tree_current_hash']) {
|
|
|
|
form_set_error('', t('This book has been modified by another user, the changes could not be saved.'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-11 17:35:58 +00:00
|
|
|
/**
|
2011-12-13 03:13:23 +00:00
|
|
|
* Form submission handler for book_admin_edit().
|
2007-09-11 17:35:58 +00:00
|
|
|
*
|
2007-11-26 16:19:37 +00:00
|
|
|
* This function takes care to save parent menu items before their children.
|
|
|
|
* Saving menu items in the incorrect order can break the menu tree.
|
|
|
|
*
|
2011-12-13 03:13:23 +00:00
|
|
|
* @see book_admin_edit_validate()
|
2007-11-26 16:19:37 +00:00
|
|
|
* @see menu_overview_form_submit()
|
2007-09-11 17:35:58 +00:00
|
|
|
*/
|
|
|
|
function book_admin_edit_submit($form, &$form_state) {
|
2007-11-26 16:19:37 +00:00
|
|
|
// Save elements in the same order as defined in post rather than the form.
|
|
|
|
// This ensures parents are updated before their children, preventing orphans.
|
2009-03-14 20:13:27 +00:00
|
|
|
$order = array_flip(array_keys($form_state['input']['table']));
|
2007-11-26 16:19:37 +00:00
|
|
|
$form['table'] = array_merge($order, $form['table']);
|
|
|
|
|
2013-03-09 05:10:19 +00:00
|
|
|
// Track updates.
|
|
|
|
$updated = FALSE;
|
2007-11-26 16:19:37 +00:00
|
|
|
foreach (element_children($form['table']) as $key) {
|
|
|
|
if ($form['table'][$key]['#item']) {
|
|
|
|
$row = $form['table'][$key];
|
|
|
|
$values = $form_state['values']['table'][$key];
|
|
|
|
|
|
|
|
// Update menu item if moved.
|
|
|
|
if ($row['plid']['#default_value'] != $values['plid'] || $row['weight']['#default_value'] != $values['weight']) {
|
2013-03-09 05:10:19 +00:00
|
|
|
$menu_link = entity_load('menu_link', $values['mlid']);
|
|
|
|
$menu_link->weight = $values['weight'];
|
|
|
|
$menu_link->plid = $values['plid'];
|
|
|
|
$menu_link->save();
|
|
|
|
$updated = TRUE;
|
2007-09-28 15:44:13 +00:00
|
|
|
}
|
2007-09-11 17:35:58 +00:00
|
|
|
|
2007-11-26 16:19:37 +00:00
|
|
|
// Update the title if changed.
|
|
|
|
if ($row['title']['#default_value'] != $values['title']) {
|
2010-02-25 09:44:51 +00:00
|
|
|
$node = node_load($values['nid']);
|
2012-03-08 15:10:59 +00:00
|
|
|
$langcode = LANGUAGE_NOT_SPECIFIED;
|
2010-01-09 21:54:01 +00:00
|
|
|
$node->title = $values['title'];
|
2007-11-26 16:19:37 +00:00
|
|
|
$node->book['link_title'] = $values['title'];
|
2012-10-14 05:44:26 +00:00
|
|
|
$node->setNewRevision();
|
2010-01-09 21:54:01 +00:00
|
|
|
$node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title']));
|
2009-10-11 03:07:21 +00:00
|
|
|
|
2012-04-26 16:44:37 +00:00
|
|
|
$node->save();
|
2012-07-22 18:07:00 +00:00
|
|
|
watchdog('content', 'book: updated %title.', array('%title' => $node->label()), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
|
2007-11-26 16:19:37 +00:00
|
|
|
}
|
2007-09-11 17:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-09 05:10:19 +00:00
|
|
|
if ($updated) {
|
|
|
|
// Flush static and cache.
|
|
|
|
drupal_static_reset('book_menu_subtree_data');
|
|
|
|
$cid = 'links:' . $form['#node']->book['menu_name'] . ':subtree-cid:' . $form['#node']->book['mlid'];
|
|
|
|
cache('menu')->delete($cid);
|
|
|
|
}
|
2007-11-26 16:19:37 +00:00
|
|
|
|
2012-07-22 18:07:00 +00:00
|
|
|
drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->label())));
|
2007-09-11 17:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-13 03:13:23 +00:00
|
|
|
* Builds the table portion of the form for the book administration page.
|
|
|
|
*
|
2013-03-10 19:05:24 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $node
|
2011-12-13 03:13:23 +00:00
|
|
|
* The node of the top-level page in the book.
|
|
|
|
* @param $form
|
2013-01-04 18:29:51 +00:00
|
|
|
* The form that is being modified, passed by reference.
|
2007-09-11 17:35:58 +00:00
|
|
|
*
|
|
|
|
* @see book_admin_edit()
|
|
|
|
*/
|
2013-03-10 19:05:24 +00:00
|
|
|
function _book_admin_table(EntityInterface $node, &$form) {
|
2008-07-05 06:00:51 +00:00
|
|
|
$form['table'] = array(
|
2007-09-11 17:35:58 +00:00
|
|
|
'#theme' => 'book_admin_table',
|
|
|
|
'#tree' => TRUE,
|
|
|
|
);
|
|
|
|
|
|
|
|
$tree = book_menu_subtree_data($node->book);
|
2007-11-26 16:19:37 +00:00
|
|
|
$tree = array_shift($tree); // Do not include the book item itself.
|
|
|
|
if ($tree['below']) {
|
2010-05-01 08:12:23 +00:00
|
|
|
$hash = drupal_hash_base64(serialize($tree['below']));
|
2008-07-05 06:00:51 +00:00
|
|
|
// Store the hash value as a hidden form element so that we can detect
|
|
|
|
// if another user changed the book hierarchy.
|
|
|
|
$form['tree_hash'] = array(
|
|
|
|
'#type' => 'hidden',
|
|
|
|
'#default_value' => $hash,
|
|
|
|
);
|
|
|
|
$form['tree_current_hash'] = array(
|
|
|
|
'#type' => 'value',
|
|
|
|
'#value' => $hash,
|
|
|
|
);
|
|
|
|
_book_admin_table_tree($tree['below'], $form['table']);
|
2007-11-26 16:19:37 +00:00
|
|
|
}
|
2008-05-15 21:19:24 +00:00
|
|
|
|
2007-09-11 17:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-13 03:13:23 +00:00
|
|
|
* Helps build the main table in the book administration page form.
|
|
|
|
*
|
|
|
|
* @param $tree
|
|
|
|
* A subtree of the book menu hierarchy.
|
|
|
|
* @param $form
|
2013-01-04 18:29:51 +00:00
|
|
|
* The form that is being modified, passed by reference.
|
2011-12-13 03:13:23 +00:00
|
|
|
*
|
|
|
|
* @return
|
2013-01-04 18:29:51 +00:00
|
|
|
* The modified form array.
|
2007-09-11 17:35:58 +00:00
|
|
|
*
|
|
|
|
* @see book_admin_edit()
|
|
|
|
*/
|
|
|
|
function _book_admin_table_tree($tree, &$form) {
|
2011-11-20 13:40:42 +00:00
|
|
|
// The delta must be big enough to give each node a distinct value.
|
|
|
|
$count = count($tree);
|
|
|
|
$delta = ($count < 30) ? 15 : intval($count / 2) + 1;
|
|
|
|
|
2008-11-08 19:33:53 +00:00
|
|
|
foreach ($tree as $data) {
|
|
|
|
$form['book-admin-' . $data['link']['nid']] = array(
|
2007-11-26 16:19:37 +00:00
|
|
|
'#item' => $data['link'],
|
2007-09-11 17:35:58 +00:00
|
|
|
'nid' => array('#type' => 'value', '#value' => $data['link']['nid']),
|
|
|
|
'depth' => array('#type' => 'value', '#value' => $data['link']['depth']),
|
|
|
|
'href' => array('#type' => 'value', '#value' => $data['link']['href']),
|
|
|
|
'title' => array(
|
|
|
|
'#type' => 'textfield',
|
|
|
|
'#default_value' => $data['link']['link_title'],
|
|
|
|
'#maxlength' => 255,
|
2007-11-26 16:19:37 +00:00
|
|
|
'#size' => 40,
|
2007-09-11 17:35:58 +00:00
|
|
|
),
|
|
|
|
'weight' => array(
|
2011-11-20 13:40:42 +00:00
|
|
|
'#type' => 'weight',
|
2007-09-11 17:35:58 +00:00
|
|
|
'#default_value' => $data['link']['weight'],
|
2011-11-20 13:40:42 +00:00
|
|
|
'#delta' => max($delta, abs($data['link']['weight'])),
|
2010-10-20 01:31:07 +00:00
|
|
|
'#title' => t('Weight for @title', array('@title' => $data['link']['title'])),
|
|
|
|
'#title_display' => 'invisible',
|
2007-09-11 17:35:58 +00:00
|
|
|
),
|
2007-11-26 16:19:37 +00:00
|
|
|
'plid' => array(
|
2010-10-20 01:31:07 +00:00
|
|
|
'#type' => 'hidden',
|
2007-11-26 16:19:37 +00:00
|
|
|
'#default_value' => $data['link']['plid'],
|
|
|
|
),
|
|
|
|
'mlid' => array(
|
|
|
|
'#type' => 'hidden',
|
|
|
|
'#default_value' => $data['link']['mlid'],
|
|
|
|
),
|
2007-09-11 17:35:58 +00:00
|
|
|
);
|
|
|
|
if ($data['below']) {
|
|
|
|
_book_admin_table_tree($data['below'], $form);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-13 15:23:03 +00:00
|
|
|
* Returns HTML for a book administration form.
|
|
|
|
*
|
2013-01-04 18:29:51 +00:00
|
|
|
* @param array $variables
|
2010-04-13 15:23:03 +00:00
|
|
|
* An associative array containing:
|
|
|
|
* - form: A render element representing the form.
|
2007-09-11 17:35:58 +00:00
|
|
|
*
|
2008-01-08 10:35:43 +00:00
|
|
|
* @see book_admin_table()
|
2010-04-13 15:23:03 +00:00
|
|
|
* @ingroup themeable
|
2007-09-11 17:35:58 +00:00
|
|
|
*/
|
2009-10-09 01:00:08 +00:00
|
|
|
function theme_book_admin_table($variables) {
|
|
|
|
$form = $variables['form'];
|
|
|
|
|
2007-11-26 16:19:37 +00:00
|
|
|
drupal_add_tabledrag('book-outline', 'match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2);
|
|
|
|
drupal_add_tabledrag('book-outline', 'order', 'sibling', 'book-weight');
|
|
|
|
|
2012-10-09 19:49:07 +00:00
|
|
|
$header = array(t('Title'), t('Weight'), t('Parent'), t('Operations'));
|
2007-09-11 17:35:58 +00:00
|
|
|
|
|
|
|
$rows = array();
|
|
|
|
$destination = drupal_get_destination();
|
|
|
|
$access = user_access('administer nodes');
|
|
|
|
foreach (element_children($form) as $key) {
|
|
|
|
$nid = $form[$key]['nid']['#value'];
|
|
|
|
$href = $form[$key]['href']['#value'];
|
2007-11-26 16:19:37 +00:00
|
|
|
|
|
|
|
// Add special classes to be used with tabledrag.js.
|
2009-08-22 14:34:23 +00:00
|
|
|
$form[$key]['plid']['#attributes']['class'] = array('book-plid');
|
|
|
|
$form[$key]['mlid']['#attributes']['class'] = array('book-mlid');
|
|
|
|
$form[$key]['weight']['#attributes']['class'] = array('book-weight');
|
2007-11-26 16:19:37 +00:00
|
|
|
|
2007-10-15 19:33:27 +00:00
|
|
|
$data = array(
|
2009-10-09 01:00:08 +00:00
|
|
|
theme('indentation', array('size' => $form[$key]['depth']['#value'] - 2)) . drupal_render($form[$key]['title']),
|
2007-09-11 17:35:58 +00:00
|
|
|
drupal_render($form[$key]['weight']),
|
2007-11-26 16:19:37 +00:00
|
|
|
drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
|
2012-10-09 19:49:07 +00:00
|
|
|
);
|
|
|
|
$links = array();
|
|
|
|
$links['view'] = array(
|
2013-02-27 22:43:00 +00:00
|
|
|
'title' => t('View'),
|
2012-10-09 19:49:07 +00:00
|
|
|
'href' => $href,
|
|
|
|
);
|
|
|
|
if ($access) {
|
|
|
|
$links['edit'] = array(
|
2013-02-27 22:43:00 +00:00
|
|
|
'title' => t('Edit'),
|
2012-10-09 19:49:07 +00:00
|
|
|
'href' => "node/$nid/edit",
|
|
|
|
'query' => $destination,
|
|
|
|
);
|
|
|
|
$links['delete'] = array(
|
2013-02-27 22:43:00 +00:00
|
|
|
'title' => t('Delete'),
|
2012-10-09 19:49:07 +00:00
|
|
|
'href' => "node/$nid/delete",
|
|
|
|
'query' => $destination,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$data[] = array(
|
|
|
|
'data' => array(
|
|
|
|
'#type' => 'operations',
|
|
|
|
'#links' => $links,
|
|
|
|
),
|
2007-09-11 17:35:58 +00:00
|
|
|
);
|
2007-10-15 19:33:27 +00:00
|
|
|
$row = array('data' => $data);
|
|
|
|
if (isset($form[$key]['#attributes'])) {
|
|
|
|
$row = array_merge($row, $form[$key]['#attributes']);
|
|
|
|
}
|
2009-08-22 14:34:23 +00:00
|
|
|
$row['class'][] = 'draggable';
|
2007-10-15 19:33:27 +00:00
|
|
|
$rows[] = $row;
|
2007-09-11 17:35:58 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 19:44:09 +00:00
|
|
|
return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'book-outline'), 'empty' => t('No book content available.')));
|
2007-10-15 19:33:27 +00:00
|
|
|
}
|