Skip to content

Commit

Permalink
Merge pull request #615 from mikopbx/revert-614-develop
Browse files Browse the repository at this point in the history
Revert "fix some errors"
  • Loading branch information
jorikfon authored Sep 6, 2023
2 parents 4174064 + 5811e39 commit be12a05
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 329 deletions.
6 changes: 5 additions & 1 deletion src/AdminCabinet/Controllers/ConferenceRoomsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

class ConferenceRoomsController extends BaseController
{


/**
* Build the list of conference rooms.
*/
Expand All @@ -34,6 +36,7 @@ public function indexAction(): void
$this->view->records = $records;
}


/**
* Edit conference room details.
*
Expand All @@ -45,14 +48,15 @@ public function modifyAction(string $uniqid = null): void
if ($record === null) {
// Create a new conference room if not found
$record = new ConferenceRooms();
$record->uniqid = ConferenceRooms::generateUniqueID(Extensions::TYPE_CONFERENCE.'-');
$record->uniqid = Extensions::TYPE_CONFERENCE.strtoupper('-' . md5(time()));
$record->extension = Extensions::getNextFreeApplicationNumber();
}
$this->view->form = new ConferenceRoomEditForm($record);
$this->view->represent = $record->getRepresent();
$this->view->extension = $record->extension;
}


/**
* Save the conference room.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function modifyAction(string $uniqid = ''): void
$app = DialplanApplications::findFirstByUniqid($uniqid);
if ($app === null) {
$app = new DialplanApplications();
$app->uniqid = DialplanApplications::generateUniqueID('DIALPLAN-APP-');
$app->uniqid = strtoupper('DIALPLAN-APP-' . md5($app->id . time()));
$app->type = 'php';
$app->extension = Extensions::getNextFreeApplicationNumber();
}
Expand Down
162 changes: 29 additions & 133 deletions src/AdminCabinet/Controllers/GeneralSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@

use MikoPBX\AdminCabinet\Forms\GeneralSettingsEditForm;
use MikoPBX\Common\Models\Codecs;
use MikoPBX\Common\Models\Extensions;
use MikoPBX\Common\Models\PbxSettings;
use MikoPBX\Common\Models\PbxSettingsConstants;
use MikoPBX\Core\System\Util;

/**
* Class GeneralSettingsController
*
* This class handles general settings for the application.
*/
class GeneralSettingsController extends BaseController
{
/**
Expand All @@ -44,23 +38,23 @@ class GeneralSettingsController extends BaseController
*/
public function modifyAction(): void
{
// Retrieve and sort audio codecs from database
// Retrieve and sort the audio codecs
$audioCodecs = Codecs::find(['conditions' => 'type="audio"'])->toArray();
usort($audioCodecs, [__CLASS__, 'sortArrayByPriority']);
$this->view->audioCodecs = $audioCodecs;

// Retrieve and sort video codecs from database
// Retrieve and sort the video codecs
$videoCodecs = Codecs::find(['conditions' => 'type="video"'])->toArray();
usort($videoCodecs, [__CLASS__, 'sortArrayByPriority']);
$this->view->videoCodecs = $videoCodecs;

// Fetch all PBX settings
// Retrieve all PBX settings
$pbxSettings = PbxSettings::getAllPbxSettings();

// Fetch and assign simple passwords for the view
// Retrieve and assign the simple passwords data to the view
$this->view->simplePasswords = $this->getSimplePasswords($pbxSettings);

// Create an instance of the GeneralSettingsEditForm and assign it to the view
// Create an instance of the GeneralSettingsEditForm
$this->view->form = new GeneralSettingsEditForm(null, $pbxSettings);
$this->view->submitMode = null;

Expand All @@ -78,18 +72,12 @@ public function modifyAction(): void
*/
private function getSimplePasswords(array $data): array
{
// Initialize an array to keep track of passwords that fail the check
$passwordCheckFail = [];

$cloudInstanceId = $data['CloudInstanceId'] ?? '';
$checkPasswordFields = [PbxSettingsConstants::SSH_PASSWORD, 'WebAdminPassword'];

// If SSH is disabled, remove the SSH_PASSWORD key
if ($data[PbxSettingsConstants::SSH_DISABLE_SSH_PASSWORD] === 'on') {
$checkPasswordFields =[PbxSettingsConstants::SSH_PASSWORD, 'WebAdminPassword'];
if ($data[PbxSettingsConstants::SSH_DISABLE_SSH_PASSWORD] === 'on'){
unset($checkPasswordFields[PbxSettingsConstants::SSH_PASSWORD]);
}

// Loop through and check passwords
foreach ($checkPasswordFields as $value) {
if (!isset($data[$value]) || $data[$value] === GeneralSettingsEditForm::HIDDEN_PASSWORD) {
continue;
Expand All @@ -114,121 +102,14 @@ public function saveAction(): void

$passwordCheckFail = $this->getSimplePasswords($data);
if (!empty($passwordCheckFail)) {
foreach ($passwordCheckFail as $settingsKey) {
$this->flash->error($this->translation->_('gs_SetPasswordError', ['password' => $data[$settingsKey]]));
}
foreach ($passwordCheckFail as $settingsKey){
$this->flash->error($this->translation->_('gs_SetPasswordError', ['password'=>$data[$settingsKey]]));
}
$this->view->success = false;
$this->view->passwordCheckFail = $passwordCheckFail;
return;
}

$this->db->begin();

list($result, $messages) = $this->updatePBXSettings($data);
if (!$result) {
$this->view->success = false;
$this->view->messages = $messages;
$this->db->rollback();
return;
}

list($result, $messages) = $this->updateCodecs($data['codecs']);
if (!$result) {
$this->view->success = false;
$this->view->messages = $messages;
$this->db->rollback();
return;
}

list($result, $messages) = $this->createParkingExtensions(
$data[PbxSettingsConstants::PBX_CALL_PARKING_START_SLOT],
$data[PbxSettingsConstants::PBX_CALL_PARKING_END_SLOT],
$data[PbxSettingsConstants::PBX_CALL_PARKING_EXT],
);

if (!$result) {
$this->view->success = false;
$this->view->messages = $messages;
$this->db->rollback();
return;
}

$this->flash->success($this->translation->_('ms_SuccessfulSaved'));
$this->view->success = true;
$this->db->commit();

}


/**
* Create parking extensions.
*
* @param int $startSlot
* @param int $endSlot
* @param int $reservedSlot
*
* @return array
*/
private function createParkingExtensions(int $startSlot, int $endSlot, int $reservedSlot): array
{
$messages = [];
// Delete all parking slots
$currentSlots = Extensions::findByType(Extensions::TYPE_PARKING);
foreach ($currentSlots as $currentSlot) {
if (!$currentSlot->delete()) {
$messages['error'][] = $currentSlot->getMessages();
}
}

// Create an array of new numbers
$numbers = range($startSlot, $endSlot);
$numbers[] = $reservedSlot;
foreach ($numbers as $number) {
$record = new Extensions();
$record->type = Extensions::TYPE_PARKING;
$record->number = $number;
if (!$record->create()) {
$messages['error'][] = $record->getMessages();
}
}

$result = count($messages) === 0;
return [$result, $messages];
}

/**
* Update codecs based on the provided data.
*
* @param string $codecsData The JSON-encoded data for codecs.
*
* @return array
*/
private function updateCodecs(string $codecsData): array
{
$messages = [];
$codecs = json_decode($codecsData, true);
foreach ($codecs as $codec) {
$record = Codecs::findFirstById($codec['codecId']);
$record->priority = $codec['priority'];
$record->disabled = $codec['disabled'] === true ? '1' : '0';
if (!$record->update()) {
$messages['error'][] = $record->getMessages();
}
}
$result = count($messages) === 0;
return [$result, $messages];
}

/**
* Update PBX settings based on the provided data.
*
* @param array $data The data containing PBX settings.
*
* @return array
*/
private function updatePBXSettings(array $data):array
{
$messages = [];
$pbxSettings = PbxSettings::getDefaultArrayValues();

// Process SSHPassword and set SSHPasswordHash accordingly
Expand All @@ -240,7 +121,7 @@ private function updatePBXSettings(array $data):array
$data[PbxSettingsConstants::SSH_PASSWORD_HASH_STRING] = md5($data[PbxSettingsConstants::SSH_PASSWORD]);
}
}

$this->db->begin();
// Update PBX settings
foreach ($pbxSettings as $key => $value) {
switch ($key) {
Expand Down Expand Up @@ -297,12 +178,27 @@ private function updatePBXSettings(array $data):array
$record->value = $newValue;

if ($record->save() === false) {
$messages['error'][] = $record->getMessages();
$errors = $record->getMessages();
$this->flash->warning(implode('<br>', $errors));
$this->view->success = false;
$this->db->rollback();

return;
}
}
}
$result = count($messages) === 0;
return [$result, $messages];

$codecs = json_decode($data['codecs'], true);
foreach ($codecs as $codec) {
$record = Codecs::findFirstById($codec['codecId']);
$record->priority = $codec['priority'];
$record->disabled = $codec['disabled'] === true ? '1' : '0';
$record->update();
}

$this->flash->success($this->translation->_('ms_SuccessfulSaved'));
$this->view->success = true;
$this->db->commit();
}

}
7 changes: 4 additions & 3 deletions src/AdminCabinet/Forms/GeneralSettingsEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function initialize($entity = null, $options = null): void
case PbxSettingsConstants::SSH_PORT:
case 'WEBPort':
case 'WEBHTTPSPort':
case PbxSettingsConstants::PBX_CALL_PARKING_EXT:
case PbxSettingsConstants::PBX_CALL_PARKING_START_SLOT:
case PbxSettingsConstants::PBX_CALL_PARKING_END_SLOT:
case 'PBXCallParkingExt':
case 'PBXCallParkingStartSlot':
case 'PBXCallParkingEndSlot':
case 'PBXFeatureDigitTimeout':
case 'PBXFeatureAtxferNoAnswerTimeout':
case '***ALL NUMBERIC ABOVE***':
Expand Down Expand Up @@ -119,6 +119,7 @@ public function initialize($entity = null, $options = null): void
'sv-sv' => $this->translation->_('ex_Swedish'),
'cs-cs' => $this->translation->_('ex_Czech'),
'tr-tr' => $this->translation->_('ex_Turkish'),

]
, [
'using' => [
Expand Down
33 changes: 10 additions & 23 deletions src/Common/Models/Extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
*
*
* @method static mixed findFirstByNumber(string|null $number)
* @method static mixed findByType(string|null $type)
* @method static mixed findByUserid(int $userid)
*
* @package MikoPBX\Common\Models
Expand All @@ -54,7 +53,6 @@ class Extensions extends ModelsBase
public const TYPE_CONFERENCE = 'CONFERENCE';
public const TYPE_MODULES = 'MODULES';
public const TYPE_SYSTEM = 'SYSTEM';
public const TYPE_PARKING = 'PARKING';

/**
* @Primary
Expand Down Expand Up @@ -138,39 +136,28 @@ public static function getNextFreeApplicationNumber(): string
}

/**
* Get the next available internal extension number.
* Get the next internal number from the database following the last entered internal number.
*
* This function retrieves the minimum existing internal extension number from the database.
* If there are no existing internal numbers, it starts from 200.
* It then checks for available extension numbers within the range and returns the next available one.
*
* @return string The next available internal extension number, or an empty string if none are available.
* @return string The next internal number.
*/
public static function getNextInternalNumber(): string
{
$parameters = [
'conditions' => 'type = "' . Extensions::TYPE_SIP . '"',
'column' => 'number',
'conditions'=>'type="'.Extensions::TYPE_SIP.'" and userid is not null'
];
$started = Extensions::minimum($parameters);
if ($started === null) {
// Get the maximum internal number from the database
$query = Extensions::maximum($parameters);
if ($query === null) {
// If there are no existing internal numbers, start from 200
$started = 200;
$query = 200;
}

$result = (int)$query + 1;
$extensionsLength = PbxSettings::getValueByKey('PBXInternalExtensionLength');
$maxExtension = (10 ** $extensionsLength) - 1;

$occupied = Extensions::find(['columns' => 'number'])->toArray();
$occupied = array_column($occupied, 'number');

for ($i = $started; $i <= $maxExtension ; $i++) {
if (!in_array((string)$i, $occupied)){
return (string)$i;
}
}
// There is no available extensions
return '';
// Check if the next internal number exceeds the maximum allowed length
return ($result <= $maxExtension) ? $result : '';
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Common/Models/PbxSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ public static function getDefaultArrayValues(): array
'PBXRecordAnnouncementIn' => '',
'PBXRecordAnnouncementOut' => '',
'PBXRecordSavePeriod' => '',
PbxSettingsConstants::PBX_CALL_PARKING_EXT => '800',
PbxSettingsConstants::PBX_CALL_PARKING_FEATURE => '*2',
PbxSettingsConstants::PBX_CALL_PARKING_DURATION => '50',
PbxSettingsConstants::PBX_CALL_PARKING_START_SLOT => '801',
PbxSettingsConstants::PBX_CALL_PARKING_END_SLOT => '820',
'PBXCallParkingExt' => '800',
'PBXCallParkingFeature' => '*2',
'PBXCallParkingDuration' => '50',
'PBXCallParkingStartSlot' => '801',
'PBXCallParkingEndSlot' => '820',
'PBXFeatureAttendedTransfer' => '##',
'PBXFeatureBlindTransfer' => '**',
'PBXFeaturePickupExten' => '*8',
Expand Down
Loading

0 comments on commit be12a05

Please sign in to comment.