multiple attachment upload

Post about your customizations to share with others.

Moderators: Developer, Contributor

Post Reply
omeslo
Posts: 16
Joined: 23 Apr 2007, 14:42

multiple attachment upload

Post by omeslo »

All,

I implemented a little hack that allows for multiple attachment upload. It is rather basic (e.g. no localization) and has not been thoroughly tested, but it seems to suit my needs.
Bear with me, as we will have to modify 5 different files.

(This hack is based on v1.0.6, but should work in newer versions, too)

1) common.js
add the following block of code:

Code: Select all

/* 
 *   Class by Stickman -- http://www.the-stickman.com
 *
 *	 Mantis integration by [omeslo]
 */
 
 
 
function MultiSelector( list_target, max ){

	// Where to write the list
	this.list_target = list_target;
	// How many elements?
	this.count = 0;
	// How many elements?
	this.id = 0;
	// Is there a maximum?
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};
	
	/**
	 * Add a new file input element
	 */
	this.addElement = function( element ){

		// Make sure it's a file input element
		if( element.tagName == 'INPUT' && element.type == 'file' ){

			// Element name -- what number am I?
			element.name = 'file_' + this.id++;

			// Add reference to this object
			element.multi_selector = this;

			// What to do when a file is selected
			element.onchange = function(){

				// New file input
				var new_element = document.createElement( 'input' );
				new_element.type = 'file';

				// Add new element
				this.parentNode.insertBefore( new_element, this );

				// Apply 'update' to element
				this.multi_selector.addElement( new_element );

				// Update list
				this.multi_selector.addListRow( this );

				// Hide this: we can't use display:none because Safari doesn't like it
				this.style.position = 'absolute';
				this.style.left = '-1000px';

			};
			// If we've reached maximum number, disable input element
			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			};

			// File element counter
			this.count++;
			// Most recent element
			this.current_element = element;
			
		} else {
			// This can only be applied to file input elements!
			alert( 'Error: not a file input element' );
		};

	};

	/**
	 * Add a new row to the list of files
	 */
	this.addListRow = function( element ){

		// Row div
		var new_row = document.createElement( 'div' );

		// Delete button
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.value = 'Delete';

		// References
		new_row.element = element;

		// Delete function
		new_row_button.onclick= function(){

			// Remove element from form
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );

			// Remove this row from the list
			this.parentNode.parentNode.removeChild( this.parentNode );

			// Decrement counter
			this.parentNode.element.multi_selector.count--;

			// Re-enable input element (if it's disabled)
			this.parentNode.element.multi_selector.current_element.disabled = false;

			// Appease Safari
			//    without it Safari wants to reload the browser window
			//    which nixes your already queued uploads
			return false;
		};

		// Set row value
		new_row.innerHTML = element.value;

		// Add button
		new_row.appendChild( new_row_button );

		// Add it to the list
		this.list_target.appendChild( new_row );
		
	};

};
2) bug_file_upload_inc.php

replace

Code: Select all

<input name="file" type="file" size="40" />
with

Code: Select all

<input id="my_file_element" type="file" name="file_1" >
		<div id="files_list"></div> <!-- queued files will be displayed here -->
		<script>
			var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );
			multi_selector.addElement( document.getElementById( 'my_file_element' ) );
		</script>
Note: This is where the maximum amount of files is configured (3 in this example)

3) bug_file_add.php
replace

Code: Select all

  $f_file_error =  ( isset( $f_file['error'] ) ) ? $f_file['error'] : 0;
	file_add( $f_bug_id, $f_file['tmp_name'], $f_file['name'], $f_file['type'], 'bug', $f_file_error );
with

Code: Select all

$file_count = sizeof($_FILES);
	$f_bug_id	= $_POST['bug_id'];
	// iterate through all upload fields ( = max uploads)
	for ($i = 0; $i < $file_count; $i++) {
		$thisfilename = "file_" . $i;
		// check if file exists
		if ( !is_blank( $_FILES[$thisfilename]['tmp_name'] ) && ( 0 < $_FILES[$thisfilename]['size'] ) ) {
			$f_file_error =  ( isset( $_FILES[$thisfilename]['error'] ) ) ? $_FILES[$thisfilename]['error'] : 0;	
			file_add( $f_bug_id, $_FILES[$thisfilename]['tmp_name'], $_FILES[$thisfilename]['name'], $_FILES[$thisfilename]['type'], 'bug', $f_file_error );
		}
	}
