FormWithEmail.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. class FormWithEmail extends CallbackForm
  3. {
  4. public $email;
  5. public $sendFile;
  6. public function __construct(string $name, string $phone, string $email, array $sendFile)
  7. {
  8. parent::__construct($name, $phone);
  9. $this->email = $email;
  10. $this->sendFile = $sendFile;
  11. }
  12. public function validate(): bool
  13. {
  14. if (!empty($this->email) && !filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
  15. return false;
  16. }
  17. if ($this->sendFile['size'] > 5242880) {
  18. echo "Недопустимо большой размер файла";
  19. return false;
  20. }
  21. if (!empty($this->sendFile['name']) && $this->sendFile['type'] !== 'application/pdf') {
  22. echo "Используйте файл с расширением *.pdf";
  23. return false;
  24. }
  25. return parent::validate();
  26. }
  27. public function uploadFiles(): void
  28. {
  29. $uploadDirectory = dirname(__DIR__, 1) . '/file/';
  30. if (!file_exists($uploadDirectory)) {
  31. mkdir($uploadDirectory, 0777);
  32. $way = $uploadDirectory . time() . $this->sendFile['name'];
  33. move_uploaded_file($this->sendFile['tmp_name'], $way);
  34. } else {
  35. $way = $uploadDirectory . time() . $this->sendFile['name'];
  36. move_uploaded_file($this->sendFile['tmp_name'], $way);
  37. }
  38. }
  39. public function send()
  40. {
  41. parent::send();
  42. if (!empty($this->email)) {
  43. echo '<br>';
  44. echo 'Email: ' . $this->email;
  45. }
  46. }
  47. }