Skip to content

Commit

Permalink
Added attachment support
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Oct 22, 2017
1 parent 77749c9 commit 124f2a5
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 9 deletions.
21 changes: 21 additions & 0 deletions Components/DatabaseMailTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ public function __construct(Connection $connection)
*/
protected function _sendMail()
{
$attachments = [];
if ($this->_mail->hasAttachments) {
$parts = $this->_mail->getParts();

/** @var \Zend_Mime_Part $part */
foreach ($parts as $part) {
if ($part->disposition === 'attachment') {
$attachments[] = [
'file_name' => $part->filename,
'content' => $part->getContent()
];
}
}
}

$this->connection->insert('s_plugin_mailcatcher', [
'created' => date('Y-m-d H:i:s'),
'senderAddress' => $this->_mail->getFrom(),
Expand All @@ -44,5 +59,11 @@ protected function _sendMail()
'bodyText' => $this->_mail->getPlainBodyText(),
'bodyHtml' => $this->_mail->getPlainBody()
]);

$insertId = $this->connection->lastInsertId();
foreach ($attachments as $attachment) {
$attachment['mail_id'] = $insertId;
$this->connection->insert('s_plugin_mailcatcher_attachments', $attachment);
}
}
}
53 changes: 52 additions & 1 deletion Controllers/Backend/Mailcatcher.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

use Doctrine\ORM\AbstractQuery;
use ShyimMailCatcher\Models\Attachment;
use ShyimMailCatcher\Models\Mails;

/**
* Class Shopware_Controllers_Backend_Mailcatcher
*/
class Shopware_Controllers_Backend_Mailcatcher extends Shopware_Controllers_Backend_Application {
class Shopware_Controllers_Backend_Mailcatcher extends Shopware_Controllers_Backend_Application implements \Shopware\Components\CSRFWhitelistAware {

/**
* @var string
Expand Down Expand Up @@ -37,11 +39,60 @@ public function getNewMailsAction()
$this->View()->mails = $mails;
}

/**
* Attachment list
*/
public function getAttachmentsAction()
{
$mailId = $this->Request()->getParam('mailId');

$qb = $this->getModelManager()->createQueryBuilder();
$result = $qb->from(Attachment::class, 'attachment')
->select(['attachment.id', 'attachment.fileName'])
->where('attachment.mail = :mailId')
->setParameter('mailId', $mailId)
->getQuery()
->setHydrationMode(AbstractQuery::HYDRATE_ARRAY)
->execute();

$this->View()->success = true;
$this->View()->data = $result;
$this->View()->total = count($result);
}

/**
* Download a attachment
*/
public function downloadAttachmentAction()
{
$attachmentId = $this->Request()->getParam('id');
$attachment = $this->getModelManager()->find(Attachment::class, $attachmentId);

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $attachment->getFileName() . '"');

echo base64_decode($attachment->getContent());
exit();
}

/**
* Clear all entries in mailbox
*/
public function clearAction()
{
$this->container->get('dbal_connection')->executeQuery('SET FOREIGN_KEY_CHECKS = 0');
$this->container->get('dbal_connection')->executeQuery('TRUNCATE TABLE s_plugin_mailcatcher_attachments');
$this->container->get('dbal_connection')->executeQuery('TRUNCATE TABLE s_plugin_mailcatcher');
$this->container->get('dbal_connection')->executeQuery('SET FOREIGN_KEY_CHECKS = 1');
}

/**
* Returns a list with actions which should not be validated for CSRF protection
*
* @return string[]
*/
public function getWhitelistedCSRFActions()
{
return ['downloadAttachment'];
}
}
106 changes: 106 additions & 0 deletions Models/Attachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace ShyimMailCatcher\Models;

use Shopware\Components\Model\ModelEntity;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table(name="s_plugin_mailcatcher_attachments")
* @ORM\Entity
*/
class Attachment extends ModelEntity
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* OWNING SIDE
*
* @var Mails
*
* @ORM\ManyToOne(targetEntity="ShyimMailCatcher\Models\Mails")
* @ORM\JoinColumn(name="mail_id", referencedColumnName="id")
*/
protected $mail;

/**
* @ORM\Column(name="file_name", type="string", nullable=false)
*/
private $fileName;

/**
* @ORM\Column(name="content", type="text", nullable=false)
*/
private $content;

/**
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}

/**
* @return Mails
*/
public function getMail()
{
return $this->mail;
}

