forked from innocraft/plugin-CustomTranslations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.php
106 lines (89 loc) · 3.15 KB
/
API.php
1
2
3
4
5
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* InnoCraft - the company of the makers of Matomo Analytics, the free/libre analytics platform
*
* @link https://www.innocraft.com
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\CustomTranslations;
use Piwik\API\Request;
use Piwik\Piwik;
use Piwik\Plugins\CustomTranslations\Dao\TranslationsDao;
use Piwik\Plugins\CustomTranslations\TranslationTypes\TranslationType;
use Piwik\Plugins\CustomTranslations\TranslationTypes\TranslationTypeProvider;
class API extends \Piwik\Plugin\API
{
/**
* @var TranslationsDao
*/
private $storage;
/**
* @var TranslationTypeProvider
*/
private $provider;
public function __construct(TranslationsDao $storage, TranslationTypeProvider $provider)
{
$this->storage = $storage;
$this->provider = $provider;
}
/**
* Sets (overwrites) the translations for a specific type. Make sure to pass all translations for the given type
* / language.
* @param string $idType
* @param string $languageCode
* @param array $translations An array where (original value => translation)
* @throws \Exception If type, language, or translations is not valid
*/
public function setTranslations($idType, $languageCode, $translations = array())
{
Piwik::checkUserHasSuperUserAccess();
$this->provider->checkTypeExists($idType);
$this->checkLanguageAvailable($languageCode);
$this->storage->set($idType, $languageCode, $translations);
}
/**
* Get all existing translations for a specific type and language.
* @param string $idType
* @param string $languageCode
* @throws \Exception If type, language, or translations is not valid
*/
public function getTranslationsForType($idType, $languageCode)
{
Piwik::checkUserHasSuperUserAccess();
$this->provider->checkTypeExists($idType);
$this->checkLanguageAvailable($languageCode);
return $this->storage->get($idType, $languageCode);
}
private function checkLanguageAvailable($languageCode)
{
$params = array('languageCode' => $languageCode);
$languageAvailable = Request::processRequest('LanguagesManager.isLanguageAvailable', $params);
if (!$languageAvailable) {
throw new \Exception('Invalid language code');
}
}
/**
* Get a list of all translatable types.
* @return array[]
*/
public function getTranslatableTypes()
{
Piwik::checkUserHasSuperUserAccess();
$types = $this->provider->getAllTranslationTypes();
usort($types, function ($a, $b) {
/** @var TranslationType $a */
/** @var TranslationType $b */
return strcmp($a->getName(), $b->getName());
});
$metadata = array();
foreach ($types as $type) {
$metadata[] = array(
'id' => $type->getId(),
'name' => $type->getName(),
'description' => $type->getDescription(),
'translationKeys' => $type->getTranslationKeys(),
);
}
return $metadata;
}
}