--- og_panels.module  2010-05-30 22:55:12.328365659 -0700
+++ og_panels_new.module  2010-05-30 22:54:21.378365793 -0700
@@ -17,6 +17,10 @@ function og_panels_init(){
         if( (arg(2) == "" || arg(2) == 'view') && variable_get('page_manager_node_view_disabled',0) == 1){
           drupal_set_message(t("In order to view the homepage, you need to enable node_view template, click ").'<a href="'.url("admin/build/pages/nojs/enable/node_view", array("query"=>"destination=node/$group_node->nid")).'">'.t("here").'</a> '.t("to enable."),"error");
         }
+        //give user a hint of the current group context
+        $bc[] = l(t('Home'), '');
+        $bc[] = l($group_node->group_title, "node/$group_node->nid");
+        drupal_set_breadcrumb($bc);
       }
   }
 }
@@ -102,6 +106,65 @@ function og_panels_menu() {
     'access callback' => 'og_panels_access_admin',
     'access arguments' => array(1),
   );
+
+  $items['admin/build/og_panels_template'] = array(
+    'title' => 'Create New OG Panels Template',
+    'description' => 'Define template for Organic Group Panels',
+    'page callback' => 'og_panels_template',
+    'type' => MENU_NORMAL_ITEM,
+    'access callback' => 'og_panels_template_access',
+    'access arguments' => array(1),
+  );
+
+  $items['admin/build/og_panels_template'] = array(
+    'title' => 'Organic Group Panels Templates',
+    'description' => 'All templates for Organic Group Panels',
+    'page callback' => 'og_panels_template',
+    'type' => MENU_NORMAL_ITEM,
+    'access callback' => 'og_panels_template_access',
+  );
+
+  $items['admin/build/og_panels_template/list'] = array(
+    'title' => 'List All OG Panels Templates',
+    'description' => 'List all templates for Organic Group Panels',
+    'page callback' => 'og_panels_template',
+    'type' => PAGE_CALLBACK,
+    'access callback' => 'og_panels_template_access',
+    'access arguments' => array(3),
+  );
+
+  $items['admin/build/og_panels_template/edit_name'] = array(
+    'title' => 'Edit Organic Groups Panels Name',
+    'page callback' => 'og_panels_template',
+    'type' => PAGE_CALLBACK,
+    'access callback' => 'og_panels_template_access',
+    'access arguments' => array(3),
+  );
+
+  $items['admin/build/og_panels_template/edit_content'] = array(
+    'title' => 'Edit Organic Groups Panels Template',
+    'page callback' => 'og_panels_template',
+    'type' => PAGE_CALLBACK,
+    'access callback' => 'og_panels_template_access',
+    'access arguments' => array(3),
+  );
+
+  $items['admin/build/og_panels_template/edit_layout'] = array(
+    'title' => 'Edit layout for OG Panels Templates',
+    'page callback' => 'og_panels_template',
+    'type' => PAGE_CALLBACK,
+    'access callback' => 'og_panels_template_access',
+    'access arguments' => array(3),
+  );
+
+  $items['admin/build/og_panels_template/delete_template'] = array(
+    'title' => 'Delete OG Panels Templates',
+    'page callback' => 'og_panels_template',
+    'type' => PAGE_CALLBACK,
+    'access callback' => 'og_panels_template_access',
+    'access arguments' => array(3),
+  );
+
   // Add generic realm of callbacks for og_panels tabs.
   $items = array_merge($items, _og_panels_nodes_menu());
   return $items;
