Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 120 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ProcessChange | |
0.00% |
0 / 120 |
|
0.00% |
0 / 6 |
240 | |
0.00% |
0 / 1 |
| readResponse | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
12 | |||
| getNewProcess | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getValidatedForm | |
0.00% |
0 / 70 |
|
0.00% |
0 / 1 |
20 | |||
| writeChangedProcess | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
20 | |||
| writeDeletedMail | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| writeConfirmedMail | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * @package Zmsadmin |
| 6 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace BO\Zmsadmin; |
| 11 | |
| 12 | use BO\Mellon\Condition; |
| 13 | use BO\Slim\Render; |
| 14 | use BO\Zmsentities\Validator\ProcessValidator; |
| 15 | use BO\Zmsentities\Process as Entity; |
| 16 | |
| 17 | /** |
| 18 | * Change process data but keep id |
| 19 | */ |
| 20 | class ProcessChange extends BaseController |
| 21 | { |
| 22 | /** |
| 23 | * @SuppressWarnings(Param) |
| 24 | * @return \Psr\Http\Message\ResponseInterface |
| 25 | */ |
| 26 | #[\Override] |
| 27 | public function readResponse( |
| 28 | \Psr\Http\Message\RequestInterface $request, |
| 29 | \Psr\Http\Message\ResponseInterface $response, |
| 30 | array $args |
| 31 | ): \Psr\Http\Message\ResponseInterface { |
| 32 | $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 2])->getEntity(); |
| 33 | $input = $request->getParams(); |
| 34 | $scope = Helper\AppointmentFormHelper::readSelectedScope($request, $workstation); |
| 35 | $oldProcess = \App::$http |
| 36 | ->readGetResult('/process/' . $input['selectedprocess'] . '/')->getEntity(); |
| 37 | $newProcess = $this->getNewProcess($input, $oldProcess, $scope); |
| 38 | $validatedForm = static::getValidatedForm($request->getAttribute('validator'), $newProcess); |
| 39 | if ($validatedForm['failed']) { |
| 40 | return \BO\Slim\Render::withJson( |
| 41 | $response, |
| 42 | $validatedForm |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | $process = static::writeChangedProcess($input, $oldProcess, $newProcess); |
| 47 | $queryParams = ('confirmed' == $process->getStatus()) ? |
| 48 | ['selectedprocess' => $process, 'success' => 'process_changed'] : |
| 49 | []; |
| 50 | |
| 51 | return \BO\Slim\Render::withHtml( |
| 52 | $response, |
| 53 | 'element/helper/messageHandler.twig', |
| 54 | $queryParams |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | protected function getNewProcess($input, $process, $scope) |
| 59 | { |
| 60 | $newProcess = clone $process; |
| 61 | $selectedTime = str_replace('-', ':', $input['selectedtime']); |
| 62 | $dateTime = \DateTime::createFromFormat('Y-m-d H:i', $input['selecteddate'] . ' ' . $selectedTime); |
| 63 | return $newProcess->withUpdatedData($input, $dateTime, $scope); |
| 64 | } |
| 65 | |
| 66 | protected static function getValidatedForm($validator, $process) |
| 67 | { |
| 68 | $processValidator = new ProcessValidator($process); |
| 69 | $delegatedProcess = $processValidator->getDelegatedProcess(); |
| 70 | $processValidator |
| 71 | ->validateName( |
| 72 | $validator->getParameter('familyName'), |
| 73 | $delegatedProcess->setter('clients', 0, 'familyName') |
| 74 | ) |
| 75 | ->validateRequests( |
| 76 | $validator->getParameter('requests'), |
| 77 | function () use ($process, $delegatedProcess) { |
| 78 | $arrayKeys = array_keys(json_decode(json_encode($process->requests), true)); |
| 79 | foreach ($arrayKeys as $key) { |
| 80 | $delegatedProcess->setter('requests', $key, 'id'); |
| 81 | $delegatedProcess->setter('requests', $key, 'source'); |
| 82 | } |
| 83 | } |
| 84 | ) |
| 85 | ->validateMail( |
| 86 | $validator->getParameter('email'), |
| 87 | $delegatedProcess->setter('clients', 0, 'email'), |
| 88 | new Condition( |
| 89 | $validator->getParameter('sendMailConfirmation')->isNumber()->isNotEqualTo(1), |
| 90 | $validator->getParameter('surveyAccepted')->isString()->isDevoidOf([1]) |
| 91 | ) |
| 92 | ) |
| 93 | ->validateTelephone( |
| 94 | $validator->getParameter('telephone'), |
| 95 | $delegatedProcess->setter('clients', 0, 'telephone'), |
| 96 | new Condition( |
| 97 | $validator->getParameter('sendConfirmation')->isNumber()->isNotEqualTo(1), |
| 98 | $validator->getParameter('sendReminder')->isNumber()->isNotEqualTo(1) |
| 99 | ) |
| 100 | ) |
| 101 | ->validateSurvey( |
| 102 | $validator->getParameter('surveyAccepted'), |
| 103 | $delegatedProcess->setter('clients', 0, 'surveyAccepted') |
| 104 | ) |
| 105 | ->validateText( |
| 106 | $validator->getParameter('amendment'), |
| 107 | $delegatedProcess->setter('amendment') |
| 108 | ) |
| 109 | ; |
| 110 | |
| 111 | $scope = $process->getCurrentScope(); |
| 112 | if ((int) $scope->getCustomTextfieldActivated()) { |
| 113 | $processValidator->validateCustomTextfield( |
| 114 | $validator->getParameter('customTextfield'), |
| 115 | $delegatedProcess->setter('customTextfield'), |
| 116 | (bool) (int) $scope->getCustomTextfieldRequired() |
| 117 | ); |
| 118 | } |
| 119 | if ((int) $scope->getCustomTextfield2Activated()) { |
| 120 | $processValidator->validateCustomTextfield( |
| 121 | $validator->getParameter('customTextfield2'), |
| 122 | $delegatedProcess->setter('customTextfield2'), |
| 123 | (bool) (int) $scope->getCustomTextfield2Required() |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | $processValidator |
| 128 | ->validateReminderTimestamp( |
| 129 | $validator->getParameter('headsUpTime'), |
| 130 | $delegatedProcess->setter('reminderTimestamp'), |
| 131 | new Condition( |
| 132 | $validator->getParameter('sendReminder')->isNumber()->isNotEqualTo(1) |
| 133 | ) |
| 134 | ) |
| 135 | ; |
| 136 | |
| 137 | |
| 138 | $processValidator->getCollection()->addValid( |
| 139 | $validator->getParameter('sendConfirmation')->isNumber(), |
| 140 | $validator->getParameter('sendReminder')->isNumber() |
| 141 | ); |
| 142 | |
| 143 | $form = $processValidator->getCollection()->getStatus(null, true); |
| 144 | $form['failed'] = $processValidator->getCollection()->hasFailed(); |
| 145 | return $form; |
| 146 | } |
| 147 | |
| 148 | protected static function writeChangedProcess($input, $oldProcess, $newProcess) |
| 149 | { |
| 150 | $oldAppointment = $oldProcess->getFirstAppointment(); |
| 151 | $newAppointment = $newProcess->getFirstAppointment(); |
| 152 | \App::$http->readPostResult( |
| 153 | '/process/' . $newProcess['id'] . '/' . $newProcess['authKey'] . '/', |
| 154 | $newProcess, |
| 155 | ['initiator' => 'admin'] |
| 156 | ); |
| 157 | if (! $oldAppointment->isMatching($newAppointment)) { |
| 158 | $newProcess = \App::$http->readPostResult( |
| 159 | '/process/' . $oldProcess->id . '/' . $oldProcess->authKey . '/appointment/', |
| 160 | $newAppointment, |
| 161 | [ |
| 162 | 'resolveReferences' => 1, |
| 163 | 'slotType' => 'intern', |
| 164 | 'clientkey' => \App::CLIENTKEY, |
| 165 | 'slotsRequired' => (isset($input['slotCount']) && 1 < $input['slotCount']) ? $input['slotCount'] : 0 |
| 166 | ] |
| 167 | )->getEntity(); |
| 168 | } |
| 169 | static::writeDeletedMail($oldProcess); |
| 170 | static::writeConfirmedMail($input, $newProcess); |
| 171 | return $newProcess; |
| 172 | } |
| 173 | |
| 174 | protected static function writeDeletedMail($oldProcess) |
| 175 | { |
| 176 | $oldProcess->status = 'deleted'; |
| 177 | ProcessDelete::writeDeleteMailNotifications($oldProcess); |
| 178 | } |
| 179 | |
| 180 | protected static function writeConfirmedMail($input, $newProcess) |
| 181 | { |
| 182 | if ('confirmed' == $newProcess->getStatus()) { |
| 183 | Helper\AppointmentFormHelper::updateMail($input, $newProcess); |
| 184 | } |
| 185 | } |
| 186 | } |