'admin/build/sections', 'title' => t('Sections'), 'description' => t('Define sections of your site and apply themes to them.'), 'callback' => 'sections_list', 'access' => $access ); $items[] = array( 'path' => 'admin/build/sections/list', 'title' => t('List'), 'callback' => 'sections_list', 'access' => $access, 'weight' => -10, 'type' => MENU_DEFAULT_LOCAL_TASK ); $items[] = array( 'path' => 'admin/build/sections/add', 'title' => t('Add section'), 'callback' => 'drupal_get_form', 'callback arguments' => array('sections_admin_settings_form'), 'access' => $access, 'type' => MENU_LOCAL_TASK ); foreach (_sections_load() as $section) { $items[] = array( 'path' => 'admin/build/sections/edit/'. $section->sid, 'title' => t('Edit section'), 'callback' => 'drupal_get_form', 'callback arguments' => array('sections_admin_settings_form', NULL, $section), 'access' => $access, 'type' => MENU_CALLBACK ); $items[] = array( 'path' => 'admin/build/sections/delete/'. $section->sid, 'title' => t('Delete section'), 'callback' => '_sections_delete', 'callback arguments' => array($section), 'access' => $access, 'type' => MENU_CALLBACK ); } } else if ($section = sections_in_section()) { // only switch to custom theme if theme is active, to prohibit a destroyed site foreach (list_themes() as $key => $theme) { if ($theme->status == 1 && $theme->name == $section->theme) { global $custom_theme; $custom_theme = $section->theme; } } } return $items; } /** * Declare administrative settings for a module. * * This hook provides an administrative interface for controlling various * settings for this module. A menu item for the module under "site * configuration > modules" will appear in the administrative menu when * this hook is implemented. * * @return * An HTML string containing form items to place on the module settings * page. * * The form items defined on the settings page will be saved with * variable_set(), and can be later retrieved with variable_get(). If you * need to store more complicated data (for example, in a separate table), * define your own administration page and link to it using hook_menu(). */ function sections_list() { $header = array( t('Section'), t('Status'), t('Theme'), t('Weight'), array('data' => t('Operations')) ); $rows = array(); foreach (_sections_load() as $section) { $rows[] = array( $section->name, $section->status ? t('Enabled') : t('Disabled'), $section->theme, $section->weight, l(t('Edit'), 'admin/build/sections/edit/'. $section->sid) .' ยท '. l(t('Delete'), 'admin/build/sections/delete/'. $section->sid) ); } $output = theme('table', $header, $rows); return $output; } function sections_admin_settings_form($edit = NULL, $section = NULL) { if ($section) { $edit = (array)$section; } $access = user_access('use PHP for block visibility'); $form = array(); $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.')); $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '')); if ($access) { $options[] = t('Show if the following PHP code returns TRUE (PHP-mode, experts only).'); $description .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '')); } $form['section_settings'] = array( '#type' => 'fieldset', '#title' => t('Section settings') ); $form['section_settings']['name'] = array( '#type' => 'textfield', '#title' => t('Name'), '#default_value' => $edit['name'], '#size' => 40, '#maxlength' => 64, '#description' => t('Give the name of the section') ); $form['section_settings']['visibility'] = array( '#type' => 'radios', '#title' => t('Activate section on the specific pages'), '#options' => $options, '#default_value' => $edit['visibility'] ? $edit['visibility'] : 0 ); $form['section_settings']['path'] = array( '#type' => 'textarea', '#title' => t('Pages'), '#default_value' => $edit['path'], '#cols' => 40, '#rows' => 5, '#description' => $description ); $form['section_settings']['status'] = array( '#type' => 'checkbox', '#title' => t('Enabled'), '#default_value' => $edit['status'], '#description' => t('Enable or disable this section') ); $form['section_settings']['theme'] = array( '#type' => 'select', '#title' => t('Select theme'), '#default_value' => $edit['theme'], '#options' => _sections_theme_select(), '#description' => t('Select the theme you want to use for this section') ); $form['section_settings']['weight'] = array( '#type' => 'weight', '#title' => t('Weight'), '#default_value' => $edit['weight'] ); if ($edit['sid']) { //we are updating drupal_set_title(t('Edit section %name', array('%name' => $section->name))); $form['sid'] = array('#type' => 'hidden', '#value' => $edit['sid']); $form['submit'] = array('#type' => 'submit', '#value' => t('Save section')); } else { //we are adding drupal_set_title(t('Add section')); $form['submit'] = array('#type' => 'submit', '#value' => t('Add section')); } return $form; } function sections_admin_settings_form_submit($id, $edit) { switch ($_POST['op']) { case t('Save section'): db_query("UPDATE {sections_data} SET name = '%s', status = %d, visibility = %d, theme = '%s', path = '%s', weight = %d WHERE sid = %d", $edit['name'], $edit['status'], $edit['visibility'], $edit['theme'], $edit['path'], $edit['weight'], $edit['sid']); drupal_set_message(t('The sections configuration has been saved.')); cache_clear_all(); menu_rebuild(); drupal_goto('admin/build/sections'); break; case t('Add section'): db_query("INSERT INTO {sections_data} (name, status, visibility, path, theme, weight) VALUES ('%s', %d, %d, '%s', '%s', %d)", $edit['name'], $edit['status'], $edit['visibility'], $edit['path'], $edit['theme'], $edit['weight']); drupal_set_message(t('The sections configuration has been saved.')); cache_clear_all(); menu_rebuild(); drupal_goto('admin/build/sections'); break; } } function _sections_delete($section) { $op = $_POST['op']; switch ($op) { case t('Delete'): db_query("DELETE FROM {sections_data} WHERE sid = %d", $section->sid); drupal_set_message(t('The section %name has been deleted.', array('%name' => $section->name))); cache_clear_all(); drupal_goto('admin/build/sections'); break; default: $output = drupal_get_form('_sections_delete_confirm', $section); break; } return $output; } function _sections_delete_confirm($section) { return confirm_form( array(), t('Delete section %name', array('%name' => $section->name)), 'admin/build/sections', '

'. t('Are you sure you want to delete the section %name?', array('%name' => $section->name)) .'

', t('Delete'), t('Cancel') ); } function _sections_load() { $sections = array(); $res = db_query("SELECT * FROM {sections_data}"); while ($row = db_fetch_object($res)) { $sections[] = $row; } return $sections; } /** * Loads the section names */ function _section_load($section_name) { return db_fetch_object(db_query("SELECT * FROM {sections_data} WHERE name = '%s' ORDER BY weight", $section_name)); } /** * Loads the options for the themes select form element */ function _sections_theme_select() { $themes = list_themes(); $options = array(); foreach (array_keys($themes) as $name) { $options[$name] = $name; } return $options; } /** * An API for modules that want to know about sections. * * This API is a function that lets you find out about settings. * * @param * Optional $setting a string containing the section you wnat to test against. * * @return * Depends on the parameter. * If you do not give $section, it will return the section object, if found. * If you give $section, it will return TRUE if you are in that section * Otherwise it will return FALSE */ function sections_in_section($section = NULL) { $output = FALSE; if (is_string($section)) { // caller wants to know if shes in the section she provided. if($section == sections_in_section()) { $output = TRUE; } } else { // caller wants to know in which section she is. $res = db_query("SELECT path, status, visibility, theme, weight FROM {sections_data} WHERE status=1 ORDER BY weight"); while ($row = db_fetch_object($res)) { if ($row->visibility == 1) { // This bizarre bit of magic courtesy of block.module $path = drupal_get_path_alias($_GET['q']); $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($row->path, '/')) .')$/'; if (preg_match($regexp, $path) && !($path == 'admin/block')) { $output = $row; } } else { if (drupal_eval($row->path)) { $output = $row; } } } } return $output; }