/
GetCodeName.php
111 lines (102 loc) · 3.9 KB
/
GetCodeName.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
107
108
109
110
111
<?php namespace App\Helpers;
/**
* Class GetCodeName
* @package App\Helpers
*/
class GetCodeName
{
/**
* get activity code name
* @param $listName
* @param $code
* @param bool $displayCode
* @return mixed
*/
public function getActivityCodeName($listName, $code, $displayCode = true)
{
return $this->getCodeName('Activity', $listName, $code, $displayCode);
}
/**
* get organization code name
* @param $listName
* @param $code
* @param bool $displayCode
* @return mixed
*/
public function getOrganizationCodeName($listName, $code, $displayCode = true)
{
return $this->getCodeName('Organization', $listName, $code, $displayCode);
}
/**
* get the corresponding code name
* @param $listType
* @param $listName
* @param $code
* @param bool $displayCode
* @return mixed
*/
public function getCodeName($listType, $listName, $code, $displayCode = true)
{
$defaultVersion = config('app.default_version_name');
$defaultLocale = config('app.fallback_locale');
$version = session()->get('version');
$locale = config('app.locale');
$rawFilePath = app_path("Core/%s/Codelist/%s/$listType/$listName.json");
$filePath = sprintf($rawFilePath, $version, $locale);
file_exists($filePath) ?: $filePath = sprintf($rawFilePath, $version, $defaultLocale);
file_exists($filePath) ?: $filePath = sprintf($rawFilePath, $defaultVersion, $locale);
file_exists($filePath) ?: $filePath = sprintf($rawFilePath, $defaultVersion, $defaultLocale);
$codeListFromFile = file_get_contents($filePath);
$codeLists = json_decode($codeListFromFile, true);
$codeList = $codeLists[$listName];
foreach ($codeList as $list) {
if ($list['code'] == $code) {
return $displayCode ? sprintf('%s [%s]', $list['name'], $list['code']) : $list['name'];
}
if (array_key_exists('category', $list)) {
if ($list['category'] == $code) {
return $displayCode ? sprintf('%s [%s]', $list['name'], $list['category']) : $list['name'];
}
}
}
}
/**
* Get Only the activity code name (without the code value)
* @param $listName
* @param $code
* @param int $default
* @param string $type
* @return string
*/
public function getCodeNameOnly($listName, $code, $default = - 4, $type = 'Activity')
{
$descriptionWithCode = $this->getCodeName($type, $listName, $code);
return sprintf("%s", substr($descriptionWithCode, 0, $default));
}
/**
* @param $listType
* @param $listName
* @param $code
* @return string
*/
public function getCode($listType, $listName, $code)
{
$defaultVersion = config('app.default_version_name');
$defaultLocale = config('app.fallback_locale');
$version = session()->get('version');
$locale = config('app.locale');
$rawFilePath = app_path("Core/%s/Codelist/%s/$listType/$listName.json");
$filePath = sprintf($rawFilePath, $version, $locale);
file_exists($filePath) ?: $filePath = sprintf($rawFilePath, $version, $defaultLocale);
file_exists($filePath) ?: $filePath = sprintf($rawFilePath, $defaultVersion, $locale);
file_exists($filePath) ?: $filePath = sprintf($rawFilePath, $defaultVersion, $defaultLocale);
$codeListFromFile = file_get_contents($filePath);
$codeLists = json_decode($codeListFromFile, true);
$codeList = $codeLists[$listName];
foreach ($codeList as $list) {
if ($list['code'] == $code) {
return sprintf('[%s]', $list['code']);
}
}
}
}