You are here

function panels_add_content in Panels 5.2

Same name and namespace in other branches
  1. 6.2 includes/display-edit.inc \panels_add_content()
1 call to panels_add_content()
panels_ajax_add_content in includes/display_edit.inc
Entry point for AJAX: 'Add Content' modal form, from which the user selects the type of pane to add.

File

includes/display_edit.inc, line 722

Code

function panels_add_content($cache, $panel_id) {
  $return = new stdClass();
  $return->type = 'display';
  $return->title = t('Choose type');

  // TODO get rid of this deprecated method
  panels_set('return', $return);
  if (!isset($cache->content_types)) {
    $cache->content_types = panels_get_available_content_types();
  }
  $weights = array(
    t('Contributed modules') => 0,
  );
  $categories = array();
  $titles = array();
  foreach ($cache->content_types as $type_name => $subtypes) {
    if (is_array($subtypes)) {
      foreach ($subtypes as $subtype_name => $subtype_info) {
        $title = filter_xss_admin($subtype_info['title']);
        $description = isset($subtype_info['description']) ? $subtype_info['description'] : $title;
        if (isset($subtype_info['icon'])) {
          $icon = $subtype_info['icon'];
          $path = isset($subtype_info['path']) ? $subtype_info['path'] : panels_get_path("content_types/{$type_name}");
        }
        else {
          $icon = 'no-icon.png';
          $path = panels_get_path('images');
        }
        if (isset($subtype_info['category'])) {
          if (is_array($subtype_info['category'])) {
            list($category, $weight) = $subtype_info['category'];
            $weights[$category] = $weight;
          }
          else {
            $category = $subtype_info['category'];
            if (!isset($weights['category'])) {
              $weights[$category] = 0;
            }
          }
        }
        else {
          $category = t('Contrib modules');
        }
        $output = '<div class="content-type-button clear-block">';
        $link_text = theme('image', $path . '/' . $icon, $description, $description);
        $output .= l($link_text, 'javascript: void()', array(
          'class' => 'panels-modal-add-config',
          'id' => $type_name . '-' . $panel_id . '-' . $subtype_name,
        ), NULL, NULL, NULL, TRUE);
        $output .= "<div>" . l($title, 'javascript: void()', array(
          'class' => 'panels-modal-add-config',
          'id' => $type_name . '-' . $panel_id . '-' . $subtype_name,
        ), NULL, NULL, NULL, TRUE) . "</div>";
        $output .= '</div>';
        if (!isset($categories[$category])) {
          $categories[$category] = array();
          $titles[$category] = array();
        }
        $categories[$category][] = $output;
        $titles[$category][] = $title;
      }
    }
  }
  if (!$categories) {
    $output = t('There are no content types you may add to this display.');
  }
  else {
    asort($weights);
    $output = '';
    $columns = 3;
    foreach (range(1, $columns) as $column) {
      $col[$column] = '';
      $size[$column] = 0;
    }
    foreach ($weights as $category => $weight) {
      $which = 1;

      // default;
      $count = count($titles[$category]) + 3;

      // Determine which column to use by seeing which column has the most
      // free space. This algorithm favors left.
      foreach (range($columns, 2) as $column) {
        if ($size[$column - 1] - $size[$column] > $count / 2) {
          $which = $column;
          break;
        }
      }
      $col[$which] .= '<div class="panels-section">';
      $col[$which] .= '<h3 class="panels-section-title">' . $category . '</h3>';
      $col[$which] .= '<div class="panels-section-decorator"></div>';
      $col[$which] .= '<div class="panels-section-content">';
      if (is_array($titles[$category])) {
        natcasesort($titles[$category]);
        foreach ($titles[$category] as $id => $title) {
          $col[$which] .= $categories[$category][$id];
        }
      }
      $col[$which] .= '</div>';
      $col[$which] .= '</div>';
      $size[$which] += $count;

      // add 3 to account for title.
    }
    foreach ($col as $column) {
      $output .= '<div class="panels-section-column">' . $column . '</div>';
    }
  }
  return $output;
}