/[drupal]/contributions/modules/BookMadeSimple/book_made_simple.module
ViewVC logotype

Contents of /contributions/modules/BookMadeSimple/book_made_simple.module

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


Revision 1.3 - (show annotations) (download) (as text)
Tue Aug 25 18:57:32 2009 UTC (3 months ago) by marcelbichon
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +159 -134 lines
File MIME type: text/x-php
- Add issue #535762
- Change way to hide book outline section to be compatible with other modules
1 <?php
2 // $Id: book_made_simple.module,v 1.1.2.1.2.5.2.1 2009/08/04 21:22:28 scottrigby Exp $
3 // Author: M. Morin
4 /**
5 * Implementation of hook_help().
6 */
7 function book_made_simple_help($path, $arg) {
8 switch ($path) {
9 case 'admin/settings/book_made_simple':
10 $output = t('On this page you can define context-types allowed as children of book and those that will be automatically created as main book pages.<br/>You can also define child pages by content-type in !content-type-settings in the BookMadeSimple section',array('!content-type-settings' => l(t('content type settings'),'admin/content/types/list')));
11 return $output;
12 break;
13 }
14 }
15 function book_made_simple_init() {
16 if (! variable_get('book_made_simple_admin_use_css',false)) {
17 drupal_add_css(drupal_get_path('module', 'book_made_simple') . '/book_made_simple.css');
18 }
19 }
20
21 function book_made_simple_perm() {
22 return array('show book reorder link','show core Outline links');
23 }
24
25 /**
26 * Implementation of hook_menu().
27 */
28 function book_made_simple_menu() {
29 $items = array();
30 $items['admin/settings/book_made_simple'] = array(
31 'title' => t('BookMadeSimple settings'),
32 'page callback' => 'drupal_get_form',
33 'page arguments' => array('book_made_simple_settings_form'),
34 'access arguments' => array('administer site configuration'),
35 'description' => t('BookMadeSimple module settings.'),
36 'type' => MENU_NORMAL_ITEM,
37 );
38
39 $items['node/%/reorder'] = array(
40 'title' => t('Reorder the book'),
41 'page callback' => 'book_made_simple_reorder',
42 'page arguments' => array(1),
43 'access callback' => 'isBookAccess',
44 'access arguments' => array(1),
45 'type' => MENU_LOCAL_TASK,
46 'weight' =>2
47
48 );
49
50 return $items;
51 }
52 /**
53 * Return access rights for book
54 */
55 function isBookAccess($arg, $node = NULL) {
56 if ($node == NULL && is_numeric($arg)) {
57 $node = node_load($arg);
58 }
59
60 if (isset($node->book) && $node->book['bid'] > 0) {
61 if (! _book_outline_access($node) || ! user_access('show book reorder link')) {return false;}
62
63
64 // Cheching permissions according to rules/
65 $book = $node->book;
66 $linkVisibilty = variable_get('book_made_simple_reorder_link', "0");
67 $allowed = true;
68 if ($linkVisibilty == "0") {
69 $mainBook = $book;
70 if ($book['nid'] != $book['bid']) {$mainBook = node_load($book['bid'])->book;}
71 $allowed = (count(book_toc($mainBook['bid'],array(),9999)) > 2);
72 }
73 return $allowed;
74 }
75 return false;
76 }
77
78 /**
79 * Shortcut to reorder book page
80 */
81 function book_made_simple_reorder($id) {
82 $node = node_load($id);
83 drupal_goto("admin/content/book/" . $node->book['bid'] , "destination=node/" . $node->nid);
84 }
85 /**
86 * Implementation of hook_settings_form).
87 */
88 function book_made_simple_settings_form() {
89 $types = node_get_types("names");
90 $form['book_made_simple'] = array(
91 '#type' => 'fieldset',
92 '#attributes' => array('class' => 'allowed-child'),
93 '#title' => t('Select node types that can be included in a book'),
94 '#collapsible' => TRUE,
95 '#collapsed' => FALSE,
96 '#description' => t('Check content-type that can be included as child in a book. This selection is overriden by selection in content-type settings.'),
97 );
98
99 $form['book_made_simple']['book_made_simple_add_types'] = array(
100 '#type' => 'checkboxes',
101 '#options' => $types,
102 '#multiple' => true,
103 '#default_value' => variable_get('book_made_simple_add_types', array()),
104 );
105
106 $form['book_made_simple2'] = array(
107 '#type' => 'fieldset',
108 '#attributes' => array('class' => 'auto-main'),
109 '#title' => t('Select node types to auto-create main book page'),
110 '#collapsible' => TRUE,
111 '#collapsed' => FALSE,
112 '#description' => t('Check content-types if you want they will automatically be created as main page of a book.'),
113 );
114 $form['book_made_simple2']['book_made_simple_auto_main_page'] = array(
115 '#type' => 'checkboxes',
116 '#options' => $types,
117 '#multiple' => true,
118 '#default_value' => variable_get('book_made_simple_auto_main_page', array()),
119 );
120 $form['book_made_simple3'] = array(
121 '#type' => 'fieldset',
122 '#attributes' => array('class' => 'other-settings'),
123 '#title' => t('Other settings'),
124 '#collapsible' => TRUE,
125 '#collapsed' => FALSE,
126 );
127 $form['book_made_simple3']['book_made_simple_admin_use_css'] = array(
128 '#type' => 'checkbox',
129 '#title' => t('Disable BMS settings layout'),
130 '#default_value' => variable_get('book_made_simple_admin_use_css', false),
131 '#description' => t('Check to disable BMS layout (content types as columns).'),
132 );
133 $form['book_made_simple3']['book_made_simple_hide_default_add_child'] = array(
134 '#type' => 'checkbox',
135 '#title' => t('Hide default add child link'),
136 '#default_value' => variable_get('book_made_simple_hide_default_add_child', true),
137 '#description' => t('Check to hide default add child page link.'),
138 );
139 $form['book_made_simple3']['book_made_simple_child_list_style'] = array(
140 '#type' => 'radios',
141 '#title' => t('Style of child list'),
142 '#options' => array("DDLIST" => t("Dropdown listbox"),"LI" => t("Unordered list"),"THEME" => t("Themeable function")),
143 '#default_value' => variable_get('book_made_simple_child_list_style', "DDLIST"),
144 '#description' => t('Choose the style of the child list.'),
145 );
146 $form['book_made_simple3']['book_made_simple_reorder_link'] = array(
147 '#type' => 'radios',
148 '#title' => t('Show reorder tab even if book is empty'),
149 '#options' => array("0"=>t("No"),"1" => t("Yes")),
150 '#default_value' => variable_get('book_made_simple_reorder_link', "0"),
151 '#description' => t('Show reorder tab even if book is empty. A book is considered empty when it has less than 2 subpages. In this case, does reordering make sense ?'),
152 );
153 return system_settings_form($form);
154 }
155
156 /**
157 * Implementation of hook_form_alter().
158 */
159 function book_made_simple_form_alter(&$form, $form_state, $form_id) {
160 if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
161 $node = $form['#node'];
162 $nid = isset($node->nid) ? $node->nid : 'new';
163 if (array_key_exists($form['type']['#value'], variable_get('book_made_simple_forbid_main_page_creation', array())) && $user->uid != 1) {
164 unset($form['book']['bid']['#options'][$nid]);
165 // The following two lines are to prevent that book_manager module sets the unset item again
166 $form['#node']->nid = isset($form['#node']->nid) ? $form['#node']->nid : '0';
167 $form['#node']->book['original_bid'] = $form['#node']->nid;
168 }
169 $book = $node->book;
170 if (user_access("show core Outline links")) {
171 if (_book_outline_access($node)) {
172 if (isBookAccess(null, $node)) {
173 $form['book']['reorder'] = array(
174 '#type' => 'item',
175 '#value' => l(t("Reorder the book"), "admin/content/book/" . $book['bid'] , array('query' =>"destination=node/" . $node->nid)),
176 '#description' => t("Shortcut to reorder the entire book"),
177 '#weight' => 10,
178 );
179 }
180 }
181 }
182 else {$form['book']['#type'] = 'hidden';}
183 return $form;
184 }
185
186 switch ($form["#id"]) {
187 case "node-type-form" : // $type . '_node_settings':
188 $type = $form["#node_type"]->type;
189 $default = array_key_exists($type,variable_get('book_made_simple_auto_main_page',array()));
190 $form['book_made_simple'] = array(
191 '#type' => 'fieldset',
192 '#attributes' => array('class' => 'bms-fieldset'),
193 '#title' => t('BookMadeSimple'),
194 '#collapsible' => TRUE,
195 '#collapsed' => TRUE,
196 );
197 $form['book_made_simple']['1'] = array(
198 '#attributes' => array('class' => 'allowed-child'),
199 '#type' => 'fieldset',
200 '#title' => t('Select content-types allowed as child.'),
201 '#collapsible' => TRUE,
202 '#collapsed' => FALSE,
203 );
204
205 $types = node_get_types("names");
206 $form['book_made_simple']['1']['book_made_simple_for_type'] = array(
207 '#type' => 'checkboxes',
208 '#options' => $types,
209 '#multiple' => true,
210 '#default_value' => variable_get('book_made_simple_for_type_' . $type, array()),
211 );
212
213 $aa = array();
214 unset($types[$type]);
215
216 foreach ($types as $ctype => $name) {
217 $a = variable_get('book_made_simple_for_type_' . $ctype, array());
218 if (array_search($type,$a) !== FALSE) {array_push($aa,$ctype);}
219 }
220 $form['book_made_simple']['2'] = array(
221 '#attributes' => array('class' => 'allowed-child'),
222 '#title' => t('Select content-types allowed as parent'),
223 '#type' => 'fieldset',
224 '#collapsible' => TRUE,
225 '#collapsed' => FALSE,
226 );
227 $form['book_made_simple']['2']['book_made_simple_for_book'] = array(
228 '#type' => 'checkboxes',
229 '#options' => $types,
230 '#multiple' => true,
231 '#default_value' => $aa,
232 );
233
234 $form['book_made_simple']['3'] = array(
235 '#attributes' => array('class' =>'bms-other-settings'),
236 '#type' => 'fieldset',
237 '#collapsible' => TRUE,
238 '#collapsed' => FALSE,
239 );
240
241 $varTypes = variable_get('book_made_simple_auto_main_page',array());
242 $default = (array_key_exists($type, $varTypes) && $varTypes[$type] != "0");
243 $form['book_made_simple']['3']['book_made_simple_auto_main_page'] = array(
244 '#attributes' => array('style' =>'float:none;clear:both'),
245 '#type' => 'checkbox',
246 '#title' => t('Auto create book main page'),
247 '#default_value' => $default,
248 '#description' => t('Checked will create a new book main page when adding.'),);
249
250 $varTypes = variable_get('book_made_simple_forbid_main_page_creation', array());
251 $default = (array_key_exists($type, $varTypes) && $varTypes[$type] != "0");
252 $form['book_made_simple']['3']['book_made_simple_forbid_main_page_creation'] = array(
253 '#type' => 'checkbox',
254 '#title' => t('Forbid creation of book main page'),
255 '#default_value' => $default,
256 '#description' => t('Checked will prevent books from being manually created with this content type as main page type.'),);
257
258 $varTypes = variable_get('book_made_simple_add_types',array());
259 $default = (array_key_exists($type, $varTypes) && $varTypes[$type] != "0");
260 $form['book_made_simple']['3']['book_made_simple_add_types'] = array(
261 '#type' => 'checkbox',
262 '#title' => t('Allow content type as child of default book.'),
263 '#default_value' => $default,
264 '#description' => t('Checked will add this content-type to books where no content-types has been selected.'),
265 );
266
267 array_push($form['#submit'],"book_made_simple_form_submit");
268 break;
269 } // endswitch $form_id
270 } // endfunction book_made_simple_form_alter()
271
272 /**
273 * Implementation of hook_form_submit().
274 */
275 function book_made_simple_form_submit($form, &$form_state) {
276 $values = $form_state["values"];
277 $text = $values["book_made_simple_auto_main_page"];
278 $type = $values["type"];
279 variable_del("book_made_simple_add_types_" . $type);
280 variable_del("book_made_simple_auto_main_page_" . $type);
281 $types = variable_get('book_made_simple_auto_main_page', array());
282 if ($text != "0") {$types[$type]= $type;}
283 else {unset($types[$type]);}
284 variable_set('book_made_simple_auto_main_page', $types);
285
286
287 $text = $values["book_made_simple_forbid_main_page_creation"];
288 $type = $values["type"];
289 variable_del("book_made_simple_forbid_main_page_creation_" . $type);
290 $types = variable_get('book_made_simple_forbid_main_page_creation', array());
291 if ($text != "0") {$types[$type]= $type;}
292 else {unset($types[$type]);}
293 variable_set('book_made_simple_forbid_main_page_creation', $types);
294
295
296 $text = $values["book_made_simple_add_types"];
297 $types = variable_get('book_made_simple_add_types', array());
298 if ($text != "0") {$types[$type]= $type;}
299 else {unset($types[$type]);}
300 variable_set('book_made_simple_add_types', $types);
301
302 $text = $values["book_made_simple_for_type"];
303 if (count($text) == 0) {variable_del("book_made_simple_for_type_" . $type);}
304
305 $types = node_get_types("names");
306 $saved = $values["book_made_simple_for_book"];
307 $a2 = array();
308 foreach ($types as $ctype => $name) {
309 if ($ctype != $type) {
310 $a = variable_get('book_made_simple_for_type_' . $ctype, array());
311 $ind = array_search($type,$a);
312 if ($ind !== FALSE) {unset($a[$ind]);}
313 variable_set('book_made_simple_for_type_' . $ctype, $a);
314 }
315 }
316 foreach ($saved as $ctype => $name) {
317 if ($name != "0") {
318 $a = variable_get('book_made_simple_for_type_' . $ctype, array());
319 $a[$type]= $type;
320 variable_set('book_made_simple_for_type_' . $ctype, $a);
321 }
322 }
323 variable_del("book_made_simple_for_book_" . $type);
324
325 }
326
327 /**
328 * Implementation of hook_link(). Add dropdown listbox to links
329 */
330 function book_made_simple_link($type, $node = null, $teaser = false) {
331 $links = array();
332 if ($type == 'node' && !$teaser && isset($node->book["bid"]) && user_access('add content to books') && $node->status == 1 && $node->book['depth'] < MENU_MAX_DEPTH) {
333 $html = "";
334 $allowedTypes = array();
335 // Search for content-type for this one
336
337 $aTypes = variable_get('book_made_simple_for_type_' . $node->type, array());
338 $style = variable_get('book_made_simple_child_list_style', "DDLIST");
339 // No content-type, so print all
340 if (count($aTypes) == 0) {
341 $aTypes = variable_get('book_made_simple_add_types', array());
342 }
343 foreach ($aTypes as $allowedType => $allowedName) {
344 $type = node_get_types("type",$allowedName);
345 if (node_access('create', $type->type)) {
346 $allowedTypes[str_replace("_","-",$type->type)] = $type->name;
347 }
348 }
349
350
351 $book_link = $node->book;
352 if (count($allowedTypes) > 0) {
353 asort($allowedTypes,SORT_STRING);
354
355 if ($style == "LI") {
356 foreach ($allowedTypes as $type => $name ) {
357 $links['book_add_' . $type] = array(
358 'title' => t('Add !content-type', array('!content-type' => t($name))),
359 'href' => "node/add/". $type,
360 'query' => "parent=". $node->book['mlid'],
361 );
362 }
363 }
364 elseif ($style == "DDLIST") {
365 $links['book_made_simple'] = array(
366 'title' => "<span id='book_made_simple'>" . book_made_simple_add_child_book_content_types_ddlist($allowedTypes, $node) . "</span>",
367 'html'=>true);
368 }
369 else {
370 $links['book_made_simple'] = array(
371 'title' => theme('add_child_book_content_types',$allowedTypes, $node),
372 'html'=>true);
373 }
374 }
375 }
376 $links["book_add_child"] = array();
377 return $links;
378
379 }
380
381 /**
382 * Implementation of hook_theme().
383 */
384 function book_made_simple_theme() {
385 return array(
386 'add_child_book_content_types' => array(
387 'arguments' => array('allowedTypes' => NULL, "node"=>null),
388 ),
389 );
390 }
391
392 /**
393 * Themeable function to display alloawed child types of book().
394 * @param $allowedTypes
395 * Array of allowed child type.
396 * @param $node
397 * Source node
398 * @return
399 * Html code to display allowed child type
400
401 */
402 function theme_add_child_book_content_types($allowedTypes, &$node) {
403 return "";
404 }
405
406 /**
407 * Display alloawed child type as dropdown listbox.
408 */
409 function book_made_simple_add_child_book_content_types_ddlist($allowedTypes, &$node) {
410 $child_type = variable_get('book_child_type', 'book');
411 $html = "<option value='" . str_replace('_', '-', $child_type) . "'>" . t("Add child page") . "</option>";
412 foreach ($allowedTypes as $type => $name ) {
413 $html .= "<option value='" .$type ."'>" . t($name) . "</option>";
414 }
415 $isCleanUrl = variable_get('clean_url', 0);
416 $url = url("node/add/");
417 $op = "&";
418 if ($isCleanUrl) {$op = "?";}
419 $newUrl = $url ."\" + this.value + \"" . $op . "parent=" . $node->book["mlid"];
420 return "<select onchange='location.href=\"" . $newUrl . "\"'>$html</select>";
421
422 }
423 /**
424 * Implementation of hook_nodeapi().
425 */
426 function book_made_simple_nodeapi(&$node, $op, $teaser, $page) {
427 if (empty($node->book["bid"])) {
428 $type = $node->type;
429 switch ($op) {
430 // Node creation
431 case 'insert':
432 if ((user_access('add content to books') || user_access('administer book outlines')) && node_access('create', $type) && $node->book['depth'] < MENU_MAX_DEPTH) {
433 $bookTypes = variable_get('book_made_simple_auto_main_page',array());
434 $bookType = $bookTypes[$type];
435 $toCreate = false;
436 if (null != $bookType && $bookType != "0") {$toCreate = true;}
437 if ($toCreate && ! isset($_GET['parent'])) {
438 $node->book["bid"] = $node->nid;
439 $node->book['nid'] = $node->nid;
440 $node->book['module'] = 'book';
441 $node->book['menu_name'] = book_menu_name($node->book['bid']);
442 _book_update_outline($node);
443 }
444 }
445 break;
446 // Hack for translation module. Adding parent property to automatically select parent book.
447 case "prepare translation":
448 $translation = $node->translation_source;
449 if (isset($translation->book) && (! isset($_GET["parent"]))) {
450 // get parent menu link for the node + language
451 $plid = $translation->book['plid'];
452 $parentPage = book_link_load($plid);
453 if (isset($parentPage) && $parentPage > 0) {
454 $pnid = $parentPage['nid'];
455 if (isset($pnid) && $pnid <> "") {
456 $query= "SELECT bid, mlid FROM {node} n left join {book} b on b.nid = n.nid where tnid= $pnid and language='" . $node->language . "'";
457 $translatedParent = db_fetch_object(db_query($query));
458 if ($translatedParent) {
459 $mlid = $translatedParent->mlid;
460 $params = array();
461 $params[] = "parent=$mlid";
462 foreach ($_GET as $key => $value) {
463 if ($key == "q") {$path = $value;}
464 else {$params[] = $key . "=" . $value;}
465 }
466 $queryString = implode("&",$params);
467 drupal_goto($path, $queryString);
468 $ok = true;
469 }
470 else {
471 drupal_set_message(t("This node has no translated parent, so no book selected"),"warning");
472 }
473 }
474 }
475 }
476 break;
477 }
478 }
479 else {
480 switch ($op) {
481 // Hide standard add Child link
482 case 'alter':
483 if (($node->links) && variable_get('book_made_simple_hide_default_add_child',true)) {
484 if (array_key_exists("book_add_child",$node->links)) {
485 unset($node->links["book_add_child"]);
486 }
487 }
488 break;
489
490 }
491 }
492 }
493
494 function book_made_simple_theme_registry_alter(&$theme_registry) {
495 global $theme;
496
497 $theme_registry['BMS']['menu_item_link'] = $theme_registry['menu_item_link'];
498 // Replace them with our own. These will "preprocess" and call the real functions.
499 $theme_registry['menu_item_link']['function'] = 'book_made_simple_theme_menu_item_link';
500 }
501
502 /**
503 * Preprocessor for menu_item_link.
504 * Adds an ID attribute to menu links and helps the module
505 * follow the recursion of menu_tree_output().
506 */
507
508 function book_made_simple_theme_menu_item_link($link) {
509 // Find out which theme function to dispatch to after preprocessing.
510 $registry = theme_get_registry();
511 if ($link['path'] == 'node/%/outline' && ! user_access("show core Outline links")) {return null;}
512 $function = $registry['BMS']['menu_item_link']['function'];
513 if ($function) {return $function($link);}
514 return theme_menu_item_link($link);
515 }
516

  ViewVC Help
Powered by ViewVC 1.1.2