mysite_get_content_element

Definition

mysite_get_content_element($owner, $edit, $label, $options, $style = 'table', $page = 0)
mysite.module, line 1940

Description

Given values, generate a table that behaves like a form so that users my add content to their MySite page.

Parameters

$owner The $user object for the owner of this MySite page.

$edit The values passes from mysite_content_form(). Originally, this was going to be a form, but it was easier to make it links to menu callbacks.

$label The name of the content type as defined by the mysite_type() array.

$options The content options available to be added to a MySite page.

$style The layout to use for the form. Values are 'table' and 'grid.'

$page The current page of the mysite collection.

Return value

A Drupal-themed table of content choices, with action links.

Code

function mysite_get_content_element($owner, $edit, $label, $options, $style = 'table', $page = 0) {
  global $user;

  // paginate the content browser
  $pager = $_GET['page'];
  if (empty($pager) || !is_numeric($pager)) {
    $pager = 1;
  }
  $item = $options['options'];
  // find items already added
  $items = array();
  $current = db_query("SELECT type, type_id FROM {mysite_data} WHERE uid = %d OR (uid = 0 AND locked = 1)", $owner->uid);
  $check = array();
  while ($data = db_fetch_object($current)) {
    $check[] = $data->type .'-'. $data->type_id;
  }
  for ($i = 0; $i < count($item['name']); $i++) {
    $test = $item['type'][$i] .'-'. $item['type_id'][$i];
    if (!in_array($test, $check)) {
      $items['group'][] = $item['group'][$i];
      $items['name'][] = $item['name'][$i];
      $items['type_id'][] = $item['type_id'][$i];
      $items['type'][] = $item['type'][$i];
      $items['icon'][] = $item['icon'][$i];
    }
  }
  $item = $items;

  // layout settings
  $threshold = variable_get('mysite_browser_pager', 25);
  $max = variable_get('mysite_browser_max', 4);

  // pagination logic
  $start = ($pager -1) * $threshold;
  $end = $start + $threshold - 1;
  if ($end >= count($item['name'])) {
    $end = count($item['name']) - 1;
  }
  $pages = ceil(count($item['name']) / $threshold);
  $pager_output = theme('mysite_pager', $pages, $pager);
  $count = count($item['type_id']);
  $total = $item['count'];
  $depth = 0;
  if (is_array($item['group'])) {
    $titles = array();
    $pos = array();
    $titles = array_unique($item['group']);
    $depth = count($titles);
    foreach ($titles as $title) {
      $pos[$title] = array_search($title, $item['group']);
    }
  }
  if ($style == 'table') {
    $max = 2;
    $header = array(ucwords($options['name']), t('Actions'));
  }
  else if (module_exists('mysite_icon')) {
    $j = 0;
    $sub = array();
    if ($count < $max) {
      $max = $count;
    }
    $icon_link = FALSE;
    if (user_access('administer mysite')) {
      $icon_link = TRUE;
    }
    $header = array(array('data' => ucwords($options['name'])), 'attributes' => array('colspan' => $max));
  }
  $rows = array();
  $group = '';
  // using positional arrays lets us keep the $items array to one-dimension, avoiding array loop issues
  for ($i = $start; $i <= $end; $i++) {
    if (($i == $start) || current($pos) == $i) {
      $group = $item['group'][$i];
      array_shift($pos);
      $rows[] = array(array('data' => t('<b>@sub</b>', array('@sub' => $item['group'][$i])), 'colspan' => $max));
    }
    // insert a content group row
    $blank_rows = FALSE;
    if (current($pos) == $i+1) {
      if ($i > 0) {
        $blank_rows = TRUE;
      }
    }
    $name = check_plain($item['name'][$i]);
    $add = mysite_add_link($owner->uid, $item['type'][$i], $item['type_id'][$i], $page);
    if ($style == 'table') {
      $rows[] = array('data' => array($name, l(t('add'), $add)));
    }
    else {
      $icon_edit = '';
      if ($icon_link) {
        $icon_edit = theme('mysite_icon_add_link', $item['type'][$i], $item['type_id'][$i]);
      }
      $image = theme('mysite_icon', $item['type'][$i], $item['type_id'][$i], $name, $item['icon'][$i]);
      $sub[] = array('data' => '<div class="mysite-browser">'. l($image, $add, array(), NULL, NULL, FALSE, TRUE) . l($name, $add) . $icon_edit .'</p></div>');
      $j++;
      if ($j == $max || $i == $end || $blank_rows) {
        $rows[] = $sub;
        unset($sub);
        $j = 0;
      }
    }
  }
  $table = '';
  $table = t('<h3>@cat to your personal page</h3>', array('@cat' => $options['label']));
  $table .= '<a name="'. filter_xss_admin($options['name']) .'"></a>';
  $table .= '<p class="help">'. filter_xss_admin($options['help']) .'</p>';
  $table .= '<fieldset>';
  if ($options['search'] && function_exists('mysite_type_'. $label .'_search')) {
    $table .= module_invoke('mysite_type', $label .'_search', $owner->uid);
  }
  if (!empty($rows)) {
    $table .= '<b>'. t('Browse available options') .'</b>';
    $table .= theme_table($header, $rows);
    if ($pager_output) {
      $table .= $pager_output;
    }
  }
  else {
    $table .= '<p>'. t('All available @type content is already on your personal page.', array('@type' => $label)) .'</p>';
  }
  $table .= '</fieldset>';

  // if this type has a unique form, it goes here
  if ($options['form']) {
    $table .= module_invoke('mysite_type', $label .'_form', $owner);
  }
  return $table;
}