Skip to content
This repository was archived by the owner on Aug 9, 2021. It is now read-only.

Commit c17f414

Browse files
committed
refactor(policy): update code for APN policy
Signed-off-by: Domingo Oropeza <[email protected]>
1 parent fa85061 commit c17f414

File tree

4 files changed

+73
-54
lines changed

4 files changed

+73
-54
lines changed

inc/policyapn.class.php

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,31 @@
3939
*/
4040
class PluginFlyvemdmPolicyApn extends PluginFlyvemdmPolicyBase implements PluginFlyvemdmPolicyInterface {
4141

42+
private $formFields = [];
43+
private $apnAuthType = [];
44+
private $apnType = [];
45+
4246
/**
4347
* PluginFlyvemdmPolicyApn constructor.
4448
* @param PluginFlyvemdmPolicy $policy
4549
*/
4650
public function __construct(PluginFlyvemdmPolicy $policy) {
51+
$this->formFields = [
52+
'apn_name' => ['label' => __('Name', 'flyvemdm'), 'type' => 'text'],
53+
'apn_fqn' => ['label' => __('APN', 'flyvemdm'), 'type' => 'text'],
54+
'apn_proxy' => ['label' => __('Proxy', 'flyvemdm'), 'type' => 'text'],
55+
'apn_port' => ['label' => __('Port', 'flyvemdm'), 'type' => 'text'],
56+
'apn_username' => ['label' => __('Username', 'flyvemdm'), 'type' => 'text'],
57+
'apn_password' => ['label' => __('Password', 'flyvemdm'), 'type' => 'password'],
58+
'apn_server' => ['label' => __('Server', 'flyvemdm'), 'type' => 'text'],
59+
'apn_mmsc' => ['label' => __('MMSC', 'flyvemdm'), 'type' => 'text'],
60+
'apn_proxy_mms' => ['label' => __('Proxy MMS', 'flyvemdm'), 'type' => 'text'],
61+
'apn_proxy_mms_port' => ['label' => __('Proxy MMC port', 'flyvemdm'), 'type' => 'text'],
62+
'apn_mmc' => ['label' => __('MMC', 'flyvemdm'), 'type' => 'text'],
63+
'apn_mnc' => ['label' => __('MNC', 'flyvemdm'), 'type' => 'text'],
64+
];
65+
$this->apnType = [__('Default'), 'MMS', 'SUPL', 'DUN', 'HIPRI', 'FOTA'];
66+
$this->apnAuthType = [__('No authentication'), 'PAP', 'CHAP', 'CHAP/PAP'];
4767
parent::__construct($policy);
4868
$this->symbol = $policy->getField('symbol');
4969
$this->unicityRequired = ($policy->getField('unicity') != '0');
@@ -58,12 +78,13 @@ public function __construct(PluginFlyvemdmPolicy $policy) {
5878
*/
5979
public function integrityCheck($value, $itemtype, $itemId) {
6080
// Check the value exists
61-
$inputNames = ['name', 'apn'];
62-
foreach ($inputNames as $key){
63-
if (!isset($value[$key])) {
64-
Session::addMessageAfterRedirect(sprintf(__('A value for "%s" is mandatory', 'flyvemdm'), $value));
65-
return false;
66-
}
81+
if (!isset($value['apn_name']) || !$value['apn_name']) {
82+
Session::addMessageAfterRedirect(__('APN name is mandatory', 'flyvemdm'));
83+
return false;
84+
}
85+
if (!isset($value['apn_fqn']) || !$value['apn_fqn']) {
86+
Session::addMessageAfterRedirect(__('APN value is mandatory', 'flyvemdm'));
87+
return false;
6788
}
6889
return true;
6990
}
@@ -75,11 +96,12 @@ public function integrityCheck($value, $itemtype, $itemId) {
7596
* @return array|bool
7697
*/
7798
public function getMqttMessage($value, $itemtype, $itemId) {
78-
if (!$this->integrityCheck($value, $itemtype, $itemId)) {
99+
$decodedValue = json_decode($value, JSON_OBJECT_AS_ARRAY);
100+
if (!$this->integrityCheck($decodedValue, $itemtype, $itemId)) {
79101
return false;
80102
}
81103
$array = [
82-
$this->symbol => $value
104+
$this->symbol => $value,
83105
];
84106
return $array;
85107
}
@@ -88,47 +110,39 @@ public function showValueInput($value = '', $itemType = '', $itemId = 0) {
88110

89111
$value = json_decode($value, JSON_OBJECT_AS_ARRAY);
90112

91-
$fields = [
92-
'apn_name' => ['label' => 'Name', 'type' => 'text'],
93-
'apn_apn' => ['label' => 'APN', 'type' => 'text'],
94-
'apn_proxy' => ['label' => 'Proxy', 'type' => 'text'],
95-
'apn_port' => ['label' => 'Port', 'type' => 'text'],
96-
'apn_username' => ['label' => 'Username', 'type' => 'text'],
97-
'apn_password' => ['label' => 'Password', 'type' => 'password'],
98-
'apn_server' => ['label' => 'Server', 'type' => 'text'],
99-
'apn_mmsc' => ['label' => 'MMSC', 'type' => 'text'],
100-
'apn_proxy_mms' => ['label' => 'Proxy MMS', 'type' => 'text'],
101-
'apn_proxy_mms_port' => ['label' => 'Proxy MMC port', 'type' => 'text'],
102-
'apn_mmc' => ['label' => 'MMC', 'type' => 'text'],
103-
'apn_mnc' => ['label' => 'MNC', 'type' => 'text'],
104-
];
105-
106113
$data = [];
107-
foreach ($fields as $inputName => $inputOptions) {
114+
foreach ($this->formFields as $inputName => $inputOptions) {
108115
$data['inputs'][] = [
109-
'id' => $inputName,
116+
'name' => "value[$inputName]",
110117
'label' => $inputOptions['label'],
111118
'type' => $inputOptions['type'],
112119
'value' => ($value[$inputName]) ? $value[$inputName] : '',
113120
];
114121
}
115122

116-
$apnAuthType = Dropdown::showFromArray('apn_auth_type',
117-
['No authentication', 'PAP', 'CHAP', 'CHAP/PAP'], ['display' => false]);
118-
$apnType = Dropdown::showFromArray('apn_type',
119-
['default', 'mms', 'supl', 'dun', 'hipri', 'fota'], ['display' => false]);
123+
$apnAuthType = ($value['apn_auth_type']) ? $value['apn_auth_type'] : 0;
124+
$apnType = ($value['apn_type']) ? $value['apn_type'] : 0;
120125

121126
$data['apnAuthType'] = [
122-
'label' => 'Authentication Type',
123-
'dropdown' => $apnAuthType,
127+
'label' => 'Authentication Type',
128+
'dropdown' => Dropdown::showFromArray('value[apn_auth_type]',
129+
$this->apnAuthType,
130+
['display' => false, 'value' => $apnAuthType]),
124131
];
125132
$data['apnType'] = [
126-
'label' => 'APN Type',
127-
'dropdown' => $apnType,
133+
'label' => 'APN Type',
134+
'dropdown' => Dropdown::showFromArray('value[apn_type]',
135+
$this->apnType,
136+
['display' => false, 'value' => $apnType]),
128137
];
129138

130139
$twig = plugin_flyvemdm_getTemplateEngine();
131140
return $twig->render('policy_apn_form.html.twig', $data);
132141
}
133142

143+
public function showValue(PluginFlyvemdmTask $task) {
144+
$values = json_decode($task->getField('value'), JSON_OBJECT_AS_ARRAY);
145+
$stringValues = $values['apn_name'];
146+
return $stringValues;
147+
}
134148
}

install/policies/peripherals.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
'type_data' => '',
226226
'unicity' => 0,
227227
'plugin_flyvemdm_policycategories_id' => $category,
228-
'comment' => __('Set-up you APN values manually','flyvemdm'),
228+
'comment' => __('Set-up you APN values manually', 'flyvemdm'),
229229
'default_value' => '',
230230
'recommended_value' => '',
231231
'is_android_policy' => '1',

tests/src/Flyvemdm/Tests/CommonTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ public static function policyList() {
705705
'Policy/disableStreamSystem',
706706
'Policy/defaultStreamType',
707707
'Policy/periodicGeolocation',
708+
'Policy/apnConfiguration',
708709
];
709710
}
710711

tpl/policy_apn_form.html.twig

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
{% for input in inputs %}
1+
<table>
2+
{% for input in inputs %}
3+
{% if loop.index0 is even %}
4+
<tr>
5+
{% endif %}
6+
<td><label for="{{ input.name }}">{{ input.label }}</label></td>
7+
<td><input type="{{ input.type }}" name="{{ input.name }}" value="{{ input.value }}"></td>
8+
{% if loop.index0 is odd %}
9+
</tr>
10+
{% endif %}
11+
{% endfor %}
212
<tr>
3-
<td for="{{ input.id }}">{{ __(input.label) }}</td>
4-
<td><input id="{{ input.id }}" type="{{ input.type }}" value="{{ input.value }}"></td>
13+
<td>
14+
<label>{{ apnAuthType.label }}</label>
15+
</td>
16+
<td>
17+
{{ apnAuthType.dropdown|raw }}
18+
</td>
19+
<td>
20+
<label>{{ apnType.label }}</label>
21+
</td>
22+
<td>
23+
{{ apnType.dropdown|raw }}
24+
</td>
525
</tr>
6-
{% endfor %}
7-
<tr>
8-
<td>
9-
<label>{{ apnAuthType.label }}</label>
10-
</td>
11-
<td>
12-
{{ apnAuthType.dropdown|raw }}
13-
</td>
14-
</tr>
15-
<tr>
16-
<td>
17-
<label>{{ apnType.label }}</label>
18-
</td>
19-
<td>
20-
{{ apnType.dropdown|raw }}
21-
</td>
22-
</tr>
26+
</table>

0 commit comments

Comments
 (0)