2015-07-19 09:37:28 +02:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* AttachmentRepository.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-05-20 12:41:23 +02:00
|
|
|
*/
|
|
|
|
|
2016-02-05 12:08:25 +01:00
|
|
|
declare(strict_types = 1);
|
2015-07-19 09:37:28 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Attachment;
|
|
|
|
|
2016-10-23 09:44:14 +02:00
|
|
|
use Carbon\Carbon;
|
2016-05-02 20:49:19 +02:00
|
|
|
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
|
2015-07-19 09:37:28 +02:00
|
|
|
use FireflyIII\Models\Attachment;
|
2016-03-03 08:38:24 +01:00
|
|
|
use FireflyIII\User;
|
2016-02-23 06:39:01 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2015-07-19 09:37:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AttachmentRepository
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Repositories\Attachment
|
|
|
|
*/
|
|
|
|
class AttachmentRepository implements AttachmentRepositoryInterface
|
|
|
|
{
|
2016-03-03 08:38:24 +01:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AttachmentRepository constructor.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function __construct(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2015-07-19 09:37:28 +02:00
|
|
|
|
2015-07-26 15:51:07 +02:00
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-06 18:59:48 +01:00
|
|
|
public function destroy(Attachment $attachment): bool
|
2015-07-26 15:51:07 +02:00
|
|
|
{
|
2016-05-02 20:49:19 +02:00
|
|
|
/** @var AttachmentHelperInterface $helper */
|
|
|
|
$helper = app(AttachmentHelperInterface::class);
|
2015-07-26 15:51:07 +02:00
|
|
|
|
|
|
|
$file = $helper->getAttachmentLocation($attachment);
|
|
|
|
unlink($file);
|
|
|
|
$attachment->delete();
|
2016-02-23 06:39:01 +01:00
|
|
|
|
2016-02-06 18:59:48 +01:00
|
|
|
return true;
|
2015-07-26 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
2016-02-23 06:39:01 +01:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function get(): Collection
|
|
|
|
{
|
2016-03-03 08:38:24 +01:00
|
|
|
return $this->user->attachments()->get();
|
2016-02-23 06:39:01 +01:00
|
|
|
}
|
|
|
|
|
2016-10-23 09:44:14 +02:00
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getBetween(Carbon $start, Carbon $end): Collection
|
|
|
|
{
|
|
|
|
$query = $this->user
|
|
|
|
->attachments()
|
|
|
|
->leftJoin('transaction_journals', 'attachments.attachable_id', '=', 'transaction_journals.id')
|
|
|
|
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
|
|
|
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
|
|
|
->get(['attachments.*']);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2015-07-19 09:37:28 +02:00
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Attachment
|
|
|
|
*/
|
2016-02-06 18:59:48 +01:00
|
|
|
public function update(Attachment $attachment, array $data): Attachment
|
2015-07-19 09:37:28 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
$attachment->title = $data['title'];
|
|
|
|
$attachment->description = $data['description'];
|
|
|
|
$attachment->notes = $data['notes'];
|
|
|
|
$attachment->save();
|
|
|
|
|
|
|
|
return $attachment;
|
|
|
|
|
|
|
|
}
|
2015-07-19 09:38:44 +02:00
|
|
|
}
|