Newer
Older
Earl Miles
committed
<?php
// $Id$
/**
* @file panels.module
*
* Core functionality for the Panels engine.
*/
define('PANELS_REQUIRED_CTOOLS_API', '2.0-alpha');
define('PANELS_TITLE_FIXED', 0); // Hide title use to be true/false. So false remains old behavior.
define('PANELS_TITLE_NONE', 1); // And true meant no title.
define('PANELS_TITLE_PANE', 2); // And this is the new behavior, where the title field will pick from a pane.
Earl Miles
committed
/**
Earl Miles
committed
* Returns the API version of Panels. This didn't exist in 1.
*
* @todo -- this should work more like the CTools API version.
*
Earl Miles
committed
* @return An array with the major and minor versions
Earl Miles
committed
*/
Earl Miles
committed
function panels_api_version() {
return array(3, 1);
Earl Miles
committed
}
// --------------------------------------------------------------------------
// Core Drupal hook implementations
/**
* Implementation of hook_theme()
*/
Earl Miles
committed
function panels_theme() {
// Safety: go away if CTools is not at an appropriate version.
if (!module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
return array();
}
Earl Miles
committed
$theme = array();
$theme['panels_layout_link'] = array(
'variables' => array('title' => NULL, 'id' => NULL, 'image' => NULL, 'link' => NULL),
Earl Miles
committed
);
$theme['panels_layout_icon'] = array(
'variables' => array('id' => NULL, 'image' => NULL, 'title' => NULL),
Earl Miles
committed
);
$theme['panels_pane'] = array(
'variables' => array('output' => array(), 'pane' => array(), 'display' => array()),
'path' => drupal_get_path('module', 'panels') . '/templates',
'template' => 'panels-pane',
Earl Miles
committed
);
$theme['panels_common_content_list'] = array(
'variables' => array('display' => NULL),
Earl Miles
committed
'file' => 'includes/common.inc',
);
$theme['panels_render_display_form'] = array(
'variables' => array('form' => NULL),
Earl Miles
committed
);
$theme['panels_dashboard'] = array(
'path' => drupal_get_path('module', 'panels') . '/templates',
'file' => '../includes/callbacks.inc',
'template' => 'panels-dashboard',
Earl Miles
committed
$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',
);
// We don't need layout and style themes in maintenance mode.
if (defined('MAINTENANCE_MODE')) {
return $theme;
}
Earl Miles
committed
// Register layout and style themes on behalf of all of these items.
ctools_include('plugins', 'panels');
Earl Miles
committed
// 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(
'variables' => array('css_id' => NULL, 'content' => NULL, 'settings' => NULL, 'display' => NULL, 'layout' => NULL, 'renderer' => NULL),
'path' => $data['path'],
'file' => $data['file'],
);
// if no theme function exists, assume template.
if (!function_exists("theme_$data[theme]")) {
$theme[$data[$callback]]['template'] = str_replace('_', '-', $data[$callback]);
$theme[$data[$callback]]['file'] = $data['file']; // for preprocess.
}
Earl Miles
committed
}
Earl Miles
committed
}
Earl Miles
committed
$styles = panels_get_styles();
foreach ($styles as $name => $data) {
if (!empty($data['render pane'])) {
$theme[$data['render pane']] = array(
'variables' => array('content' => NULL, 'pane' => NULL, 'display' => NULL, 'style' => NULL, 'settings' => NULL),
'path' => $data['path'],
'file' => $data['file'],
Earl Miles
committed
);
}
if (!empty($data['render region'])) {
$theme[$data['render region']] = array(
'variables' => array('display' => NULL, 'owner_id' => NULL, 'panes' => NULL, 'settings' => NULL, 'region_id' => NULL, 'style' => NULL),
'path' => $data['path'],
'file' => $data['file'],
Earl Miles
committed
);
}
if (!empty($data['hook theme'])) {
if (is_array($data['hook theme'])) {
$theme += $data['hook theme'];
}
else if (function_exists($data['hook theme'])) {
$data['hook theme']($theme, $data);
}
}
Earl Miles
committed
}
return $theme;
Earl Miles
committed
}
/**
Earl Miles
committed
* Implementation of hook_menu
Earl Miles
committed
*/
Earl Miles
committed
function panels_menu() {
// Safety: go away if CTools is not at an appropriate version.
if (!module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
return array();
}
Earl Miles
committed
$items = array();
// Base AJAX router callback.
$items['panels/ajax'] = array(
Earl Miles
committed
'access arguments' => array('access content'),
'page callback' => 'panels_ajax_router',
Earl Miles
committed
'type' => MENU_CALLBACK,
);
$admin_base = array(
'file' => 'includes/callbacks.inc',
'access arguments' => array('use panels dashboard'),
);
Earl Miles
committed
// Provide a nice location for a panels admin panel.
$items['admin/structure/panels'] = array(
Sam Boyer
committed
'title' => 'Panels',
Earl Miles
committed
'page callback' => 'panels_admin_page',
'description' => 'Get a bird\'s eye view of items related to Panels.',
) + $admin_base;
$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'];
}
else if (function_exists($data['hook menu'])) {
$data['hook menu']($items, $data);
}
}
}
Earl Miles
committed
Earl Miles
committed
return $items;
Earl Miles
committed
}
/**
* 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);
}
Earl Miles
committed
/**
Earl Miles
committed
* Implementation of hook_init()
Earl Miles
committed
*/
Earl Miles
committed
function panels_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');
}
return;
}
ctools_add_css('panels', 'panels');
ctools_add_js('panels', 'panels');
Earl Miles
committed
}
Earl Miles
committed
/**
* Implementation of hook_permission().
*
* @todo Almost all of these need to be moved into pipelines.
Earl Miles
committed
*/
function panels_permission() {
Earl Miles
committed
return array(
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
'use panels dashboard' => array(
'title' => t("Use Panels Dashboard"),
'description' => t("Allows a user to access the !link.", array('!link' => l('Panels Dashboard', 'admin/structure/panels'))),
),
'view pane admin links' => array( // @todo
'title' => t("View administrative links on Panel panes"),
'description' => t(""),
),
'administer pane access' => array( // @todo should we really have a global perm for this, or should it be moved into a pipeline question?
'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."),
),
'administer advanced pane settings' => array(
'title' => t("Configure advanced settings on Panel panes"),
'description' => t(""),
),
'administer panels layouts' => array(
'title' => t("Administer Panels layouts"),
'description' => t("Allows a user to administer exported Panels layout plugins & instances."),
),
'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."),
),
Earl Miles
committed
);
}
Earl Miles
committed
/**
* Implementation of hook_flush_caches().
*
* We implement this so that we can be sure our legacy rendering state setting
* in $conf is updated whenever caches are cleared.
Earl Miles
committed
*/
//function panels_flush_caches() {
// $legacy = panels_get_legacy_state();
// $legacy->determineStatus();
//}
Earl Miles
committed
// ---------------------------------------------------------------------------
// CTools hook implementations
//
// These aren't core Drupal hooks but they are just as important.
Earl Miles
committed
/**
* Implementation of hook_ctools_plugin_directory() to let the system know
* we implement task and task_handler plugins.
Earl Miles
committed
*/
function panels_ctools_plugin_directory($module, $plugin) {
// Safety: go away if CTools is not at an appropriate version.
if (!module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
return;
}
if ($module == 'page_manager' || $module == 'panels' || $module == 'ctools' || $module == 'stylizer') {
return 'plugins/' . $plugin;
}
Earl Miles
committed
}
Earl Miles
committed
/**
* Implements hook_ctools_plugin_type().
*
* Register layout, style, cache, and display_renderer plugin types, declaring
* relevant plugin type information as necessary.
Earl Miles
committed
*/
function panels_ctools_plugin_type() {
return array(
'layouts' => array(
'load themes' => TRUE, // Can define layouts in themes
'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'),
),
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
);
}
/**
* Ensure a layout has a minimal set of data.
*/
function panels_layout_process(&$plugin) {
$plugin += array(
'category' => t('Miscellaneous'),
'description' => '',
);
}
/**
* Implementation of hook_ctools_plugin_api().
*
* Inform CTools about version information for various plugins implemented by
* Panels.
*
* @param string $owner
* The system name of the module owning the API about which information is
* being requested.
* @param string $api
* The name of the API about which information is being requested.
*/
function panels_ctools_plugin_api($owner, $api) {
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',
);
}
}
/**
* Implementation of 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.
*
* @see _ctools_process_data()
*
* @param array $plugin
* The style plugin that is being processed.
* @param array $info
* The style plugin type info array.
*/
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',
'theme variables' => array('settings' => NULL, 'class' => NULL, 'content' => NULL),
),
'pane' => array(
'title' => t('Panel pane'),
'preview' => 'panels_stylizer_pane_preview',
'theme variables' => array('settings' => NULL, 'content' => NULL, 'pane' => NULL, 'display' => NULL),
),
);
}
function panels_stylizer_lipsum() {
return "
<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>
";
}
/**
* Generate a preview given the current settings.
*/
function panels_stylizer_region_preview($plugin, $settings) {
ctools_stylizer_add_css($plugin, $settings);
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();
$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'])) {
return theme($plugin['theme'], array('settings' => $settings, 'content' => $content, 'pane' => $pane, 'display' => $display));
}
else {
return theme('panels_pane', array('output' => $content, 'pane' => $pane, 'display' => $display));
Earl Miles
committed
}
Earl Miles
committed
// ---------------------------------------------------------------------------
// Panels display editing
Earl Miles
committed
/**
Earl Miles
committed
* @defgroup mainapi Functions comprising the main panels API
* @{
Earl Miles
committed
*/
Earl Miles
committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/**
* Main API entry point to edit a panel display.
*
* 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().
*
* @ingroup mainapi
*
* @param object $display instanceof panels_display \n
* A fully loaded panels $display object, as returned from panels_load_display().
* Merely passing a did is NOT sufficient. \n
* 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 \n
* 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 \n
* 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
* 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's 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: \n
* print theme('page', panels_edit($display, $destination, $content_types), FALSE); \n
* -# 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.
Earl Miles
committed
*/
function panels_edit($display, $destination = NULL, $content_types = NULL, $title = FALSE) {
ctools_include('display-edit', 'panels');
ctools_include('ajax');
ctools_include('plugins', 'panels');
return _panels_edit($display, $destination, $content_types, $title);
Earl Miles
committed
}
Earl Miles
committed
Earl Miles
committed
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/**
* API entry point for selecting a layout for a given display.
*
* 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.
*
* @param object $display instanceof panels_display \n
* A fully loaded panels $display object, as returned from panels_load_display().
* Merely passing a did is NOT sufficient.
* @param string $finish
* 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').
* @param mixed $destination
* 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().
* @param mixed $allowed_layouts
* 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.
* @see panels_common_set_allowed_layouts()
* #- 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
* 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.
* @see panels_edit()
*/
function panels_edit_layout($display, $finish, $destination = NULL, $allowed_layouts = NULL) {
ctools_include('display-layout', 'panels');
ctools_include('plugins', 'panels');
Earl Miles
committed
return _panels_edit_layout($display, $finish, $destination, $allowed_layouts);
Earl Miles
committed
}
Earl Miles
committed
// ---------------------------------------------------------------------------
// Panels database functions
Earl Miles
committed
Earl Miles
committed
/**
Earl Miles
committed
* Forms the basis of a panel display
*
Earl Miles
committed
*/
Earl Miles
committed
class panels_display {
var $args = array();
var $content = array();
var $panels = array();
var $incoming_content = NULL;
var $css_id = NULL;
var $context = array();
var $did = 'new';
var $renderer = 'standard';
function add_pane(&$pane, $location = NULL) {
// If no location specified, use what's set in the pane.
if (empty($location)) {
$location = $pane->panel;
Earl Miles
committed
}
Earl Miles
committed
else {
$pane->panel = $location;
Earl Miles
committed
}
// Get a temporary pid for this pane.
$pane->pid = "new-" . $this->next_new_pid();
// Add the pane to the approprate spots.
$this->content[$pane->pid] = &$pane;
$this->panels[$location][] = $pane->pid;
Earl Miles
committed
}
Earl Miles
committed
function duplicate_pane($pid, $location = FALSE) {
$pane = $this->clone_pane($pid);
$this->add_pane($pane, $location);
Earl Miles
committed
}
Earl Miles
committed
function clone_pane($pid) {
$pane = drupal_clone($this->content[$pid]);
return $pane;
}
Earl Miles
committed
Earl Miles
committed
function next_new_pid() {
// We don't use static vars to record the next new pid because
// temporary pids can last for years in exports and in caching
// during editing.
Earl Miles
committed
$id = array(0);
foreach (array_keys($this->content) as $pid) {
if (!is_numeric($pid)) {
$id[] = substr($pid, 4);
}
}
$next_id = max($id);
Earl Miles
committed
return ++$next_id;
Earl Miles
committed
}
Earl Miles
committed
/**
* 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.
*
* @return
* 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.
*/
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:
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;
}
Earl Miles
committed
}
Earl Miles
committed
/**
* 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.
*/
function render($renderer = NULL) {
$layout = panels_get_layout($this->layout);
if (!$layout) {
return NULL;
}
Earl Miles
committed
// 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();
Earl Miles
committed
// 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;
Earl Miles
committed
}
Earl Miles
committed
}
Earl Miles
committed
/**
* }@ End of 'defgroup mainapi', although other functions are specifically added later
*/
Earl Miles
committed
/**
* 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);
Earl Miles
committed
$display->did = 'new';
return $display;
}
Earl Miles
committed
/**
* 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);
Earl Miles
committed
$pane->pid = 'new';
$pane->type = $type;
$pane->subtype = $subtype;
if ($set_defaults) {
$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);
}
Earl Miles
committed
return $pane;
}
Earl Miles
committed
Earl Miles
committed
/**
* Load and fill the requested $display object(s).
*
* Helper function primarily for for panels_load_display().
*
* @param array $dids
* An indexed array of dids to be loaded from the database.
*
* @return $displays
* An array of displays, keyed by their display dids.
*
* @todo schema API can drasticly simplify this code.
Earl Miles
committed
*/
function panels_load_displays($dids) {
$displays = array();
if (empty($dids) || !is_array($dids)) {
return $displays;
}
Earl Miles
committed
$result = db_query("SELECT * FROM {panels_display} WHERE did IN (:dids)", array(':dids' => $dids));
Earl Miles
committed
ctools_include('export');
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.
Earl Miles
committed
}
$result = db_query("SELECT * FROM {panels_pane} WHERE did IN (:dids) ORDER BY did, panel, position", array(':dids' => $dids));
foreach ($result as $obj) {
$pane = ctools_export_unpack_object('panels_pane', $obj);
Earl Miles
committed
$displays[$pane->did]->panels[$pane->panel][] = $pane->pid;
$displays[$pane->did]->content[$pane->pid] = $pane;
}
Earl Miles
committed
return $displays;
Earl Miles
committed
}
/**
Earl Miles
committed
* Load a single display.
*
* @ingroup mainapi
*
* @param int $did
* The display id (did) of the display to be loaded.
*
* @return object $display instanceof panels_display \n
* Returns a partially-loaded panels_display object. $display objects returned from
* from this function have only the following data:
* - $display->did (the display id)
* - $display->name (the 'name' of the display, where applicable - it often isn't)
* - $display->layout (a string with the system name of the display's layout)
* - $display->panel_settings (custom layout style settings contained in an associative array; NULL if none)
* - $display->layout_settings (panel size and configuration settings for Flexible layouts; NULL if none)
* - $display->css_id (the special css_id that has been assigned to this display, if any; NULL if none)
* - $display->content (an array of pane objects, keyed by pane id (pid))
* - $display->panels (an associative array of panel regions, each an indexed array of pids in the order they appear in that region)
* - $display->cache (any relevant data from panels_simple_cache)
* - $display->args
* - $display->incoming_content
*
* While all of these members are defined, $display->context is NEVER defined in the returned $display;
* it must be set using one of the ctools_context_create() functions.
Earl Miles
committed
*/
Earl Miles
committed
function panels_load_display($did) {
$displays = panels_load_displays(array($did));
if (!empty($displays)) {
return array_shift($displays);
}
}
Earl Miles
committed
Earl Miles
committed
/**
* Save a display object.
*
* @ingroup mainapi
*
* Note a new $display only receives a real did once it is run through this function.
* Until then, it uses a string placeholder, 'new', in place of a real did. The same
* applies to all new panes (whether on a new $display or not); in addition,
* panes have sequential numbers appended, of the form 'new-1', 'new-2', etc.
*
* @param object $display instanceof panels_display \n
* The display object to be saved. Passed by reference so the caller need not use
* the return value for any reason except convenience.
*
* @return object $display instanceof panels_display \n
*/
function panels_save_display(&$display) {
$update = (isset($display->did) && is_numeric($display->did)) ? array('did') : array();
drupal_write_record('panels_display', $display, $update);
$pids = array();
if ($update) {
Earl Miles
committed
// Get a list of all panes currently in the database for this display so we can know if there
// are panes that need to be deleted. (i.e, aren't currently in our list of panes).
$result = db_query("SELECT pid FROM {panels_pane} WHERE did = :did", array(':did' => $display->did));
foreach ($result as $pane) {
Earl Miles
committed
$pids[$pane->pid] = $pane->pid;
Earl Miles
committed
}
Earl Miles
committed
}
Earl Miles
committed
Earl Miles
committed
// update all the panes
ctools_include('plugins', 'panels');
ctools_include('content');
Earl Miles
committed
foreach ($display->panels as $id => $panes) {
Earl Miles
committed
$position = 0;
$new_panes = array();
foreach ((array) $panes as $pid) {
if (!isset($display->content[$pid])) {
continue;
}
Earl Miles
committed
$pane = $display->content[$pid];
$type = ctools_get_content_type($pane->type);
Earl Miles
committed
$pane->position = $position++;
$pane->did = $display->did;
$old_pid = $pane->pid;
drupal_write_record('panels_pane', $pane, is_numeric($pid) ? array('pid') : array());
if ($pane->pid != $old_pid) {
// and put it back so our pids and positions can be used
unset($display->content[$id]);
$display->content[$pane->pid] = $pane;
// If the title pane was one of our panes that just got its ID changed,
// we need to change it in the database, too.
if (isset($display->title_pane) && $display->title_pane == $old_pid) {
$display->title_pane = $pane->pid;
// Do a simple update query to write it so we don't have to rewrite
// the whole record. We can't just save writing the whole record here
// because it was needed to get the did. Chicken, egg, more chicken.
db_update('panels_display')
->fields(array(
'title_pane' => $pane->pid
))
->condition('did', $display->did)
->execute();
}
Earl Miles
committed
}
// re-add this to the list of content for this panel.
Earl Miles
committed
$new_panes[] = $pane->pid;
// Remove this from the list of panes scheduled for deletion.
Earl Miles
committed
if (isset($pids[$pane->pid])) {
unset($pids[$pane->pid]);
Earl Miles
committed
}
}
Earl Miles
committed
$display->panels[$id] = $new_panes;
}
Earl Miles
committed
if (!empty($pids)) {
db_delete('panels_pane')->condition('pid', $pids)->execute();
Earl Miles
committed
}
Earl Miles
committed
// Clear any cached content for this display.
panels_clear_cached_content($display);
Earl Miles
committed
// Allow other modules to take action when a display is saved.
module_invoke_all('panels_display_save', $display);
// Log the change to watchdog, using the same style as node.module
$watchdog_args = array('%did' => $display->did);
if (!empty($display->title)) {
$watchdog_args['%title'] = $display->title;
watchdog('content', 'Panels: saved display "%title" with display id %did', $watchdog_args, WATCHDOG_NOTICE);
}
else {
watchdog('content', 'Panels: saved display with id %did', $watchdog_args, WATCHDOG_NOTICE);
}
Earl Miles
committed
// to be nice, even tho we have a reference.
return $display;
Earl Miles
committed
}
/**
Earl Miles
committed
* Delete a display.
Earl Miles
committed
*/
Earl Miles
committed
function panels_delete_display($display) {
if (is_object($display)) {
$did = $display->did;
}
else {
$did = $display;
}
db_delete('panels_display')->condition('did', $did)->execute();
db_delete('panels_pane')->condition('did', $did)->execute();
Earl Miles
committed
}
/**
Earl Miles
committed
* Exports the provided display into portable code.
*
* This function is primarily intended as a mechanism for cloning displays.
* It generates an exact replica (in code) of the provided $display, with
* the exception that it replaces all ids (dids and pids) with 'new-*' values.
* Only once panels_save_display() is called on the code version of $display will
* the exported display written to the database and permanently saved.
*
* @see panels_page_export() or _panels_page_fetch_display() for sample implementations.
*
* @ingroup mainapi
*
* @param object $display instanceof panels_display \n
* This export function does no loading of additional data about the provided
* display. Consequently, the caller should make sure that all the desired data
* has been loaded into the $display before calling this function.
* @param string $prefix
* A string prefix that is prepended to each line of exported code. This is primarily
* used for prepending a double space when exporting so that the code indents and lines up nicely.
*
* @return string $output
* The passed-in $display expressed as code, ready to be imported. Import by running
* eval($output) in the caller function; doing so will create a new $display variable
* with all the exported values. Note that if you have already defined a $display variable in
* the same scope as where you eval(), your existing $display variable WILL be overwritten.
Earl Miles
committed
*/
Earl Miles
committed
function panels_export_display($display, $prefix = '') {
ctools_include('export');
$output = ctools_export_object('panels_display', $display, $prefix);
Earl Miles
committed
// Initialize empty properties.
Earl Miles
committed
$output .= $prefix . '$display->content = array()' . ";\n";
$output .= $prefix . '$display->panels = array()' . ";\n";
$panels = array();
$title_pid = 0;
Earl Miles
committed
if (!empty($display->content)) {
$pid_counter = 0;
$region_counters = array();
foreach ($display->content as $pane) {
$pid = 'new-' . ++$pid_counter;
if ($pane->pid == $display->title_pane) {
$title_pid = $pid;
}
$pane->pid = $pid;
$output .= ctools_export_object('panels_pane', $pane, $prefix . ' ');
Earl Miles
committed
$output .= "$prefix " . '$display->content[\'' . $pane->pid . '\'] = $pane' . ";\n";
if (!isset($region_counters[$pane->panel])) {
$region_counters[$pane->panel] = 0;
Earl Miles
committed
}
Earl Miles
committed
$output .= "$prefix " . '$display->panels[\'' . $pane->panel . '\'][' . $region_counters[$pane->panel]++ .'] = \'' . $pane->pid . "';\n";
Earl Miles
committed
}
}