You are here

function homebox_build in Homebox 7.2

Same name and namespace in other branches
  1. 6.3 homebox.module \homebox_build()
  2. 6 homebox.module \homebox_build()
  3. 6.2 homebox.module \homebox_build()
  4. 7.3 homebox.module \homebox_build()

Responsible for firing the hook_theme().

Parameters

$page: A page object

Return value

homebox_theme() call

1 call to homebox_build()
homebox_og_override_group in homebox_og/homebox_og.module
Override a group's homepage with a homebox
2 string references to 'homebox_build'
homebox_menu in ./homebox.module
Implements hook_menu().
homebox_og_menu in homebox_og/homebox_og.module
Implements hook_menu().

File

./homebox.module, line 348
Homebox main file, takes care of global functions settings constants, etc.

Code

function homebox_build($page) {
  global $user;

  // If no default block layout is set, return a simple message
  if (empty($page->settings['blocks'])) {
    return t('This page has not yet been configured.');
  }

  // Get every block placed into its region sorted by weight
  $column_count = $page->settings['regions'];
  $regions = array_fill(1, $column_count, array());

  // Extract blocks from the page
  $blocks = $page->settings['blocks'];
  $allowed_blocks = array();
  $info = array();
  foreach ($blocks as $block) {
    if (_homebox_can_view_block((object) $block)) {
      if (!isset($info[$block['module']])) {
        $info[$block['module']] = module_invoke($block['module'], 'block_info');
      }
      if (isset($info[$block['module']][$block['delta']])) {
        $allowed_blocks[$block['module']][$block['delta']] = $info[$block['module']][$block['delta']];
        if (!empty($block['title'])) {
          $allowed_blocks[$block['module']][$block['delta']]['info'] = $block['title'];
        }
      }
    }
  }

  // Get user settings, so custom blocks are placed in regions.
  $user_blocks = _homebox_get_user_settings($page);
  if ($user_blocks !== FALSE) {

    // Add custom blocks.
    foreach ($user_blocks as $key => $block) {
      if (isset($allowed_blocks[$block['module']][$block['delta']])) {
        $blocks[$key] = $user_blocks[$key];
      }
    }
  }

  // Preparing blocks object for theming
  foreach ($blocks as $key => $block_settings) {

    // Adds block to its regions
    if ($block_settings['status']) {
      $block = homebox_prepare_block($key, $page);
      if (!is_null($block)) {

        // If user defined region is greater than real column count put block in
        // the last column/region.
        $regions[min($block->region, $column_count)][$block->weight][] = $block;
        $allowed_blocks[$block->module][$block->delta]['used'] = TRUE;
      }
    }
  }

  // If custom widths aren't set, make each width a percentage of the total available.
  if (!count($page->settings['widths'])) {
    $num_regions = count($regions);
    for ($i = 1; $i <= $num_regions; $i++) {

      // We use a combination of round() and floor() to get this rounded to two decimal places
      // since the $mode argument isn't introducted into round() until PHP 5.3.0.
      $page->settings['widths'][$i] = round(floor(100 / $num_regions * 100) / 100, 2);
    }
  }

  // Sort each region/column based on key value
  // Also separate the regions into rows based on the region widths
  $sum_width = 0;
  $row = 0;
  for ($i = 1; $i <= count($regions); $i++) {
    ksort($regions[$i]);
    if (!empty($page->settings['widths'][$i])) {
      $sum_width += $page->settings['widths'][$i];
      if ($sum_width > 100) {
        $row++;
        $sum_width = $page->settings['widths'][$i];
      }
    }
    $page->settings['rows'][$i] = $row;
  }

  // Add block links
  if ($user->uid) {
    $add_links = array();
    foreach ($allowed_blocks as $module => $blocks) {
      foreach ($blocks as $delta => $info) {
        $options = array();
        if (isset($info['used'])) {
          $options['attributes'] = array(
            'class' => array(
              'used',
            ),
          );
        }
        $add_links[] = homebox_add_link($info['info'], $page, $module, $delta, $options);
      }
    }
    $add_links['restore'] = l(t('Restore to defaults'), 'homebox/' . $page->name . '/restore', array(
      'attributes' => array(
        'class' => array(
          'restore',
        ),
      ),
    ));
    $add_links = theme('item_list', array(
      'items' => $add_links,
      'title' => NULL,
      'type' => 'ul',
      'attributes' => array(
        'class' => array(
          'clearfix',
        ),
      ),
    ));
    $form = drupal_get_form('homebox_save_form', $page);
    $save_form = drupal_render($form);
  }
  else {
    $add_links = NULL;
    $save_form = NULL;
  }

  // Build output
  $output = theme('homebox', array(
    'regions' => $regions,
    'available_blocks' => $allowed_blocks,
    'column_count' => $column_count,
    'page' => $page,
    'add_links' => $add_links,
    'save_form' => $save_form,
  ));

  // Build the page
  if ($page->settings['full']) {

    // TODO Changed based on discussion at http://drupal.org/node/224333#theme_page.
    // print theme('page', $output, FALSE);
    drupal_set_page_content($output);

    // Return page attributes, and add a custom attribute to
    // look for in hook_page_alter().
    $page = element_info('page');
    $page['#homebox_hide_blocks'] = TRUE;
    return $page;
  }
  else {

    // Simply return the page
    return $output;
  }
}