From 0945f4c5164ce2882c3f007893c8e96c2b9c9549 Mon Sep 17 00:00:00 2001 From: Prashob K Date: Tue, 28 May 2024 18:01:28 +0530 Subject: [PATCH] FREEPBX17-784 added validations to ucp voicemail widget --- Voicemail.class.php | 5 +++ ucp/Voicemail.class.php | 77 +++++++++++++++++++++++++++++------------ 2 files changed, 59 insertions(+), 23 deletions(-) diff --git a/Voicemail.class.php b/Voicemail.class.php index 8fdcde0e..57d4ca02 100644 --- a/Voicemail.class.php +++ b/Voicemail.class.php @@ -2964,4 +2964,9 @@ private function regenerateVMMsgId($data, $messages) { } return $data; } + + public function checkVoicemailEnabled($extension) { + $voicemailStatus = $this->astman->database_get("AMPUSER", $extension."/voicemail"); + return $voicemailStatus != 'novm' ? true : false; + } } diff --git a/ucp/Voicemail.class.php b/ucp/Voicemail.class.php index 0e23ccd5..208bd2c7 100644 --- a/ucp/Voicemail.class.php +++ b/ucp/Voicemail.class.php @@ -60,15 +60,21 @@ function __construct($Modules) { } public function getWidgetList() { - if (!$this->enabled) { - return array(); + $responseData = array( + "rawname" => "voicemail", + "display" => _("Voicemail"), + "icon" => "fa fa-inbox", + "list" => [] + ); + $errors = $this->validate(); + if ($errors['hasError']) { + return array_merge($responseData, $errors); } $widgets = array(); $extensions = $this->extensions; if (!empty($extensions)) { - $boxes = $this->getMailboxCount($extensions); foreach ($extensions as $extension) { $data = $this->UCP->FreePBX->Core->getDevice($extension); if (empty($data) || empty($data['description'])) { @@ -78,36 +84,61 @@ public function getWidgetList() { else { $name = $data['description']; } - $o = $this->UCP->FreePBX->Voicemail->getVoicemailBoxByExtension($extension); - if (!empty($o) && isset($boxes['extensions'][$extension])) { - $widgets[$extension] = array( - "display" => $name, - "hasSettings" => true, - "minsize" => array( "height" => 5, "width" => 5 ), - "defaultsize" => array( "height" => 7, "width" => 6 ) - ); - } + $widgets[$extension] = array( + "display" => $name, + "hasSettings" => true, + "minsize" => array( "height" => 5, "width" => 5 ), + "defaultsize" => array( "height" => 7, "width" => 6 ) + ); } } - if (empty($widgets)) { - return array(); - } + $responseData['list'] = $widgets; + return $responseData; + } - return array( - "rawname" => "voicemail", - "display" => _("Voicemail"), - "icon" => "fa fa-inbox", - "list" => $widgets + /** + * validate against rules + */ + private function validate($extension = false) { + $data = array( + 'hasError' => false, + 'errorMessages' => [] ); + + if (!$this->enabled) { + $data['hasError'] = true; + $data['errorMessages'][] = _('UCP Voicemail is not enabled for this user.'); + } + if (empty($this->extensions)) { + $data['hasError'] = true; + $data['errorMessages'][] = _('There are no assigned extensions.'); + } + if ($extension !== false) { + if (empty($extension)) { + $data['hasError'] = true; + $data['errorMessages'][] = _('The given extension is empty.'); + } + if (!$this->_checkExtension($extension)) { + $data['hasError'] = true; + $data['errorMessages'][] = _('This extension is not assigned to this user.'); + } + if (!$this->UCP->FreePBX->Voicemail->checkVoicemailEnabled($extension)) { + $data['hasError'] = true; + $data['errorMessages'][] = _('Voicemail is not enabled for this extension.'); + } + } + + return $data; } public function getWidgetDisplay($id) { + $errors = $this->validate($id); + if ($errors['hasError']) { + return $errors; + } $html = ''; $ext = !empty($id) ? $id : ''; - if (!empty($ext) && !$this->_checkExtension($ext)) { - return array(); - } $view = !empty($_REQUEST['view']) ? $_REQUEST['view'] : 'folder'; $page = !empty($_REQUEST['page']) ? $_REQUEST['page'] : 1; $folders = $this->UCP->FreePBX->Voicemail->getFolders();