? .DS_Store
? panels2compatibility.patch
Index: panelsblock.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/panelsblock/panelsblock.module,v
retrieving revision 1.9
diff -u -F^f -r1.9 panelsblock.module
--- panelsblock.module	11 Jun 2007 21:35:53 -0000	1.9
+++ panelsblock.module	6 Jul 2007 19:15:26 -0000
@@ -1,8 +1,5 @@
 <?php
-// $Id: panelsblock.module,v 1.9 2007/06/11 21:35:53 nedjo Exp $
-
-// Panels layout area used for blocks.
-define('PANELSBLOCK_DEFAULT_AREA', 'left');
+// $Id: panelsblock.module,v 1.1.2.7 2007/06/11 21:36:30 nedjo Exp $
 
 /**
  * @file
@@ -13,35 +10,86 @@
  * Implementation of hook_form_alter().
  */
 function panelsblock_form_alter($form_id, &$form) {
-  if ($form_id == 'panels_edit_form') {
-    $blocks = variable_get('panelsblock', array());
-    $form['#submit']['panelsblock_form_submit'] = array();
-    $form['panelsblock'] = array(
+  if ($form_id == 'panels_page_edit_form') {
+    // Get basic variables.
+    $blocks = variable_get('panelsblock', array()); // Contains all panelsblock settings.
+    $pid = $form['panel_page']['#value']->pid; // The panels page id.
+
+    // Check for problems.
+    $problem = FALSE;
+    if (!isset($form['panel_page']['#value']->display->panels)) {
+      // The panels page is not yet saved.
+      // As as a result, the available panels for the chosen layout are not
+      // yet available. We need these to create the select form item to choose
+      // the exposed panel.
+      $problem = TRUE;
+      $description = t('You must save this panels page before you can configure a panelsblock.');
+    }
+    elseif (0 == count($form['panel_page']['#value']->display->panels)) {
+      // The panels page does not yet have any panels with at least one pane
+      // added to it.
+      // As as a result, the available panels for the chosen layout are not
+      // yet available. We need these to create the select form item to choose
+      // the exposed panel.
+      $problem = TRUE;
+      $description = t('You must add at least one pane to a panel before you can ');
+    }
+    else {
+      $description = NULL;
+    }
+
+    $form['left']['panelsblock'] = array(
       '#type' => 'fieldset',
       '#title' => t('Block'),
-      '#weight' => 0,
-    );
-    $form['panelsblock']['expose_block'] = array(
-      '#type' => 'checkbox',
-      '#title' => t('Expose as a block'),
-      '#description' => t('Select whether you wish to make this panels page available as a block.'),
-      '#default_value' => $blocks[$form['did']['#value']] && $blocks[$form['did']['#value']]['expose'] ? $blocks[$form['did']['#value']]['expose'] : 0,
-    );
-    $options = panelsblock_get_areas($form['layout']['#value']);
-    $form['panelsblock']['expose_area'] = array(
-      '#type' => 'select',
-      '#title' => t('Display area'),
-      '#default_value' => $blocks[$form['did']['#value']] && $blocks[$form['did']['#value']]['area'] ? $blocks[$form['did']['#value']]['area'] : (array_key_exists(PANELSBLOCK_DEFAULT_AREA, $options) ? PANELSBLOCK_DEFAULT_AREA : ''),
-      '#options' => $options,
-      '#description' => t('Select the area that will be displayed.'),
-    );
-    $form['panelsblock']['expose_style'] = array(
-      '#type' => 'radios',
-      '#title' => t('Display style'),
-      '#options' => array('default' => t('Default panels'), 'tabs' => t('Tabs')),
-      '#description' => t('Select the display style for items in this panels set.'),
-      '#default_value' => $blocks[$form['did']['#value']] && $blocks[$form['did']['#value']]['style'] ? $blocks[$form['did']['#value']]['style'] : 'default',
+      '#description' => $description,
+      '#weight' => 1,
     );
+    if (!$problem) {
+      $form['left']['panelsblock']['expose_block'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Expose as a block'),
+        '#description' => t('Select whether you wish to make this panels page available as a block.'),
+        '#default_value' => $blocks[$pid] && $blocks[$pid]['expose'] ? $blocks[$pid]['expose'] : 0,
+      );
+
+      // Create an array of options, consisting of a list of panels available in
+      // the current panel page.
+      $options = array();
+      $panels = array_keys($form['panel_page']['#value']->display->panels);
+      foreach ($panels as $panel) {
+        // Create a human-friendly name for each panel.
+        if (preg_match("/row_(\d+)_(\d+)/i", $panel, $matches)) {
+          $options[$panel] = "Row {$matches[1]}, Column {$matches[2]}";
+        }      
+        else {
+          $options[$panel] = ucfirst($panel);
+        }
+      }
+
+      // Compatibility fix for Panels 2 alpha 5, which creates an empty first
+      // option if you've chosen the "Flexible" layout.
+      // TODO: this can be removed as soon as this is fixed in Panels 2. See
+      // http://drupal.org/node/157345
+      if ($panels[0] == '') {
+        array_shift($options);
+      }
+
+      $form['left']['panelsblock']['expose_panel'] = array(
+        '#type' => 'select',
+        '#title' => t('Exposed panel'),
+        '#default_value' => $blocks[$pid] && $blocks[$pid]['panel'] ? $blocks[$pid]['panel'] : $panels[0],
+        '#options' => $options,
+        '#description' => t('Select the panel that will be displayed. You cannot choose an empty panel.'),
+      );
+      $form['left']['panelsblock']['expose_style'] = array(
+        '#type' => 'radios',
+        '#title' => t('Display style'),
+        '#options' => array('default' => t('Default panels'), 'tabs' => t('Tabs')),
+        '#description' => t('Select the display style for the chosen panel.'),
+        '#default_value' => $blocks[$pid] && $blocks[$pid]['style'] ? $blocks[$pid]['style'] : 'default',
+      );
+      $form['#submit']['panelsblock_form_submit'] = array();
+    }
   }
 }
 
@@ -53,10 +101,10 @@ function panelsblock_block($op = 'list',
     case 'list':
       $blocks = array();
       $panelsblocks = variable_get('panelsblock', array());
-      foreach ($panelsblocks as $did => $block) {
+      foreach ($panelsblocks as $pid => $block) {
         if ($block['expose']) {
-          if ($panels = panels_load_panels($did)) {
-            $blocks[$did] = array(
+          if ($panels = panels_page_load($pid)) {
+            $blocks[$pid] = array(
               'info' => t('@title panel', array('@title' => $panels->title ? $panels->title : $panels->path)),
             );
           }
@@ -70,14 +118,15 @@ function panelsblock_block($op = 'list',
 }
 
 /**
- * Save tabs information for a panel.
+ * Save tabs information for a panel page.
  */
 function panelsblock_form_submit($form_id, $form_values) {
   $blocks = variable_get('panelsblock', array());
-  $blocks[$form_values['did']] = array(
+  $pid = $form_values['panel_page']->pid;
+  $blocks[$pid] = array(
     'expose' => $form_values['expose_block'],
     'style' => $form_values['expose_style'],
-    'area' => $form_values['expose_area'],
+    'panel' => $form_values['expose_panel'],
   );
   variable_set('panelsblock', $blocks);
 }
@@ -85,51 +134,43 @@ function panelsblock_form_submit($form_i
 /**
  * Load a panels block.
  */
-function panelsblock_get_block($did) {
+function panelsblock_get_block($pid) {
+
+  $blocks = variable_get('panelsblock', array());
 
   // Some types, e.g, views, can change the title and breadcrumb.
   // To avoid these changes, cache the existing values.
   $cached_title = drupal_get_title();
   $cached_breadcrumb = drupal_get_breadcrumb();
 
-  $panels = panels_load_panels($did);
-  if (!$panels) {
+  // Load a panel page and its associated display.
+  $panels_page = panels_page_load($pid);
+  if (!$panels_page) {
     return MENU_NOT_FOUND;
   }
 
-  $layouts = panels_get_layouts();
-  $layout = $layouts[$panels->layout];
-  $layout['css_id'] = $panels->css_id;
-
-  if (!$layout) {
-    watchdog('panels', t('Unable to find requested layout %s', array('%s' => check_plain($panels->layout))));
-    return MENU_NOT_FOUND;
+  // Remove the panes that are not inside the panel that will be displayed.
+  $displayed_panel = $blocks[$pid]['panel'];
+  foreach ($panels_page->display->content as $pane_id => $pane) {
+    if ($pane->panel != $displayed_panel) {
+      unset($panels_page->display->content[$pane_id]);
+    }
   }
-
-  $blocks = variable_get('panelsblock', array());
-
-  panels_is_panels_page(TRUE);
-  $content_types = panels_get_content_types();
-  $content = array();
-  panelsblock_load_content_types();
-
-  $display_area = !empty($blocks[$did]['area']) ? $blocks[$did]['area'] : PANELSBLOCK_DEFAULT_AREA;
-
-  foreach ($panels->content as $location => $list) {
-    foreach ($list as $area) {
-      if ($area->area == $display_area) {
-        $function = $content_types[$area->type]['callback'] .'_block';
-        if (function_exists($function)) {
-          $content[] = $function($area->configuration);
-        }
-      }
+  
+  // Render the panes. Based on code of panels_render_display().
+  $display = $panels_page->display;
+  panels_sanitize_display($display);
+  foreach ($display->content as $pane_id => $pane) {
+    if (panels_pane_access($pane)) {
+      $content[$pane->panel][$pane_id] = panels_get_pane_content($pane, $display->args, $display->context, $display->incoming_content);
     }
   }
 
-  $theme_function = 'panelsblock_block_'. ($blocks[$did] ? $blocks[$did]['style'] : 'default');
+  // Now put the rendered panes inside the chosen presentation (default/tabs).
+  $theme_function = 'panelsblock_block_'. ($blocks[$pid] ? $blocks[$pid]['style'] : 'default');
   $block = array(
-    'subject' => $panels->title ? $panels->title : '',
-    'content' => theme($theme_function, $did, $content),
+    'subject' => $panels_page->title ? $panels_page->title : '',
+    'content' => theme($theme_function, $pid, $content),
   );
 
   // Restore title and breadcrumb.
@@ -140,32 +181,15 @@ function panelsblock_get_block($did) {
 }
 
 /**
- * Load the supported content types.
- */
-function panelsblock_load_content_types() {
-  $path = drupal_get_path('module', 'panelsblock') . '/content_types';
-  $files = drupal_system_listing('.inc$', $path, 'name', 0);
-
-  foreach($files as $file) {
-    require_once('./' . $file->filename);
-  }
-}
-
-/**
- * Given a panel layout, return its content areas.
- */
-function panelsblock_get_areas($layout) {
-  $layouts = panels_get_layouts();
-  return $layouts[$layout]['content areas'];
-}
-
-/**
  * Default presentation of block.
  */
-function theme_panelsblock_block_default($did, $content) {
+function theme_panelsblock_block_default($pid, $content) {
   $items = array();
-  foreach ($content as $item) {
-    $items[] = '<div class="title">'. $item['title'] .'</div><div class="body">'. $item['body'] .'</div>';
+
+  foreach ($content as $panel) {
+    foreach ($panel as $pane_id => $block) {
+      $items[] = theme('block', $block);
+    }
   }
   return theme('item_list', $items);
 }
@@ -173,21 +197,20 @@ function theme_panelsblock_block_default
 /**
  * Presentation of block in tabs.
  */
-function theme_panelsblock_block_tabs($did, $content) {
+function theme_panelsblock_block_tabs($pid, $content) {
   $tabs = array();
 
-  $tabs['panelsblock_'. $did] = array(
+  $tabs['panelsblock_'. $pid] = array(
     '#type' => 'tabset',
   );
-  foreach ($content as $key => $item) {
-    $body = trim(strip_tags($item['body']));
-    if (!empty($body)) {
-      $tabs['panelsblock_'. $did]['tab_'. $key] = array(
+  foreach ($content as $panel) {
+    foreach ($panel as $pane_id => $block) {
+      $tabs["panelsblock_$pid"]["tab_$pane_id"] = array(
         '#type' => 'tabpage',
-        '#title' => $item['title'],
-        '#content' => $item['body'],
+        '#title' => $block->subject,
+        '#content' => $block->content,
       );
     }
   }
   return tabs_render($tabs);
-}
\ No newline at end of file
+}
