--- workspace.module.org 2008-08-07 03:49:45.000000000 +0100 +++ workspace.module 2009-04-21 17:10:45.000000000 +0100 @@ -100,8 +100,15 @@ */ function workspace_access($account, $module_enabled = TRUE) { global $user; - return user_access('access content') // If you may not see content, you may not see workspaces. - && (user_access('view all workspaces') // Must either have permission to see all workspaces + // Let admin see $account + if ($user->uid == 1 && $account->uid > 1) { + return user_access('access content', $account) + && user_access('access workspace', $account) + && $module_enabled; + } + return user_access('access content', $user) // If you may not see content, you may not see workspaces. + && user_access('access workspace', $user) // Allowed to see own workspace + && (user_access('view all workspaces', $user) // Must either have permission to see all workspaces || (($user->uid == $account->uid) // or must be your workspace && ($user->uid != 0))) // and you must not be an anonymous user && $module_enabled; // If, e.g., comment module is disabled, remove comment local task. @@ -112,6 +119,10 @@ */ function workspace_configure_access($account) { global $user; + if ($user->uid == 1 && $account->uid > 1) { + return user_access('administer workspaces', $account) + || user_access('administer own workspace', $account); + } return ($user->uid != 0) // May not be anonymous user. && user_access('administer workspaces') // Must either have permission to administer all workspaces || ($user->uid == $account->uid // Or if this is your workspace @@ -123,7 +134,7 @@ * Implementation of hook_perm(). */ function workspace_perm() { - return array('administer own workspace', 'administer workspaces', 'view all workspaces'); + return array('access workspace', 'administer own workspace', 'administer workspaces', 'view all workspaces'); } /** @@ -143,16 +154,25 @@ '#type' => 'textfield', '#title' => t('Number of items'), '#description' => t('Maximum number of items to display in your workspace.'), - '#default_value' => $account->workspaces ? $account->workspaces['default']['maxnodes'] : 50, + '#default_value' => $account->workspaces['default']['maxnodes'] ? $account->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' => $account->workspaces ? $account->workspaces['default']['maxfilenames'] : 50, + '#default_value' => $account->workspaces['default']['maxfilenames'] ? $account->workspaces['default']['maxfilenames'] : 50, + '#size' => 4 + ); + if ( module_exists('comment') ) { + $form['maxcomments'] = array( + '#type' => 'textfield', + '#title' => t('Number of comments'), + '#description' => t('Maximum number of comments to display in your workspace.'), + '#default_value' => $account->workspaces['default']['maxcomments'] ? $account->workspaces['default']['maxcomments'] : 50, '#size' => 4 ); + } $form['uid'] = array( '#type' => 'value', '#value' => $account->uid, @@ -176,6 +196,11 @@ if (!is_numeric($form_state['values']['maxfilenames']) && !form_get_errors()) { form_set_error('maxfilenames', t('Please enter a numeric value.')); } + if ( module_exists('comment') ) { + if (!is_numeric($form_state['values']['maxcomments']) && !form_get_errors()) { + form_set_error('maxfilenames', t('Please enter a numeric value.')); + } + } } /** @@ -205,7 +230,7 @@ function workspace_list_content($account) { global $user; drupal_set_title(t('Workspace: @name', array('@name' => $account->name))); - $max = isset($user->workspaces) ? $user->workspaces['default']['maxnodes'] : 50; + $max = isset($user->workspaces['default']['maxnodes']) ? $user->workspaces['default']['maxnodes'] : 50; $comments_enabled = module_exists('comment'); if ($comments_enabled) { // If the comment module is enabled, we need to get comment counts too. @@ -217,6 +242,10 @@ FROM {node} n LEFT JOIN {node_comment_statistics} s ON n.nid = s.nid WHERE n.uid = %d'; + $check_count_sql = 'SELECT COUNT(n.nid) AS ct + FROM {node} n + LEFT JOIN {node_comment_statistics} s ON n.nid = s.nid + WHERE n.uid = %d'; } else { // Otherwise we use a simpler query. @@ -226,8 +255,16 @@ $count_sql = 'SELECT COUNT(n.nid) FROM {node} n WHERE n.uid = %d'; + $check_count_sql = 'SELECT COUNT(n.nid) AS ct + FROM {node} n + WHERE n.uid = %d'; } + // check wether this user has any comments, if not skip pager to avoid div by zero error + $result = db_query($check_count_sql, array($account->uid)); + $row = db_fetch_array($result); + $has_contents = $row['ct']; + $header = array( array('data' => t('Type'), 'field' => 'type'), array('data' => t('Title'), 'field' => 'title'), @@ -237,15 +274,21 @@ $comments_enabled ? array('data' => t('Replies'), 'field' => 'comment_count') : array('data' => ''), array('data' => t('Operations'), 'colspan' => 2) ); + $cols = 8; + if ($has_contents) { $result = pager_query(db_rewrite_sql($sql . tablesort_sql($header)), $max, 0, db_rewrite_sql($count_sql), $account->uid); $rows = workspace_build_rows($result, $account); + } + else { + $rows = FALSE; + } $output = ''; // Only add the content add form if the user is viewing his/her own workspace. - if ($user->uid == $account->uid) { + if ($user->uid == 1 || user_access('view all workspaces') || $user->uid == $account->uid) { $output = drupal_get_form('workspace_add_form'); } - $output .= theme_workspace_list($header, $rows, $max); + $output .= theme_workspace_list($header, $rows, $max, $cols); return $output; } @@ -257,13 +300,17 @@ */ function workspace_list_comments($account) { drupal_set_title(t('Workspace: @name', array('@name' => $account->name))); - $max = isset($account->workspaces) ? $account->workspaces['default']['maxcomments'] : 50; + $max = isset($account->workspaces['default']['maxcomments']) ? $account->workspaces['default']['maxcomments'] : 50; $sql = 'SELECT c.nid, c.uid, c.cid, c.subject, c.status, c.timestamp, c.pid, 0 FROM {comments} c WHERE c.uid = %d'; $count_sql = 'SELECT COUNT(cid) FROM {comments} WHERE uid = %d'; + // check wether this user has any comments, if not skip pager to avoid div by zero error + $result = db_query('SELECT COUNT(cid) AS ct FROM {comments} WHERE uid = %d', array($account->uid)); + $row = db_fetch_array($result); + $has_comments = $row['ct']; // Build the comment listing. $header = array( @@ -273,9 +320,15 @@ array('data' => t('Replies'), 'field' => 'comment_count'), array('data' => t('Operations'), 'colspan' => 2) ); - $result = pager_query($sql . tablesort_sql($header), $max, 0, $count_sql, $account->uid); + $cols = 6; + if ($has_comments) { + $result = pager_query(db_rewrite_sql($sql . tablesort_sql($header)), $max, 0, db_rewrite_sql($count_sql), $account->uid); $rows = workspace_build_rows($result, $account); - return theme_workspace_list($header, $rows, $max); + } + else { + $rows = FALSE; + } + return theme_workspace_list($header, $rows, $max, $cols); } /** @@ -369,8 +423,9 @@ * User object representing user whose workspace is being listed. */ function workspace_list_files($account) { + global $user; drupal_set_title(t('Workspace: @name', array('@name' => $account->name))); - $max = isset($user->workspaces) ? $user->workspaces['default']['maxfilenames'] : 50; + $max = isset($user->workspaces['default']['maxfilenames']) ? $user->workspaces['default']['maxfilenames'] : 50; $download = t('download'); $rows = array(); $header = array( @@ -380,7 +435,7 @@ array('data' => t('Size'), 'field' => 'f.filesize'), array('data' => t('Operations')), ); - + $cols = 6; $sql = "SELECT u.nid, f.filemime, f.filename, f.filesize, f.timestamp, f.filepath FROM {files} f LEFT JOIN {upload} u ON f.fid = u.fid @@ -397,7 +452,7 @@ ); } - return theme('workspace_list', $header, $rows, $max); + return theme('workspace_list', $header, $rows, $max, $cols); } /** @@ -406,7 +461,7 @@ function workspace_theme() { return array( 'workspace_list' => array( - 'arguments' => array('header' => array(), 'rows' => array(), $max = 50), + 'arguments' => array('header' => array(), 'rows' => array(), $max = 50, $cols = 8), ), ); } @@ -414,13 +469,13 @@ /** * Theme the list of content. Turn the results into a table. */ -function theme_workspace_list($header, $rows, $max) { +function theme_workspace_list($header, $rows, $max, $cols) { if ($rows) { - $pager = theme('pager', NULL, $max, 2); + $pager = theme('pager', NULL, $max); if (!empty($pager)) { - $rows[] = array(array('data' => $pager, 'colspan' => 3)); + $rows[] = array(array('data' => $pager, 'colspan' => $cols)); } - $output .= theme('table', $header, $rows); + $output = theme('table', $header, $rows); } else { $output = t('Your workspace is currently empty.'); @@ -489,6 +544,11 @@ $options[$type] = $object->name; } if (isset($options[$node_type])) { + /* Start hack */ + if (stristr($node_type,"_") > -1) { + $node_type = str_replace("_","-",$node_type); + } + /* End hack */ drupal_goto('node/add/' . $node_type); } } @@ -529,22 +589,19 @@ */ function workspace_user($op, &$edit, &$account) { global $user; - if ($op == 'view' && user_access('view all workspaces')) { - $allowed = FALSE; - foreach ($user->roles as $rid => $name) { - if (variable_get('workspace_user_profile_'. $rid, 0)) { - $allowed = TRUE; - break; - } - } - if ($allowed) { + if ($op == 'view') { + if( workspace_access($account)) { $variables['%username'] = $account->name; $link_title = variable_get('workspace_user_profile_link', t('View recent content')); - - $account->content['summary']['workspace'] = array( - '#type' => 'user_profile_item', + $link = l(strtr($link_title, $variables), 'workspace/'. $account->uid); + $account->content['workspace'] = array( + '#type' => 'user_profile_category', '#title' => variable_get('workspace_user_profile_title', t('Content')), - '#value' => l(strtr($link_title, $variables), 'workspace/'. $account->uid), + 'link' => array( + '#value' => $link, + '#type' => 'user_profile_item', + ), + '#weight' => -10, ); } }