--- private_upload.module.orig 2008-04-25 09:30:01.000000000 -0700 +++ private_upload.module 2008-05-01 22:27:59.000000000 -0700 @@ -222,11 +222,35 @@ function _private_upload_requirements($p function private_upload_nodeapi(&$node, $op, $teaser) { switch ($op) { case 'insert': + // Modification for upload roles + if ($node->upload_role) { + if ($node->upload_role > 0) { + $sql = "INSERT INTO {private_upload_roles} (nid,rid) VALUES (%d, %d)"; + db_query($sql, $node->nid, $node->upload_role); + } + } + break; + case 'delete': + // Modification for upload roles + // Delete existing role for this node + $sql = "DELETE FROM {private_upload_roles} WHERE nid = %d"; + db_query($sql, $node->nid); + break; case 'update': // ****************************************************** // *** INSERT/UPDATE // *** Move a file from public to private, or vise-verse // ****************************************************** + // Modification for upload roles + if ($node->upload_role) { + // Delete existing role for this node + $sql = "DELETE FROM {private_upload_roles} WHERE nid = %d"; + db_query($sql, $node->nid); + if ($node->upload_role > 0) { + $sql = "INSERT INTO {private_upload_roles} (nid,rid) VALUES (%d, %d)"; + db_query($sql, $node->nid, $node->upload_role); + } + } if (user_access('upload files')) { if( is_array($node->files) ) { foreach ($node->files as $fid => $file) { @@ -293,7 +317,7 @@ function private_upload_nodeapi(&$node, } // Always rebuild the files table and overwrite default. - $node->content['files']['#value'] = theme('private_upload_attachments', $node->files); + $node->content['files']['#value'] = theme('private_upload_attachments', private_upload_table_rewrite($node->files)); break; } } @@ -311,6 +335,18 @@ function private_upload_form_alter($form $node = $form['#node']; if ($form['type']['#value'] .'_node_form' == $form_id && variable_get("upload_$node->type", TRUE)) { if (is_array($node->files) && count($node->files)) { // hijack theme function + // + // Modification: Create pulldown menu to select role for private files (upload roles) + // + $roles = private_upload_get_roles(); + $form['attachments']['upload_role'] = array( + '#type' => 'select', + '#title' => t('Role for viewing private files'), + '#options' => $roles, + '#default_value' => private_upload_get_role($node->nid), + '#description' => t('Select the role (other than anonymous and/or authenticated) to which you wish to restrict viewing/downloading of files marked private. No Additional Restriction means that any role which could normally see the private files(s) will continue to be able to do so. Selecting a role here means only users with this role will be able to view/download files marked as private'), + ); + $form['attachments']['wrapper']['files']['#theme'] = 'private_upload_form'; $form['#validate']['private_upload_form_validate'] = array(); //$form['#validate'][] = 'private_upload_form_validate'; @@ -341,6 +377,17 @@ function private_upload_form_alter($form } } elseif( $form_id == 'upload_js' ) { + // + // Modification: Create pulldown menu to select role for private files (upload roles) + // + $roles = private_upload_get_roles(); + $form['upload_role'] = array( + '#type' => 'select', + '#title' => t('Role for viewing private files'), + '#options' => $roles, + '#default_value' => private_upload_get_role($node->nid), + '#description' => t('Select the role (other than anonymous and/or authenticated) to which you wish to restrict viewing/downloading of files marked private. No Additional Restriction means that any role which could normally see the private files(s) will continue to be able to do so. Selecting a role here means only users with this role will be able to view/download files marked as private'), + ); $form['files']['#theme'] = 'private_upload_form'; foreach ($form['files'] as $fid => $file) { if( !_private_upload_starts_with( $fid, '#' ) ) { // Ignore the properties. @@ -785,3 +832,58 @@ function private_upload_views_handler_al function private_upload_views_handler_listed_files($fieldinfo, $fielddata, $value, $data) { return private_upload_views_handler_all_files($fieldinfo, $fielddata, $value, $data, TRUE); } + +// ***************************************************************************** +// Customizations ************************************************************* +// ***************************************************************************** +function private_upload_table_rewrite($files) { + global $user; + $copyfiles = array(); + $copy = 'no'; + foreach ($files as $fid => $file) { + if ( !_private_upload_is_file_private($file->filepath)) { + $copy = 'yes'; + } + if (in_array(private_upload_get_role($file->nid), array_keys($user->roles))) { + $copy = 'yes'; + } + if ( _private_upload_is_file_private($file->filepath) && !user_access('view uploaded files')) { + $copy = 'no'; + } + if ( _private_upload_is_file_private($file->filepath) && user_access('view uploaded files') && private_upload_get_role($file->nid) == 0) { + $copy = 'yes'; + } + if ($copy == 'yes') { + $copyfiles[$fid] = $file; + } + } + return $copyfiles; +} + +/** + * Get all roles. + */ +function private_upload_get_roles() { + $roles = array(); + $all_roles = user_roles(); + $roles[0] = 'No Additional Restriction'; + foreach ($all_roles as $rid => $role) { + if ($rid > 2) { + $roles[$rid] = $role; + } + } + return $roles; +} + +/** + * Get all role selected for this nodeID. + */ +function private_upload_get_role($nid) { + $role = 0; + $sql = "SELECT rid FROM {private_upload_roles} WHERE nid = %d"; + $results = db_query($sql, $nid); + while ( $data = db_fetch_object($results) ) { + $role = $data->rid; + } + return $role; +} \ No newline at end of file