FinanceBuddy: card money badge on line 3 (v0.3.1)
This commit is contained in:
@@ -1,10 +1,38 @@
|
||||
/*
|
||||
* FinanceBuddy styles.
|
||||
*
|
||||
* The money badge (.fb-badge / .fb-installments) and the per-column total (.fb-column-total)
|
||||
* styling are added as those features land in later versions.
|
||||
* The per-column total (.fb-column-total) styling is added in a later version.
|
||||
*/
|
||||
|
||||
/* Line-3 money badge on board cards. Its own background guarantees contrast on any card color. */
|
||||
.fb-line {
|
||||
margin: 2px 0 4px;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.fb-badge {
|
||||
display: inline-block;
|
||||
padding: 1px 6px;
|
||||
border-radius: 3px;
|
||||
color: #fff;
|
||||
font-weight: bold;
|
||||
font-size: 1.1em;
|
||||
}
|
||||
|
||||
.fb-badge-debit {
|
||||
background: #b94a48;
|
||||
}
|
||||
|
||||
.fb-badge-credit {
|
||||
background: #468847;
|
||||
}
|
||||
|
||||
.fb-installments {
|
||||
margin-left: 6px;
|
||||
color: #000;
|
||||
font-size: 1.05em;
|
||||
}
|
||||
|
||||
/* Task-form finance fieldset (middle column). */
|
||||
.fb-form-directions {
|
||||
margin-bottom: 8px;
|
||||
|
||||
@@ -23,6 +23,9 @@ class FinanceHelper extends Base
|
||||
// non-finance task updates (moves, API calls) are ignored by the persistence hooks.
|
||||
const FORM_MARKER = 'financebuddy_form';
|
||||
|
||||
// Memoized per project_id: the board hooks below call isEnabled() once per card.
|
||||
private static $enabledCache = array();
|
||||
|
||||
/**
|
||||
* Is FinanceBuddy enabled for this board?
|
||||
*
|
||||
@@ -31,22 +34,31 @@ class FinanceHelper extends Base
|
||||
*/
|
||||
public function isEnabled($project_id)
|
||||
{
|
||||
return (int) $this->projectMetadataModel->get($project_id, self::ENABLED_KEY, 0) === 1;
|
||||
$project_id = (int) $project_id;
|
||||
|
||||
if (! isset(self::$enabledCache[$project_id])) {
|
||||
self::$enabledCache[$project_id] = (int) $this->projectMetadataModel->get($project_id, self::ENABLED_KEY, 0) === 1;
|
||||
}
|
||||
|
||||
return self::$enabledCache[$project_id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Stored finance values for a task, with defaults.
|
||||
* Stored finance values for a task, with defaults. One query (getAll) since the board renders
|
||||
* this per card.
|
||||
*
|
||||
* @param integer $task_id
|
||||
* @return array
|
||||
*/
|
||||
public function get($task_id)
|
||||
{
|
||||
$all = $this->taskMetadataModel->getAll($task_id);
|
||||
|
||||
return array(
|
||||
'amount' => $this->taskMetadataModel->get($task_id, self::AMOUNT_KEY, ''),
|
||||
'direction' => $this->taskMetadataModel->get($task_id, self::DIRECTION_KEY, 'debit') ?: 'debit',
|
||||
'installment_current' => $this->taskMetadataModel->get($task_id, self::INSTALLMENT_CURRENT_KEY, ''),
|
||||
'installment_total' => $this->taskMetadataModel->get($task_id, self::INSTALLMENT_TOTAL_KEY, ''),
|
||||
'amount' => isset($all[self::AMOUNT_KEY]) ? $all[self::AMOUNT_KEY] : '',
|
||||
'direction' => (isset($all[self::DIRECTION_KEY]) && $all[self::DIRECTION_KEY] === 'credit') ? 'credit' : 'debit',
|
||||
'installment_current' => isset($all[self::INSTALLMENT_CURRENT_KEY]) ? $all[self::INSTALLMENT_CURRENT_KEY] : '',
|
||||
'installment_total' => isset($all[self::INSTALLMENT_TOTAL_KEY]) ? $all[self::INSTALLMENT_TOTAL_KEY] : '',
|
||||
);
|
||||
}
|
||||
|
||||
@@ -95,4 +107,16 @@ class FinanceHelper extends Base
|
||||
|
||||
return $value > 0 ? $value : 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a money value for display: two decimals, comma decimal separator, no thousand
|
||||
* separator (e.g. 20.9 -> "20,90"). Accepts a float or a canonical dot-string.
|
||||
*
|
||||
* @param float|string $amount
|
||||
* @return string
|
||||
*/
|
||||
public static function money($amount)
|
||||
{
|
||||
return number_format((float) $amount, 2, ',', '');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,11 @@ class Plugin extends Base
|
||||
$this->hook->on('model:task:creation:prepare', array($persistence, 'onCreationPrepare'));
|
||||
$this->hook->on('model:task:creation:aftersave', array($persistence, 'onCreationAfterSave'));
|
||||
$this->hook->on('model:task:modification:prepare', array($persistence, 'onModificationPrepare'));
|
||||
|
||||
// The line-3 money badge on each board card, rendered directly under the title. Two hooks:
|
||||
// "private" is the normal board, "public" is the read-only board shared by token.
|
||||
$this->template->hook->attach('template:board:private:task:after-title', 'financeBuddy:board/task_money');
|
||||
$this->template->hook->attach('template:board:public:task:after-title', 'financeBuddy:board/task_money');
|
||||
}
|
||||
|
||||
public function getHelpers()
|
||||
@@ -54,7 +59,7 @@ class Plugin extends Base
|
||||
|
||||
public function getPluginVersion()
|
||||
{
|
||||
return '0.3.0';
|
||||
return '0.3.1';
|
||||
}
|
||||
|
||||
public function getPluginHomepage()
|
||||
|
||||
27
Template/board/task_money.php
Normal file
27
Template/board/task_money.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
// Line-3 money badge, rendered under the card title via the after-title hooks. Server-side, so
|
||||
// it re-renders correctly on every board refresh. Nothing shows unless the board is enabled and
|
||||
// the card has a positive amount.
|
||||
if (empty($task['project_id']) || ! $this->FinanceHelper->isEnabled($task['project_id'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$fb = $this->FinanceHelper->get($task['id']);
|
||||
|
||||
if ($fb['amount'] === '' || (float) $fb['amount'] <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$credit = $fb['direction'] === 'credit';
|
||||
$sign = $credit ? '+' : '-';
|
||||
$total = $fb['installment_total'];
|
||||
$current = $fb['installment_current'] !== '' ? $fb['installment_current'] : '1';
|
||||
?>
|
||||
<div class="fb-line">
|
||||
<span class="fb-badge <?= $credit ? 'fb-badge-credit' : 'fb-badge-debit' ?>">
|
||||
<?= $sign ?>R$<?= \Kanboard\Plugin\FinanceBuddy\Helper\FinanceHelper::money($fb['amount']) ?>
|
||||
</span>
|
||||
<?php if ($total !== ''): ?>
|
||||
<span class="fb-installments">p<?= $this->text->e($current) ?>/<?= $this->text->e($total) ?></span>
|
||||
<?php endif ?>
|
||||
</div>
|
||||
Reference in New Issue
Block a user