mysite_prepare_columns

Definition

mysite_prepare_columns($mysite, $data = array(), $cols = 1)
mysite.module, line 850

Description

Prepare the output into the proper column for the layout.

If a column is empty or does not exist, this function will collapse the data into the nearest available column. For example, if the user had a three-column layout, with items assigned $position 1, 2, and 3, and switches to a one-column layout, this function will collapse items in positions 2 and 3 into position 1.

The function does not save position changes to the database.

All layout functions must call this function to prepare data for presentation.

Parameters

$mysite Data object for this user's MySite page taken from the {mysite} table.

$data The $data array passed to the layout function by mysite_page().

$cols The number of columns (or regions) on the layout.

Return value

$data array An array of data as defined by mysite_page(), but with empty regions collapsed and filled with a message to the user.

Code

function mysite_prepare_columns($mysite, $data = array(), $cols = 1) {
  global $user;
  // reset the key to zero if there is only one column
  if (count($data) == 1) {
    sort($data);
    $new = $data;
  }
  else {
    // otherwise, slide existing data to a new array and unset the old
    $new = array();
    for ($i = 0; $i < $cols; $i++) {
      $new[$i] = $data[$i];
      unset($data[$i]);
    }
    // if there is leftover data, merge it into the last element of the new array;
    if (!empty($data)) {
      // set the columns correctly for the new keys
      $set = $cols - 1;
      if (empty($new[$set])) {
        $new[$set] = array();
      }
      // loop the remaining data and merge it to the last region
      foreach ($data as $array) {
        if (!empty($array)) {
          $new[$set] = array_merge($new[$set], $array);
        }
      }
    }
  }
  if ($cols > 1) {
    // handles empty regions gracefully
    for ($i = 0; $i < $cols; $i++) {
      if (empty($new[$i])) {
        if (user_access('administer mysite') || (user_access('edit mysite') && $user->uid == $mysite->uid)) {
          $new[$i][0]['title'] = t('Add content');
          $new[$i][0]['mid'] = NULL;
          $new[$i][0]['format'] = $mysite->format;
          $new[$i][0]['output']['items'][0]['content'] = theme('mysite_empty_column', $mysite);
        }
        else {
          // this should probably be themed
          $new[$i][0] = array();
        }
      }
    }
  }
  // We only have one column, so return it.
  else {
    $new = $new[0];
  }
  return $new;
}