uid > 0); $items['workspace'] = array( 'title' => t('My workspace'), 'page callback' => 'workspace_admin', 'access arguments' => array ('administer own workspace'), ); $items['workspace/list'] = array( 'title' => t('List'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, ); $items['workspace/configure'] = array( 'title' => t('Configure'), 'page callback' => 'workspace_configure', 'access arguments' => array ('administer own workspace'), 'type' => MENU_LOCAL_TASK ); $items['workspace/delete'] = array( 'title' => t('Delete'), 'page callback' => 'workspace_delete', 'type' => MENU_CALLBACK ); return $items; } /** * Implementation of hook_perm(). */ function workspace_perm() { return array('administer own workspace'); } /******************************************************************** * Module Functions :: Controllers ********************************************************************/ function workspace_admin() { global $user; // This is bad form but still secure since we're just doing a drupal_goto(). if (isset($_POST['content_type'])) { $node_type = $_POST['content_type']; if ($node_type == t('Select...')) { drupal_goto('node/add'); } foreach (node_get_types() as $type => $typeobject) { if (node_access('create', $type, $user)) { $options[$type] = $typeobject->name; } if (isset($options[$node_type])) { drupal_goto('node/add/' . $node_type); } } } $title = t('Workspace') . ' : ' . $user->name; drupal_set_title($title); $output = workspace_list(); return $output; } /** * The configuration page. */ function workspace_configure_form() { global $user; $form = array(); $form['maxnodes'] = array( '#type' => 'textfield', '#title' => t('Number of items'), '#description' => t('Maximum number of items to display in your workspace.'), '#default_value' => $user->workspaces ? $user->workspaces['default']['maxnodes'] : 50, '#size' => 4 ); $form['maxfilenames'] = array( '#type' => 'textfield', '#title' => t('Number of files'), '#description' => t('Maximum number of filenames to display in your workspace.'), '#default_value' => $user->workspaces ? $user->workspaces['default']['maxfilenames'] : 50, '#size' => 4 ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save configuration') ); return $form; } function workspace_configure() { global $user; $output = drupal_get_form('workspace_configure_form'); drupal_set_title(t('Workspace : !username', array('!username' => $user->name))); return $output; } function workspace_configure_form_validate($form, &$form_state) { if (!is_numeric($form_state['values']['maxnodes'])) { form_set_error('maxnodes', t('Please enter a numeric value.')); } if (!is_numeric($form_state['values']['maxfilenames']) && !form_get_errors()) { form_set_error('maxfilenames', t('Please enter a numeric value.')); } } function workspace_configure_form_submit($form, &$form_state) { global $user; $user = user_load(array('uid' => $user->uid)); if (!isset($user->workspaces)) { $user->workspaces = array(); } $user->workspaces['default']['maxnodes'] = (int) $form_state['values']['maxnodes']; $user->workspaces['default']['maxfilenames'] = (int) $form_state['values']['maxfilenames']; user_save($user, array('workspaces' => $user->workspaces)); drupal_set_message(t('Your workspace preferences have been saved.')); return 'workspace'; } /******************************************************************** * Module Functions :: Views ********************************************************************/ /** * The default admin interface is a list of content. */ 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.'); } if ( module_exists('upload') ) { // 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, {upload} u WHERE n.uid = $user->uid AND n.nid = u.nid and u.fid = f.fid" . 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 .= '