DataHelper.php 1.97 KB
Newer Older
Dainis Abols's avatar
Dainis Abols committed
1
2
3
4
<?php

namespace Lu\LuContacts\Helpers;

Dainis Abols's avatar
Dainis Abols committed
5
use TYPO3\CMS\Core\Localization\LanguageService;
Dainis Abols's avatar
Dainis Abols committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

/**
 * Class DataHelper
 *
 * @author  Dainis Abols <dainis.abols@lu.lv>
 * @owner   University of Latvia
 * @version 3.0.0
 * @since   21.06.2018
 *
 * @package Lu\LuContacts\Helpers
 */
class DataHelper
{
    /**
     * Localisation prefix
     */
    private $L10N_PREFIX = 'LLL:EXT:lu_contacts/Resources/Private/Language/locallang.xlf';

    /**
     * DataHelper constructor.
     *
     * Add additional data on init
     */
    public function __construct()
    {
        @include_once ExtensionManagementUtility::extPath('lu_api')."cli/convertLuisXmlToSolrJson.php";
    }

    /**
     * Change language file location
     *
     * @param string $L10N_PREFIX
     */
    public function setL10NPrefix(string $L10N_PREFIX)
    {
        $this->L10N_PREFIX = $L10N_PREFIX;
    }

    /**
     * Decode API returned JSON or return null, if failed
     *
     * @param $jsonData
     *
     * @return string
     */
    public function APIDecode($jsonData)
    {
        // Decode result
        $decoded = json_decode($jsonData, true);
        if ($decoded['code'] == 200) {
            $data = $decoded['data'];
        } else {
            $data = null;
        }

        return $data;
    }

    /**
     * Retrieve language value for requested string
     *
     * @TODO: Implement different language requests
     *
     * @param string $string
     *
     * @return string
     */
    public function getLangString(string $string): string
    {
        return $this->getLanguageService()->sL($this->L10N_PREFIX.':'.$string);
    }

    /**
     * Retrieve LanguageService
     *
Dainis Abols's avatar
Dainis Abols committed
82
     * @return mixed|\TYPO3\CMS\Core\Localization\LanguageService
Dainis Abols's avatar
Dainis Abols committed
83
84
85
     */
    private function getLanguageService()
    {
Dainis Abols's avatar
Dainis Abols committed
86
87
88
89
        // Ensure language object is initialized
        \TYPO3\CMS\Core\Core\Bootstrap::initializeLanguageObject();

        return $GLOBALS['LANG'];
Dainis Abols's avatar
Dainis Abols committed
90
    }
Dainis Abols's avatar
Dainis Abols committed
91
}