I would like to extend my learning-component that I'm writing. For this reason I'm creating a little ticketing-system which fetches mail from IMAP and saves attachments if they have a specific filetype (only png, jpg, ...) .
While saving the attachments the filename is hashed and saved afterwards.
Of course I would like to extend my ticket-view aswell: In my ticket-view there are all messages assigned to this ticket (used models: ticket, messages).
Now I would like to show downloadable attachments for each message in this ticket which has one or more attachments. Clicking "download attachment" should start download-process.
I tried to extend messages-model with left join but it does not work that good.
This is my code (model):
<?php
defined('_JEXEC') or die;
class BestiaModelMessages extends JModelList
{
/**
* __construct function.
*
* @access public
* @param array $config (default: array())
* @return void
*/
public function __construct($config = array())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'id', 'a.id',
'ticketid' , 'a.ticketid',
'from', 'a.from',
'to', 'a.to',
'date', 'a.date',
'content', 'a.content',
'title', 'a.title'
);
}
parent::__construct($config);
}
/**
* populateState function.
*
* @access protected
* @param mixed $ordering (default: null)
* @param mixed $direction (default: null)
* @return void
*/
protected function populateState($ordering = null, $direction = null)
{
$published = $this->getUserStateFromRequest($this->context.'.filter.state', 'filter_state', '', 'string');
$this->setState('filter.state', $published);
parent::populateState('a.id', 'desc');
}
/**
* getListQuery function.
*
* @access protected
* @return void
*/
protected function getListQuery()
{
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->select(
$this->getState(
'list.select',
'a.id, a.ticketid, a.from, a.to, a.date, a.title, a.content'));
$query->from($db->quoteName('#__bestia_tickets_messages').' AS a');
// Filter by search in title
$search = $this->getState('filter.search');
if (!empty($search))
{
if (stripos($search, 'id:') === 0)
{
$query->where('a.id = ' . (int) substr($search, 3));
}
else
{
$search = $db->quote('%' . $db->escape($search, true) . '%');
$query->where('(a.content LIKE ' . $search.' OR a.id LIKE ' . $search . 'OR a.from LIKE ' . $search . 'OR a.to LIKE ' . $search . 'OR a.date LIKE ' . $search .' OR a.title LIKE ' . $search . ')');
}
}
$query->select('GROUP_CONCAT(cast(concat(tma.filename,\', \',tma.filepath)AS char) SEPARATOR \', \') AS attachments')
->join('LEFT', '#__bestia_tickets_messages_attachments as tma ON a.id = tma.messageid');
$model =& $this->getInstance('Ticket', 'BestiaModel');
$item = $model->getItem();
$ticketid = $item->ticketid;
$query->where('a.ticketid = '.$db->quote($ticketid));
$query->order('a.date DESC');
$query->group('a.id');
// var_dump((string)$query);
return $query;
} // ./protected function getListQuery
} // ./class BestiaModelMessages
The resulting output looks like this:
object(stdClass)#375 (8) {
["id"]=> string(3) "725"
["ticketid"]=> string(11) "123456789"
["from"]=> string(16) "[email protected]"
["to"]=> string(27) "[email protected]"
["date"]=> string(19) "2015-08-03 14:47:27"
["title"]=> string(17) "Ticket mit Anhang"
["content"]=> string(775) "Ticket mit Anhang…"
["attachments"]=> string(217) "Test.png, asdaadasasgsdafasdasdasgasfasf, Bildschirmfoto 2015-07-14 um 17.10.43.png, 52d0833774bc94698e4101c021deebecf5724a4c65d1efc477948180c072c61778b6c0174040cdef6d94d8669251f0a6646c5caf6e1966b9796625979973b2a4.png"
}
Now I would like to associate the filename and the file path to use it like this:
foreach($message->attachments as $attachment)
{
$attachment->filename;
$attachment->filepath;
}
How could I solve this problem?