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

Commit

Permalink
refactor(policy): update code for APN policy
Browse files Browse the repository at this point in the history
Signed-off-by: Domingo Oropeza <[email protected]>
  • Loading branch information
DIOHz0r committed Jul 19, 2018
1 parent 5f7d823 commit f1ca252
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 54 deletions.
80 changes: 47 additions & 33 deletions inc/policyapn.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,31 @@
*/
class PluginFlyvemdmPolicyApn extends PluginFlyvemdmPolicyBase implements PluginFlyvemdmPolicyInterface {

private $formFields = [];
private $apnAuthType = [];
private $apnType = [];

/**
* PluginFlyvemdmPolicyApn constructor.
* @param PluginFlyvemdmPolicy $policy
*/
public function __construct(PluginFlyvemdmPolicy $policy) {
$this->formFields = [
'apn_name' => ['label' => __('Name', 'flyvemdm'), 'type' => 'text'],
'apn_fqn' => ['label' => __('APN', 'flyvemdm'), 'type' => 'text'],
'apn_proxy' => ['label' => __('Proxy', 'flyvemdm'), 'type' => 'text'],
'apn_port' => ['label' => __('Port', 'flyvemdm'), 'type' => 'text'],
'apn_username' => ['label' => __('Username', 'flyvemdm'), 'type' => 'text'],
'apn_password' => ['label' => __('Password', 'flyvemdm'), 'type' => 'password'],
'apn_server' => ['label' => __('Server', 'flyvemdm'), 'type' => 'text'],
'apn_mmsc' => ['label' => __('MMSC', 'flyvemdm'), 'type' => 'text'],
'apn_proxy_mms' => ['label' => __('Proxy MMS', 'flyvemdm'), 'type' => 'text'],
'apn_proxy_mms_port' => ['label' => __('Proxy MMC port', 'flyvemdm'), 'type' => 'text'],
'apn_mmc' => ['label' => __('MMC', 'flyvemdm'), 'type' => 'text'],
'apn_mnc' => ['label' => __('MNC', 'flyvemdm'), 'type' => 'text'],
];
$this->apnType = [__('Default'), 'MMS', 'SUPL', 'DUN', 'HIPRI', 'FOTA'];
$this->apnAuthType = [__('No authentication'), 'PAP', 'CHAP', 'CHAP/PAP'];
parent::__construct($policy);
$this->symbol = $policy->getField('symbol');
$this->unicityRequired = ($policy->getField('unicity') != '0');
Expand All @@ -58,12 +78,13 @@ public function __construct(PluginFlyvemdmPolicy $policy) {
*/
public function integrityCheck($value, $itemtype, $itemId) {
// Check the value exists
$inputNames = ['name', 'apn'];
foreach ($inputNames as $key){
if (!isset($value[$key])) {
Session::addMessageAfterRedirect(sprintf(__('A value for "%s" is mandatory', 'flyvemdm'), $value));
return false;
}
if (!isset($value['apn_name']) || !$value['apn_name']) {
Session::addMessageAfterRedirect(__('APN name is mandatory', 'flyvemdm'));
return false;
}
if (!isset($value['apn_fqn']) || !$value['apn_fqn']) {
Session::addMessageAfterRedirect(__('APN value is mandatory', 'flyvemdm'));
return false;
}
return true;
}
Expand All @@ -75,11 +96,12 @@ public function integrityCheck($value, $itemtype, $itemId) {
* @return array|bool
*/
public function getMqttMessage($value, $itemtype, $itemId) {
if (!$this->integrityCheck($value, $itemtype, $itemId)) {
$decodedValue = json_decode($value, JSON_OBJECT_AS_ARRAY);
if (!$this->integrityCheck($decodedValue, $itemtype, $itemId)) {
return false;
}
$array = [
$this->symbol => $value
$this->symbol => $value,
];
return $array;
}
Expand All @@ -88,47 +110,39 @@ public function showValueInput($value = '', $itemType = '', $itemId = 0) {

$value = json_decode($value, JSON_OBJECT_AS_ARRAY);

$fields = [
'apn_name' => ['label' => 'Name', 'type' => 'text'],
'apn_apn' => ['label' => 'APN', 'type' => 'text'],
'apn_proxy' => ['label' => 'Proxy', 'type' => 'text'],
'apn_port' => ['label' => 'Port', 'type' => 'text'],
'apn_username' => ['label' => 'Username', 'type' => 'text'],
'apn_password' => ['label' => 'Password', 'type' => 'password'],
'apn_server' => ['label' => 'Server', 'type' => 'text'],
'apn_mmsc' => ['label' => 'MMSC', 'type' => 'text'],
'apn_proxy_mms' => ['label' => 'Proxy MMS', 'type' => 'text'],
'apn_proxy_mms_port' => ['label' => 'Proxy MMC port', 'type' => 'text'],
'apn_mmc' => ['label' => 'MMC', 'type' => 'text'],
'apn_mnc' => ['label' => 'MNC', 'type' => 'text'],
];

$data = [];
foreach ($fields as $inputName => $inputOptions) {
foreach ($this->formFields as $inputName => $inputOptions) {
$data['inputs'][] = [
'id' => $inputName,
'name' => "value[$inputName]",
'label' => $inputOptions['label'],
'type' => $inputOptions['type'],
'value' => ($value[$inputName]) ? $value[$inputName] : '',
];
}

$apnAuthType = Dropdown::showFromArray('apn_auth_type',
['No authentication', 'PAP', 'CHAP', 'CHAP/PAP'], ['display' => false]);
$apnType = Dropdown::showFromArray('apn_type',
['default', 'mms', 'supl', 'dun', 'hipri', 'fota'], ['display' => false]);
$apnAuthType = ($value['apn_auth_type']) ? $value['apn_auth_type'] : 0;
$apnType = ($value['apn_type']) ? $value['apn_type'] : 0;

$data['apnAuthType'] = [
'label' => 'Authentication Type',
'dropdown' => $apnAuthType,
'label' => 'Authentication Type',
'dropdown' => Dropdown::showFromArray('value[apn_auth_type]',
$this->apnAuthType,
['display' => false, 'value' => $apnAuthType]),
];
$data['apnType'] = [
'label' => 'APN Type',
'dropdown' => $apnType,
'label' => 'APN Type',
'dropdown' => Dropdown::showFromArray('value[apn_type]',
$this->apnType,
['display' => false, 'value' => $apnType]),
];

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

public function showValue(PluginFlyvemdmTask $task) {
$values = json_decode($task->getField('value'), JSON_OBJECT_AS_ARRAY);
$stringValues = $values['apn_name'];
return $stringValues;
}
}
2 changes: 1 addition & 1 deletion install/policies/peripherals.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
'type_data' => '',
'unicity' => 0,
'plugin_flyvemdm_policycategories_id' => $category,
'comment' => __('Set-up you APN values manually','flyvemdm'),
'comment' => __('Set-up you APN values manually', 'flyvemdm'),
'default_value' => '',
'recommended_value' => '',
'is_android_policy' => '1',
Expand Down
1 change: 1 addition & 0 deletions tests/src/Flyvemdm/Tests/CommonTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ public static function policyList() {
'Policy/disableStreamDTMF',
'Policy/disableStreamSystem',
'Policy/defaultStreamType',
'Policy/apnConfiguration',
];
}
}
44 changes: 24 additions & 20 deletions tpl/policy_apn_form.html.twig
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
{% for input in inputs %}
<table>
{% for input in inputs %}
{% if loop.index0 is even %}
<tr>
{% endif %}
<td><label for="{{ input.name }}">{{ input.label }}</label></td>
<td><input type="{{ input.type }}" name="{{ input.name }}" value="{{ input.value }}"></td>
{% if loop.index0 is odd %}
</tr>
{% endif %}
{% endfor %}
<tr>
<td for="{{ input.id }}">{{ __(input.label) }}</td>
<td><input id="{{ input.id }}" type="{{ input.type }}" value="{{ input.value }}"></td>
<td>
<label>{{ apnAuthType.label }}</label>
</td>
<td>
{{ apnAuthType.dropdown|raw }}
</td>
<td>
<label>{{ apnType.label }}</label>
</td>
<td>
{{ apnType.dropdown|raw }}
</td>
</tr>
{% endfor %}
<tr>
<td>
<label>{{ apnAuthType.label }}</label>
</td>
<td>
{{ apnAuthType.dropdown|raw }}
</td>
</tr>
<tr>
<td>
<label>{{ apnType.label }}</label>
</td>
<td>
{{ apnType.dropdown|raw }}
</td>
</tr>
</table>

0 comments on commit f1ca252

Please sign in to comment.