new language strings and updated code

This commit is contained in:
James Cole
2018-07-28 06:27:30 +02:00
parent 0ccc1271a6
commit 07ae64693e
49 changed files with 298 additions and 269 deletions

View File

@@ -24,13 +24,17 @@ declare(strict_types=1);
namespace FireflyIII\Factory;
use Exception;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use Log;
/**
* Class AccountMetaFactory
*/
class AccountMetaFactory
{
/**
* @param array $data
*
@@ -41,4 +45,45 @@ class AccountMetaFactory
return AccountMeta::create($data);
}
/**
* Create update or delete meta data.
*
* @param Account $account
* @param string $field
* @param string $value
*
* @return AccountMeta|null
*/
public function crud(Account $account, string $field, string $value): ?AccountMeta
{
/** @var AccountMeta $entry */
$entry = $account->accountMeta()->where('name', $field)->first();
// must not be an empty string:
if ('' !== $value) {
// if $data has field and $entry is null, create new one:
if (null === $entry) {
Log::debug(sprintf('Created meta-field "%s":"%s" for account #%d ("%s") ', $field, $value, $account->id, $account->name));
$this->create(['account_id' => $account->id, 'name' => $field, 'data' => $value]);
}
// if $data has field and $entry is not null, update $entry:
if (null !== $entry) {
$entry->data = $value;
$entry->save();
Log::debug(sprintf('Updated meta-field "%s":"%s" for #%d ("%s") ', $field, $value, $account->id, $account->name));
}
}
if ('' === $value && null !== $entry && isset($data[$field])) {
try {
$entry->delete();
} catch (Exception $e) {
Log::debug(sprintf('Could not delete entry: %s', $e->getMessage()));
}
}
return $entry;
}
}