You are here

function workspace_list in Workspace 5

The default admin interface is a list of content.

1 call to workspace_list()
workspace_admin in ./workspace.module

File

./workspace.module, line 156
Presents a user-centric view of content.

Code

function workspace_list() {
  global $user;
  $maxnodes = $user->workspaces ? $user->workspaces['default']['maxnodes'] : 50;
  $maxfilenames = $user->workspaces ? $user->workspaces['default']['maxfilenames'] : 50;
  if (!is_numeric($maxnodes)) {
    $maxnodes = 50;
  }
  if (!is_numeric($maxfilenames)) {
    $maxfilenames = 50;
  }
  $comments_enabled = module_exists('comment');
  $nodeperm_role_enabled = module_exists('nodeperm_role');
  $flexinode_enabled = module_exists('flexinode');
  $cck_enabled = module_exists('content');
  $output = _workspace_addform();
  $node_select = array(
    'n.nid',
    'n.uid',
    'n.type',
    '0 AS cid',
    'n.title',
    'n.status',
    'n.changed',
    's.comment_count',
    '1 AS node',
  );
  $node_from = array(
    '{node} n',
  );
  $node_join = array(
    'LEFT JOIN {node_comment_statistics} s ON n.nid = s.nid',
  );
  $node_where = array(
    'n.uid = ' . db_escape_string($user->uid),
  );
  if ($nodeperm_role_enabled) {
    $node_select[] = 'na.grant_update';
    $node_select[] = 'na.grant_delete';
    $node_join[] = 'LEFT JOIN node_access na ON n.nid = na.nid';
    $node_where[] = 'OR (na.realm = "nodeperm_role" AND na.grant_view = 1 AND na.gid IN(' . db_escape_string(implode(',', array_flip($user->roles))) . '))';
  }
  $sql = 'SELECT ' . implode(', ', $node_select) . ' FROM ' . implode(', ', $node_from) . ' ' . implode(' ', $node_join) . ' WHERE ' . implode(' ', $node_where);
  $count_sql = 'SELECT COUNT(n.nid) FROM ' . implode(', ', $node_from) . ' ' . implode(' ', $node_join) . ' WHERE ' . implode(' ', $node_where);
  if ($comments_enabled) {
    $select = '';
    if ($nodeperm_role_enabled) {

      // Need to add placeholders for columns to avoid misaligning our union clause.
      $select = ", '' AS grant_update, '' AS grant_delete";
    }
    $comment_sql = 'SELECT c.nid AS cnid, c.uid, "" AS type, c.cid, c.subject, c.status, c.timestamp, c.pid, 0 ' . $select . ' FROM {comments} AS c, {node} n LEFT JOIN {node_comment_statistics} s ON n.nid = s.nid WHERE c.uid = ' . db_escape_string($user->uid);
    $sql .= ' UNION ' . $comment_sql;
    $count_sql = 'SELECT COUNT(n.nid) + COUNT(c.cid) FROM ' . implode(', ', $node_from) . ' ' . implode(' ', $node_join) . ' LEFT JOIN {comments} AS c ON c.uid = ' . db_escape_string($user->uid) . ' WHERE ' . implode(' ', $node_where);
  }

  // Build the combined node/comment listing.
  $header = array(
    array(
      'data' => t('Type'),
      'field' => 'type',
    ),
    array(
      'data' => t('Title'),
      'field' => 'title',
    ),
    array(
      'data' => t('Owner'),
      'field' => 'uid',
    ),
    array(
      'data' => t('Published'),
      'field' => 'status',
    ),
    array(
      'data' => t('Modified'),
      'field' => 'changed',
      'sort' => 'desc',
    ),
    $comments_enabled ? array(
      'data' => t('Replies'),
      'field' => 'comment_count',
    ) : array(
      'data' => '',
    ),
    array(
      'data' => t('Operations'),
      'colspan' => 2,
    ),
  );
  $result = pager_query($sql . tablesort_sql($header), $maxnodes, 0, $count_sql);
  $yes = t('yes');
  $no = t('no');
  while ($row = db_fetch_object($result)) {

    // It's a node.
    if ($row->node == 1) {

      // Edit and delete permissions are set by the node type's access hook.
      // If no access hook is found, node-level permissions are then used.
      // This approach is part of Drupal's core design.
      $can_edit = FALSE;
      $can_delete = FALSE;

      //Check the node type's access hook.
      $function = $row->type . '_access';
      if ($flexinode_enabled && strstr($function, 'flexinode-')) {
        $function = 'flexinode_access';
      }
      elseif ($cck_enabled && strstr($function, 'content_')) {
        $function = 'content_access';
      }
      else {
        $function = 'node_access';
      }
      if (function_exists($function)) {
        $can_edit = $function('update', $row) ? TRUE : FALSE;
        $can_delete = $function('delete', $row) ? TRUE : FALSE;
      }
      elseif ($nodeperm_role_enabled) {
        $can_edit = $row->grant_update ? TRUE : FALSE;
        $can_delete = $row->grant_delete ? TRUE : FALSE;
      }

      // The name of the owner of this node.
      $name = $user->uid == $row->uid ? $user->name : db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $row->uid));
      $rows[] = array(
        node_get_types('name', $row->type),
        l($row->title, "node/{$row->nid}"),
        $name,
        $row->status ? $yes : $no,
        format_date($row->changed, 'small'),
        $comments_enabled ? array(
          'data' => $row->comment_count ? $row->comment_count : 0,
          'align' => 'right',
        ) : array(
          'data' => '',
        ),
        $can_edit ? l(t('edit'), "node/{$row->nid}/edit") : '',
        $can_delete ? l(t('delete'), "workspace/delete/{$row->nid}") : '',
      );
    }
    else {

      // It's a comment.
      $num_replies = comment_num_replies($row->cid);
      $comment->cid = $row->cid;
      $comment->nid = $row->nid;

      // Delegate access permission checks and link generation to comments.module.
      $com_links = comment_links($comment, 0);
      $link = l(t('edit'), "comment/edit/{$comment->cid}");
      $edit_link = $com_links['comment_edit'] ? $link : '';
      $link = l(t('delete'), "comment/delete/{$comment->cid}");
      $delete_link = $com_links['comment_delete'] ? $link : '';
      $rows[] = array(
        t('comment'),
        l($row->title, "node/{$row->nid}", array(), NULL, "comment-{$row->cid}", FALSE, TRUE),
        $user->name,
        $row->status ? $no : $yes,
        format_date($row->changed, 'small'),
        array(
          'data' => $num_replies,
          'align' => 'right',
        ),
        $edit_link,
        $delete_link,
      );
    }
  }
  if ($rows) {
    $pager = theme('pager', NULL, $maxnodes, 0);
    if (!empty($pager)) {
      $rows[] = array(
        array(
          'data' => $pager,
          'colspan' => 8,
        ),
      );
    }
    $output .= theme('table', $header, $rows);
  }
  else {
    $output .= t('Your workspace is currently empty.');
  }

  // Build the attachment listing.
  $rows = array();
  $header = array(
    array(
      'data' => t('Type'),
      'field' => 'f.filemime',
    ),
    array(
      'data' => t('Filename'),
      'field' => 'f.filename',
    ),
    array(
      'data' => t('Size'),
      'field' => 'f.filesize',
    ),
  );
  $result = pager_query("SELECT n.nid, f.filemime, f.filename, f.filesize FROM {files} f, {node} n WHERE n.uid = {$user->uid} AND n.nid = f.nid" . tablesort_sql($header), $maxfilenames, 2);
  while ($row = db_fetch_object($result)) {
    $rows[] = array(
      $row->filemime,
      l($row->filename, "node/{$row->nid}"),
      format_size($row->filesize),
    );
  }
  if ($rows) {
    $output .= '<h3>' . t('Files') . '</h3>';
    $pager = theme('pager', NULL, $maxfilenames, 2);
    if (!empty($pager)) {
      $rows[] = array(
        array(
          'data' => $pager,
          'colspan' => 3,
        ),
      );
    }
    $output .= theme('table', $header, $rows);
  }
  return $output;
}