00001 <?php
00002
00003
00012 function book_admin_overview() {
00013 $rows = array();
00014 foreach (book_get_books() as $book) {
00015 $rows[] = array(l($book['title'], $book['href'], $book['options']), l(t('edit order and titles'), "admin/content/book/" . $book['nid']));
00016 }
00017 $headers = array(t('Book'), t('Operations'));
00018
00019 return theme('table', $headers, $rows);
00020 }
00021
00029 function book_admin_settings() {
00030 $types = node_get_types('names');
00031 $form['book_allowed_types'] = array(
00032 '#type' => 'checkboxes',
00033 '#title' => t('Allowed book outline types'),
00034 '#default_value' => variable_get('book_allowed_types', array('book')),
00035 '#options' => $types,
00036 '#description' => t('Select content types which users with the %add-perm permission will be allowed to add to the book hierarchy. Users with the %outline-perm permission can add all content types.', array('%add-perm' => t('add content to books'), '%outline-perm' => t('administer book outlines'))),
00037 '#required' => TRUE,
00038 );
00039 $form['book_child_type'] = array(
00040 '#type' => 'radios',
00041 '#title' => t('Default child page type'),
00042 '#default_value' => variable_get('book_child_type', 'book'),
00043 '#options' => $types,
00044 '#description' => t('The content type for the %add-child link must be one of those selected as an allowed book outline type.', array('%add-child' => t('Add child page'))),
00045 '#required' => TRUE,
00046 );
00047 $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
00048 $form['#validate'][] = 'book_admin_settings_validate';
00049
00050 return system_settings_form($form);
00051 }
00052
00058 function book_admin_settings_validate($form, &$form_state) {
00059 $child_type = $form_state['values']['book_child_type'];
00060 if (empty($form_state['values']['book_allowed_types'][$child_type])) {
00061 form_set_error('book_child_type', t('The content type for the %add-child link must be one of those selected as an allowed book outline type.', array('%add-child' => t('Add child page'))));
00062 }
00063 }
00064
00072 function book_admin_edit($form_state, $node) {
00073 drupal_set_title(check_plain($node->title));
00074 $form = array();
00075 $form['#node'] = $node;
00076 $form['table'] = _book_admin_table($node);
00077 $form['save'] = array(
00078 '#type' => 'submit',
00079 '#value' => t('Save book pages'),
00080 );
00081
00082 return $form;
00083 }
00084
00094 function book_admin_edit_submit($form, &$form_state) {
00095
00096
00097 $order = array_flip(array_keys($form['#post']['table']));
00098 $form['table'] = array_merge($order, $form['table']);
00099
00100 foreach (element_children($form['table']) as $key) {
00101 if ($form['table'][$key]['#item']) {
00102 $row = $form['table'][$key];
00103 $values = $form_state['values']['table'][$key];
00104
00105
00106 if ($row['plid']['#default_value'] != $values['plid'] || $row['weight']['#default_value'] != $values['weight']) {
00107 $row['#item']['plid'] = $values['plid'];
00108 $row['#item']['weight'] = $values['weight'];
00109 menu_link_save($row['#item']);
00110 }
00111
00112
00113 if ($row['title']['#default_value'] != $values['title']) {
00114 $node = node_load($values['nid'], FALSE);
00115 $node->title = $values['title'];
00116 $node->book['link_title'] = $values['title'];
00117 $node->revision = 1;
00118 $node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title']));
00119 node_save($node);
00120 watchdog('content', 'book: updated %title.', array('%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
00121 }
00122 }
00123 }
00124
00125 drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->title)));
00126 }
00127
00133 function _book_admin_table($node) {
00134 $form = array(
00135 '#theme' => 'book_admin_table',
00136 '#tree' => TRUE,
00137 );
00138
00139 $tree = book_menu_subtree_data($node->book);
00140 $tree = array_shift($tree);
00141 if ($tree['below']) {
00142 _book_admin_table_tree($tree['below'], $form);
00143 }
00144
00145 return $form;
00146 }
00147
00153 function _book_admin_table_tree($tree, &$form) {
00154 foreach ($tree as $key => $data) {
00155 $form[$key] = array(
00156 '#item' => $data['link'],
00157 'nid' => array('#type' => 'value', '#value' => $data['link']['nid']),
00158 'depth' => array('#type' => 'value', '#value' => $data['link']['depth']),
00159 'href' => array('#type' => 'value', '#value' => $data['link']['href']),
00160 'title' => array(
00161 '#type' => 'textfield',
00162 '#default_value' => $data['link']['link_title'],
00163 '#maxlength' => 255,
00164 '#size' => 40,
00165 ),
00166 'weight' => array(
00167 '#type' => 'weight',
00168 '#default_value' => $data['link']['weight'],
00169 '#delta' => 15,
00170 ),
00171 'plid' => array(
00172 '#type' => 'textfield',
00173 '#default_value' => $data['link']['plid'],
00174 '#size' => 6,
00175 ),
00176 'mlid' => array(
00177 '#type' => 'hidden',
00178 '#default_value' => $data['link']['mlid'],
00179 ),
00180 );
00181 if ($data['below']) {
00182 _book_admin_table_tree($data['below'], $form);
00183 }
00184 }
00185
00186 return $form;
00187 }
00188
00195 function theme_book_admin_table($form) {
00196 drupal_add_tabledrag('book-outline', 'match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2);
00197 drupal_add_tabledrag('book-outline', 'order', 'sibling', 'book-weight');
00198
00199 $header = array(t('Title'), t('Weight'), t('Parent'), array('data' => t('Operations'), 'colspan' => '3'));
00200
00201 $rows = array();
00202 $destination = drupal_get_destination();
00203 $access = user_access('administer nodes');
00204 foreach (element_children($form) as $key) {
00205 $nid = $form[$key]['nid']['#value'];
00206 $href = $form[$key]['href']['#value'];
00207
00208
00209 $form[$key]['plid']['#attributes']['class'] = 'book-plid';
00210 $form[$key]['mlid']['#attributes']['class'] = 'book-mlid';
00211 $form[$key]['weight']['#attributes']['class'] = 'book-weight';
00212
00213 $data = array(
00214 theme('indentation', $form[$key]['depth']['#value'] - 2) . drupal_render($form[$key]['title']),
00215 drupal_render($form[$key]['weight']),
00216 drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
00217 l(t('view'), $href),
00218 $access ? l(t('edit'), 'node/' . $nid . '/edit', array('query' => $destination)) : ' ',
00219 $access ? l(t('delete'), 'node/' . $nid . '/delete', array('query' => $destination) ) : ' ',
00220 );
00221 $row = array('data' => $data);
00222 if (isset($form[$key]['#attributes'])) {
00223 $row = array_merge($row, $form[$key]['#attributes']);
00224 }
00225 $row['class'] = empty($row['class']) ? 'draggable' : $row['class'] . ' draggable';
00226 $rows[] = $row;
00227 }
00228
00229 return theme('table', $header, $rows, array('id' => 'book-outline'));
00230 }
00231
00235 function _book_admin_sort_tree(&$tree) {
00236 uasort($tree, '_book_admin_compare');
00237 foreach ($tree as $key => $subtree) {
00238 if (!empty($tree[$key]['below'])) {
00239 _book_admin_sort_tree($tree[$key]['below']);
00240 }
00241 }
00242 }
00243
00247 function _book_admin_compare($a, $b) {
00248 $weight = $a['link']['weight'] - $b['link']['weight'];
00249 if ($weight) {
00250 return $weight;
00251 }
00252
00253 return strncmp($a['link']['title'], $b['link']['title']);
00254 }