Completed
Branch EDTR/master (6bd139)
by
unknown
35:03 queued 26:41
created

LegacyAccountingAssetManager::loadAccountingJs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\assets;
4
5
use DomainException;
6
use EE_Currency_Config;
7
use EventEspresso\core\domain\DomainInterface;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
use EventEspresso\core\exceptions\InvalidEntityException;
10
use EventEspresso\core\services\assets\AssetCollection;
11
use EventEspresso\core\services\assets\AssetManager;
12
use EventEspresso\core\services\assets\Registry;
13
use EventEspresso\core\services\collections\DuplicateCollectionIdentifierException;
14
15
16
/**
17
 * LegacyEditorAssetManager
18
 * assets for the EE legacy event editor
19
 *
20
 * @package EventEspresso\core\domain\services\assets
21
 * @author  Brent Christensen
22
 * @since   $VID:$
23
 */
24
class LegacyAccountingAssetManager extends AssetManager
25
{
26
27
    const JS_HANDLE_UNDERSCORE = 'underscore';
28
29
    const JS_HANDLE_ACCOUNTING_CORE = 'ee-accounting-core';
30
31
    const JS_HANDLE_ACCOUNTING = 'ee-accounting';
32
33
34
    /**
35
     * @var EE_Currency_Config $currency_config
36
     */
37
    protected $currency_config;
38
39
    /**
40
     * CoreAssetRegister constructor.
41
     *
42
     * @param AssetCollection    $assets
43
     * @param DomainInterface    $domain
44
     * @param Registry           $registry
45
     * @param EE_Currency_Config $currency_config
46
     */
47
    public function __construct(
48
        AssetCollection $assets,
49
        DomainInterface $domain,
50
        Registry $registry,
51
        EE_Currency_Config $currency_config
52
    ) {
53
        $this->currency_config = $currency_config;
54
        parent::__construct($domain, $assets, $registry);
55
    }
56
57
58
59
    /**
60
     * @throws InvalidDataTypeException
61
     * @throws InvalidEntityException
62
     * @throws DuplicateCollectionIdentifierException
63
     * @throws DomainException
64
     */
65
    public function addAssets()
66
    {
67
        $this->loadAccountingJs();
68
    }
69
70
71
    /**
72
     * accounting.js for performing client-side calculations
73
     *
74
     * @throws DomainException
75
     * @throws DuplicateCollectionIdentifierException
76
     * @throws InvalidDataTypeException
77
     * @throws InvalidEntityException
78
     * @since $VID:$
79
     */
80
    private function loadAccountingJs()
81
    {
82
        //accounting.js library
83
        // @link http://josscrowcroft.github.io/accounting.js/
84
        $this->addJavascript(
85
            LegacyAccountingAssetManager::JS_HANDLE_ACCOUNTING_CORE,
86
            EE_THIRD_PARTY_URL . 'accounting/accounting.js',
87
            [LegacyAccountingAssetManager::JS_HANDLE_UNDERSCORE],
88
            true,
89
            '0.3.2'
90
        );
91
92
        $this->addJavascript(
93
            LegacyAccountingAssetManager::JS_HANDLE_ACCOUNTING,
94
            EE_GLOBAL_ASSETS_URL . 'scripts/ee-accounting-config.js',
95
            [LegacyAccountingAssetManager::JS_HANDLE_ACCOUNTING_CORE]
96
        )
97
             ->setInlineDataCallback(
98
                 function () {
99
                     wp_localize_script(
100
                         LegacyAccountingAssetManager::JS_HANDLE_ACCOUNTING,
101
                         'EE_ACCOUNTING_CFG',
102
                         $this->getAccountingSettings()
103
                     );
104
                 }
105
             );
106
    }
107
108
109
    /**
110
     * Returns configuration data for the accounting-js library.
111
     *
112
     * @return array
113
     * @since $VID:$
114
     */
115
    private function getAccountingSettings()
116
    {
117
        return [
118
            'currency' => [
119
                'symbol'    => $this->currency_config->sign,
120
                'format'    => [
121
                    'pos'  => $this->currency_config->sign_b4 ? '%s%v' : '%v%s',
122
                    'neg'  => $this->currency_config->sign_b4 ? '- %s%v' : '- %v%s',
123
                    'zero' => $this->currency_config->sign_b4 ? '%s--' : '--%s',
124
                ],
125
                'decimal'   => $this->currency_config->dec_mrk,
126
                'thousand'  => $this->currency_config->thsnds,
127
                'precision' => $this->currency_config->dec_plc,
128
            ],
129
            'number'   => [
130
                'precision' => $this->currency_config->dec_plc,
131
                'thousand'  => $this->currency_config->thsnds,
132
                'decimal'   => $this->currency_config->dec_mrk,
133
            ],
134
        ];
135
    }
136
}
137