Skip to content

Commit

Permalink
Refactor checkbox usage
Browse files Browse the repository at this point in the history
Replaced redundant checkbox code with a new `addCheckBox` method across multiple forms for consistency and maintainability. Updated relevant dependencies and forms to ensure proper integration, and fixed minor issues in JavaScript files.
  • Loading branch information
jorikfon committed Oct 16, 2024
1 parent d309611 commit dd7b3a8
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 243 deletions.
18 changes: 3 additions & 15 deletions src/AdminCabinet/Forms/AsteriskManagerEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,10 @@ public function initialize($entity = null, $options = null): void
// Secret
$this->add(new Text('secret', ["class" => "confidential-field"]));


// Rights
foreach ($options['array_of_checkboxes'] as $checkBox) {
$cheskarr = [];
$this->add(new Check($checkBox . '_main', $cheskarr));

if (strpos($entity->$checkBox, 'read') !== false) {
$cheskarr = ['checked' => 'checked', 'value' => null];
}
$this->add(new Check($checkBox . '_read', $cheskarr));

if (strpos($entity->$checkBox, 'write') !== false) {
$cheskarr = ['checked' => 'checked', 'value' => null];
} else {
$cheskarr = ['value' => null];
}
$this->add(new Check($checkBox . '_write', $cheskarr));
$this->addCheckBox($checkBox . '_read', str_contains($entity->$checkBox, 'read'));
$this->addCheckBox($checkBox . '_write', str_contains($entity->$checkBox, 'write'));
}

// Networkfilterid
Expand Down
18 changes: 18 additions & 0 deletions src/AdminCabinet/Forms/BaseForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use MikoPBX\Common\Providers\PBXConfModulesProvider;
use MikoPBX\Modules\Config\WebUIConfigInterface;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\TextArea;
use Phalcon\Forms\Form;

Expand Down Expand Up @@ -64,4 +65,21 @@ public function addTextArea(string $areaName, string $areaValue, int $areaWidth
$options["value"] = $areaValue;
$this->add(new TextArea($areaName, $options));
}

/**
* Adds a checkbox to the form field with the given name.
*
* @param string $fieldName The name of the form field.
* @param bool $checked Indicates whether the checkbox is checked by default.
* @param string $checkedValue The value assigned to the checkbox when it is checked.
* @return void
*/
public function addCheckBox(string $fieldName, bool $checked, string $checkedValue = 'on'): void
{
$checkAr = ['value' => null];
if ($checked) {
$checkAr = ['checked' => $checkedValue,'value' => $checkedValue];
}
$this->add(new Check($fieldName, $checkAr));
}
}
22 changes: 3 additions & 19 deletions src/AdminCabinet/Forms/CallQueueEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@ public function initialize($entity = null, $options = null): void
$this->add(new Numeric('seconds_for_wrapup', ["maxlength" => 2, "style" => "width: 80px;"]));

// Recivecallswhileonacall
$cheskarr = ['value' => null];
if ($entity->recive_calls_while_on_a_call) {
$cheskarr = ['checked' => 'checked', 'value' => null];
}

$this->add(new Check('recive_calls_while_on_a_call', $cheskarr));
$this->addCheckBox('recive_calls_while_on_a_call', intval($entity->recive_calls_while_on_a_call) === 1);

