Skip to content
panels.module 68.8 KiB
Newer Older
Jakob Perry's avatar
Jakob Perry committed
 * @file
 * Core functionality for the Panels engine.
 */

Jakob Perry's avatar
Jakob Perry committed
define('PANELS_REQUIRED_CTOOLS_API', '2.0.9');

/**
 * The current working panels version.
 *
 * In a release, it should be 7.x-3.x, which should match what drush make will
 * create. In a dev format, it should be 7.x-3.(x+1)-dev, which will allow
 * modules depending on new features in panels to depend on panels > 7.x-3.x.
 *
 * To define a specific version of Panels as a dependency for another module,
 * simply include a dependency line in that module's info file, e.g.:
 *   ; Requires Panels v7.x-3.4 or newer.
 *   dependencies[] = panels (>=3.4)
 */
Jakob Perry's avatar
Jakob Perry committed
define('PANELS_VERSION', '7.x-3.8');
Jakob Perry's avatar
Jakob Perry committed
// Hide title use to be TRUE/FALSE. So FALSE remains old behavior.
define('PANELS_TITLE_FIXED', 0);
// And TRUE meant no title.
define('PANELS_TITLE_NONE', 1);
// And this is the new behavior, where the title field will pick from a pane.
define('PANELS_TITLE_PANE', 2);
 * Returns the API version of Panels. This didn't exist in 1.
 *
 * @todo -- this should work more like the CTools API version.
 *
Jakob Perry's avatar
Jakob Perry committed
 * @return array
 *   An array with the major and minor versions
