You are here

panels_sections.module in Panels Sections 6

Same filename and directory in other branches
  1. 6.2 panels_sections.module
  2. 7.3 panels_sections.module

Allows you to define panels_sections of your site and apply s to those panels_sections.

File

panels_sections.module
View source
<?php

/**
 * @file
 * Allows you to define panels_sections of your site and apply s to those panels_sections.
 */

/**
 * Implementation of hook_help().
 */
function panels_sections_help($path, $arg) {
  switch ($path) {
    case 'admin/modules#description':
      return t('Allows you to define sections of your site and specify a Panels Page to take over.');
  }
}

/**
 * Implementation of hook_perm().
 *
 * Since the access to our new custom pages will be granted based on
 * special permissions, we need to define what those permissions are here.
 * This ensures that they are available to enable on the user role
 * administration pages.
 */
function panels_sections_perm() {
  return array(
    'administer panels_sections',
  );
}

/**
 * Implementation of hook_menu().
 */
function panels_sections_menu() {
  $items = array();
  $access = array(
    'administer panels_sections',
  );

  // This is the minimum information you can provide for a menu item.
  $items['admin/panels/panels_sections'] = array(
    'title' => 'Panels Sections',
    'description' => 'Define sections of your site and give them a Panels Page layout.',
    'page callback' => 'panels_sections_list',
    'access arguments' => $access,
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/panels/panels_sections/list'] = array(
    'title' => 'List',
    'page callback' => 'panels_sections_list',
    'access arguments' => $access,
    'weight' => -10,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/panels/panels_sections/add'] = array(
    'title' => 'Add section',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'panels_sections_admin_settings_form',
    ),
    'access arguments' => $access,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/panels/panels_sections/edit/%panels_sections_section'] = array(
    'title' => 'Edit section',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'panels_sections_admin_settings_form',
      4,
    ),
    'access arguments' => $access,
    'type' => MENU_CALLBACK,
  );
  $items['admin/panels/panels_sections/delete/%panels_sections_section'] = array(
    'title' => 'Edit section',
    'page callback' => '_panels_sections_delete',
    'page arguments' => array(
      4,
    ),
    'access arguments' => $access,
    'type' => MENU_CALLBACK,
  );
  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 panels_sections_list() {
  $header = array(
    t('Section'),
    t('Status'),
    t('Panels Page'),
    t('Weight'),
    array(
      'data' => t('Operations'),
    ),
  );
  $rows = array();
  foreach (_panels_sections_load() as $section) {
    $rows[] = array(
      $section->name,
      $section->status ? t('Enabled') : t('Disabled'),
      $section->panels_page_name,
      $section->weight,
      l(t('Edit'), 'admin/panels/panels_sections/edit/' . $section->sid) . ' · ' . l(t('Delete'), 'admin/panels/panels_sections/delete/' . $section->sid),
    );
  }
  $output = theme('table', $header, $rows);
  return $output;
}
function panels_sections_admin_settings_form($form_state, $section = NULL) {
  module_load_include('.inc', 'panels_page', 'panels_page.read');
  if (!is_object($section)) {
    $section = new stdClass();
  }
  $access = user_access('use PHP for block visibility');
  $form = array();
  $options = array(
    t('Take over all pages except the listed pages.'),
    t('Take over 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' => '<front>',
  ));
  if ($access) {
    $options[] = t('Take over if the following PHP code returns <code>TRUE</code> (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' => '<?php ?>',
    ));
  }
  $form['section_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Section settings'),
  );
  $form['section_settings']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $section->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' => $section->visibility ? $section->visibility : 0,
  );
  $form['section_settings']['path'] = array(
    '#type' => 'textarea',
    '#title' => t('Pages'),
    '#default_value' => $section->path,
    '#cols' => 40,
    '#rows' => 5,
    '#description' => $description,
  );
  $form['section_settings']['status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enabled'),
    '#default_value' => $section->status,
    '#description' => t('Enable or disable this section'),
  );
  $form['section_settings']['panels_page'] = array(
    '#type' => 'select',
    '#title' => t('Select Panels Page'),
    '#default_value' => $section->panels_page,
    '#options' => _panels_sections_panels_page_select(),
    '#description' => t('Select the Panels Page you want to use for this section'),
  );
  $form['section_settings']['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight'),
    '#default_value' => $section->weight,
  );
  if ($section->sid) {

    //we are updating
    drupal_set_title(t('Edit section %name', array(
      '%name' => $section->name,
    )));
    $form['sid'] = array(
      '#type' => 'hidden',
      '#value' => $section->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 panels_sections_admin_settings_form_submit($form, &$form_state) {
  switch ($form_state['values']['op']) {
    case t('Save section'):
      db_query("UPDATE {panels_sections_data} SET name = '%s', status = %d, visibility = %d, panels_page = '%s', path = '%s', weight = %d WHERE sid = %d", $form_state['values']['name'], $form_state['values']['status'], $form_state['values']['visibility'], $form_state['values']['panels_page'], $form_state['values']['path'], $form_state['values']['weight'], $form_state['values']['sid']);
      drupal_set_message(t('The panels_sections configuration has been saved.'));
      cache_clear_all();
      menu_rebuild();
      drupal_goto('admin/panels/panels_sections');
      break;
    case t('Add section'):
      db_query("INSERT INTO {panels_sections_data} (name, status, visibility, path, panels_page, weight) VALUES ('%s', %d, %d, '%s', '%s', %d)", $form_state['values']['name'], $form_state['values']['status'], $form_state['values']['visibility'], $form_state['values']['path'], $form_state['values']['panels_page'], $form_state['values']['weight']);
      drupal_set_message(t('The panels_sections configuration has been saved.'));
      cache_clear_all();
      menu_rebuild();
      drupal_goto('admin/panels/panels_sections');
      break;
  }
}

/**
 * delete a section
 *
 * @param object $section
 */
function _panels_sections_delete($section) {
  $op = $_POST['op'];
  switch ($op) {
    case t('Delete'):
      db_query("DELETE FROM {panels_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/panels/panels_sections');
      break;
    default:
      $output = drupal_get_form('_panels_sections_delete_confirm', $section);
      break;
  }
  return $output;
}

/**
 * confirm the deletion of a section
 *
 * @param $form_state
 * @param object $section
 */
function _panels_sections_delete_confirm(&$form_state, $section) {
  return confirm_form(array(), t('Delete section %name', array(
    '%name' => $section->name,
  )), 'admin/panels/panels_sections', '<p>' . t('Are you sure you want to delete the section %name?', array(
    '%name' => $section->name,
  )) . '</p>', t('Delete'), t('Cancel'));
}

/**
 * load and return all sections
 */
function _panels_sections_load() {
  $panels_sections = array();
  $res = db_query("SELECT ps.sid, ps.name, ps.status, ps.path, ps.panels_page, pp.name as panels_page_name, ps.visibility, ps.weight FROM {panels_sections_data} as ps JOIN {panels_page} as pp ON pp.pid = ps.panels_page");
  while ($row = db_fetch_object($res)) {
    $panels_sections[] = $row;
  }
  return $panels_sections;
}

/**
 * load and return a section object given an $sid
 */
function panels_sections_section_load($sid) {
  static $sections = array();
  if (!key_exists($sid, $sections)) {
    $result = db_query('SELECT * FROM {panels_sections_data} WHERE sid = %d', $sid);
    $section = db_fetch_object($result);
    $sections[$sid] = $section;
  }
  return $sections[$sid];
}

/**
 * Loads the options for the Panels Page select form element
 */
function _panels_sections_panels_page_select() {
  module_load_include('inc', 'panels_page', 'panels_page.read');
  $panels_pages = panels_page_load_all();
  $options = array();
  foreach ($panels_pages as $item) {
    $name = $item->name;
    $pid = $item->pid;
    $options[$pid] = $name;
  }
  return $options;
}

/**
 * An API for modules that want to know about panels_sections.
 *
 * This API is a function that lets you find out about sections.
 *
 * @param
 *  Optional $section 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 panels_sections_in_section($section = NULL) {
  $output = FALSE;
  if (is_string($section)) {

    // caller wants to know if shes in the section she provided.
    if ($section == panels_sections_in_section()) {
      $output = TRUE;
    }
  }
  else {

    // caller wants to know in which section she is.
    $res = db_query("SELECT sid, name, path, status, visibility, panels_page, weight FROM {panels_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)/',
          '/\\\\\\*/',
          '/(^|\\|)\\\\<front\\\\>($|\\|)/',
        ), 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;
}

/**
 * API to allow you to get a text string back with the
 * name of the current section, or returns TRUE/FALSE if 
 * the section name passed in is the current section
 * 
 * This is the functionality I thought panels_sections_insection should
 * provide, rather than returning an object with everything but the
 * section name. I wrote this function leveraging rather than rewriting 
 * panels_sections_in_section.
 *
 * @param
 *  Optional $section_to_match a string containing the section name
 *  you want to test against
 *
 * @return
 *   Depends on the parameter.
 *   If you do not give $section, it will return the section name, if found.
 *   If you give $section, it will return TRUE if you are in that section
 *   Otherwise it will return FALSE
 */
function panels_sections_check_section($section_to_match = NULL) {

  //get section object
  $section_row = panels_sections_in_section();

  //get section from object
  $section = $section_row->name;
  if (is_string($section_to_match)) {

    // caller wants to know if shes in the section she provided.
    if ($section == $section_to_match) {
      $return_value = TRUE;
    }
    else {
      $return_value = FALSE;
    }
  }
  else {

    //else return the section name
    $return_value = $section;
  }
  return $return_value;
}

/**
 * Implementation of hook_block
 * - provides Panels Content Placeholder (%CONTENT%) block for panels stuffing
 */
function panels_sections_block($op = 'list', $delta = 0) {

  //emspace_profile_css_insert();
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Panels Content Placeholder');
      return $blocks;
    case 'view':
      if ($delta == 0) {
        $block['subject'] = t('Panels Content Placeholder');
        $block['content'] = '%CONTENT%';
        return $block;
      }
  }
}