// Callerhear
$arrActions = [
Expand All @@ -113,21 +108,10 @@ public function initialize($entity = null, $options = null): void
$this->add($callerhear);

// Announceposition
$cheskarr = ['value' => null];
if ($entity->announce_position) {
$cheskarr = ['checked' => 'checked', 'value' => null];
}

$this->add(new Check('announce_position', $cheskarr));
$this->addCheckBox('announce_position', intval($entity->announce_position) === 1);

// Announceholdtime
$cheskarr = ['value' => null];
if ($entity->announce_hold_time) {
$cheskarr = ['checked' => 'checked', 'value' => null];
}

$this->add(new Check('announce_hold_time', $cheskarr));

$this->addCheckBox('announce_hold_time', intval($entity->announce_hold_time) === 1);

$periodicannouncesoundid = new Select(
'periodic_announce_sound_id',
Expand Down
19 changes: 4 additions & 15 deletions src/AdminCabinet/Forms/ExtensionEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,10 @@ public function initialize($entity = null, $options = null): void
$this->add(new Hidden('is_general_user_number'));

// Show_in_phonebook
$cheskarr = ['value' => null];
if ($entity->show_in_phonebook) {
$cheskarr = ['checked' => '1'];
}
$this->add(new Check('show_in_phonebook', $cheskarr));
$this->addCheckBox('show_in_phonebook', intval($entity->show_in_phonebook) === 1);

// Public_access
$cheskarr = ['value' => null];
if ($entity->public_access) {
$cheskarr = ['checked' => '1'];
}
$this->add(new Check('public_access', $cheskarr));
$this->addCheckBox('public_access', intval($entity->public_access) === 1);

// USER ID
$this->add(new Hidden('user_id', ["value" => $entity->user_id]));
Expand Down Expand Up @@ -143,11 +135,8 @@ public function initialize($entity = null, $options = null): void
$this->add($dtmfmode);

// SIP EnableRecording
$checkArr = ['value' => null];
if ($entity->sip_enableRecording !== '0') {
$checkArr = ['checked' => '1'];
}
$this->add(new Check('sip_enableRecording', $checkArr));
$this->addCheckBox('sip_enableRecording', intval($entity->sip_enableRecording) !== 0);


// SIP Transport
$arrTransport = [
Expand Down
10 changes: 4 additions & 6 deletions src/AdminCabinet/Forms/Fail2BanEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

use MikoPBX\Common\Models\PbxSettings;
use MikoPBX\Common\Providers\TranslationProvider;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Numeric;
use Phalcon\Forms\Element\Text;
Expand Down Expand Up @@ -59,10 +58,9 @@ public function initialize($entity = null, $options = null): void
}
}

$cheskarr = ['value' => null];
if ($options[PbxSettings::PBX_FAIL2BAN_ENABLED] === "1") {
$cheskarr = ['checked' => 'checked', 'value' => null];
}
$this->add(new Check(PbxSettings::PBX_FAIL2BAN_ENABLED, $cheskarr));
$this->addCheckBox(
PbxSettings::PBX_FAIL2BAN_ENABLED,
intval($options[PbxSettings::PBX_FAIL2BAN_ENABLED]) === 1
);
}
}
60 changes: 10 additions & 50 deletions src/AdminCabinet/Forms/FirewallEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

namespace MikoPBX\AdminCabinet\Forms;

use MikoPBX\AdminCabinet\Library\Cidr;
use MikoPBX\Common\Providers\TranslationProvider;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\Element\Text;
Expand All @@ -43,42 +43,7 @@ public function initialize($entity = null, $options = null): void
$this->add(new Text('network', ['value' => $options['network']]));

// Makes subnet select
$arrMasks = [
"0" => "0 - 0.0.0.0",
"1" => "1 - 128.0.0.0",
"2" => "2 - 192.0.0.0",
"3" => "3 - 224.0.0.0",
"4" => "4 - 240.0.0.0",
"5" => "5 - 248.0.0.0",
"6" => "6 - 252.0.0.0",
"7" => "7 - 254.0.0.0",
"8" => "8 - 255.0.0.0",
"9" => "9 - 255.128.0.0",
"10" => "10 - 255.192.0.0",
"11" => "11 - 255.224.0.0",
"12" => "12 - 255.240.0.0",
"13" => "13 - 255.248.0.0",
"14" => "14 - 255.252.0.0",
"15" => "15 - 255.254.0.0",
"16" => "16 - 255.255.0.0",
"17" => "17 - 255.255.128.0",
"18" => "18 - 255.255.192.0",
"19" => "19 - 255.255.224.0",
"20" => "20 - 255.255.240.0",
"21" => "21 - 255.255.248.0",
"22" => "22 - 255.255.252.0",
"23" => "23 - 255.255.254.0",
"24" => "24 - 255.255.255.0",
"25" => "25 - 255.255.255.128",
"26" => "26 - 255.255.255.192",
"27" => "27 - 255.255.255.224",
"28" => "28 - 255.255.255.240",
"29" => "29 - 255.255.255.248",
"30" => "30 - 255.255.255.252",
"31" => "31 - 255.255.255.254",
"32" => "32 - 255.255.255.255",
];
krsort($arrMasks, SORT_NUMERIC);
$arrMasks = Cidr::getNetMasks();

