Commit a5c87a8d authored by Batyr Mackenov's avatar Batyr Mackenov

Minor improvements to JS

parent eaa0aec9
<?php
//##copyright##
class iaBackendController extends iaAbstractControllerModuleBackend
{
protected $_name = 'currencies';
protected $_table = 'currencies';
protected $_gridColumns = '`code`, `title`, `rate`, `default`, `order`, `code` `id`, 1 `update`, 1 `delete`';
protected $_gridFilters = ['code' => self::EQUAL, 'title' => self::LIKE];
public function init()
{
$this->_path = IA_ADMIN_URL . $this->getModuleName() . IA_URL_DELIMITER . $this->getName() . IA_URL_DELIMITER;
$this->_template = 'form-currencies';
}
protected function _setPageTitle(&$iaView, array $entryData, $action)
{
$iaView->title(iaLanguage::getf($action . '_currency', $entryData));
}
public function getById($id)
{
return $this->_iaDb->row(iaDb::ALL_COLUMNS_SELECTION, iaDb::convertIds($id, 'code'));
}
protected function _entryUpdate(array $entryData, $entryId)
{
$this->_iaDb->update($entryData, iaDb::convertIds($entryId, 'code'));
return (0 === $this->_iaDb->getErrorNumber());
}
protected function _setDefaultValues(array &$entry)
{
$entry = [
'code' => '',
'title' => '',
'rate' => 0.00,
'sym' => '',
'sym_pos' => 'pre',
'default' => false
];
}
protected function _preSaveEntry(array &$entry, array $data, $action)
{
$entry['code'] = $data['code'];
$entry['title'] = $data['title'];
$entry['rate'] = (float)$data['rate'];
$entry['sym'] = $data['sym'];
$entry['sym_pos'] = $data['sym_pos'];
$entry['default'] = (int)$data['default'];
$requiredFields = ['code', 'title', 'rate', 'sym'];
foreach ($requiredFields as $fieldName) {
if (empty($entry[$fieldName])) {
$this->addMessage(iaLanguage::getf('field_is_empty', ['field' => iaLanguage::get($fieldName)]), false);
}
}
return !$this->getMessages();
}
}
\ No newline at end of file
<?php
//##copyright##
class iaCurrency extends abstractModuleFront
{
const CACHE_KEY = 'commerce_currencies';
const SESSION_KEY = 'cmrc_currency';
protected static $_table = 'currencies';
protected $_moduleName = 'commerce';
protected $_iaCache;
public function init()
{
parent::init();
$this->_iaCache = $this->iaCore->factory('cache');
}
public function getByCode($code)
{
$entries = $this->fetch();
return isset($entries[$code]) ? $entries[$code] : null;
}
public function fetch()
{
$result = $this->_iaCache->get(self::CACHE_KEY, 604800, true);
if (false === $result) {
$result = $this->_fetchFromDb();
$this->_iaCache->write(self::CACHE_KEY, $result);
}
return $result;
}
public function get()
{
if (isset($_SESSION[self::SESSION_KEY])) {
return $_SESSION[self::SESSION_KEY];
} else {
foreach ($this->fetch() as $entry) {
if ($entry['default']) {
$this->set($entry['code']);
break;
}
}
return $entry;
}
}
public function set($currencyCode)
{
if ($currency = $this->getByCode($currencyCode)) {
$_SESSION[self::SESSION_KEY] = $currency;
}
}
public function format($number)
{
$currency = $this->get();
$converted = $this->iaCore->get('currency') != $currency['code']
? $number * $currency['rate']
: $number;
$converted = number_format($converted, 2);
$prefix = ('pre' == $currency['sym_pos']) ? $currency['sym'] : '';
$postfix = ('post' == $currency['sym_pos']) ? $currency['sym'] : '';
return $prefix . $converted . $postfix;
}
protected function _fetchFromDb()
{
$rows = $this->iaDb->all(iaDb::ALL_COLUMNS_SELECTION, '1 ORDER BY `order`', null, null, self::getTable());
$result = [];
foreach ($rows as $row) {
unset($row['order']);
$row['rate'] = (float)$row['rate'];
$row['default'] = (bool)$row['default'];
$result[$row['code']] = $row;
}
return $result;
}
public function refreshRates()
{
$provider = $this->_instantiateProvider($this->iaCore->get('commerce_rates_provider'));
if ($provider) {
if ($rates = $provider->fetch()) {
$this->iaDb->setTable(self::getTable());
foreach ($rates as $currencyCode => $rate) {
$this->iaDb->update(['rate' => $rate], iaDb::convertIds($currencyCode, 'code'));
}
$this->iaDb->resetTable();
}
}
}
protected function _instantiateProvider($name)
{
$class = 'iaRatesProvider' . ucfirst($name);
$file = IA_MODULES . $this->getModuleName() . '/includes/providers/' . $name . iaSystem::EXECUTABLE_FILE_EXT;
if (file_exists($file)) {
require_once $file;
if (class_exists($class)) {
$instance = new $class($this);
$instance->init();
return $instance;
}
}
return false;
}
}
\ No newline at end of file
<?php
//##copyright##
$iaCore->factoryModule('currency', 'commerce')->refreshRates();
\ No newline at end of file
......@@ -17,7 +17,7 @@ $(function(){
})
// TODO: wrap it into Commerce object
$('.js-cmd-quick-view').on('click', function(e){
$('.cards.products').on('click', '.js-cmd-quick-view', function(e){
e.preventDefault()
// the better place to activate backdrop animation is here
......@@ -35,12 +35,6 @@ $(function(){
}})
})
$('a', '#js-cmrc-currencies').on('click', function(e) {
e.preventDefault()
window.location = intelli.config.packages.commerce.url + 'currency/' + $(this).data('code') + '/';
})
$('#js-sorting-selector').on('change', function(){
intelli.commerce.utils.insertUriParam('sorting', $('option:selected', this).val())
})
......
$(function()
{
$('.plus, .minus').on('click', function(){
var $input = $(this).closest('.quantity').find('.qty'),
value = parseFloat($input.val()),
max = parseFloat($input.data('max')),
min = parseFloat($input.data('min')),
step = $input.attr('step')
$(function() {
$('.plus, .minus').on('click', function(){
var $input = $(this).closest('.quantity').find('.qty'),
value = parseFloat($input.val()),
max = parseFloat($input.data('max')),
min = parseFloat($input.data('min')),
step = $input.attr('step')
value && '' !== value && 'NaN' !== value || (value = 0), ('' === max || 'NaN' === max) && (max = ''), ('' === min || 'NaN' === min) && (min = 0), ('any' === step || '' === step || void 0 === step || 'NaN' === parseFloat(step)) && (step = 1),
$(this).is('.plus') ? $input.val(max && (max == value || value > max) ? max : value + parseFloat(step)) : min && (min == value || min > value) ? $input.val(min) : value > 0 && $input.val(value - parseFloat(step))
value && '' !== value && 'NaN' !== value || (value = 0), ('' === max || 'NaN' === max) && (max = ''), ('' === min || 'NaN' === min) && (min = 0), ('any' === step || '' === step || void 0 === step || 'NaN' === parseFloat(step)) && (step = 1),
$(this).is('.plus') ? $input.val(max && (max == value || value > max) ? max : value + parseFloat(step)) : min && (min == value || min > value) ? $input.val(min) : value > 0 && $input.val(value - parseFloat(step))
$('.js-quantity-input').trigger('change')
})
$('.js-quantity-input').trigger('change')
})
$('.js-quantity-input').on('change', function(){
$('#js-cmd-update-cart').removeClass('disabled').prop('disabled', false)
})
$('.js-quantity-input').on('change', function(){
$('#js-cmd-update-cart').removeClass('disabled').prop('disabled', false)
})
})
\ No newline at end of file
<ul id="js-cmrc-currencies">
{foreach $commerce.currencies as $currency}
<li{if $currency.default} class="active"{/if}><a href="#" data-code="{$currency.code}">{$currency.title|escape}</a></li>
{/foreach}
</ul>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment