Files
FinanceBuddy/Model/FinancePersistence.php

124 lines
4.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Kanboard\Plugin\FinanceBuddy\Model;
use Kanboard\Core\Base;
use Kanboard\Plugin\FinanceBuddy\Helper\FinanceHelper;
/**
* Persists the FinanceBuddy fields that live in the core task form.
*
* PicoDb's insert()/update() build SQL straight from the value keys, so any non-column field left
* in $values would break task saving (this is why core unset()s "tags"). We therefore:
* (a) strip every financebuddy_* key from $values in the create/modify "prepare" hooks, and
* (b) write the values to task metadata separately.
*
* Everything here runs synchronously inside the model call during the HTTP request (the "prepare"
* and "aftersave" reference hooks), so it does NOT depend on the optionally-async event queue. The
* finance values are read straight from $values (getValues() cannot be reused: the controller
* already consumed the CSRF token). The task id comes from the aftersave hook (create) or the
* route parameter (modify).
*/
class FinancePersistence extends Base
{
private $pending = null;
/**
* model:task:creation:prepare -- fires before INSERT, $values by reference, no task id yet.
*/
public function onCreationPrepare(array &$values)
{
$this->pending = null;
if (isset($values[FinanceHelper::FORM_MARKER])) {
$this->pending = array(
'project_id' => isset($values['project_id']) ? (int) $values['project_id'] : 0,
'fields' => $this->extract($values),
);
}
$this->strip($values);
}
/**
* model:task:creation:aftersave -- fires after INSERT, hands over the new task id by reference.
*/
public function onCreationAfterSave(&$task_id)
{
if ($this->pending !== null) {
$this->persist((int) $task_id, $this->pending['project_id'], $this->pending['fields']);
$this->pending = null;
}
}
/**
* model:task:modification:prepare -- fires before UPDATE, $values by reference. The task id is
* removed from $values by core, but it is the route parameter of the modification request.
*/
public function onModificationPrepare(array &$values)
{
if (isset($values[FinanceHelper::FORM_MARKER])) {
$task_id = $this->request->getIntegerParam('task_id');
$project_id = isset($values['project_id']) ? (int) $values['project_id'] : 0;
if ($task_id > 0) {
$this->persist($task_id, $project_id, $this->extract($values));
}
}
$this->strip($values);
}
private function extract(array $values)
{
return array(
'amount' => isset($values[FinanceHelper::AMOUNT_KEY]) ? $values[FinanceHelper::AMOUNT_KEY] : '',
'direction' => (isset($values[FinanceHelper::DIRECTION_KEY]) && $values[FinanceHelper::DIRECTION_KEY] === 'credit') ? 'credit' : 'debit',
'installment_current' => isset($values[FinanceHelper::INSTALLMENT_CURRENT_KEY]) ? $values[FinanceHelper::INSTALLMENT_CURRENT_KEY] : '',
'installment_total' => isset($values[FinanceHelper::INSTALLMENT_TOTAL_KEY]) ? $values[FinanceHelper::INSTALLMENT_TOTAL_KEY] : '',
);
}
private function strip(array &$values)
{
foreach (array_keys($values) as $key) {
if (strpos($key, 'financebuddy_') === 0) {
unset($values[$key]);
}
}
}
private function persist($task_id, $project_id, array $fields)
{
// Only touch metadata on boards where a manager opted in.
if ((int) $this->projectMetadataModel->get($project_id, FinanceHelper::ENABLED_KEY, 0) !== 1) {
return;
}
$amount = FinanceHelper::parseAmount($fields['amount']);
// No (or cleared) amount: drop any finance metadata so the card goes back to plain.
if ($amount <= 0) {
foreach (array(FinanceHelper::AMOUNT_KEY, FinanceHelper::DIRECTION_KEY, FinanceHelper::INSTALLMENT_CURRENT_KEY, FinanceHelper::INSTALLMENT_TOTAL_KEY) as $key) {
$this->taskMetadataModel->remove($task_id, $key);
}
return;
}
$this->taskMetadataModel->save($task_id, array(
FinanceHelper::AMOUNT_KEY => number_format($amount, 2, '.', ''),
FinanceHelper::DIRECTION_KEY => $fields['direction'] === 'credit' ? 'credit' : 'debit',
FinanceHelper::INSTALLMENT_CURRENT_KEY => $this->intOrBlank($fields['installment_current']),
FinanceHelper::INSTALLMENT_TOTAL_KEY => $this->intOrBlank($fields['installment_total']),
));
}
private function intOrBlank($raw)
{
$raw = trim((string) $raw);
return ($raw !== '' && ctype_digit($raw) && (int) $raw > 0) ? (string) (int) $raw : '';
}
}