/**
* @param Mails $mail
*/
public function setMail(Mails $mail)
{
$this->mail = $mail;
}

/**
* @return mixed
*/
public function getFileName()
{
return $this->fileName;
}

/**
* @param mixed $fileName
*/
public function setFileName($fileName)
{
$this->fileName = $fileName;
}

/**
* @return mixed
*/
public function getContent()
{
return $this->content;
}

/**
* @param mixed $content
*/
public function setContent($content)
{
$this->content = $content;
}
}
34 changes: 34 additions & 0 deletions Models/Mails.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ShyimMailCatcher\Models;

use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Shopware\Components\Model\ModelEntity;
use Doctrine\ORM\Mapping as ORM;

Expand Down Expand Up @@ -57,6 +58,23 @@ class Mails extends ModelEntity
*/
private $bodyHtml;

/**
* INVERSE SIDE
*
* @ORM\OneToMany(targetEntity="ShyimMailCatcher\Models\Attachment", mappedBy="mail", orphanRemoval=true, cascade={"persist"})
*
* @var ArrayCollection
*/
protected $attachments;

/**
* Mails constructor.
*/
public function __construct()
{
$this->attachments = new ArrayCollection();
}

/**
* @return int
*/
Expand Down Expand Up @@ -168,4 +186,20 @@ public function setBodyHtml($bodyHtml)
{
$this->bodyHtml = $bodyHtml;
}

/**
* @return array
*/
public function getAttachments()
{
return $this->attachments;
}

/**
* @param array $attachments
*/
public function setAttachments($attachments)
{
$this->attachments = $attachments;
}
}
4 changes: 2 additions & 2 deletions Resources/views/backend/mailcatcher/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Ext.define('Shopware.apps.Mailcatcher', {
'list.Window'
],

models: [ 'Mails' ],
stores: [ 'Mails' ],
models: [ 'Mails', 'Attachment' ],
stores: [ 'Mails', 'Attachment' ],

launch: function() {
return this.getController('Main').mainWindow;
Expand Down
14 changes: 14 additions & 0 deletions Resources/views/backend/mailcatcher/model/attachment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Ext.define('Shopware.apps.Mailcatcher.model.Attachment', {
extend: 'Ext.data.Model',

fields: [
{
name: 'id',
type: 'integer'
},
{
name: 'fileName',
type: 'string'
}
]
});
20 changes: 20 additions & 0 deletions Resources/views/backend/mailcatcher/store/attachment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Ext.define('Shopware.apps.Mailcatcher.store.Attachment', {
extend:'Ext.data.Store',
model: 'Shopware.apps.Mailcatcher.model.Attachment',

autoLoad: true,
remoteSort: true,
remoteFilter: true,

proxy:{
type:'ajax',

url: '{url controller=Mailcatcher action=getAttachments}',

reader:{
type:'json',
root:'data',
totalProperty:'total'
}
}
});
42 changes: 42 additions & 0 deletions Resources/views/backend/mailcatcher/view/list/attachment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Ext.define('Shopware.apps.Mailcatcher.view.list.Attachment', {
extend: 'Ext.grid.Panel',
title: 'Attachments',
height: '100%',
layout: 'fit',

initComponent: function () {
var me = this;

me.store = Ext.create('Shopware.apps.Mailcatcher.store.Attachment');
me.store.getProxy().extraParams.mailId = me.mailId;

me.store.on('load', function () {
me.setTitle('Attachments (' + me.store.count().toString() + ')');
});

me.columns = [
{
text: 'Name',
dataIndex: 'fileName',
flex: 1
},
Ext.create('Ext.grid.column.Action', {
width: 30,
items: [
{
iconCls: 'sprite-drive-download',
tooltip: 'Download',
handler: function (view, rowIndex, colIndex, item) {
var store = view.getStore(),
record = store.getAt(rowIndex);

window.open('{url action=downloadAttachment}?id=' + record.get('id'));
}
}
]
})
];

me.callParent(arguments);
}
});
3 changes: 3 additions & 0 deletions Resources/views/backend/mailcatcher/view/list/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Ext.define('Shopware.apps.Mailcatcher.view.list.Preview', {
});
}

items.push(Ext.create('Shopware.apps.Mailcatcher.view.list.Attachment', {
mailId: me.record.raw.id
}));

me.items = {
xtype: 'tabpanel',
Expand Down
Loading

0 comments on commit 124f2a5

Please sign in to comment.