@@ -115,6 +178,7 @@ function og_panels_menu() {
  * We can use 1 as the argument here, as a pane set to be the homepage always gets tab_num #1
  */
 function og_panels_menu_alter(&$items) {
+  //$items['node/%node']['page callback'] = 'og_panels_homepage_render';
   $items['node/%node/view'] = array(
     'title callback' => 'og_panels_tab_title',
     'title arguments' => array(1, NULL, TRUE),
@@ -314,7 +378,14 @@ function og_panels_main_tab_title($node)
  * Add import and export OG panels pages permission
  */
 function og_panels_perm() {
-  return array('manage OG panels pages','import and export OG panels pages');
+  return array(
+    'manage OG panels pages',
+    'import and export OG panels pages',
+    'view OG panels template list',
+    'create new OG panels template',
+    'edit OG panels template',
+    'delete OG panels template',
+  );
 }

 /**
@@ -538,13 +609,22 @@ function og_panels_page($nid, $tab_num,
  **/
 function og_panels_form(&$form_state, $group_node, $did = NULL) {
   drupal_set_title(check_plain($group_node->title));
+  $use_template = 0;
+  $template_did = 0;
   if (!is_null($did)) {
     $display = og_panels_get_one_by_display($did);
+    $panel_info = db_fetch_object(db_query("SELECT use_template, template_did FROM {og_panels} WHERE did = %d", $did));
+    if(is_object($panel_info)){
+      $use_template = $panel_info->use_template;
+      $template_did = $panel_info->template_did;
+    }
   }
   else {
     $display = new stdClass;
   }

+  $template_list = og_panels_get_template_list();
+
   $form['page_title'] = array(
     '#title' => t('Page title'),
     '#type' => 'textfield',
@@ -582,6 +662,20 @@ function og_panels_form(&$form_state, $g
     '#description' => t('If unchecked, this page is only accessible by group or site administrators. Thats useful while you are configuring the page.'),
   );

+  $form['use_templates'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Use Preset Templates'),
+    '#default_value' => $use_template,
+    '#description' => t('If checked, this page is going to use the preset templates instead of the custom ones'),
+  );
+
+  $form['template_list'] = array(
+    '#type' => 'select',
+    '#title' => t('Preset Templates'),
+    '#default_value' => $template_did,
+    '#options' => $template_list,
+  );
+
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => $did ? t('Update page') : t('Create page'),
@@ -687,6 +781,9 @@ function og_panels_form_validate($form,
   else if (db_result(db_query("SELECT path FROM {og_panels} WHERE path = '%s' AND did <> %d AND nid = %d", $form_state['values']['path'], $form_state['values']['did'], $form_state['values']['nid']))) {
     form_error($form['path'], t("That path is currently in use by another one of your group's pages. Please enter another path."));
   }
+  else if($form_state['values']['use_templates'] == 1 && $form_state['values']['template_list'] == 0){
+    form_error($form['use_templates'], t('Please select a template!'));
+  }
 }

 /**
@@ -699,25 +796,47 @@ function og_panels_form_validate($form,
  **/
 function og_panels_form_submit($form, &$form_state) {
   if ($form_state['values']['did']) {
-    $sql = "UPDATE {og_panels} SET page_title = '%s', path = '%s', published = %d, show_blocks = %d WHERE did = %d";
-    db_query($sql, $form_state['values']['page_title'], $form_state['values']['path'], $form_state['values']['published'], $form_state['values']['show_blocks'], $form_state['values']['did']);
+    if($form['values']['use_templates'] == 1 && $form_state['values']['template_list'] > 0){
+      og_panels_display_clone($form_state['values']['template_list'], $form_state['values']['did']);
+    }
+    $sql = "UPDATE {og_panels} SET page_title = '%s', path = '%s', published = %d, show_blocks = %d, use_template = %d, template_did = %d WHERE did = %d";
+    db_query($sql, $form_state['values']['page_title'], $form_state['values']['path'], $form_state['values']['published'], $form_state['values']['show_blocks'], $form_state['values']['use_templates'], $form_state['values']['template_list'], $form_state['values']['did']);
     if ($form_state['values']['path'] != '' && module_exists('path')) {
       _og_panels_tab_path_alias($form_state['values']['nid'], $form_state['values']['tab_num'], $form_state['values']['path']);
     }
     drupal_set_message(t('Group page updated.'));
   }
   else {
-    // Create a new display and record that.
-    $display = panels_new_display();
-    // TODO: let user select layout during creation.
-    $display->layout = 'twocol_stacked';
-    panels_save_display($display);
-    $form_state['values']['tab_num'] = _og_panels_tab_num($form_state['values']['nid']);
-    $sql = "INSERT INTO {og_panels} (did, nid, page_title, path, published, show_blocks, tab_num) VALUES (%d, %d, '%s', '%s', %d, %d, %d)";
-    db_query($sql, $display->did, $form_state['values']['nid'], $form_state['values']['page_title'], $form_state['values']['path'], $form_state['values']['published'], $form_state['values']['show_blocks'], $form_state['values']['tab_num']);
-    _og_panels_tab_renumber($form_state['values']['nid']);
-    drupal_set_message(t('Group page created.'));
-    $form_state['redirect'] = 'node/'. $form_state['values']['nid']. '/og_panels';
+    if($form_state['values']['use_templates'] == 0){
+      // Create a new display and record that.
+      $display = panels_new_display();
+      // TODO: let user select layout during creation.
+      $display->layout = 'twocol_stacked';
+      panels_save_display($display);
+      $form_state['values']['tab_num'] = _og_panels_tab_num($form_state['values']['nid']);
+      $sql = "INSERT INTO {og_panels} (did, nid, page_title, path, published, show_blocks, tab_num) VALUES (%d, %d, '%s', '%s', %d, %d, %d)";
+      db_query($sql, $display->did, $form_state['values']['nid'], $form_state['values']['page_title'], $form_state['values']['path'], $form_state['values']['published'], $form_state['values']['show_blocks'], $form_state['values']['tab_num']);
+      _og_panels_tab_renumber($form_state['values']['nid']);
+      drupal_set_message(t('Group page created.'));
+      $form_state['redirect'] = 'node/'. $form_state['values']['nid']. '/og_panels';
+    }
+    else{
+      $did = $form_state['values']['template_list'];
+      if($did > 0){
+        $display = panels_load_display($did);
+        $display->did = NULL;
+        panels_save_display($display);
+        $form_state['values']['tab_num'] = _og_panels_tab_num($form_state['values']['nid']);
+        $sql = "INSERT INTO {og_panels} (did, nid, page_title, path, published, show_blocks, tab_num, use_template, template_did) VALUES (%d, %d, '%s', '%s', %d, %d, %d, %d, %d)";
+        db_query($sql, $display->did, $form_state['values']['nid'], $form_state['values']['page_title'], $form_state['values']['path'], $form_state['values']['published'], $form_state['values']['show_blocks'], $form_state['values']['tab_num'], 1, $did);
+        _og_panels_tab_renumber($form_state['values']['nid']);
+        drupal_set_message(t('Group page created.'));
+        $form_state['redirect'] = 'node/'. $form_state['values']['nid']. '/og_panels';
+      }
+      else{
+        drupal_set_message(t('Please select a template!'), 'error');
+      }
+    }
   }
 }

@@ -805,14 +924,27 @@ function og_panels_nodeapi($node, $op) {
             $result = db_query("SELECT page_title FROM {og_panels} WHERE nid = %d AND published = 1 AND default_page = 0 AND tab_num = %d", $node->nid, $did);
             $row = db_fetch_object($result);
             if(is_object($row)){
+                //store the original title to another variable
+                $node->group_title = $node->title;
                 $node->title = $row->page_title;
             }
           }
         }
-        else{
+        else if(arg(2) == 'og_panels' && arg(3) == ''){
+          $node->group_title = $node->title;
+          $node->title = "";
+        }
+        else if(arg(2) == 'edit'){
+          $node->group_title = $node->title;
+        }
+        else if(arg(2) == 'og_panels' && arg(3) == 'form'){
+          $node->group_title = $node->title;
+        }
+        else if(arg(2) == ''){
           $result = db_query("SELECT page_title FROM {og_panels} WHERE nid = %d AND published = 1 AND default_page = 1", $node->nid);
           $row = db_fetch_object($result);
           if(is_object($row)){
+            $node->group_title = $node->title;
             $node->title = $row->page_title;
           }
         }
@@ -881,11 +1013,17 @@ function og_panels_admin_content() {
  * as expected while not destroying the OG panel tabs
  */
 function og_panels_form_alter(&$form, $form_state, $form_id){
-    if($form_id == 'panels_edit_display_form' && arg(0) == 'node' && arg(1) > 0 && arg(2) == 'og_panels' && arg(3) > 0){
-        if(user_access('import and export OG panels pages')){
-            $did = (int)arg(3);
-            $form['#prefix'] = drupal_get_form('og_panels_export_import_form', $did);
-        }
+    if($form_id == 'panels_edit_display_form'){
+      if(arg(0) == 'node' && arg(1) > 0 && arg(2) == 'og_panels' && arg(3) > 0){
+          if(user_access('import and export OG panels pages')){
+              $did = (int)arg(3);
+              $form['#prefix'] = drupal_get_form('og_panels_export_import_form', $did);
+          }
+      }
+      else if(arg(0) == 'admin' && arg(1) == 'settings' && arg(2) == 'og_panels_template'){
+        //do not render the cancel button
+        unset($form['buttons']['cancel']);
+      }
     }
     else if($form_id == 'views_exposed_form'){
       if(arg(0) == 'node' && arg(1) > 0){
@@ -993,4 +1131,343 @@ function og_panels_import_from_code($cod
             db_query("UPDATE {panels_pane} SET did = '%d' WHERE did = '%d'", $old_did, $new_did);
         }
    }
+}
+
+/**
+ * render the og panels template set up page
+ */
+function og_panels_template(){
+  //we define an editor to let user create a pre-defined template
+  drupal_set_breadcrumb($bc);
+  $output = "";
+  $arg_3 = arg(3);
+
+  if(strlen($arg_3) == 0){
+    $arg_3 = "list";
+  }
+
+  if(function_exists("og_panels_template_$arg_3")){
+    //set breadcrumb
+    $bc[] = l(t('Home'), '');
+    $bc[] = l(t('Administrator'), "admin");
+    $bc[] = l(t('Site Building'), "admin/build");
+    $bc[] = l(t('Organic Group Panels Templates'), "admin/build/og_panels_template");
+    drupal_set_breadcrumb($bc);
+    $output = call_user_func("og_panels_template_$arg_3");
+  }
+
+  return $output;
+}
+
+/**
+ * render the og panels template access function
+ */
+function og_panels_template_access($argument = 'list'){
+  switch($argument){
+    case 'list':
+      return user_access("view OG panels template list");
+      break;
+    case 'edit_name':
+      return user_access("edit OG panels template");
+      break;
+    case 'edit_layout':
+      return user_access("edit OG panels template");
+      break;
+    case 'edit_content':
+      return user_access("edit OG panels template");
+      break;
+    case 'delete_template':
+      return user_access("delete OG panels template");
+      break;
+  }
+}
+
+/**
+ * list og panels template list
+ */
+function og_panels_template_list(){
+  $output = "";
+
+  //present a list of templates
+  $result = db_query("SELECT * FROM {og_panels_templates} ORDER BY did DESC");
+  $rows = array();
+  $header[] = t('Template Name');
+  while($row = db_fetch_object($result)){
+    $row_data[] = t($row->name);
+    if(user_access('edit OG panels template')){
+      $header[] = t('Edit Name');
+      $row_data[] = l(t('Edit'), 'admin/build/og_panels_template/edit_name/'.$row->did);
+    }
+    if(user_access('edit OG panels template')){
+      $header[] = t('Edit Layout');
+      $row_data[] =  l(t('Edit'), 'admin/build/og_panels_template/edit_layout/'.$row->did);
+    }
+    if(user_access('edit OG panels template')){
+      $header[] = t('Edit Content');
+      $row_data[] =  l(t('Edit'), 'admin/build/og_panels_template/edit_content/'.$row->did);
+    }
+    if(user_access('delete OG panels template')){
+      $header[] = t('Delete Template');
+      $row_data[] =  l(t('Delete'),'admin/build/og_panels_template/delete_template/'.$row->did, array('query' => drupal_get_destination()) );
+    }
+
+
+    $rows[] = array(
+      'data' => $row_data,
+    );
+  }
+  $output .= drupal_render($form);
+  $output .= theme('table', $header, $rows, array('id' => 'og-panels-template-list-table')). $output;
+
+  //present with the og_panels_new_template_form
+  $output .= drupal_get_form('og_panels_new_template_form');
+  return $output;
+}
+
+/**
+ * create new og panels template
+ */
+function og_panels_template_edit_content(){
+  $display_id = arg(4);
+  if($display_id > 0){
+    drupal_set_title(t("Edit $template_name"));
+    $display = panels_load_display($display_id);
+    $layout = $display->layout;
+    if(strlen($layout) > 0){
+      panels_load_include('plugins');
+      ctools_include('context');
+      panels_load_include('common');
+      $display->content_types = panels_common_get_allowed_types('og_panels');
+      if(strlen($layout) > 0){
+        $display->layout = $layout;
+      }
+      print theme('page', panels_edit($display, "admin/build/og_panels_template/edit_content/$display_id", $display->content_types));
+      exit();
+    }
+    else{
+      drupal_goto("admin/build/og_panels_template/edit_layout/$display_id");
+    }
+  }
+  return $output;
+}
+
+function og_panels_template_edit_layout(){
+  $display_id = arg(4);
+  if($display_id > 0){
+    $display = panels_load_display($display_id);
+    if( strlen($_POST['layout']) > 0){
+      $layout = trim($_POST['layout']);
+      db_query("UPDATE {panels_display} SET layout = '%s' WHERE did = '%d'", $layout,$display_id);
+      if( strlen($display->layout) == 0 || $layout == $display->layout || count($display->content) == 0){
+        //1. no layout is defined yet, this is a new template, go to the content editing page
+        //2. the submitted layout is the same as the existing display layout, also go to the content editing page
+        //3. the display's content is empty, go to the content editing page
+        drupal_goto("admin/build/og_panels_template/edit_content/$display_id");
+        exit();
+      }
+    }
+    else{
+      if(strlen($display->layout) == 0){
+        //this display does not have a layout defined yet, select the default one, which will be the first available og_panels template
+        panels_load_include('common');
+        $allowed_layouts = panels_common_get_allowed_layout_object("og_panels");
+        if(is_array($allowed_layouts->allowed_layout_settings) && count($allowed_layouts->allowed_layout_settings) > 0){
+          $allowed_layout_names = array_keys($allowed_layouts->allowed_layout_settings);
+          $layout = $allowed_layout_names[0];
+          $display->layout = $layout;
+          db_query("UPDATE {panels_display} SET layout = '%s' WHERE did = '%d'", $layout,$display_id);
+        }
+      }
+    }
+    $output = panels_edit_layout($display, t('Save'),"admin/build/og_panels_template/edit_content/$display_id","og_panels");
+  }
+  return $output;
+}
+
+function og_panels_template_edit_name(){
+  $did = arg(4);
+  return drupal_get_form("og_panels_template_edit_info_form", $did);
+}
+
+function og_panels_template_edit_info_form(&$form_state, $did){
+  $result = db_query("SELECT name FROM {og_panels_templates} WHERE did = %d", $did);
+  $template_name = db_result($result);
+
+  $form['template']['template_name'] = array(
+    '#title' => t('Template Name'),
+    '#type' => 'textfield',
+    '#required' => TRUE,
+    '#default_value' => $template_name,
+    '#description' => t('This is the name for the OG panels template'),
+    '#size' => 32,
+  );
+
+  $form['template']['did'] = array(
+    '#type' => 'hidden',
+    '#default_value' => $did,
+  );
+
+  $form['template']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save'),
+  );
+  return $form;
+}
+
+function og_panels_template_edit_info_form_validate($form, &$form_state){
+  $template_name = trim($form_state['values']['template_name']);
+  $did = (int)$form_state['values']['did'];
+  if(strlen($template_name) == 0){
+    form_error($form['template']['template_name'], t("Template name can not be empty!"));
+  }
+  else if(preg_match("/[^A-Za-z0-9_\s]/", $template_name)){
+    form_error($form['template']['template_name'], t("Template name can only contain space, alphanumeric characters and underscore."));
+  }
+  else{
+    //see if the template name already exists
+    $result = db_query("SELECT COUNT(did) FROM {og_panels_templates} WHERE name = '%s' AND did <> %d", $template_name, $did);
+    $num = db_result($result);
+    if($num > 0){
+      form_error($form['template']['template_name'], t("Template with name $template_name already exists!"));
+    }
+  }
+}
+
+function og_panels_template_edit_info_form_submit($form_id, $form_values){
+  $template_name = $form_values['values']['template_name'];
+  $did = $form_values['values']['did'];
+  if($did > 0){
+    db_query("UPDATE {og_panels_templates} SET name = '%s' WHERE did = %d", $template_name, $did);
+  }
+}
+
+/**
+ * Delete an og panels template
+ */
+function og_panels_template_delete_template(){
+  $display_id = arg(4);
+  $output = drupal_get_form('og_panels_template_delete_confirm', $display_id);
+  return $output;
+}
+
+function og_panels_template_delete_confirm(&$form_state, $did) {
+  $form['did'] = array('#type' => 'value', '#value' => $did);
+  $sql = "SELECT name FROM {og_panels_templates} WHERE did = %d";
+  $template_name = db_result(db_query($sql, $did));
+  $form['template_name'] = array('#type' => 'value', '#value' => $template_name);
+
+  return confirm_form($form,
+    t('Are you sure you want to delete %name?', array('%name' => $template_name)),
+    isset($_GET['destination']) ? $_GET['destination']: 'admin/build/og_panels_template',
+    t('This action cannot be undone.'),
+    t('Delete'), t('Cancel')
+  );
+}
+
+function og_panels_template_delete_confirm_submit($form_id, $form_values){
+  $did = $form_values['values']['did'];
+  //first, check if the display id exists in og_panels_templates table
+  $result = db_result(db_query("SELECT COUNT(did) FROM {og_panels_templates} WHERE did = %d", $did));
+  if($result > 0){
+    db_query("DELETE FROM {og_panels_templates} WHERE did = %d", $did);
+    //remove this display from panels_display and panels_pane also
+    db_query("DELETE FROM {panels_display} WHERE did = '%d'", $did);
+    db_query("DELETE FROM {panels_pane} WHERE did = '%d'", $did);
+  }
+}
+
+function og_panels_new_template_form(){
+  $form['template'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Create new Organic Group Panels Template'),
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE,
+  );
+  $form['template']['template_name'] = array(
+    '#title' => t('Template Name'),
+    '#type' => 'textfield',
+    '#required' => TRUE,
+    '#default_value' => '',
+    '#description' => t('This is the name for the OG panels template'),
+    '#size' => 32,
+  );
+  $form['template']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Create'),
+  );
+  return $form;
+}
+
+function og_panels_new_template_form_submit($form_id, $form_values){
+  //see if the template name has already exists
+  $template_name = check_plain(trim($form_values['values']['template_name']));
+  if(strlen($template_name) > 0){
+    $result = db_query("SELECT count(did) as template_count FROM {og_panels_templates} WHERE name = '%s'", $template_name);
+    $row = db_fetch_object($result);
+    if(is_object($row) && $row->template_count == 0){
+      $display = panels_new_display();
+      //by default, set the display's layout
+      panels_save_display($display);
+      $display_id = $display->did;
+      db_query("INSERT INTO {og_panels_templates}(did, name) VALUES('%d','%s')", $display_id, $template_name);
+      drupal_goto("admin/build/og_panels_template/edit_layout/$display_id");
+    }
+    else{
+      drupal_set_message(t('The template has existed!'), 'error');
+    }
+  }
+}
+
+function og_panels_get_template_list(){
+  $templates[0] = '['.t("SELECT").']';
+  $result = db_query("SELECT * FROM {og_panels_templates} ORDER BY did DESC");
+  while($row = db_fetch_object($result)){
+    $templates[$row->did] = $row->name;
+  }
+  return $templates;
+}
+
+/**
+ * A utility function to clone the source display to the target display
+ */
+function og_panels_display_clone($src_did, $target_did){
+  $display_info = db_fetch_object(db_query(
+      "SELECT * FROM {panels_display} WHERE did = %d", $src_did
+  ));
+  $panes = db_query(
+      "SELECT * FROM {panels_pane} WHERE did = %d", $src_did
+  );
+  if(is_object($display_info)){
+      //tricky, we need to update the display's layout, layout_settings, panel_settings, cache, title, hide_title, title_pane
+      db_query("UPDATE
+                  {panels_display}
+                SET layout = '%s',
+                    layout_settings = '%s',
+                    panel_settings = '%s',
+                    cache = '%s',
+                    title = '%s',
+                    hide_title = '%d',
+                    title_pane = '%d'
+                WHERE did = '%d'",
+                $display_info->layout,
+                $display_info->layout_settings,
+                $display_info->panel_settings,
+                $display_info->cache,
+                $display_info->title,
+                $display_info->hide_title,
+                $display_info->title_pane,
+                $target_did);
+
+      //now, we need to update all panels_panes
+      //first, delete all old panes
+      db_query("DELETE FROM {panels_pane} WHERE did = %d", $target_did);
+      //then insert the new content
+      while($panes_info = db_fetch_object($panes)){
+        db_query(
+          "INSERT INTO {panels_pane}(did, panel, type, subtype, shown, access, configuration, cache, style, css, extras, position)
+                              values('%d', '%s', '%s', '%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%d')
+          ", $target_did, $panes_info->panel, $panes_info->type, $panes_info->subtype, $panes_info->shown, $panes_info->access, $panes_info->configuration, $panes_info->cache, $panes_info->style, $panes_info->css, $panes_info->extras, $panes_info->position
+        );
+      }
+   }
 }
