1,293c1,293 < array('name' => t('bookexpand'), 'base' => 'bookexpand')); < } < < /** < * Implementation of hook_help(). < */ < function bookexpand_help($section) { < switch ($section) { < case 'admin/help#bookexpand': < $output = '

'. t('The book expand module allows admin users to restrict to which books their users are allowed to add pages.') .'

'; < $output .= '

'. t('The current choices are restricting by role, by user, and by Organic Group.') .'

'; < return $output; < case 'admin/modules#description': < return t('The book expand module allows admin users to restrict to which books their users are allowed to pages.'); < //case 'admin/settings/bookexpand': < // return t('Select the filter you want to use. Depending on your choice, this is how the parent pages of books will be selected in the drop down options. Note that the Private filter means that nodes that are marked as private will ONLY be visible in the tree structure by the author and required the Private module is installed.'); < } < } < < /** < * Implementation of hook_menu < */ < function bookexpand_menu($may_cache) { < if ($may_cache) { < $items[] = array( < 'path' => 'admin/settings/bookexpand', < 'title' => t('Book Expansion'), < 'description' => t('Describes what the settings generally do.'), < 'callback' => 'bookexpand_admin_settings', < 'access' => user_access('administer site configuration'), < 'type' => MENU_NORMAL_ITEM, < ); < } < return $items; < } < < /** < * Implementation of hook_settings < */ < function bookexpand_admin_settings() { < return drupal_get_form('bookexpand_admin_settings_form', $form); < } < < function bookexpand_admin_settings_form () { < $options = array ('None', 'Role', 'Group', 'User', 'Private'); < $form['bookexpand_filter'] = array( < '#type' => 'select', < '#title' => t('Pick Filter Type'), < '#default_value' => variable_get('bookexpand_filter', 0), < '#options' => $options, < '#description' => t('Pick which filter you wish to use on the available parent book pages') < ); < return system_settings_form($form); < } < < /** < * Implementation of hook_form_alter < * < */ < function bookexpand_form_alter ($form_id, &$form) { < if ($form_id == "book_node_form") { < if (isset($form['parent'])) { < switch (variable_get('bookexpand_filter', 0)) { < case 0: < $form['parent'] = array( < '#type' => 'select', < '#title' => t('Parent'), < '#default_value' => ($node->parent ? $node->parent : arg(4)), < '#options' => book_toc($node->nid), < '#weight' => -4, < '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is <top-level> is an independent, top-level book.') : t('The parent that this page belongs in.'), < ); < break; < case 1: < $form['parent'] = array( < '#type' => 'select', < '#title' => t('Parent'), < '#default_value' => ($node->parent ? $node->parent : arg(4)), < '#options' => bookexpand_toc_role($node->nid), < '#weight' => -4, < '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is <top-level> is an independent, top-level book.') : t('The parent that this page belongs in.'), < ); < break; < case 2: < $form['parent'] = array( < '#type' => 'select', < '#title' => t('Parent'), < '#default_value' => ($node->parent ? $node->parent : arg(4)), < '#options' => bookexpand_toc_group($node->nid), < '#weight' => -4, < '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is <top-level> is an independent, top-level book.') : t('The parent that this page belongs in.'), < ); < break; < case 3: < $form['parent'] = array( < '#type' => 'select', < '#title' => t('Parent'), < '#default_value' => ($node->parent ? $node->parent : arg(4)), < '#options' => bookexpand_toc_user($node->nid), < '#weight' => -4, < '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is <top-level> is an independent, top-level book.') : t('The parent that this page belongs in.'), < ); < case 4: < $form['parent'] = array( < '#type' => 'select', < '#title' => t('Parent'), < '#default_value' => ($node->parent ? $node->parent : arg(4)), < '#options' => bookexpand_toc_private($node->nid), < '#weight' => -4, < '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is <top-level> is an independent, top-level book.') : t('The parent that this page belongs in.'), < ); < } < } < } < } < < /** < * Implementation of hook_nodeapi(). < * < * Automatically creates a corresponding top level book page each time an organic group is added. < */ < function bookexpand_nodeapi($group_node, $op, $teaser = NULL) { < switch ($op) { < case 'insert': < if ($group_node->type == 'og') { < $og_public = og_get_visibility_default(); < $node = array('title' => $group_node->title. ' '. t('Handbook'), < 'uid' => $group_node->uid, < 'type' => 'book', < 'status' => 1, < 'og_groups' => array($group_node->nid => 1), < 'og_public' => $og_public, < 'log' => t('automatically created for use within the \'%name\' group', array('%name' => $group_node->title)), < 'parent' => 0, < ); < if ($node = node_submit($node)) { < node_save($node); < } < $sql = "INSERT INTO {og_book} (og_nid, book_nid) VALUES (%d, %d)"; < db_query($sql, $group_node->nid, $node->nid); < } < break; < case 'delete': < if ($node->type == 'og') { < $sql = "DELETE FROM {og_book} WHERE og_nid = %d"; < db_query($sql, $node->nid); < } < break; < } < } < < /* < * Changes the query to match the chosen filter - This one is by Role < **/ < function bookexpand_toc_role ($exclude = 0) { < global $user; < $uid = $user->uid; < $rid = array_keys($user->roles); < < $r = '('.implode(",", $rid).')'; < < if ($uid==1) { < $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title'; < } else { < $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM ({node} n INNER JOIN {users_roles} u ON n.uid = u.uid) INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND u.rid IN '.$r.' ORDER BY b.weight, n.title'; < } < < $output = bookexpand_toc($query, $exclude); < return $output; < } < < /* < * Changes the query to match the chosen filter - This one is by Group < **/ < function bookexpand_toc_group ($exclude = 0) { < < $request = $_SERVER['REQUEST_URI']; < preg_match('/edit\[og_groups\]\[\]\=(.*)/', $request, $match); < < global $user; < $uid = $user->uid; < $group = $user->og_groups; < $gid = array_keys($group); < < if (($uid != 1)&&(module_exist('og'))) { < if ($match[1]) { < $output = og_book_get_options($match[1]); < } else { < $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title'; < $output = bookexpand_toc($query, $exclude); < } < } else { < $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title'; < $output = bookexpand_toc($query, $exclude); < } < < return $output; < } < < function og_book_get_options($gid, $exclude = 0) { < $root_nid = og_book_get_root($gid); < < $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 ORDER BY b.weight, n.title'; < < $toc = bookexpand_toc($query, $exclude, $root_nid); < < return $toc; < } < < function og_book_get_root($gid) { < $sql = "SELECT book_nid FROM {og_book} WHERE og_nid = %d"; < return db_result(db_query($sql, $gid)); < } < < /* < * Changes the query to match the chosen filter - This one is by User < **/ < function bookexpand_toc_user ($nid) { < global $user; < $uid = $user->uid; < < if (($uid != 1)&&(module_exist('private'))) { < $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid INNER JOIN {private} p ON n.nid = p.nid WHERE n.status = 1 AND n.uid = '.$uid.' AND p.private < 1 ORDER BY b.weight, n.title'; < } else { < $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title'; < } < $exclude = 0; < $output = bookexpand_toc($query, $exclude); < < return $output; < } < < /* < * Changes the query to match the chosen filter - This one is by User < **/ < function bookexpand_toc_private($nid) { < global $user; < $uid = $user->uid; < < if ($uid != 1) { < $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid INNER JOIN {private} p ON n.nid = p.nid WHERE n.status = 1 AND p.private <> 1 ORDER BY b.weight, n.title'; < } else { < // User 1 can see all choices. < $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title'; < } < < $output = bookexpand_toc($query, $exclude); < < return $output; < } < < /** < * Returns an array of titles and nid entries of book pages in table of contents order by query. < */ < function bookexpand_toc($q, $exclude = 0, $nid = 0) { < $result = db_query(db_rewrite_sql($q)); < < while ($node = db_fetch_object($result)) { < if (!$children[$node->parent]) { < $children[$node->parent] = array(); < } < $children[$node->parent][] = $node; < } < < if ($nid) { < $sql = "SELECT n.title FROM {node} n WHERE n.nid = %d"; < $toc = array(); < $toc[$nid] = db_result(db_query($sql, $nid)); < } else { < $toc = array(); < // If the user has permission to create new books, add the top-level book page to the menu; < if (user_access('create new books')) { < $toc[0] = '<'. t('top-level') .'>'; < } < } < < $toc = book_toc_recurse($nid, '--', $toc, $children, $exclude); < < return $toc; < } < --- > // $Id: bookexpand.module,v 1.0 2007/01/14 11:44 dwees Exp $ > > /** > * @file > * Alters the book creation form to restrict the allowed pages to the current Organize Group, user role, or user ID. > */ > > /** > * Implementation of hook_node_info(). > */ > function bookexpand_info() { > return array('bookexpand' => array('name' => t('bookexpand'), 'base' => 'bookexpand')); > } > > /** > * Implementation of hook_help(). > */ > function bookexpand_help($section) { > switch ($section) { > case 'admin/help#bookexpand': > $output = '

'. t('The book expand module allows admin users to restrict to which books their users are allowed to add pages.') .'

'; > $output .= '

'. t('The current choices are restricting by role, by user, and by Organic Group.') .'

'; > return $output; > case 'admin/modules#description': > return t('The book expand module allows admin users to restrict to which books their users are allowed to pages.'); > //case 'admin/settings/bookexpand': > // return t('Select the filter you want to use. Depending on your choice, this is how the parent pages of books will be selected in the drop down options. Note that the Private filter means that nodes that are marked as private will ONLY be visible in the tree structure by the author and required the Private module is installed.'); > } > } > > /** > * Implementation of hook_menu > */ > function bookexpand_menu($may_cache) { > if ($may_cache) { > $items[] = array( > 'path' => 'admin/settings/bookexpand', > 'title' => t('Book Expansion'), > 'description' => t('Describes what the settings generally do.'), > 'callback' => 'bookexpand_admin_settings', > 'access' => user_access('administer site configuration'), > 'type' => MENU_NORMAL_ITEM, > ); > } > return $items; > } > > /** > * Implementation of hook_settings > */ > function bookexpand_admin_settings() { > return drupal_get_form('bookexpand_admin_settings_form', $form); > } > > function bookexpand_admin_settings_form () { > $options = array ('None', 'Role', 'Group', 'User', 'Private'); > $form['bookexpand_filter'] = array( > '#type' => 'select', > '#title' => t('Pick Filter Type'), > '#default_value' => variable_get('bookexpand_filter', 0), > '#options' => $options, > '#description' => t('Pick which filter you wish to use on the available parent book pages') > ); > return system_settings_form($form); > } > > /** > * Implementation of hook_form_alter > * > */ > function bookexpand_form_alter ($form_id, &$form) { > if ($form_id == "book_node_form") { > if (isset($form['parent'])) { > switch (variable_get('bookexpand_filter', 0)) { > case 0: > $form['parent'] = array( > '#type' => 'select', > '#title' => t('Parent'), > '#default_value' => ($node->parent ? $node->parent : arg(4)), > '#options' => book_toc($node->nid), > '#weight' => -4, > '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is <top-level> is an independent, top-level book.') : t('The parent that this page belongs in.'), > ); > break; > case 1: > $form['parent'] = array( > '#type' => 'select', > '#title' => t('Parent'), > '#default_value' => ($node->parent ? $node->parent : arg(4)), > '#options' => bookexpand_toc_role($node->nid), > '#weight' => -4, > '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is <top-level> is an independent, top-level book.') : t('The parent that this page belongs in.'), > ); > break; > case 2: > $form['parent'] = array( > '#type' => 'select', > '#title' => t('Parent'), > '#default_value' => ($node->parent ? $node->parent : arg(4)), > '#options' => bookexpand_toc_group($node->nid), > '#weight' => -4, > '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is <top-level> is an independent, top-level book.') : t('The parent that this page belongs in.'), > ); > break; > case 3: > $form['parent'] = array( > '#type' => 'select', > '#title' => t('Parent'), > '#default_value' => ($node->parent ? $node->parent : arg(4)), > '#options' => bookexpand_toc_user($node->nid), > '#weight' => -4, > '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is <top-level> is an independent, top-level book.') : t('The parent that this page belongs in.'), > ); > case 4: > $form['parent'] = array( > '#type' => 'select', > '#title' => t('Parent'), > '#default_value' => ($node->parent ? $node->parent : arg(4)), > '#options' => bookexpand_toc_private($node->nid), > '#weight' => -4, > '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is <top-level> is an independent, top-level book.') : t('The parent that this page belongs in.'), > ); > } > } > } > } > > /** > * Implementation of hook_nodeapi(). > * > * Automatically creates a corresponding top level book page each time an organic group is added. > */ > function bookexpand_nodeapi($group_node, $op, $teaser = NULL) { > switch ($op) { > case 'insert': > if ($group_node->type == 'og') { > $og_public = og_get_visibility_default(); > $node = array('title' => $group_node->title. ' '. t('Handbook'), > 'uid' => $group_node->uid, > 'type' => 'book', > 'status' => 1, > 'og_groups' => array($group_node->nid => 1), > 'og_public' => $og_public, > 'log' => t('automatically created for use within the \'%name\' group', array('%name' => $group_node->title)), > 'parent' => 0, > ); > if ($node = node_submit($node)) { > node_save($node); > } > $sql = "INSERT INTO {og_book} (og_nid, book_nid) VALUES (%d, %d)"; > db_query($sql, $group_node->nid, $node->nid); > } > break; > case 'delete': > if ($node->type == 'og') { > $sql = "DELETE FROM {og_book} WHERE og_nid = %d"; > db_query($sql, $node->nid); > } > break; > } > } > > /* > * Changes the query to match the chosen filter - This one is by Role > **/ > function bookexpand_toc_role ($exclude = 0) { > global $user; > $uid = $user->uid; > $rid = array_keys($user->roles); > > $r = '('.implode(",", $rid).')'; > > if ($uid==1) { > $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title'; > } else { > $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM ({node} n INNER JOIN {users_roles} u ON n.uid = u.uid) INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND u.rid IN '.$r.' ORDER BY b.weight, n.title'; > } > > $output = bookexpand_toc($query, $exclude); > return $output; > } > > /* > * Changes the query to match the chosen filter - This one is by Group > **/ > function bookexpand_toc_group ($exclude = 0) { > > $request = $_SERVER['REQUEST_URI']; > preg_match('/edit\[og_groups\]\[\]\=(.*)/', $request, $match); > > global $user; > $uid = $user->uid; > $group = $user->og_groups; > $gid = array_keys($group); > > if (($uid != 1)&&(module_exist('og'))) { > if ($match[1]) { > $output = og_book_get_options($match[1]); > } else { > $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title'; > $output = bookexpand_toc($query, $exclude); > } > } else { > $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title'; > $output = bookexpand_toc($query, $exclude); > } > > return $output; > } > > function og_book_get_options($gid, $exclude = 0) { > $root_nid = og_book_get_root($gid); > > $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 ORDER BY b.weight, n.title'; > > $toc = bookexpand_toc($query, $exclude, $root_nid); > > return $toc; > } > > function og_book_get_root($gid) { > $sql = "SELECT book_nid FROM {og_book} WHERE og_nid = %d"; > return db_result(db_query($sql, $gid)); > } > > /* > * Changes the query to match the chosen filter - This one is by User > **/ > function bookexpand_toc_user ($nid) { > global $user; > $uid = $user->uid; > > if ($uid != 1) { > $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND n.uid = '.$uid.' ORDER BY b.weight, n.title'; > } else { > $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title'; > } > $exclude = 0; > $output = bookexpand_toc($query, $exclude); > > return $output; > } > > /* > * Changes the query to match the chosen filter - This one is by User > **/ > function bookexpand_toc_private($nid) { > global $user; > $uid = $user->uid; > > if if (($uid != 1)&&(module_exist('private'))) { > $query = "SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid INNER JOIN {private} p ON n.nid = p.nid WHERE n.status = 1 AND p.private <> 1 OR p.nid = ANY (SELECT no.nid FROM {node} no WHERE no.uid = $uid) ORDER BY b.weight, n.title"; > } else { > // User 1 can see all choices. > $query = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title'; > } > > $output = bookexpand_toc($query, $exclude); > > return $output; > } > > /** > * Returns an array of titles and nid entries of book pages in table of contents order by query. > */ > function bookexpand_toc($q, $exclude = 0, $nid = 0) { > $result = db_query(db_rewrite_sql($q)); > > while ($node = db_fetch_object($result)) { > if (!$children[$node->parent]) { > $children[$node->parent] = array(); > } > $children[$node->parent][] = $node; > } > > if ($nid) { > $sql = "SELECT n.title FROM {node} n WHERE n.nid = %d"; > $toc = array(); > $toc[$nid] = db_result(db_query($sql, $nid)); > } else { > $toc = array(); > // If the user has permission to create new books, add the top-level book page to the menu; > if (user_access('create new books')) { > $toc[0] = '<'. t('top-level') .'>'; > } > } > > $toc = book_toc_recurse($nid, '--', $toc, $children, $exclude); > > return $toc; > } >