contextsService = ServiceContainer::getService(DynamicContexts::class); } private function getUserPreorder(): UserPreorder { if ($this->userPreorder === null) { [$userId, $preorderDateId] = explode('-', $this->getID()); $this->userPreorder = new UserPreorder((int) $userId, (int) $preorderDateId); } return $this->userPreorder; } protected function getObject(): array { $up = $this->getUserPreorder()->getTemplateVars(); usort($up['items'], fn ($i, $j) => $i['title'] <=> $j['title']); return $up; } public function get_vars(): array { $updateKeys = array_intersect(array_keys($_POST), ['SaveItemPieces', 'AddNewMessage']); if (!empty($updateKeys)) { $this->forceUpdate(); } $vars = parent::get_vars(); $body = &$vars['body']; $data = &$body['data']; $userPreorder = $this->getUserPreorder(); $this->contextsService->activateUserPreorder($userPreorder); $body['isClosed'] = \DateTimeImmutable::createFromFormat('Y-m-d', $data['date_end']) <= (new \DateTimeImmutable())->sub(\DateInterval::createFromDateString('1 day')); $body['messages'] = $userPreorder->getMessages(); $body['custom_data'] = $userPreorder->getCustomData(); $body['errors'] = $this->getItemErrors($data['items']); return $vars; } private function getItemErrors(array $items): array { $errors = []; foreach ($items as $item) { if (($item['has_variations'] ?? false) && empty($item['id_variation'])) { $errors[] = sprintf( translate('variationNotSelected', 'preordersUsers'), (int) $item['id'], $item['product_title'], ); } } return $errors; } public function hasRights($name = null): bool { switch ($name) { case \Window::RIGHT_DUPLICATE: return false; default: return true; } } public function handleUpdate(): void { $userPreorder = $this->getUserPreorder(); $data = $this->getData(); if (!empty($data['user_address'])) { $customData = $userPreorder->getCustomData(); $customData->set('user_address', $data['user_address']); $customData->save(); } if (getVal('SaveItemPieces', null, false) && $items = getVal('items')) { foreach ($items as $id => $item) { $userPreorder->setItem( $id, null, null, $item['pieces'], $item['pieces_sent'], ); } $this->returnOK('Kusy produktů uloženy.'); } if (getVal('AddNewMessage')) { $this->handleSendMessage(); } $this->returnOK(); } private function handleSendMessage(): void { $userMessages = $this->getUserPreorder()->getMessages(); $text = getVal('new_message'); if (!$text) { $this->returnError('Zpráva nebyla odeslána - žádný text.'); } $admin = getAdminUser(); $newMessage = new UserPreorderMessage( text: $text, admin: $admin['login'] ?? 'NEZNÁMÝ ADMIN', ); $userMessages->add($newMessage); if ($exception = $userMessages->save()) { $this->returnError('Nepodařilo se uložit zprávu. '.$exception->getMessage()); } if (!getVal('message_do_not_notify')) { if ($statusMessage = getVal('status_message')) { $emailService = ServiceContainer::getService(UserPreorderMessageEmail::class); $emailService->setUserMessage($statusMessage); } else { $emailService = ServiceContainer::getService(UserPreorderEmail::class); } $email = $emailService->setUserPreorder($this->getUserPreorder())->setMessage($newMessage); $emailRes = $emailService->sendEmail($email->getEmail()); if (!$emailRes) { $this->returnError('Nepodařilo se poslat e-mail uživateli.'); } $newMessage->notified = NotifyState::EmailNotified; $userMessages->save(); } $this->returnOK('Zpráva úspěšně odeslána.'); } public function handleDelete(): void { $userPreorder = $this->getUserPreorder(); $items = $userPreorder->getItems(true); $ids = array_column($items, 'id'); sqlQueryBuilder()->delete('preorders_items') ->andWhere(Op::inIntArray($ids, 'id')) ->execute(); sqlQueryBuilder()->delete('preorders_users_data') ->andWhere(Op::equals([ 'id_preorder_date' => $userPreorder->preorderDateId, 'id_user' => $userPreorder->userId, ])) ->execute(); redirect('launch.php?s=preordersUsers.php&acn=erased'); } public function getUserEmails(): UserMessagesInterface { return ServiceContainer::getService(UserPreorderMessageEmail::class)->setUserPreorder($this->userPreorder); } } return preordersUsers::class;