$mask = new Select(
'subnet',
Expand All @@ -96,20 +61,15 @@ public function initialize($entity = null, $options = null): void
$this->add($mask);

// Newer_block_ip
$cheskarr = ['value' => null];
if ($entity->newer_block_ip) {
$cheskarr = ['checked' => 'checked', 'value' => null];
}

$this->add(new Check('newer_block_ip', $cheskarr));

$this->addCheckBox(
'newer_block_ip',
intval($entity->newer_block_ip) === 1
);

// Local_network
$cheskarr = ['value' => null];
if ($entity->local_network) {
$cheskarr = ['checked' => 'checked', 'value' => null];
}

$this->add(new Check('local_network', $cheskarr));
$this->addCheckBox(
'local_network',
intval($entity->local_network) === 1
);
}
}
10 changes: 4 additions & 6 deletions src/AdminCabinet/Forms/GeneralSettingsEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use MikoPBX\Common\Models\PbxSettings;
use MikoPBX\Common\Models\SoundFiles;
use MikoPBX\Common\Providers\TranslationProvider;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Numeric;
use Phalcon\Forms\Element\Password;
Expand Down Expand Up @@ -194,11 +193,10 @@ public function initialize($entity = null, $options = null): void
case PbxSettings::DISABLE_ALL_MODULES:
case PbxSettings::SSH_DISABLE_SSH_PASSWORD:
case '***ALL CHECK BOXES ABOVE***':
$cheskarr = ['value' => null];
if ($value) {
$cheskarr = ['checked' => 'checked', 'value' => null];
}
$this->add(new Check($key, $cheskarr));
$this->addCheckBox(
$key,
intval($value) === 1
);
break;
default:
$this->add(new Text($key, ['value' => $value]));
Expand Down
20 changes: 8 additions & 12 deletions src/AdminCabinet/Forms/IaxProviderEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
namespace MikoPBX\AdminCabinet\Forms;

use MikoPBX\Common\Providers\TranslationProvider;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Password;
use Phalcon\Forms\Element\Text;
Expand Down Expand Up @@ -65,19 +64,16 @@ public function initialize($entity = null, $options = null): void
$this->add(new Text('host'));

// Qualify
$cheskarr = ['value' => null];
if ($entity->qualify) {
$cheskarr = ['checked' => 'checked', 'value' => null];
}

$this->add(new Check('qualify', $cheskarr));
$this->addCheckBox(
'qualify',
intval($entity->qualify) === 1
);

// Noregister
$cheskarr = ['value' => null];
if ($entity->noregister) {
$cheskarr = ['checked' => 'checked', 'value' => null];
}
$this->add(new Check('noregister', $cheskarr));
$this->addCheckBox(
'noregister',
intval($entity->noregister) === 1
);

// Manualattributes
$this->addTextArea('manualattributes', $entity->getManualAttributes() ?? '', 80);
Expand Down
11 changes: 4 additions & 7 deletions src/AdminCabinet/Forms/IvrMenuEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
namespace MikoPBX\AdminCabinet\Forms;

use MikoPBX\Common\Providers\TranslationProvider;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Numeric;
use Phalcon\Forms\Element\Select;
Expand Down Expand Up @@ -105,12 +104,10 @@ public function initialize($entity = null, $options = null): void
$this->add($audioMessage);

//Allow_enter_any_internal_extension
$cheskarr = ['value' => null];
if ($entity->allow_enter_any_internal_extension) {
$cheskarr = ['checked' => 'checked', 'value' => null];
}

$this->add(new Check('allow_enter_any_internal_extension', $cheskarr));
$this->addCheckBox(
'allow_enter_any_internal_extension',
intval($entity->allow_enter_any_internal_extension) === 1
);

// Description
$this->addTextArea('description', $entity->description ?? '', 65);
Expand Down
3 changes: 1 addition & 2 deletions src/AdminCabinet/Forms/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
namespace MikoPBX\AdminCabinet\Forms;

use MikoPBX\Common\Providers\TranslationProvider;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\Password;
use Phalcon\Forms\Element\Text;

Expand All @@ -45,6 +44,6 @@ public function initialize($entity = null, $options = null): void
$this->add($password);

// RememberMe
$this->add(new Check('rememberMeCheckBox', ['value' => null]));
$this->addCheckBox('rememberMeCheckBox', false);
}
}
7 changes: 1 addition & 6 deletions src/AdminCabinet/Forms/MailSettingsEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

use MikoPBX\Common\Models\PbxSettings;
use MikoPBX\Common\Providers\TranslationProvider;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\Password;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\TextArea;
Expand All @@ -44,11 +43,7 @@ public function initialize($entity = null, $options = null): void
case PbxSettings::MAIL_ENABLE_NOTIFICATIONS:
case PbxSettings::MAIL_SMTP_USE_TLS:
case PbxSettings::MAIL_SMTP_CERT_CHECK:
$cheskarr = ['value' => null];
if ($value) {
$cheskarr = ['checked' => 'checked', 'value' => null];
}
$this->add(new Check($key, $cheskarr));
$this->addCheckBox($key, intval($value) === 1);
break;

case PbxSettings::MAIL_TPL_MISSED_CALL_BODY:
Expand Down
Loading

0 comments on commit dd7b3a8

Please sign in to comment.