1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/kanboard_ynh.git synced 2024-09-03 19:36:17 +02:00
kanboard_ynh/sources/app/Model/Currency.php
2015-04-21 17:56:16 +02:00

104 lines
2.3 KiB
PHP

<?php
namespace Model;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
/**
* Currency
*
* @package model
* @author Frederic Guillot
*/
class Currency extends Base
{
/**
* SQL table name
*
* @var string
*/
const TABLE = 'currencies';
/**
* Get all currency rates
*
* @access public
* @return array
*/
public function getAll()
{
return $this->db->table(self::TABLE)->findAll();
}
/**
* Calculate the price for the reference currency
*
* @access public
* @return array
*/
public function getPrice($currency, $price)
{
static $rates = null;
$reference = $this->config->get('application_currency', 'USD');
if ($reference !== $currency) {
$rates = $rates === null ? $this->db->hashtable(self::TABLE)->getAll('currency', 'rate') : array();
$rate = isset($rates[$currency]) ? $rates[$currency] : 1;
return $rate * $price;
}
return $price;
}
/**
* Add a new currency rate
*
* @access public
* @param string $currency
* @param float $rate
* @return boolean|integer
*/
public function create($currency, $rate)
{
if ($this->db->table(self::TABLE)->eq('currency', $currency)->count() === 1) {
return $this->update($currency, $rate);
}
return $this->persist(self::TABLE, compact('currency', 'rate'));
}
/**
* Update a currency rate
*
* @access public
* @param string $currency
* @param float $rate
* @return boolean
*/
public function update($currency, $rate)
{
return $this->db->table(self::TABLE)->eq('currency', $currency)->update(array('rate' => $rate));
}
/**
* Validate
*
* @access public
* @param array $values Form values
* @return array $valid, $errors [0] = Success or not, [1] = List of errors
*/
public function validate(array $values)
{
$v = new Validator($values, array(
new Validators\Required('currency', t('Field required')),
new Validators\Required('rate', t('Field required')),
));
return array(
$v->execute(),
$v->getErrors()
);
}
}