EmailController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\File;
  5. use App\Http\Services\EmailService;
  6. use App\Models\Account;
  7. use App\Models\AppSession;
  8. use App\Models\Invoice;
  9. use App\Models\FinancialTransaction;
  10. use App\Models\CompanyClient;
  11. use App\Models\Appointment;
  12. use Illuminate\Support\Facades\Cookie;
  13. class EmailController extends Controller
  14. {
  15. protected $performer;
  16. protected $sessionKey;
  17. public $appUrl;
  18. protected $emailService;
  19. public $appInternalName;
  20. public $stringMappingConfig;
  21. protected $emailFromName;
  22. public function __construct(EmailService $emailService)
  23. {
  24. $this->sessionKey = Cookie::get('sessionKey');
  25. $this->performer = AppSession::where('session_key', $this->sessionKey)->first();
  26. $this->emailFromName = config('app.emailFromName');
  27. $this->appUrl = config('app.url');
  28. $this->emailService = $emailService;
  29. $this->appInternalName = config('app.internalName');
  30. $this->stringMappingConfig = config('constants.' . $this->appInternalName);
  31. }
  32. public function previewEmail($email = null)
  33. {
  34. $pro = @$this->performer->pro;
  35. if (!$pro) {
  36. abort(404);
  37. }
  38. $appInternalName = $this->appInternalName;
  39. $stringMappingConfig = $this->stringMappingConfig;
  40. if ($email) {
  41. $destinationPath = resource_path('views/emails/templates/' . $email . '.blade.php');
  42. if (File::exists($destinationPath)) {
  43. $content = File::get($destinationPath);
  44. $appUrl = $this->appUrl;
  45. $emailFromName = $this->emailFromName;
  46. $toEmailAddress = $this->pro ? $this->pro->email_address : 'john@doe.com';
  47. $toName = $this->pro ? $this->pro->displayName() : 'John Doe';
  48. $message = 'Client information has been updated';
  49. $appointment = Appointment::first();
  50. return view('emails/templates/' . $email, compact(
  51. 'appUrl',
  52. 'toEmailAddress',
  53. 'toName',
  54. 'message',
  55. 'stringMappingConfig',
  56. 'appInternalName',
  57. 'emailFromName',
  58. 'appointment'
  59. ));
  60. } else {
  61. abort(404);
  62. }
  63. }
  64. $files = File::allFiles(resource_path('views/emails/templates/'));
  65. $fileNames = [];
  66. foreach ($files as $file) {
  67. $fileName = $file->getFilename();
  68. $fileNameParts = explode('.', $fileName);
  69. $fileRef = @$fileNameParts[0];
  70. if ($fileRef) array_push($fileNames, $fileRef);
  71. }
  72. return view('emails.preview-email', compact('fileNames'));
  73. }
  74. }