Jakob Perry's avatar
Jakob Perry committed
 * Implements hook_theme().
  // Safety: go away if CTools is not at an appropriate version.
  if (!module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
    return array();
  }

  $theme = array();
  $theme['panels_layout_link'] = array(
Jakob Perry's avatar
Jakob Perry committed
    'variables' => array(
      'title' => NULL,
      'id' => NULL,
      'image' => NULL,
      'link' => NULL,
      'class' => NULL,
    ),
Jakob Perry's avatar
Jakob Perry committed
    'variables' => array(
      'id' => NULL,
      'image' => NULL,
      'title' => NULL,
    ),
Jakob Perry's avatar
Jakob Perry committed
    'variables' => array(
      'content' => array(),
      'pane' => array(),
      'display' => array(),
    ),
    'path' => drupal_get_path('module', 'panels') . '/templates',
    'template' => 'panels-pane',
  );
  $theme['panels_common_content_list'] = array(
    'variables' => array('display' => NULL),
  $theme['panels_render_display_form'] = array(
    'render element' => 'element',
    'variables' => array(),
    'path' => drupal_get_path('module', 'panels') . '/templates',
    'file' => '../includes/callbacks.inc',
    'template' => 'panels-dashboard',
  $theme['panels_dashboard_link'] = array(
    'variables' => array('link' => array()),
    'path' => drupal_get_path('module', 'panels') . '/templates',
    'file' => '../includes/callbacks.inc',
    'template' => 'panels-dashboard-link',
  );

  $theme['panels_dashboard_block'] = array(
    'variables' => array('block' => array()),
    'path' => drupal_get_path('module', 'panels') . '/templates',
    'file' => '../includes/callbacks.inc',
    'template' => 'panels-dashboard-block',
  );

  $theme['panels_add_content_modal'] = array(
Jakob Perry's avatar
Jakob Perry committed
    'variables' => array(
      'renderer' => NULL,
      'categories' => array(),
      'region' => NULL,
      'category' => NULL,
      'column_count' => 2,
    ),
    'path' => drupal_get_path('module', 'panels') . '/templates',
    'file' => '../includes/add-content.inc',
    'template' => 'panels-add-content-modal',
  );

  $theme['panels_add_content_link'] = array(
Jakob Perry's avatar
Jakob Perry committed
    'variables' => array(
      'renderer' => NULL,
      'region' => NULL,
      'content_type' => NULL,
    ),
    'path' => drupal_get_path('module', 'panels') . '/templates',
    'file' => '../includes/add-content.inc',
    'template' => 'panels-add-content-link',
  );

  // Register layout and style themes on behalf of all of these items.
  ctools_include('plugins', 'panels');

  // No need to worry about files; the plugin has to already be loaded for us
  // to even know what the theme function is, so files will be auto included.
  $layouts = panels_get_layouts();
  foreach ($layouts as $name => $data) {
    foreach (array('theme', 'admin theme') as $callback) {
      if (!empty($data[$callback])) {
        $theme[$data[$callback]] = array(
Jakob Perry's avatar
Jakob Perry committed
          'variables' => array(
            'css_id' => NULL,
            'content' => NULL,
            'settings' => NULL,
            'display' => NULL,
            'layout' => NULL,
            'renderer' => NULL,
          ),
Jakob Perry's avatar
Jakob Perry committed
        // If no theme function exists, assume template.
        if (!function_exists("theme_$data[theme]")) {
          $theme[$data[$callback]]['template'] = str_replace('_', '-', $data[$callback]);
Jakob Perry's avatar
Jakob Perry committed
          // For preprocess.
          $theme[$data[$callback]]['file'] = $data['file'];

  $styles = panels_get_styles();
  foreach ($styles as $name => $data) {
    if (!empty($data['render pane'])) {
      $theme[$data['render pane']] = array(
Jakob Perry's avatar
Jakob Perry committed
        'variables' => array(
          'content' => NULL,
          'pane' => NULL,
          'display' => NULL,
          'style' => NULL,
          'settings' => NULL,
        ),
        'path' => $data['path'],
        'file' => $data['file'],
    if (!empty($data['render region'])) {
      $theme[$data['render region']] = array(
Jakob Perry's avatar
Jakob Perry committed
        'variables' => array(
          'display' => NULL,
          'owner_id' => NULL,
          'panes' => NULL,
          'settings' => NULL,
          'region_id' => NULL,
          'style' => NULL,
        ),
        'path' => $data['path'],
        'file' => $data['file'],
    if (!empty($data['hook theme'])) {
      if (is_array($data['hook theme'])) {
        $theme += $data['hook theme'];
      }
Jakob Perry's avatar
Jakob Perry committed
      elseif (function_exists($data['hook theme'])) {
Jakob Perry's avatar
Jakob Perry committed
 * Implements hook_menu().
  // Safety: go away if CTools is not at an appropriate version.
  if (!module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
    return array();
  }
  // Base AJAX router callback.
  $items['panels/ajax'] = array(
    'access arguments' => array('access content'),
    'page callback' => 'panels_ajax_router',
    'theme callback' => 'ajax_base_page_theme',
    'delivery callback' => 'ajax_deliver',
  $admin_base = array(
    'file' => 'includes/callbacks.inc',
    'access arguments' => array('use panels dashboard'),
  );
  // Provide a nice location for a panels admin panel.
  $items['admin/structure/panels'] = array(
    'page callback' => 'panels_admin_page',
    'description' => 'Get a bird\'s eye view of items related to Panels.',
  $items['admin/structure/panels/dashboard'] = array(
    'title' => 'Dashboard',
    'page callback' => 'panels_admin_page',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  ) + $admin_base;

  $items['admin/structure/panels/settings'] = array(
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('panels_admin_settings_page'),
    'type' => MENU_LOCAL_TASK,
  ) + $admin_base;

  $items['admin/structure/panels/settings/general'] = array(
    'title' => 'General',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('panels_admin_settings_page'),
    'access arguments' => array('administer page manager'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  ) + $admin_base;

  if (module_exists('page_manager')) {
    $items['admin/structure/panels/settings/panel-page'] = array(
      'title' => 'Panel pages',
      'page callback' => 'panels_admin_panel_context_page',
      'type' => MENU_LOCAL_TASK,
      'weight' => -10,
    ) + $admin_base;
  }

  ctools_include('plugins', 'panels');
  $layouts = panels_get_layouts();
  foreach ($layouts as $name => $data) {
    if (!empty($data['hook menu'])) {
      if (is_array($data['hook menu'])) {
        $items += $data['hook menu'];
      }
Jakob Perry's avatar
Jakob Perry committed
      elseif (function_exists($data['hook menu'])) {
/**
 * Menu loader function to load a cache item for Panels AJAX.
 *
 * This load all of the includes needed to perform AJAX, and loads the
 * cache object and makes sure it is valid.
 */
function panels_edit_cache_load($cache_key) {
  ctools_include('display-edit', 'panels');
  ctools_include('plugins', 'panels');
  ctools_include('ajax');
  ctools_include('modal');
  ctools_include('context');

  return panels_edit_cache_get($cache_key);
}

Jakob Perry's avatar
Jakob Perry committed
 * Implements hook_init().
  // Safety: go away if CTools is not at an appropriate version.
  if (!module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
    if (user_access('administer site configuration')) {
      drupal_set_message(t('Panels is enabled but CTools is out of date. All Panels modules are disabled until CTools is updated. See the status page for more information.'), 'error');
    }
  ctools_add_css('panels', 'panels');
Jakob Perry's avatar
Jakob Perry committed
 * Implements hook_permission().
 *
 * @todo Almost all of these need to be moved into pipelines.
function panels_permission() {
    'use panels dashboard' => array(
      'title' => t("Use Panels Dashboard"),
      'description' => t('Allows a user to access the <a href="@url">Panels Dashboard</a>.', array('@url' => url('admin/structure/panels'))),
Jakob Perry's avatar
Jakob Perry committed
    // @todo
    'view pane admin links' => array(
      'title' => t("View administrative links on Panel panes"),
Jakob Perry's avatar
Jakob Perry committed
      'description' => "",
Jakob Perry's avatar
Jakob Perry committed
    // @todo should we really have a global perm for this, or should it be moved into a pipeline question?
    'administer pane access' => array(
      'title' => t("Configure access settings on Panel panes"),
      'description' => t("Access rules (often also called visibility rules) can be configured on a per-pane basis. This permission allows users to configure those settings."),
    ),
    'use panels in place editing' => array(
      'title' => t("Use the Panels In-Place Editor"),
      'description' => t("Allows a user to utilize Panels' In-Place Editor."),
    ),
    'change layouts in place editing' => array(
      'title' => t("Change layouts with the Panels In-Place Editor"),
      'description' => t("Allows a user to change layouts with the IPE."),
    ),
    'bypass access in place editing' => array(
      'title' => t("Bypass access checks when using Panels In-Place Editor"),
      'description' => t("Allows using IPE even if user does not have additional permissions granted by other modules."),
      'restrict access' => TRUE,
    ),
    'administer advanced pane settings' => array(
      'title' => t("Configure advanced settings on Panel panes"),
Jakob Perry's avatar
Jakob Perry committed
      'description' => "",
    ),
    'administer panels layouts' => array(
      'title' => t("Administer Panels layouts"),
      'description' => t("Allows a user to administer exported Panels layout plugins & instances."),
    ),
    'administer panels styles' => array(
      'title' => t("Administer Panels styles"),
      'description' => t("DEPRECATED: Modules using this permission should use specific style permissions. See Issue #2329419 for more info."),
    ),
    'administer panels display styles' => array(
      'title' => t("Administer Panels display styles"),
      'description' => t("Allows a user to administer the styles of Panel displays."),
    ),
    'administer panels pane styles' => array(
      'title' => t("Administer Panels pane styles"),
      'description' => t("Allows a user to administer the styles of Panel panes."),
    ),
    'administer panels region styles' => array(
      'title' => t("Administer Panels region styles"),
      'description' => t("Allows a user to administer the styles of Panel regions."),
    ),
    'use panels caching features' => array(
      'title' => t("Configure caching settings on Panels"),
      'description' => t("Allows a user to configure caching on Panels displays and panes."),
    ),
    'use panels locks' => array(
      'title' => t('Use panel locks'),
      'description' => t('Allows a user to lock and unlock panes in a panel display.'),
    ),
    'use ipe with page manager' => array(
      'title' => t("Use the Panels In-Place Editor with Page Manager"),
      'description' => t('Allows users with access to the In-Place editor to administer page manager pages. This permission is only needed for users without "use page manager" access.'),
    ),
/**
 * Implements hook_flush_caches().
 */
function panels_flush_caches() {
  if (db_table_exists('cache_panels')) {
    return array('cache_panels');
  }
Jakob Perry's avatar
Jakob Perry committed
/**
 * CTools hook implementations.
 *
 * These aren't core Drupal hooks but they are just as important.
 */
Jakob Perry's avatar
Jakob Perry committed
 * Implements hook_ctools_plugin_directory().
function panels_ctools_plugin_directory($module, $plugin) {
Jakob Perry's avatar
Jakob Perry committed
  // To let the system know we implement task and task_handler plugins.
  // Safety: go away if CTools is not at an appropriate version.
  if (!module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
    return;
  }

  // We don't support the 'ctools' 'cache' plugin and pretending to causes
  // errors when they're in use.
  if ($module == 'ctools' && $plugin == 'cache') {
Jakob Perry's avatar
Jakob Perry committed
    // If we did we'd make a plugin/ctools_cache or something.
  if ($module == 'page_manager' || $module == 'panels' || $module == 'ctools' || $module == 'stylizer') {
    // Panels and CTools both implement a 'cache' plugin but we don't implement
    // the CTools version.
    if ($module == 'ctools' && $plugin == 'cache') {
      return;
    }
 * Implements hook_ctools_plugin_type().
 *
 * Register layout, style, cache, and display_renderer plugin types, declaring
 * relevant plugin type information as necessary.
function panels_ctools_plugin_type() {
    'layouts' => array(
Jakob Perry's avatar
Jakob Perry committed
      // We can define layouts in themes.
      'load themes' => TRUE,
      'process' => 'panels_layout_process',
      'child plugins' => TRUE,
    ),
    'styles' => array(
      'load themes' => TRUE,
      'process' => 'panels_plugin_styles_process',
      'child plugins' => TRUE,
    ),
    'cache' => array(),
    'display_renderers' => array(
      'classes' => array('renderer'),
    ),
    'panels_storage' => array(),
  );
}

/**
 * Ensure a layout has a minimal set of data.
 */
function panels_layout_process(&$plugin) {
  $plugin += array(
    'category' => t('Miscellaneous'),
    'description' => '',
  );
}

/**
Jakob Perry's avatar
Jakob Perry committed
 * Implements hook_ctools_plugin_api().
 */
function panels_ctools_plugin_api($owner, $api) {
Jakob Perry's avatar
Jakob Perry committed
  // Inform CTools about version information for various plugins implemented by
  // panels.
  if ($owner == 'panels' && $api == 'styles') {
    // As of 6.x-3.6, Panels has a slightly new system for style plugins.
    return array('version' => 2.0);
  }

  if ($owner == 'panels' && $api == 'pipelines') {
    return array(
      'version' => 1,
      'path' => drupal_get_path('module', 'panels') . '/includes',
    );
  }
}

/**
Jakob Perry's avatar
Jakob Perry committed
 * Implements hook_views_api().
 */
function panels_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'panels') . '/plugins/views',
  );
}

/**
 * Perform additional processing on a style plugin.
 *
 * Currently this is only being used to apply versioning information to style
 * plugins in order to ensure the legacy renderer passes the right type of
 * parameters to a style plugin in a hybrid environment of both new and old
 * plugins.
 *
 * @param array $plugin
 *   The style plugin that is being processed.
 * @param array $info
 *   The style plugin type info array.
Jakob Perry's avatar
Jakob Perry committed
 *
 * @see _ctools_process_data()
 */
function panels_plugin_styles_process(&$plugin, $info) {
  $plugin += array(
    'weight' => 0,
  );

  $compliant_modules = ctools_plugin_api_info('panels', 'styles', 2.0, 2.0);
  $plugin['version'] = empty($compliant_modules[$plugin['module']]) ? 1.0 : $compliant_modules[$plugin['module']]['version'];
}

/**
 * Declare what style types Panels uses.
 */
function panels_ctools_style_base_types() {
  return array(
    'region' => array(
      'title' => t('Panel region'),
      'preview' => 'panels_stylizer_region_preview',
Jakob Perry's avatar
Jakob Perry committed
      'theme variables' => array(
        'settings' => NULL,
        'class' => NULL,
        'content' => NULL,
      ),
    ),
    'pane' => array(
      'title' => t('Panel pane'),
      'preview' => 'panels_stylizer_pane_preview',
Jakob Perry's avatar
Jakob Perry committed
      'theme variables' => array(
        'settings' => NULL,
        'content' => NULL,
        'pane' => NULL,
        'display' => NULL,
      ),
Jakob Perry's avatar
Jakob Perry committed
/**
 * Generates Lorem Ipsum.
 *
 * @return string
 *   Lorem ipsum string.
 */
function panels_stylizer_lipsum() {
Jakob Perry's avatar
Jakob Perry committed
  return <<<LIPSUM
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus at 
      velit dolor. Donec egestas tellus sit amet urna rhoncus adipiscing. Proin 
      nec porttitor sem. Maecenas aliquam, purus nec tempus dignissim, nulla arcu
      aliquam diam, non tincidunt massa ante vel dolor. Aliquam sapien sapien,
      tincidunt id tristique at, pretium sagittis libero.
    </p>
    <p>
      Nulla facilisi. Curabitur lacinia, tellus sed tristique consequat, diam
      lorem scelerisque felis, at dictum purus augue facilisis lorem. Duis
      pharetra dignissim rutrum. Curabitur ac elit id dui dapibus tincidunt.
      Nulla eget sem quam, non eleifend eros. Cras porttitor tempus lectus ac
      scelerisque. Curabitur vehicula bibendum lorem, vitae ornare ligula
      venenatis ut.
    </p>
LIPSUM;
}

/**
 * Generate a preview given the current settings.
 */
function panels_stylizer_region_preview($plugin, $settings) {
  ctools_stylizer_add_css($plugin, $settings);
Jakob Perry's avatar
Jakob Perry committed
  return theme(
    $plugin['theme'],
    array(
      'settings' => $settings,
      'class' => ctools_stylizer_get_css_class($plugin, $settings),
      'content' => panels_stylizer_lipsum(),
    )
  );
}

/**
 * Generate a preview given the current settings.
 */
function panels_stylizer_pane_preview($plugin, $settings) {
  ctools_stylizer_add_css($plugin, $settings);
  $pane = new stdClass();

Jakob Perry's avatar
Jakob Perry committed
  $content = new stdClass();
  $content->title = t('Lorem ipsum');
  $content->content = panels_stylizer_lipsum();
  $content->type = 'dummy';
  $content->subtype = 'dummy';

  $content->css_class = ctools_stylizer_get_css_class($plugin, $settings);

  $display = new panels_display();

  if (!empty($plugin['theme'])) {
Jakob Perry's avatar
Jakob Perry committed
    return theme(
      $plugin['theme'],
      array(
        'settings' => $settings,
        'content' => $content,
        'pane' => $pane,
        'display' => $display,
      )
    );
Jakob Perry's avatar
Jakob Perry committed
    return theme(
      'panels_pane',
      array(
        'content' => $content,
        'pane' => $pane,
        'display' => $display,
      )
    );
Jakob Perry's avatar
Jakob Perry committed
/**
 * Panels display editing.
 */
Jakob Perry's avatar
Jakob Perry committed
 * @defgroup mainapi Functions comprising the main panels API.
/**
 * Main API entry point to edit a panel display.
 *
Jakob Perry's avatar
Jakob Perry committed
 * Sample implementations utiltizing the the complex $destination behavior can
 * be found in panels_page_edit_content() and, in a separate contrib module,
 * OG Blueprints (http://drupal.org/project/og_blueprints),
 * og_blueprints_blueprint_edit().
Jakob Perry's avatar
Jakob Perry committed
 * @param object $display
 *   Instanceof panels_display.
 *
 *   A fully loaded panels $display object, as returned from
 *   panels_load_display(). Merely passing a did is NOT sufficient.
 *   Note that 'fully loaded' means the $display must already be loaded with
 *   any contexts the caller wishes to have set for the display.
 * @param mixed $destination
 *   The redirect destination that the user should be taken to on form
 *   submission or cancellation. With panels_edit, $destination has complex
 *   effects on the return values of panels_edit() once the form has been
 *   submitted. See the explanation of the return value below to understand the
 *   different types of values returned by panels_edit() at different stages of
 *   FAPI. Under most circumstances, simply passing in drupal_get_destination()
 *   is all that's necessary.
 * @param array $content_types
 *   An associative array of allowed content types, typically as returned from
 *   panels_common_get_allowed_types(). Note that context partially governs
 *   available content types, so you will want to create any relevant contexts
 *   using panels_create_context() or panels_create_context_empty() to make sure
 *   all the appropriate content types are available.
 *
 * @return mixed
 *   Because the functions called by panels_edit() invoke the form API,
 *   this function returns different values depending on the stage of form
 *   submission we're at. In Drupal 5, the phase of form submission is indicated
 *   by the contents of $_POST['op']. Here is what you'll get at different
 *   stages:
 *     -  If !$_POST['op']: then we're on on the initial passthrough and the
 *        form is being rendered, so it's the $form itself that's being
 *        returned. Because negative margins, a common CSS technique, bork the
 *        display editor's ajax drag-and-drop, it's important that the $output
 *        be printed, not returned. Use this syntax in the caller function:
 *        print theme('page', panels_edit($display, $destination, $content_types), FALSE);
 *     -  If $_POST['op'] == t('Cancel'): form submission has been cancelled.
 *        If empty($destination) == FALSE, then there is no return value and the
 *        panels API takes care of redirecting to $destination.
 *        If empty($destination) == TRUE, then there's still no return value,
 *        but the caller function has to take care of form redirection.
 *     -  If $_POST['op'] == ('Save'): the form has been submitted successfully
 *        and has run through panels_edit_display_submit().
 *        $output depends on the value of $destination:
 *     -  If empty($destination) == TRUE: $output contains the modified $display
 *        object, and no redirection will occur. This option is useful if the
 *        caller needs to perform additional operations on or with the modified
 *        $display before the page request is complete. Using hook_form_alter()
 *        to add an additional submit handler is typically the preferred method
 *        for something like this, but there are certain use cases where that is
 *        infeasible and $destination = NULL should be used instead. If this
 *        method is employed, the caller will need to handle form redirection.
 *        Note that having $_REQUEST['destination'] set, whether via
 *        drupal_get_destination() or some other method, will NOT interfere with
 *        this functionality; consequently, you can use drupal_get_destination()
 *        to safely store your desired redirect in the caller function, then
 *        simply use drupal_goto() once panels_edit() has done its business.
 *     -  If empty($destination) == FALSE: the form will redirect to the URL
 *        string given in $destination and NO value will be returned.
Jakob Perry's avatar
Jakob Perry committed
 * @ingroup mainapi
function panels_edit($display, $destination = NULL, $content_types = NULL, $title = FALSE) {
  ctools_include('display-edit', 'panels');
  ctools_include('plugins', 'panels');
  return _panels_edit($display, $destination, $content_types, $title);
/**
 * API entry point for selecting a layout for a given display.
 *
Jakob Perry's avatar
Jakob Perry committed
 * Layout selection is nothing more than a list of radio items encompassing the
 * available layouts for this display, as defined by .inc files in the
 * panels/layouts subdirectory. The only real complexity occurs when a user
 * attempts to change the layout of a display that has some content in it.
Jakob Perry's avatar
Jakob Perry committed
 * @param object $display
 *   A fully loaded panels $display object, as returned from
 *   panels_load_display(). Merely passing a did is NOT sufficient.
Jakob Perry's avatar
Jakob Perry committed
 *   A string that will be used for the text of the form submission button. If
 *   no value is provided, then the form submission button will default to
 *   t('Save').
Jakob Perry's avatar
Jakob Perry committed
 *   Basic usage is a string containing the URL that the form should redirect to
 *   upon submission. For a discussion of advanced usages, see panels_edit().
Jakob Perry's avatar
Jakob Perry committed
 *   Allowed layouts has three different behaviors that depend on which of three
 *   value types are passed in by the caller:
 *     #- if $allowed_layouts instanceof panels_allowed_layouts
 *        (includes subclasses): the most complex use of the API. The caller is
 *        passing in a loaded panels_allowed_layouts object that the client
 *        module previously created and stored somewhere using a custom storage
 *        mechanism.
 *     #- if is_string($allowed_layouts): the string will be used in a call to
 *        variable_get() which will call the
 *        $allowed_layouts . '_allowed_layouts' var. If the data was stored
 *        properly in the system var, the $allowed_layouts object will be
 *        unserialized and recreated.
 *     #- if is_null($allowed_layouts): the default behavior, which also
 *        provides backwards compatibility for implementations of the Panels2
 *        API written before beta4. In this case, a dummy panels_allowed_layouts
 *        object is created which does not restrict any layouts. Subsequent
 *        behavior is indistinguishable from pre-beta4 behavior.
 *
 * @return mixed
 *   Can return nothing, or a modified $display object, or a redirection string;
 *   return values for the panels_edit* family of functions are quite complex.
 *   See panels_edit() for detailed discussion.
 *
Jakob Perry's avatar
Jakob Perry committed
 * @see panels_common_set_allowed_layouts()
 */
function panels_edit_layout($display, $finish, $destination = NULL, $allowed_layouts = NULL) {
  ctools_include('display-layout', 'panels');
  ctools_include('plugins', 'panels');
  return _panels_edit_layout($display, $finish, $destination, $allowed_layouts);
Jakob Perry's avatar
Jakob Perry committed
/**
 * Panels database functions.
 */
Jakob Perry's avatar
Jakob Perry committed
 * Forms the basis of a panel display.
Jakob Perry's avatar
Jakob Perry committed
  public $args = array();
  public $content = array();
  public $panels = array();
  public $incoming_content = NULL;
  public $css_id = NULL;
  public $context = array();
  public $did = 'new';
  public $renderer = 'standard';

  /**
   * Add a pane.
   */
  public function add_pane(&$pane, $location = NULL) {
    // If no location specified, use what's set in the pane.
    if (empty($location)) {
      $location = $pane->panel;
    // Generate a permanent uuid for this pane, and use
    // it as a temporary pid.
    $pane->uuid = ctools_uuid_generate();
    $pane->pid = 'new-' . $pane->uuid;
Jakob Perry's avatar
Jakob Perry committed
    // Add the pane to the appropriate spots.
    $this->content[$pane->pid] = &$pane;
    $this->panels[$location][] = $pane->pid;
Jakob Perry's avatar
Jakob Perry committed
  /**
   * Duplicate a pane.
   */
  public function duplicate_pane($pid, $location = FALSE) {
    $pane = $this->clone_pane($pid);
    $this->add_pane($pane, $location);
  /**
   * Get the title from a display.
   *
   * The display must have already been rendered, or the setting to set the
   * display's title from a pane's title will not have worked.
   *
Jakob Perry's avatar
Jakob Perry committed
   * @return mixed
   *   The title to use. If NULL, this means to let any default title that may
   *   be in use pass through. i.e, do not actually set the title.
Jakob Perry's avatar
Jakob Perry committed
  public function get_title() {
    switch ($this->hide_title) {
      case PANELS_TITLE_NONE:
        return '';

      case PANELS_TITLE_PANE:
        return isset($this->stored_pane_title) ? $this->stored_pane_title : '';

      case PANELS_TITLE_FIXED:
Jakob Perry's avatar
Jakob Perry committed
      case FALSE;
        // For old exported panels that are not in the database.
        if (!empty($this->title)) {
          return filter_xss_admin(ctools_context_keyword_substitute($this->title, array(), $this->context));
        }
        return NULL;
    }
  /**
   * Render this panels display.
   *
   * After checking to ensure the designated layout plugin is valid, a
   * display renderer object is spawned and runs its rendering logic.
   *
   * @param mixed $renderer
   *    An instantiated display renderer object, or the name of a display
   *    renderer plugin+class to be fetched. Defaults to NULL. When NULL, the
   *    predesignated display renderer will be used.
Jakob Perry's avatar
Jakob Perry committed
   *
   * @return mixed
   *    NULL or output of render function.
Jakob Perry's avatar
Jakob Perry committed
  public function render($renderer = NULL) {
    $layout = panels_get_layout($this->layout);
    if (!$layout) {
      return NULL;
    }
    // If we were not given a renderer object, load it.
    if (!is_object($renderer)) {
      // If the renderer was not specified, default to $this->renderer
      // which is either standard or was already set for us.
      $renderer = panels_get_renderer_handler(!empty($renderer) ? $renderer : $this->renderer, $this);
      if (!$renderer) {
        return NULL;
      }
    }

    $output = '';
    // Let modules act just prior to render.
    foreach (module_implements('panels_pre_render') as $module) {
      $function = $module . '_panels_pre_render';
      $output .= $function($this, $renderer);
    }

    $output .= $renderer->render();
    // Let modules act just after render.
    foreach (module_implements('panels_post_render') as $module) {
      $function = $module . '_panels_post_render';
      $output .= $function($this, $renderer);
    }
    return $output;

  /**
   * Determine if the given user can perform the requested operation.
   *
   * @param string $op
   *   An operation like: create, read, update, or delete.
   * @param object $account
   *   (optional) The account to check access for.
   *
   * @return bool
   *   TRUE if access is granted; otherwise FALSE.
   */
Jakob Perry's avatar
Jakob Perry committed
  public function access($op, $account = NULL) {
    global $user;

    if (!$account) {
      $account = $user;
    }

    // Even administrators need to go through the access system. However, to
    // support legacy plugins, user 1 gets full access no matter what.
    if ($account->uid == 1) {
      return TRUE;
    }

    if (!in_array($op, array('create', 'read', 'update', 'delete', 'change layout'))) {
      return FALSE;
    }

    if (empty($this->storage_type) || empty($this->storage_id)) {
      return FALSE;
    }

    if ($this->storage_type == 'unknown') {
      return FALSE;
    }

    $storage_plugin = panels_get_panels_storage_plugin($this->storage_type);
    if (!$storage_plugin) {
      return FALSE;
    }

    $access_callback = panels_plugin_get_function('panels_storage', $storage_plugin, 'access callback');
    if (!$access_callback) {
      return FALSE;
    }

    return $access_callback($this->storage_type, $this->storage_id, $op, $account);
  }
Jakob Perry's avatar
Jakob Perry committed
 * End of 'defgroup mainapi', although other functions are specifically added later.
/**
 * Creates a new display, setting the ID to our magic new id.
 */
function panels_new_display() {
  ctools_include('export');
  $display = ctools_export_new_object('panels_display', FALSE);
/**
 * Create a new pane.
 *
 * @todo -- use schema API for some of this?
 */
function panels_new_pane($type, $subtype, $set_defaults = FALSE) {
  ctools_include('export');
  $pane = ctools_export_new_object('panels_pane', FALSE);
  $pane->pid = 'new';
  $pane->type = $type;
  $pane->subtype = $subtype;
    ctools_include('content');
    $content_type = ctools_get_content_type($type);
    $content_subtype = ctools_content_get_subtype($content_type, $subtype);
    $pane->configuration = ctools_content_get_defaults($content_type, $content_subtype);
  }
  drupal_alter('panels_new_pane', $pane);
/**
 * Load and fill the requested $display object(s).
 *
 * Helper function primarily for for panels_load_display().
 *
 * @param array $dids
Jakob Perry's avatar
Jakob Perry committed
 *   An indexed array of dids to be loaded from the database.
Jakob Perry's avatar
Jakob Perry committed
 * @return array
 *   An array of displays, keyed by their display dids.
 *
 * @todo schema API can drasticly simplify this code.
 */
function panels_load_displays($dids) {
  $displays = array();
  if (empty($dids) || !is_array($dids)) {
    return $displays;
  }
Jakob Perry's avatar
Jakob Perry committed
  $result = db_query(
    "SELECT * FROM {panels_display} WHERE did IN (:dids)",
    array(':dids' => $dids)
  );
  foreach ($result as $obj) {
    $displays[$obj->did] = ctools_export_unpack_object('panels_display', $obj);
    // Modify the hide_title field to go from a bool to an int if necessary.
Jakob Perry's avatar
Jakob Perry committed
  $result = db_query(