/**
 * This function must be called once from template.php
 * it will determine which panel to use based by querying panels_page
 * and panels_sections, then load the correct panel and do some text replacing
 * to convert %CONTENT% to the page content so you can use panels on non-node pages 
 *
 * @param unknown_type $vars
 */
function panels_sections_takeover(&$vars, $debug = FALSE) {

  /*
   * Panels management
   */

  //ask panels_page if there is a page specific panels_page for this path
  $current_panel = panels_page_get_current();
  if (isset($current_panel->pid)) {
    $msg = 'Page specific';

    //$panel = $vars['content'];
    $panel = panels_page_view_page($current_panel->pid, false, isset($vars['node']) ? $vars['node']->nid : null);
  }
  else {

    //query panels_section module for correct section
    $panels_section = panels_sections_in_section();

    //if panels_section returns a panels_page, take over current page
    if ($panels_section->panels_page) {
      $msg = 'Panel: ' . $panels_section->name;
      $current_panel = panels_page_load($panels_section->panels_page);
      $panel = panels_page_view_page($current_panel, false, isset($vars['node']) ? $vars['node']->nid : null);
    }
  }
  $vars['panel_current'] = $current_panel->name;
  $vars['panel_edit'] = l($current_panel->name, 'admin/panels/panel-page/' . $current_panel->name . '/edit/content');

  //output the message so I know what's going on
  if ($debug) {
    dpr($msg);
  }
  $in_panel = '';
  if (strpos('x' . $panel, '%CONTENT%')) {

    // Build up some content
    if (isset($vars['help'])) {
      if (strlen($vars['help']) > 35) {

        // Yes josh you hate hard-coding, me too.
        $in_panel .= $vars['help'];
      }
      $vars['help'] = '';
    }
    if (isset($vars['messages'])) {
      $in_panel .= $vars['messages'];
      $vars['messages'] = '';
    }
    if (isset($vars['title'])) {
      drupal_set_title($vars['node']->title);
      if ($vars['title'] == 'Empty' && isset($vars['node'])) {

        // Who knows where this problem started... ??
        $vars['title'] = $vars['node']->title;
      }
      $in_panel .= '<h1 class="title">' . $vars['title'] . '</h1>';
      $vars['title'] = '';
    }
    if (isset($vars['tabs'])) {
      if (!user_access('view simplemenu')) {

        // Only show tabs if the user doesn't have simplemenu - tabs are pushed into 'current page'
        $in_panel .= '<div class="tabs">' . $vars['tabs'] . '</div>';
      }

      //      $vars['tabs'] = '';
    }
    if (isset($vars['node'])) {

      // wrap a nice node type div for dan
      $vars['content'] = "\n" . '<div class="dan-rocks panel-pane-' . $vars['node']->type . '">' . $vars['content'] . "</div>\n";
    }
    $in_panel .= $vars['content'];
    $vars['content'] = '';
    $panel = str_replace('%CONTENT%', $in_panel, $panel);
  }

  //stuff the panel
  $vars['content'] = $panel;
}

Functions

Namesort descending Description
panels_sections_admin_settings_form
panels_sections_admin_settings_form_submit
panels_sections_block Implementation of hook_block
panels_sections_check_section API to allow you to get a text string back with the name of the current section, or returns TRUE/FALSE if the section name passed in is the current section
panels_sections_help Implementation of hook_help().
panels_sections_in_section An API for modules that want to know about panels_sections.
panels_sections_list Declare administrative settings for a module.
panels_sections_menu Implementation of hook_menu().
panels_sections_perm Implementation of hook_perm().
panels_sections_section_load load and return a section object given an $sid
panels_sections_takeover This function must be called once from template.php it will determine which panel to use based by querying panels_page and panels_sections, then load the correct panel and do some text replacing to convert %CONTENT% to the page content so you can use…
_panels_sections_delete delete a section
_panels_sections_delete_confirm confirm the deletion of a section
_panels_sections_load load and return all sections
_panels_sections_panels_page_select Loads the options for the Panels Page select form element