mysite_page

Definition

mysite_page($uid = NULL, $page = 0, $pageview = TRUE)
mysite.module, line 669

Description

The default MySite page view.

Parameters

$uid The user id of the owner of the mysite.

$page The current page of the mysite collection.

$pageview Boolean flag that indicates the data will be viewed using theme_page. If you want to access this data externally, set $pageview = FALSE.

Return value

$content array. If $pageview = FALSE, the function will return a $content array with three elements. $content['owner'] = the $user object of the MySite page's owner. $content['mysite'] = MySite data for the page's owner. $content['data'] = MySite data as defined by mysite_type_{name}_data(). If the user does not have a MySite page, $content will return an empty array().

Code

function mysite_page($uid = NULL, $page = 0, $pageview = TRUE) {
  global $user;

  // get the $owner of this mysite, use as a $user surrogate
  if (is_null($uid)) {
    $uid = arg(1);
  }
  if (is_numeric($uid)) {
    $owner = user_load(array('uid' => $uid));
  }
  if (!isset($owner->uid)) {
    $owner = $user;
  }
  // Load the settings for this page.
  $mysite = mysite_load_page($owner->uid, $page);

  // access control set by the user 'status' flag
  $show = FALSE;
  // if the user is looking at the MySite data
  if ($user->uid == $owner->uid) {
    $show = TRUE;
    if ($pageview) {
      // get any system messages waiting for this user
      mysite_message($owner->uid);

      // this user has no page, give them help making one
      if (user_access('edit mysite')) {
        if (!isset($mysite->uid)) {
          $output = theme('mysite_create_help', $owner);
          return $output;
        }
      }
      else {
        return drupal_access_denied();
      }
    }
  }
  // access checks: can this user view this data?
  // these checks are here for external use of this function
  else if (user_access('administer mysite') || $mysite->uid == 0 || $mysite->status == 1) {
    $show = TRUE;
  }
  // show the page or return the data
  if ($show) {
    mysite_load($mysite, $pageview);
    // the cache wrapper
    $content = NULL;
    $cache_set = variable_get('mysite_cache', 0);
    if ($cache_set > 0) {
      $expire = db_result(db_query("SELECT expire FROM {cache} WHERE cid = '%s'", 'mysite:'. $mysite->uid .':'. $mysite->page));
      if (time() < $expire) {
        $data = cache_get('mysite:'. $mysite->uid .':'. $mysite->page, 'cache');
        $content = unserialize($data->data);
      }
      if (isset($content['owner']) && empty($content['data'])) {
        // This page is blank in the cache, so just render the approprate message.
        return mysite_output($owner, $mysite, $content = array(), $pageview);
      }
    }
    if (empty($content)) {
      $data = array();
      /* get the data for this user
          for each type of content listed in the user's mysite_data set
          run the appropriate include / theme function
          append the themed data to the $data array
          using the order value as the key
      */

      $sql = 'SELECT mid, uid, page, title, type, type_id, sort, position, format, settings, locked FROM {mysite_data} WHERE (uid = %d OR (uid = 0 AND locked = 1)) AND page = %d ORDER BY position ASC, sort ASC';
      $result = db_query($sql, $owner->uid, $page);
      $i = 0;
      $allowed = variable_get('mysite_content', array());
      while ($item = db_fetch_object($result)) {
        // check to see if the content option is still valid
        if (!empty($allowed[$item->type])) {
          mysite_load_includes('types', $item->type);
          // enable the format files
          mysite_load_includes('formats', $item->format);
          $data[$item->position][$i]['title'] = check_plain($item->title);
          $data[$item->position][$i]['mid'] = $item->mid;
          $data[$item->position][$i]['format'] = $item->format;
          $data[$item->position][$i]['locked'] = $item->locked;
          $settings = unserialize($item->settings);
          $data[$item->position][$i]['output'] = module_invoke('mysite_type', $item->type .'_data', $item->type_id, $settings);
          $data[$item->position][$i]['link'] = $data[$i]['output']['base'];
          if (!$item->locked && (user_access('edit mysite') && $user->uid == $uid) || user_access('administer mysite')) {
            $data[$item->position][$i]['actions'] = theme('mysite_actions', $item->uid, $item->mid, $page);
          }
          $i++;
        }
        else {
          $sql = "DELETE FROM {mysite_data} WHERE mid = %d";
          db_query($sql, $item->mid);
          drupal_set_message(t('The %title item is no longer available.', array('%title' => $item->title)));
        }
      }
      // cast the data sets as a single array so they can be passed
      $content['owner'] = $owner;
      $content['mysite'] = $mysite;
      $content['data'] = $data;
      if ($cache_set > 0) {
        cache_set('mysite:'. $mysite->uid .':'. $mysite->page, 'cache', serialize($content), time() + $cache_set);
      }
    }
    else {
      // if reading from cache, we must load all the format includes
      mysite_load_includes('formats', NULL, TRUE);
    }
    return mysite_output($owner, $mysite, $content, $pageview);
  }
  else if ($user->uid > 0 && user_access('edit mysite')) {
    drupal_goto('mysite/'. $user->uid .'/view');
  }
  else {
    return drupal_access_denied();
  }
}