mysite_content

Definition

mysite_content($uid = NULL, $page = 0, $type = NULL)
mysite.module, line 999

Description

Page for adding content to a MySite page.

Parameters

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

$page The current page of the MySite collection.

$type The type of content options to display.

Code

function mysite_content($uid = NULL, $page = 0, $type = NULL) {
  global $user;
  $submenu = mysite_content_menu();
  $check = FALSE;

  // get the $owner of this MySite page, use as a $user surrogate
  $uid = arg(1);
  if (is_numeric($uid) && $uid > 0) {
    $owner = user_load(array('uid' => $uid));
  }

  // Make sure the $type is valid and active.
  $type = arg(4);
  $allowed = variable_get('mysite_content', array());
  if ($type === $allowed[$type]) {
    $only = $type;
  }
  else {
    drupal_goto('mysite/'. $uid .'/content/'. $page);
  }

  // set values if we are updating the admin settings
  if ($uid == 0 && user_access('administer mysite')) {
    $owner->uid = 0;
    $check = TRUE;
  }

  // must have a valid uid to continue
  if ($owner->uid > 0 || $check) {
    // get any system messages waiting for this user
    mysite_message($owner->uid);

    // create a new account, if needed.
    $mysite = mysite_get($owner->uid, TRUE);
    if (!isset($mysite->uid)) {
      $mysite = mysite_create_account($owner);
    }

    if ($_POST) {
      $edit = $_POST['edit'];
    }
    // add submenu links
    if (!empty($submenu)) {
      $output .= theme('mysite_submenu', $submenu, $page);
    }
    $output .= '<div class="mysite-mainmenu">';
    if (empty($only)) {
      $layout = mysite_get_layout($owner->uid);
      $output .= t('<h3>Content</h3>');
      // ajax-generated message class
      $output .=  '<div class="mysite-ajax"></div>';
      $header = array(t('position'), t('sort'), t('title'), t('actions'), t('rename'));
      $sql = 'SELECT mid, uid, page, title, type, type_id, sort, position FROM {mysite_data} WHERE uid = %d AND page = %d ORDER BY position ASC, sort ASC';
      $result = db_query($sql, $owner->uid, $page);
      $rows = array();
      $edit = array();
      $mid = array();
      while ($entry = db_fetch_array($result)) {
        $mid[] = $entry['mid'];
        // check the position against the layout
        $pos = $entry['position'];
        if ($pos > $layout['count'] - 1) {
          $pos = $layout['count'] - 1;
        }
        $rows[$pos][] = array('data' => array(check_plain($entry['title']), theme('mysite_actions', $entry['uid'], $entry['mid'], $page)));
        $edit[] = $entry;
      }
      for ($i = 0 ; $i <= $layout['count'] - 1; $i++) {
        if (empty($rows[$i])) {
          $rows[$i][] = array('data' => array(t('This region is empty'), NULL));
        }
      }
      if (!empty($rows)) {
        $output .= '<div class="mysite-current">';
        $output .= t('<h3>Current content</h3>');
        $i = 0;
        $move = drupal_get_path('module', 'mysite') .'/plugins/icons/move.png';
        $msg = t('Click and drag to sort');
        foreach ($rows as $key => $data) {
          $output .= '<h5 class="mysite-divider">'. t('!name region', array('!name' => $layout['regions'][$key])) .'</h5>';
          $output .= '<div class="mysite-sortable" id="mysite-sort'. $key .'">';
          foreach ($data as $row) {
            $output .= '<div class="mysite-dnd sortable-item" id="m'. $mid[$i] .'">';
            if (!empty($row['data'][1])) {
              $output .= '<div class="mysite-grabber"><span class="mysite-header">'. theme('image', $move, $msg, $msg) .'</span></div>';
            }
            foreach ($row['data'] as $key => $data) {
              $output .= '<div class="mysite-table-'. $key .'">'. $data .'</div>';
            }
            $output .= '</div>';
            $i++;
          }
          $output .= '</div>';
        }
        $output .= '</div>';
        $output .= theme('mysite_submenu', $submenu, $page, TRUE, TRUE, 'mysite-options');
      }
      else {
        $output .= t('<p>You have no content saved.</p>');
        $output .= theme('mysite_content_help', $owner, $page);
      }
    }

    // set the page title
    drupal_set_title(check_plain($mysite->title) .' : '. mysite_sitename());

    // show the add content forms
    if (!empty($only)) {
      $max = variable_get('mysite_items', 10);
      $result = db_fetch_object(db_query("SELECT COUNT(mid) as count FROM {mysite_data} WHERE uid = %d", $owner->uid));
      if ($result->count < $max) {
        $output .= mysite_content_form($owner, $edit, $only, $page);
      }
      else {
        $output .= t('<h3>Personal content is full</h3>');
        $output .= t('<p>You cannot add any new content without first removing an item.</p><p>Only @max items can be displayed on your personal page at one time.</p>', array('@max' => $max));
        $output .= '<p>'. l(t('View your personal page'), 'mysite/'. $owner->uid .'/view') .'</p>';
      }
    }
    $output .= '</div>';
    print theme('page', $output, variable_get('mysite_fullscreen', 1));
  }
  else {
    drupal_not_found();
    return;
  }
}