You are here

function panels_ajax_flexible_edit_remove in Panels 6.3

Same name and namespace in other branches
  1. 7.3 plugins/layouts/flexible/flexible.inc \panels_ajax_flexible_edit_remove()

AJAX responder to remove an existing row, column or region from a flexible layout.

1 string reference to 'panels_ajax_flexible_edit_remove'
flexible.inc in plugins/layouts/flexible/flexible.inc

File

plugins/layouts/flexible/flexible.inc, line 1545

Code

function panels_ajax_flexible_edit_remove($handler, $id) {
  $settings =& $handler->display->layout_settings;
  panels_flexible_convert_settings($settings, $handler->plugins['layout']);
  if (empty($settings['items'][$id])) {
    ctools_ajax_render_error(t('Invalid item id.'));
  }
  $item =& $settings['items'][$id];
  $css_id = isset($handler->display->css_id) ? $handler->display->css_id : '';

  // Create a renderer object so we can render our new stuff.
  $renderer = panels_flexible_create_renderer(TRUE, $css_id, array(), $settings, $handler->display, $handler->plugins['layout'], $handler);
  $siblings =& $settings['items'][$item['parent']]['children'];
  $parent_class = '.' . $renderer->base[$settings['items'][$item['parent']]['type']] . '-' . $item['parent'];

  // Find the offset of our array. This will also be the key because
  // this is a simple array.
  $offset = array_search($id, $siblings);

  // Only bother with this stuff if our item is fluid, since fixed is
  // as fixed does.
  if ($item['type'] != 'row') {
    if (isset($siblings[$offset + 1])) {
      $next = $siblings[$offset + 1];
    }
    if (isset($siblings[$offset - 1])) {
      $prev = $siblings[$offset - 1];
    }
    if ($item['width_type'] == '%') {

      // First, try next.
      if (isset($next) && $settings['items'][$next]['width_type'] == '%') {
        $settings['items'][$next]['width'] += $item['width'];
      }
      else {
        if (isset($prev) && $settings['items'][$prev]['width_type'] == '%') {
          $settings['items'][$prev]['width'] += $item['width'];
        }
      }
    }

    // Not sure what happens if they both failed. Maybe nothing.
  }

  // Remove the item.
  array_splice($siblings, $offset, 1);
  unset($settings['items'][$id]);

  // Save our new state.
  panels_edit_cache_set($handler->cache);
  $class = $renderer->base[$item['type']] . '-' . $id;
  $output = array();
  $output[] = ctools_ajax_command_remove('#panels-dnd-main .' . $class);

  // Regenerate the CSS for siblings.
  if (!empty($siblings)) {

    // Get all the CSS necessary for the entire row (as width adjustments may
    // have cascaded).
    $css = array();
    panels_flexible_get_css_group($css, $renderer, $siblings, $parent_class, $item['type'], $item['parent']);
    foreach ($css as $selector => $data) {
      $output[] = ctools_ajax_command_css($selector, $data);
    }
  }

  // There are potentially two splitters linked to this item to be removed.
  if (!empty($prev)) {
    $output[] = ctools_ajax_command_remove('.flexible-splitter-for-' . $renderer->base[$item['type']] . '-' . $prev);
  }

  // Try to remove the 'next' one even if there isn't a $next.
  $output[] = ctools_ajax_command_remove('.flexible-splitter-for-' . $renderer->base[$item['type']] . '-' . $id);
  if (!empty($prev) && !empty($next)) {

    // Add a new splitter that links $prev and $next:
    $splitter = panels_flexible_render_splitter($renderer, $prev, $next);
    $prev_class = '#panels-dnd-main .' . $renderer->base[$item['type']] . '-' . $prev;
    $output[] = ctools_ajax_command_after($prev_class, $splitter);

    // Send our fix height command.
    $output[] = array(
      'command' => 'flexible_fix_height',
    );
  }

  // Rerender our parent item links:
  $output[] = ctools_ajax_command_replace('.flexible-links-' . $item['parent'], panels_flexible_render_item_links($renderer, $item['parent'], $settings['items'][$item['parent']]));
  $output[] = array(
    'command' => 'flexible_fix_firstlast',
    'selector' => $parent_class . '-inside',
    'base' => 'panels-flexible-' . $item['type'],
  );
  $handler->commands = $output;
}