Added multiple upload file in issue.

This fixes issue 54.
This commit is contained in:
Loic d'Anterroches 2008-11-27 11:10:23 +01:00
parent 566c90cf6a
commit 0273e535e0
6 changed files with 254 additions and 135 deletions

View File

@ -72,8 +72,9 @@ class IDF_Form_IssueCreate extends Pluf_Form
// We add .dummy to try to mitigate security issues in the
// case of someone allowing the upload path to be accessible
// to everybody.
for ($i=1;$i<4;$i++) {
$filename = substr($md5, 0, 2).'/'.substr($md5, 2, 2).'/'.substr($md5, 4).'/%s.dummy';
$this->fields['attachment'] = new Pluf_Form_Field_File(
$this->fields['attachment'.$i] = new Pluf_Form_Field_File(
array('required' => false,
'label' => __('Attach a file'),
'move_function_params' =>
@ -83,6 +84,7 @@ class IDF_Form_IssueCreate extends Pluf_Form
)
)
);
}
if ($this->show_full) {
$this->fields['status'] = new Pluf_Form_Field_Varchar(
@ -194,6 +196,21 @@ class IDF_Form_IssueCreate extends Pluf_Form
return $this->cleaned_data['status'];
}
/**
* Clean the attachments post failure.
*/
function failed()
{
$upload_path = Pluf::f('upload_issue_path', false);
if ($upload_path == false) return;
for ($i=1;$i<4;$i++) {
if (!empty($this->cleaned_data['attachment'.$i]) and
file_exists($upload_path.'/'.$this->cleaned_data['attachment'.$i])) {
@unlink($upload_path.'/'.$this->cleaned_data['attachment'.$i]);
}
}
}
/**
* Save the model in the database.
*
@ -203,7 +220,9 @@ class IDF_Form_IssueCreate extends Pluf_Form
*/
function save($commit=true)
{
if ($this->isValid()) {
if (!$this->isValid()) {
throw new Exception(__('Cannot save the model from an invalid form.'));
}
// Add a tag for each label
$tags = array();
if ($this->show_full) {
@ -248,16 +267,16 @@ class IDF_Form_IssueCreate extends Pluf_Form
$comment->create();
// If we have a file, create the IDF_IssueFile and attach
// it to the comment.
if ($this->cleaned_data['attachment']) {
for ($i=1;$i<4;$i++) {
if ($this->cleaned_data['attachment'.$i]) {
$file = new IDF_IssueFile();
$file->attachment = $this->cleaned_data['attachment'];
$file->attachment = $this->cleaned_data['attachment'.$i];
$file->submitter = $this->user;
$file->comment = $comment;
$file->create();
}
return $issue;
}
throw new Exception(__('Cannot save the model from an invalid form.'));
return $issue;
}
/**

View File

@ -68,8 +68,9 @@ class IDF_Form_IssueUpdate extends IDF_Form_IssueCreate
// We add .dummy to try to mitigate security issues in the
// case of someone allowing the upload path to be accessible
// to everybody.
for ($i=1;$i<4;$i++) {
$filename = substr($md5, 0, 2).'/'.substr($md5, 2, 2).'/'.substr($md5, 4).'/%s.dummy';
$this->fields['attachment'] = new Pluf_Form_Field_File(
$this->fields['attachment'.$i] = new Pluf_Form_Field_File(
array('required' => false,
'label' => __('Attach a file'),
'move_function_params' =>
@ -79,6 +80,7 @@ class IDF_Form_IssueUpdate extends IDF_Form_IssueCreate
)
)
);
}
if ($this->show_full) {
$this->fields['status'] = new Pluf_Form_Field_Varchar(
@ -123,6 +125,21 @@ class IDF_Form_IssueUpdate extends IDF_Form_IssueCreate
}
}
/**
* Clean the attachments post failure.
*/
function failed()
{
$upload_path = Pluf::f('upload_issue_path', false);
if ($upload_path == false) return;
for ($i=1;$i<4;$i++) {
if (!empty($this->cleaned_data['attachment'.$i]) and
file_exists($upload_path.'/'.$this->cleaned_data['attachment'.$i])) {
@unlink($upload_path.'/'.$this->cleaned_data['attachment'.$i]);
}
}
}
/**
* We check that something is really changed.
*/
@ -202,7 +219,9 @@ class IDF_Form_IssueUpdate extends IDF_Form_IssueCreate
*/
function save($commit=true)
{
if ($this->isValid()) {
if (!$this->isValid()) {
throw new Exception(__('Cannot save the model from an invalid form.'));
}
if ($this->show_full) {
// Add a tag for each label
$tags = array();
@ -277,15 +296,15 @@ class IDF_Form_IssueUpdate extends IDF_Form_IssueCreate
$this->issue->submitter != $this->user->id) {
$this->issue->setAssoc($this->user); // interested user.
}
if ($this->cleaned_data['attachment']) {
for ($i=1;$i<4;$i++) {
if ($this->cleaned_data['attachment'.$i]) {
$file = new IDF_IssueFile();
$file->attachment = $this->cleaned_data['attachment'];
$file->attachment = $this->cleaned_data['attachment'.$i];
$file->submitter = $this->user;
$file->comment = $comment;
$file->create();
}
}
return $this->issue;
}
throw new Exception(__('Cannot save the model from an invalid form.'));
}
}

View File

@ -114,6 +114,7 @@ class IDF_IssueFile extends Pluf_Model
$this->filename = substr(basename($file), 0, -6);
$img_extensions = array('jpeg', 'jpg', 'png', 'gif');
$info = pathinfo($this->filename);
if (!isset($info['extension'])) $info['extension'] = '';
if (in_array(strtolower($info['extension']), $img_extensions)) {
$this->type = 'img';
} else {

View File

@ -24,10 +24,22 @@
{$form.f.content|unsafe}
</td>
</tr>
<tr>
<th>{$form.f.attachment.labelTag}:</th>
<td>{if $form.f.attachment.errors}{$form.f.attachment.fieldErrors}{/if}
{$form.f.attachment|unsafe}
<tr id="form-attachment-1">
<th>{$form.f.attachment1.labelTag}:</th>
<td>{if $form.f.attachment1.errors}{$form.f.attachment1.fieldErrors}{/if}
{$form.f.attachment1|unsafe}
</td>
</tr>
<tr id="form-attachment-2">
<th>{$form.f.attachment2.labelTag}:</th>
<td>{if $form.f.attachment2.errors}{$form.f.attachment2.fieldErrors}{/if}
{$form.f.attachment2|unsafe}
</td>
</tr>
<tr id="form-attachment-3">
<th>{$form.f.attachment3.labelTag}:</th>
<td>{if $form.f.attachment3.errors}{$form.f.attachment3.fieldErrors}{/if}
{$form.f.attachment3|unsafe}
</td>
</tr>{if $isOwner or $isMember}
<tr>
@ -74,7 +86,34 @@
{/block}
{block javascript}
<script type="text/javascript">
document.getElementById('id_summary').focus()
document.getElementById('id_summary').focus();{literal}
$(document).ready(function(){
// Hide the upload forms, we insert before the first attach file
// row an "Attach File" little link.
// We hide all the rows.
$("#form-attachment-1").before("{/literal}<tr id=\"form-block-0\"><td>&nbsp;</td><td><img style=\"vertical-align: text-bottom;\" src=\"{media '/idf/img/attachment.png'}\" alt=\" \" align=\"bottom\" /><a id=\"form-show-0\" href=\"#\">{trans 'Attach file'}{literal}</a></td></tr>");
$("#form-show-0").click(function(){
$("#form-attachment-1").show();
$("#form-block-0").hide();
});
$("#form-attachment-1 td").append("<span id=\"form-block-1\"><a id=\"form-show-1\" href=\"#\">{/literal}{trans 'Attach another file'}{literal}</a></span>");
$("#form-show-1").click(function(){
$("#form-attachment-2").show();
$("#form-block-1").hide();
});
$("#form-attachment-2 td").append("<span id=\"form-block-2\"><a id=\"form-show-2\" href=\"#\">{/literal}{trans 'Attach another file'}{literal}</a></span>");
$("#form-show-2").click(function(){
$("#form-attachment-3").show();
$("#form-block-2").hide();
});
var j=0;
for (j=1;j<4;j=j+1) {
$("#form-attachment-"+j).hide();
}
});
</script>
{/literal}{/block}
{include 'idf/issues/js-autocomplete.html'}{/block}

View File

@ -59,10 +59,22 @@
{$form.f.content|unsafe}
</td>
</tr>
<tr>
<th>{$form.f.attachment.labelTag}:</th>
<td>{if $form.f.attachment.errors}{$form.f.attachment.fieldErrors}{/if}
{$form.f.attachment|unsafe}
<tr id="form-attachment-1">
<th>{$form.f.attachment1.labelTag}:</th>
<td>{if $form.f.attachment1.errors}{$form.f.attachment1.fieldErrors}{/if}
{$form.f.attachment1|unsafe}
</td>
</tr>
<tr id="form-attachment-2">
<th>{$form.f.attachment2.labelTag}:</th>
<td>{if $form.f.attachment2.errors}{$form.f.attachment2.fieldErrors}{/if}
{$form.f.attachment2|unsafe}
</td>
</tr>
<tr id="form-attachment-3">
<th>{$form.f.attachment3.labelTag}:</th>
<td>{if $form.f.attachment3.errors}{$form.f.attachment3.fieldErrors}{/if}
{$form.f.attachment3|unsafe}
</td>
</tr>{if $isOwner or $isMember}
<tr>
@ -122,4 +134,33 @@
</p>{/if}
</div>
{/block}
{block javascript}{if $form}{include 'idf/issues/js-autocomplete.html'}{/if}{/block}
{block javascript}{if $form}{include 'idf/issues/js-autocomplete.html'}
<script type="text/javascript">
{literal}
$(document).ready(function(){
// Hide the upload forms, we insert before the first attach file
// row an "Attach File" little link.
// We hide all the rows.
$("#form-attachment-1").before("{/literal}<tr id=\"form-block-0\"><td>&nbsp;</td><td><img style=\"vertical-align: text-bottom;\" src=\"{media '/idf/img/attachment.png'}\" alt=\" \" align=\"bottom\" /><a id=\"form-show-0\" href=\"#\">{trans 'Attach file'}{literal}</a></td></tr>");
$("#form-show-0").click(function(){
$("#form-attachment-1").show();
$("#form-block-0").hide();
});
$("#form-attachment-1 td").append("<span id=\"form-block-1\"><a id=\"form-show-1\" href=\"#\">{/literal}{trans 'Attach another file'}{literal}</a></span>");
$("#form-show-1").click(function(){
$("#form-attachment-2").show();
$("#form-block-1").hide();
});
$("#form-attachment-2 td").append("<span id=\"form-block-2\"><a id=\"form-show-2\" href=\"#\">{/literal}{trans 'Attach another file'}{literal}</a></span>");
$("#form-show-2").click(function(){
$("#form-attachment-3").show();
$("#form-block-2").hide();
});
var j=0;
for (j=1;j<4;j=j+1) {
$("#form-attachment-"+j).hide();
}
});{/literal}
</script>
{/if}{/block}

Binary file not shown.

After

Width:  |  Height:  |  Size: 649 B