From 0273e535e050ccb83915b1a649c4267eb2de1a9e Mon Sep 17 00:00:00 2001 From: Loic d'Anterroches Date: Thu, 27 Nov 2008 11:10:23 +0100 Subject: [PATCH] Added multiple upload file in issue. This fixes issue 54. --- src/IDF/Form/IssueCreate.php | 115 ++++++++------- src/IDF/Form/IssueUpdate.php | 173 +++++++++++++---------- src/IDF/IssueFile.php | 1 + src/IDF/templates/idf/issues/create.html | 49 ++++++- src/IDF/templates/idf/issues/view.html | 51 ++++++- www/media/idf/img/attachment.png | Bin 0 -> 649 bytes 6 files changed, 254 insertions(+), 135 deletions(-) create mode 100644 www/media/idf/img/attachment.png diff --git a/src/IDF/Form/IssueCreate.php b/src/IDF/Form/IssueCreate.php index 0743e72..67afca7 100644 --- a/src/IDF/Form/IssueCreate.php +++ b/src/IDF/Form/IssueCreate.php @@ -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. - $filename = substr($md5, 0, 2).'/'.substr($md5, 2, 2).'/'.substr($md5, 4).'/%s.dummy'; - $this->fields['attachment'] = new Pluf_Form_Field_File( + for ($i=1;$i<4;$i++) { + $filename = substr($md5, 0, 2).'/'.substr($md5, 2, 2).'/'.substr($md5, 4).'/%s.dummy'; + $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,61 +220,63 @@ class IDF_Form_IssueCreate extends Pluf_Form */ function save($commit=true) { - if ($this->isValid()) { - // Add a tag for each label - $tags = array(); - if ($this->show_full) { - for ($i=1;$i<7;$i++) { - if (strlen($this->cleaned_data['label'.$i]) > 0) { - if (strpos($this->cleaned_data['label'.$i], ':') !== false) { - list($class, $name) = explode(':', $this->cleaned_data['label'.$i], 2); - list($class, $name) = array(trim($class), trim($name)); - } else { - $class = 'Other'; - $name = trim($this->cleaned_data['label'.$i]); - } - $tags[] = IDF_Tag::add($name, $this->project, $class); + 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) { + for ($i=1;$i<7;$i++) { + if (strlen($this->cleaned_data['label'.$i]) > 0) { + if (strpos($this->cleaned_data['label'.$i], ':') !== false) { + list($class, $name) = explode(':', $this->cleaned_data['label'.$i], 2); + list($class, $name) = array(trim($class), trim($name)); + } else { + $class = 'Other'; + $name = trim($this->cleaned_data['label'.$i]); } + $tags[] = IDF_Tag::add($name, $this->project, $class); } - } else { - $tags[] = IDF_Tag::add('Medium', $this->project, 'Priority'); - $tags[] = IDF_Tag::add('Defect', $this->project, 'Type'); } - // Create the issue - $issue = new IDF_Issue(); - $issue->project = $this->project; - $issue->submitter = $this->user; - if ($this->show_full) { - $issue->status = IDF_Tag::add(trim($this->cleaned_data['status']), $this->project, 'Status'); - $issue->owner = self::findUser($this->cleaned_data['owner']); - } else { - $_t = $this->project->getTagIdsByStatus('open'); - $issue->status = new IDF_Tag($_t[0]); // first one is the default - $issue->owner = null; - } - $issue->summary = trim($this->cleaned_data['summary']); - $issue->create(); - foreach ($tags as $tag) { - $issue->setAssoc($tag); - } - // add the first comment - $comment = new IDF_IssueComment(); - $comment->issue = $issue; - $comment->content = $this->cleaned_data['content']; - $comment->submitter = $this->user; - $comment->create(); - // If we have a file, create the IDF_IssueFile and attach - // it to the comment. - if ($this->cleaned_data['attachment']) { + } else { + $tags[] = IDF_Tag::add('Medium', $this->project, 'Priority'); + $tags[] = IDF_Tag::add('Defect', $this->project, 'Type'); + } + // Create the issue + $issue = new IDF_Issue(); + $issue->project = $this->project; + $issue->submitter = $this->user; + if ($this->show_full) { + $issue->status = IDF_Tag::add(trim($this->cleaned_data['status']), $this->project, 'Status'); + $issue->owner = self::findUser($this->cleaned_data['owner']); + } else { + $_t = $this->project->getTagIdsByStatus('open'); + $issue->status = new IDF_Tag($_t[0]); // first one is the default + $issue->owner = null; + } + $issue->summary = trim($this->cleaned_data['summary']); + $issue->create(); + foreach ($tags as $tag) { + $issue->setAssoc($tag); + } + // add the first comment + $comment = new IDF_IssueComment(); + $comment->issue = $issue; + $comment->content = $this->cleaned_data['content']; + $comment->submitter = $this->user; + $comment->create(); + // If we have a file, create the IDF_IssueFile and attach + // it to the comment. + 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; } /** diff --git a/src/IDF/Form/IssueUpdate.php b/src/IDF/Form/IssueUpdate.php index 550889e..0d36e72 100644 --- a/src/IDF/Form/IssueUpdate.php +++ b/src/IDF/Form/IssueUpdate.php @@ -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. - $filename = substr($md5, 0, 2).'/'.substr($md5, 2, 2).'/'.substr($md5, 4).'/%s.dummy'; - $this->fields['attachment'] = new Pluf_Form_Field_File( + for ($i=1;$i<4;$i++) { + $filename = substr($md5, 0, 2).'/'.substr($md5, 2, 2).'/'.substr($md5, 4).'/%s.dummy'; + $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,90 +219,92 @@ class IDF_Form_IssueUpdate extends IDF_Form_IssueCreate */ function save($commit=true) { - if ($this->isValid()) { - if ($this->show_full) { - // Add a tag for each label - $tags = array(); - $tagids = array(); - for ($i=1;$i<7;$i++) { - if (strlen($this->cleaned_data['label'.$i]) > 0) { - if (strpos($this->cleaned_data['label'.$i], ':') !== false) { - list($class, $name) = explode(':', $this->cleaned_data['label'.$i], 2); - list($class, $name) = array(trim($class), trim($name)); - } else { - $class = 'Other'; - $name = trim($this->cleaned_data['label'.$i]); - } - $tag = IDF_Tag::add($name, $this->project, $class); - $tags[] = $tag; - $tagids[] = $tag->id; + 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(); + $tagids = array(); + for ($i=1;$i<7;$i++) { + if (strlen($this->cleaned_data['label'.$i]) > 0) { + if (strpos($this->cleaned_data['label'.$i], ':') !== false) { + list($class, $name) = explode(':', $this->cleaned_data['label'.$i], 2); + list($class, $name) = array(trim($class), trim($name)); + } else { + $class = 'Other'; + $name = trim($this->cleaned_data['label'.$i]); } + $tag = IDF_Tag::add($name, $this->project, $class); + $tags[] = $tag; + $tagids[] = $tag->id; } - // Compare between the old and the new data - $changes = array(); - $oldtags = $this->issue->get_tags_list(); - foreach ($tags as $tag) { - if (!Pluf_Model_InArray($tag, $oldtags)) { - if (!isset($changes['lb'])) $changes['lb'] = array(); - if ($tag->class != 'Other') { - $changes['lb'][] = (string) $tag; //new tag - } else { - $changes['lb'][] = (string) $tag->name; - } - } - } - foreach ($oldtags as $tag) { - if (!Pluf_Model_InArray($tag, $tags)) { - if (!isset($changes['lb'])) $changes['lb'] = array(); - if ($tag->class != 'Other') { - $changes['lb'][] = '-'.(string) $tag; //new tag - } else { - $changes['lb'][] = '-'.(string) $tag->name; - } - } - } - // Status, summary and owner - $status = IDF_Tag::add(trim($this->cleaned_data['status']), $this->project, 'Status'); - if ($status->id != $this->issue->status) { - $changes['st'] = $status->name; - } - if (trim($this->issue->summary) != trim($this->cleaned_data['summary'])) { - $changes['su'] = trim($this->cleaned_data['summary']); - } - $owner = self::findUser($this->cleaned_data['owner']); - if ((is_null($owner) and !is_null($this->issue->get_owner())) - or (!is_null($owner) and is_null($this->issue->get_owner())) - or ((!is_null($owner) and !is_null($this->issue->get_owner())) and $owner->id != $this->issue->get_owner()->id)) { - $changes['ow'] = (is_null($owner)) ? '---' : $owner->login; - } - // Update the issue - $this->issue->batchAssoc('IDF_Tag', $tagids); - $this->issue->summary = trim($this->cleaned_data['summary']); - $this->issue->status = $status; - $this->issue->owner = $owner; } - // Create the comment - $comment = new IDF_IssueComment(); - $comment->issue = $this->issue; - $comment->content = $this->cleaned_data['content']; - $comment->submitter = $this->user; - if (!$this->show_full) $changes = array(); - $comment->changes = $changes; - $comment->create(); - $this->issue->update(); - if ($this->issue->owner != $this->user->id and - $this->issue->submitter != $this->user->id) { - $this->issue->setAssoc($this->user); // interested user. + // Compare between the old and the new data + $changes = array(); + $oldtags = $this->issue->get_tags_list(); + foreach ($tags as $tag) { + if (!Pluf_Model_InArray($tag, $oldtags)) { + if (!isset($changes['lb'])) $changes['lb'] = array(); + if ($tag->class != 'Other') { + $changes['lb'][] = (string) $tag; //new tag + } else { + $changes['lb'][] = (string) $tag->name; + } + } } - if ($this->cleaned_data['attachment']) { + foreach ($oldtags as $tag) { + if (!Pluf_Model_InArray($tag, $tags)) { + if (!isset($changes['lb'])) $changes['lb'] = array(); + if ($tag->class != 'Other') { + $changes['lb'][] = '-'.(string) $tag; //new tag + } else { + $changes['lb'][] = '-'.(string) $tag->name; + } + } + } + // Status, summary and owner + $status = IDF_Tag::add(trim($this->cleaned_data['status']), $this->project, 'Status'); + if ($status->id != $this->issue->status) { + $changes['st'] = $status->name; + } + if (trim($this->issue->summary) != trim($this->cleaned_data['summary'])) { + $changes['su'] = trim($this->cleaned_data['summary']); + } + $owner = self::findUser($this->cleaned_data['owner']); + if ((is_null($owner) and !is_null($this->issue->get_owner())) + or (!is_null($owner) and is_null($this->issue->get_owner())) + or ((!is_null($owner) and !is_null($this->issue->get_owner())) and $owner->id != $this->issue->get_owner()->id)) { + $changes['ow'] = (is_null($owner)) ? '---' : $owner->login; + } + // Update the issue + $this->issue->batchAssoc('IDF_Tag', $tagids); + $this->issue->summary = trim($this->cleaned_data['summary']); + $this->issue->status = $status; + $this->issue->owner = $owner; + } + // Create the comment + $comment = new IDF_IssueComment(); + $comment->issue = $this->issue; + $comment->content = $this->cleaned_data['content']; + $comment->submitter = $this->user; + if (!$this->show_full) $changes = array(); + $comment->changes = $changes; + $comment->create(); + $this->issue->update(); + if ($this->issue->owner != $this->user->id and + $this->issue->submitter != $this->user->id) { + $this->issue->setAssoc($this->user); // interested user. + } + 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.')); + return $this->issue; } } diff --git a/src/IDF/IssueFile.php b/src/IDF/IssueFile.php index f4367dd..f0745e8 100644 --- a/src/IDF/IssueFile.php +++ b/src/IDF/IssueFile.php @@ -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 { diff --git a/src/IDF/templates/idf/issues/create.html b/src/IDF/templates/idf/issues/create.html index e8f4a5b..faaa743 100644 --- a/src/IDF/templates/idf/issues/create.html +++ b/src/IDF/templates/idf/issues/create.html @@ -24,10 +24,22 @@ {$form.f.content|unsafe} - -{$form.f.attachment.labelTag}: -{if $form.f.attachment.errors}{$form.f.attachment.fieldErrors}{/if} -{$form.f.attachment|unsafe} + +{$form.f.attachment1.labelTag}: +{if $form.f.attachment1.errors}{$form.f.attachment1.fieldErrors}{/if} +{$form.f.attachment1|unsafe} + + + +{$form.f.attachment2.labelTag}: +{if $form.f.attachment2.errors}{$form.f.attachment2.fieldErrors}{/if} +{$form.f.attachment2|unsafe} + + + +{$form.f.attachment3.labelTag}: +{if $form.f.attachment3.errors}{$form.f.attachment3.fieldErrors}{/if} +{$form.f.attachment3|unsafe} {if $isOwner or $isMember} @@ -74,7 +86,34 @@ {/block} {block javascript} +{/literal}{/block} + {include 'idf/issues/js-autocomplete.html'}{/block} diff --git a/src/IDF/templates/idf/issues/view.html b/src/IDF/templates/idf/issues/view.html index cb9e085..ad56d05 100644 --- a/src/IDF/templates/idf/issues/view.html +++ b/src/IDF/templates/idf/issues/view.html @@ -59,10 +59,22 @@ {$form.f.content|unsafe} - -{$form.f.attachment.labelTag}: -{if $form.f.attachment.errors}{$form.f.attachment.fieldErrors}{/if} -{$form.f.attachment|unsafe} + +{$form.f.attachment1.labelTag}: +{if $form.f.attachment1.errors}{$form.f.attachment1.fieldErrors}{/if} +{$form.f.attachment1|unsafe} + + + +{$form.f.attachment2.labelTag}: +{if $form.f.attachment2.errors}{$form.f.attachment2.fieldErrors}{/if} +{$form.f.attachment2|unsafe} + + + +{$form.f.attachment3.labelTag}: +{if $form.f.attachment3.errors}{$form.f.attachment3.fieldErrors}{/if} +{$form.f.attachment3|unsafe} {if $isOwner or $isMember} @@ -122,4 +134,33 @@

{/if} {/block} -{block javascript}{if $form}{include 'idf/issues/js-autocomplete.html'}{/if}{/block} +{block javascript}{if $form}{include 'idf/issues/js-autocomplete.html'} + +{/if}{/block} diff --git a/www/media/idf/img/attachment.png b/www/media/idf/img/attachment.png new file mode 100644 index 0000000000000000000000000000000000000000..529bb7f554dfa585a4a8d6b9135956b188be3f37 GIT binary patch literal 649 zcmV;40(Sk0P)s%e4^YA&(d3cVhTCLK&Bw17g6TmTG z1V{n*fT@5#xT4HeH3O0?b^tS7rmoSw{r%V-b~ZOQiN#_pEk6BTsZ=}xe=t$cSGQab zmz{mi(ebe{R#sQJ6}U|#@&J>`gv;$R0T-3Ye*j4q&q%WP893+j`Iw##vHWIvYHO=7 zl>V7MAB{f7>-7Sh`X>Nf@%e^rz^+b1Cz(tJ;8MUJTm){cudQL&u@hi-JD_9GGYEiI z(AHxWNfwU+*8l`T2hh+oL9th9b-HH241~;Pvnr8Dpb>-?Kv{uGC;SGqx_WF6pgJv| z1wdDW+XMBRg})V4l@)2!{{iYJm2wreS`E+xP%4!GD3*%b1NmGYfLE_yGT`11Fln=P z^YPOM0P?w93((NSLZJrC%||(L{3KS3&1kV$2}foCC=?3Y0_)jq)}YtxNhW_}CU0Ih z_SzjhiasQ}k;T#LB>pW95Nr2@*@dMACdQA@(``3uG#cK$e@nSi#bYy1oMBO%h6bQrj!1R8#$B#Q=Wvpxb0Q=5Y{ jFbmuT!U2DDE_ literal 0 HcmV?d00001