EmailController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 Illuminate\Support\Facades\Cookie;
  12. class EmailController extends Controller
  13. {
  14. protected $performer;
  15. protected $sessionKey;
  16. public $appUrl;
  17. protected $emailService;
  18. public $appInternalName;
  19. public $stringMappingConfig;
  20. protected $emailFromName;
  21. public function __construct(EmailService $emailService)
  22. {
  23. $this->sessionKey = Cookie::get('sessionKey');
  24. $this->performer = AppSession::where('session_key', $this->sessionKey)->first();
  25. $this->emailFromName = config('app.emailFromName');
  26. $this->appUrl = config('app.url');
  27. $this->emailService = $emailService;
  28. $this->appInternalName = config('app.internalName');
  29. $this->stringMappingConfig = config('constants.' . $this->appInternalName);
  30. }
  31. public function previewEmail($email = null)
  32. {
  33. $pro = @$this->performer->pro;
  34. if (!$pro) {
  35. abort(404);
  36. }
  37. $appInternalName = $this->appInternalName;
  38. $stringMappingConfig = $this->stringMappingConfig;
  39. if ($email) {
  40. $destinationPath = resource_path('views/emails/templates/' . $email . '.blade.php');
  41. if (File::exists($destinationPath)) {
  42. $content = File::get($destinationPath);
  43. $appUrl = $this->appUrl;
  44. $emailFromName = $this->emailFromName;
  45. $toEmailAddress = $this->pro ? $this->pro->email_address : 'john@doe.com';
  46. $toName = $this->pro ? $this->pro->displayName() : 'John Doe';
  47. $message = 'Client information has been updated';
  48. return view('emails/templates/' . $email, compact('appUrl', 'toEmailAddress', 'toName', 'message', 'stringMappingConfig', 'appInternalName', 'emailFromName'));
  49. } else {
  50. abort(404);
  51. }
  52. }
  53. $files = File::allFiles(resource_path('views/emails/templates/'));
  54. $fileNames = [];
  55. foreach ($files as $file) {
  56. $fileName = $file->getFilename();
  57. $fileNameParts = explode('.', $fileName);
  58. $fileRef = @$fileNameParts[0];
  59. if ($fileRef) array_push($fileNames, $fileRef);
  60. }
  61. return view('emails.preview-email', compact('fileNames'));
  62. }
  63. }