Skip to content

Commit 23322cf

Browse files
authored
Merge pull request #17 from ptisp/joao
fix - customfields order
2 parents 5648530 + 55149aa commit 23322cf

1 file changed

Lines changed: 58 additions & 52 deletions

File tree

ptisp.php

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
//v2.2.12
3+
//v2.2.13
44

55
require_once("RestRequest.inc.php");
66
use WHMCS\Database\Capsule;
@@ -286,10 +286,13 @@ function ptisp_RegisterDomain($params) {
286286
$regperiod = $params["regperiod"];
287287

288288
if (ptisp_isTaxIdEnabled()) {
289-
$vatid = $params["tax_id"];
289+
$vatid = trim($params["tax_id"]);
290290
} else {
291-
$vatcustomfield = ptisp_getTaxIdCustomfieldRef($params);
292-
$vatid = !is_null($vatcustomfield) ? $params[$vatcustomfield] : null;
291+
$vatid = ptisp_getCustomTaxId($params);
292+
if (is_null($vatid)) {
293+
$values["error"] = "Cannot get the Tax ID data. Please check the 'Tax ID Custom Field' setting in the module configuration.";
294+
return $values;
295+
}
293296
}
294297

295298
if (!empty($params["additionalfields"]["Nichandle"])) {
@@ -374,31 +377,22 @@ function utf8ToUnicode($str) {
374377
}
375378

376379
function ptisp_getCustomfieldDropdownOptions($params) {
377-
try {
378-
$fields = Capsule::table("tblcustomfields")
379-
->select()
380-
->where("type", "=", "client")
381-
->where("fieldtype", "=", "text")
382-
->get();
380+
$fields = ptisp_getClientCustomFields($params) ?? [];
381+
$selectedField = ptisp_getSelectedCustomField($params);
383382

384-
preg_match('/^customfields(\d+)$/', $params["Vatcustom"], $matches);
385-
//retrocompatible with old configuration settings
386-
if (isset($matches[1])) {
387-
$fieldName = ptisp_getCustomFieldName($matches[1]);
388-
$options = array($fieldName => $fieldName);
389-
} else {
390-
$options = array("" => "None");
391-
}
383+
if (!is_null($selectedField)) {
384+
$options = array($selectedField->id => $selectedField->fieldname);
385+
} else {
386+
$options = array("" => "None");
387+
}
392388

393-
foreach ($fields as $field) {
394-
$options[$field->fieldname] = $field->fieldname;
389+
foreach ($fields as $field) {
390+
if ($field->fieldtype == "text" && $field->id != $selectedField->id) {
391+
$options[$field->id] = $field->fieldname;
395392
}
396-
397-
return $options;
398-
} catch (\Exception $e) {
399-
error_log($e->getMessage());
400-
return "";
401393
}
394+
395+
return $options;
402396
}
403397

404398
function ptisp_isTaxIdEnabled() {
@@ -419,42 +413,54 @@ function ptisp_isTaxIdEnabled() {
419413
}
420414
}
421415

422-
function ptisp_getTaxIdCustomfieldRef($params) {
423-
$taxIdField = $params["Vatcustom"];
424-
$hasOldSetting = preg_match('/^customfields(\d+)$/', $taxIdField);
416+
function ptisp_getCustomTaxId($params) {
417+
$selectedField = ptisp_getSelectedCustomField($params);
425418

426-
//retrocompatible with old configuration settings
427-
if ($hasOldSetting) {
428-
return $taxIdField;
419+
if (!is_null($selectedField) && isset($params["customfields"])) {
420+
$key = array_search($selectedField->id, array_column($params["customfields"], "id"));
421+
return trim($params["customfields"][$key]["value"]);
422+
} else {
423+
return null;
429424
}
425+
}
430426

431-
try {
432-
$field = Capsule::table("tblcustomfields")
433-
->select()
434-
->where("fieldname", "=", $taxIdField)
435-
->first();
436-
if (is_null($field)) {
437-
return null;
438-
} else {
439-
return "customfields" . $field->sortorder;
440-
}
441-
} catch (\Exception $e) {
442-
error_log($e->getMessage());
427+
function ptisp_getSelectedCustomField($params) {
428+
$vatCustomSetting = $params["Vatcustom"];
429+
430+
if (empty(trim($vatCustomSetting))) {
443431
return null;
444432
}
433+
434+
$fields = ptisp_getClientCustomFields($params) ?? [];
435+
436+
//retrocompatible with old configuration settings
437+
preg_match("/^customfields(\d+)$/", $vatCustomSetting, $matches);
438+
439+
if (isset($matches[1])) {
440+
$index = $matches[1] - 1;
441+
if (isset($fields[$index])) {
442+
$selectedField = $fields[$index];
443+
}
444+
} else {
445+
foreach ($fields as $field) {
446+
if ($field->id == $vatCustomSetting) {
447+
$selectedField = $field;
448+
break;
449+
}
450+
}
451+
}
452+
453+
return $selectedField;
445454
}
446455

447-
function ptisp_getCustomFieldName($index) {
456+
function ptisp_getClientCustomFields($params) {
448457
try {
449-
$field = Capsule::table("tblcustomfields")
458+
$fields = Capsule::table("tblcustomfields")
450459
->select()
451-
->where("sortorder", "=", $index)
452-
->first();
453-
if (is_null($field)) {
454-
return null;
455-
} else {
456-
return $field->fieldname;
457-
}
460+
->where("type", "=", "client")
461+
->orderBy("id", "ASC")
462+
->get();
463+
return $fields;
458464
} catch (\Exception $e) {
459465
error_log($e->getMessage());
460466
return null;

0 commit comments

Comments
 (0)