and comment-out the line

Code: Select all

$f_file = gpc_get_file( 'file' );
around line 24, as we don't need it anymore, and it will throw an error.

4) bug_report_page.php

replace

Code: Select all

<input tabindex="8" name="file" type="file" size="60" />
with

Code: Select all

		<input id="my_file_element" type="file" name="file_1" >
		<div id="files_list"></div> <!-- queued files will be displayed here -->
		<script>
			var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );
			multi_selector.addElement( document.getElementById( 'my_file_element' ) );
		</script>
5) bug_report.php

replace

Code: Select all

# Handle the file upload
	if ( !is_blank( $f_file['tmp_name'] ) && ( 0 < $f_file['size'] ) ) {
    	$f_file_error =  ( isset( $f_file['error'] ) ) ? $f_file['error'] : 0;
		file_add( $t_bug_id, $f_file['tmp_name'], $f_file['name'], $f_file['type'], 'bug', $f_file_error );
	}
with

Code: Select all

	# Handle the file upload
	$file_count = sizeof($_FILES);
	// iterate through all upload fields ( = max uploads)
	for ($i = 0; $i < $file_count; $i++) {
		$thisfilename = "file_" . $i;
		// check if file exists
		if ( !is_blank( $_FILES[$thisfilename]['tmp_name'] ) && ( 0 < $_FILES[$thisfilename]['size'] ) ) {
			$f_file_error =  ( isset( $_FILES[$thisfilename]['error'] ) ) ? $_FILES[$thisfilename]['error'] : 0;	
			file_add( $t_bug_id, $_FILES[$thisfilename]['tmp_name'], $_FILES[$thisfilename]['name'], $_FILES[$thisfilename]['type'], 'bug', $f_file_error );
		}
	}

That should do it.
Most probably I made some typo's here and there, so if things aren't working out for you, please post a reply and I'll look into it.
Enjoy!

PS.
Credits go to 'Stickman' for writing the nice javascript, and giving me the idea. See the code comments for his website and the original script.
vboctor
Site Admin
Posts: 1293
Joined: 13 Feb 2005, 22:11
Location: Redmond, Washington
Contact:

Post by vboctor »

Thanks omeslo for your contribution. It would be great if you can put some screenshots so that users can get an idea of the end result before having to implement the changes.
Migrate your MantisBT to the MantisHub Cloud
omeslo
Posts: 16
Joined: 23 Apr 2007, 14:42

Post by omeslo »

No problem.

Here's a screenshot of view.php
Image

And this is what bug_report_page.php will look like.
Image

Nothing fancy, but hey..

Note: the label 'bladeren' is rendered by the browser (NL in my case), and greyed out, as the maximum number of files is reached. This number is 3, but can be configured.
robert
Posts: 1
Joined: 30 Jul 2007, 23:02

Thanks for posting this hack!

Post by robert »

Thanks for posting this hack!
danich
Posts: 18
Joined: 21 May 2007, 17:39

Post by danich »

good hack, unluckily its not working 4 me
Im using Mantis 1.1.0a3.
After submiting the issue, the screen goes blank and the issue ir reported, but no file is uploaded.
danich
Posts: 18
Joined: 21 May 2007, 17:39

Post by danich »

[edit]
worked like a charm !
=)
fxm
Posts: 26
Joined: 01 Feb 2007, 09:42
Location: Namur (Belgium)

Post by fxm »

Hello,

This function is very good, but is it possible to integrate it into next version of Mantis ?

Thanks in advance for your answer.

François
omeslo
Posts: 16
Joined: 23 Apr 2007, 14:42

Re:

Post by omeslo »

fxm wrote:Hello,

This function is very good, but is it possible to integrate it into next version of Mantis ?

Thanks in advance for your answer.

François
Fine with me.. I guess you should PM vboctor about this, or make a feature request.
vboctor
Site Admin
Posts: 1293
Joined: 13 Feb 2005, 22:11
Location: Redmond, Washington
Contact:

Re: multiple attachment upload

Post by vboctor »

Please report a feature request in the bug tracker and include in it a link to this topic.

Thanks.
Migrate your MantisBT to the MantisHub Cloud
seiji
Posts: 15
Joined: 03 Nov 2007, 09:43
Location: Chiba, Japan

Re: multiple attachment upload

Post by seiji »

The request has been already reported.
http://www.mantisbt.org/bugs/view.php?id=5228
Post Reply