diff --git a/app/internal/module/Olcs/src/Controller/Messages/AbstractCreateConversationController.php b/app/internal/module/Olcs/src/Controller/Messages/AbstractCreateConversationController.php index 93062ad12e..ae6c894c44 100644 --- a/app/internal/module/Olcs/src/Controller/Messages/AbstractCreateConversationController.php +++ b/app/internal/module/Olcs/src/Controller/Messages/AbstractCreateConversationController.php @@ -27,14 +27,17 @@ class AbstractCreateConversationController extends AbstractInternalController im protected $toggleConfig = [ 'default' => [ - FeatureToggle::MESSAGING + FeatureToggle::MESSAGING, ], ]; protected $inlineScripts = [ - 'addAction' => ['forms/message-categories'] + 'addAction' => ['forms/message-categories'], ]; + protected string $application = ''; + protected string $licence = ''; + public function getLeftView(): ViewModel { $view = new ViewModel(['navigationId' => $this->navigationId]); @@ -43,89 +46,18 @@ public function getLeftView(): ViewModel return $view; } - public function alterFormForAdd($form) - { - $appLicNoSelect = $form->get('fields')->get('appOrLicNo'); - - if ($this->params()->fromRoute('licence')){ - $licenceId = $this->params()->fromRoute('licence'); - $data = $this->handleQuery( - ByLicenceToOrganisation::create(['licence' => $licenceId]) - ); - } elseif ($this->params()->fromRoute('application')) { - $applicationId = $this->params()->fromRoute('application'); - $data = $this->handleQuery( - ByApplicationToOrganisation::create(['application' => $applicationId]) - ); - } elseif ($this->params()->fromRoute('case')) { - $caseId = $this->params()->fromRoute('case'); - $data = $this->handleQuery( - ByCaseToOrganisation::create(['case' => $caseId]) - ); - } else { - throw new RuntimeException('Error: licence or application required'); - } - - $applicationLicenceArray = json_decode($data->getHttpResponse()->getBody(), true); - - $this->prefixArrayKey($applicationLicenceArray['results']['licences'], 'L'); - $this->prefixArrayKey($applicationLicenceArray['results']['applications'], 'A'); - - $options = []; - - if($applicationLicenceArray['results']['licences']){ - $options['licence'] = [ - 'label' => 'Licences', - 'options' => $applicationLicenceArray['results']['licences'], - ]; - } - - if($applicationLicenceArray['results']['applications']){ - $options['application'] = [ - 'label' => 'Applications', - 'options' => $applicationLicenceArray['results']['applications'], - ]; - } - - $appLicNoSelect->setValueOptions($options); - - return $form; - } - public function onDispatch(MvcEvent $e) { - if ($this->getRequest()->isPost()) { - $postData = $this->getRequest()->getPost(); - $postFields = $postData->get('fields'); - $appOrLicNo = $postFields['appOrLicNo'] ?? null; - if (!empty($appOrLicNo)) { - switch ( str_split($appOrLicNo, 1)[0] ) - { - case 'A': - $postFields['application'] = substr_replace($appOrLicNo, '', 0, 1); - $postFields['licence'] = ''; - break; - case 'L': - $postFields['licence'] = substr_replace($appOrLicNo, '', 0, 1); - $postFields['application'] = ''; - break; - default: - throw new \Laminas\Validator\Exception\RuntimeException('Unexpected prefix in appOrLicNo'); - } - $postData->set('fields', $postFields); - $this->getRequest()->setPost($postData); - } + $params = $e->getRouteMatch()->getParams(); + if (isset($params['licence'])) { + $this->licence = $params['licence']; } + if (isset($params['application'])) { + $this->application = $params['application']; + } + $this->defaultData['application'] = $this->application; + $this->defaultData['licence'] = $this->licence; return parent::onDispatch($e); } - - private function prefixArrayKey(array &$array, string $prefix): void - { - foreach ($array as $k => $v) - { - $array[$prefix . $k] = $v; - unset($array[$k]); - } - } } diff --git a/app/internal/module/Olcs/src/Controller/Messages/CaseCreateConversationController.php b/app/internal/module/Olcs/src/Controller/Messages/CaseCreateConversationController.php index 3138c71c7c..ed498a98a1 100644 --- a/app/internal/module/Olcs/src/Controller/Messages/CaseCreateConversationController.php +++ b/app/internal/module/Olcs/src/Controller/Messages/CaseCreateConversationController.php @@ -4,8 +4,10 @@ namespace Olcs\Controller\Messages; +use Dvsa\Olcs\Transfer\Query\Cases\Cases; +use Exception; +use Laminas\Mvc\MvcEvent; use Olcs\Controller\Interfaces\CaseControllerInterface; -use Olcs\Controller\Interfaces\LicenceControllerInterface; class CaseCreateConversationController extends AbstractCreateConversationController implements CaseControllerInterface { @@ -22,4 +24,26 @@ class CaseCreateConversationController extends AbstractCreateConversationControl 'reUseParams' => true ] ]; + + public function onDispatch(MvcEvent $e) + { + if ($this->getRequest()->isPost()) { + $caseId = $e->getRouteMatch()->getParam('case'); + $queryResponse = $this->handleQuery(Cases::create(['id' => $caseId])); + if (!$queryResponse->isOk()) { + throw new Exception( + sprintf( + 'Unexpected error when loading case. Response: HTTP %s :: %s', + $queryResponse->getStatusCode(), + $queryResponse->getBody(), + ), + ); + } + + $queryResult = $queryResponse->getResult(); + $this->licence = (string)$queryResult['licence']['id']; + } + + return parent::onDispatch($e); + } } diff --git a/app/internal/module/Olcs/src/Form/Model/Fieldset/Conversation.php b/app/internal/module/Olcs/src/Form/Model/Fieldset/Conversation.php index 7178212382..3a36b1126e 100644 --- a/app/internal/module/Olcs/src/Form/Model/Fieldset/Conversation.php +++ b/app/internal/module/Olcs/src/Form/Model/Fieldset/Conversation.php @@ -6,8 +6,6 @@ use Common\Form\Element\DynamicSelect; use Laminas\Form\Annotation as Form; -use Laminas\Form\Element\Hidden; -use Laminas\Form\Element\Select; use Laminas\Form\Element\Textarea; use Laminas\Filter\StringTrim; use Laminas\Validator\StringLength; @@ -30,34 +28,12 @@ class Conversation */ public ?DynamicSelect $messageSubject = null; - /** - * @Form\Attributes({"id": "appOrLicNo","placeholder": ""}) - * @Form\Options({ - * "label": "Application or licence ID", - * "empty_option": "Please Select", - * }) - * @Form\Type(Select::class) - */ - public ?Select $appOrLicNo = null; - /** - * @Form\Attributes({"class": "extra-long","id": ""}) + * @Form\Attributes({"class": "extra-long", "id": ""}) * @Form\Options({"label": "Message"}) * @Form\Type(Textarea::class) * @Form\Filter(StringTrim::class) * @Form\Validator(StringLength::class, options={"min": 5, "max": 1000}) */ public ?Textarea $messageContent = null; - - /** - * @Form\Type(Hidden::class) - * @Form\Options({"value": ""}) - */ - public ?Hidden $licence = null; - - /** - * @Form\Type(Hidden::class) - * @Form\Options({"value": ""}) - */ - public ?Hidden $application = null; } diff --git a/app/internal/module/Olcs/src/Form/Model/Form/Conversation.php b/app/internal/module/Olcs/src/Form/Model/Form/Conversation.php index c16c9dd9a8..e33b88fd94 100644 --- a/app/internal/module/Olcs/src/Form/Model/Form/Conversation.php +++ b/app/internal/module/Olcs/src/Form/Model/Form/Conversation.php @@ -1,13 +1,17 @@