EmailController.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\AccountInvite;
  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. $accountInvite = AccountInvite::first();
  51. return view('emails/templates/' . $email, compact(
  52. 'appUrl',
  53. 'toEmailAddress',
  54. 'toName',
  55. 'message',
  56. 'stringMappingConfig',
  57. 'appInternalName',
  58. 'emailFromName',
  59. 'appointment',
  60. 'accountInvite'
  61. ));
  62. } else {
  63. abort(404);
  64. }
  65. }
  66. $files = File::allFiles(resource_path('views/emails/templates/'));
  67. $fileNames = [];
  68. foreach ($files as $file) {
  69. $fileName = $file->getFilename();
  70. $fileNameParts = explode('.', $fileName);
  71. $fileRef = @$fileNameParts[0];
  72. if ($fileRef) array_push($fileNames, $fileRef);
  73. }
  74. return view('emails.preview-email', compact('fileNames'));
  75. }
  76. }