DocumentsController.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Company;
  4. use Illuminate\Http\Request;
  5. use App\Models\CompanyProDocument;
  6. use PDF;
  7. use Illuminate\Support\Carbon;
  8. class DocumentsController extends Controller
  9. {
  10. public function generateDocumentPDF($uid)
  11. {
  12. $company_pro_document = CompanyProDocument::where('uid', $uid)->first();
  13. if (!$company_pro_document) abort(404);
  14. $company = Company::where('id', $company_pro_document->company_id)->first();
  15. if (!$company) abort(404);
  16. $pro = $company_pro_document->pro;
  17. // {token} replacements
  18. // 1. replace "custom" fields (entered by candidate pro)
  19. $html = $company_pro_document->content_html;
  20. if($company_pro_document->custom_fields_data) {
  21. $customFieldsData = json_decode($company_pro_document->custom_fields_data, true);
  22. foreach ($customFieldsData as $k => $v) {
  23. $html = str_replace('{' . $k . '}', '<span>' . $v . '</span>', $html);
  24. }
  25. }
  26. // replace database driven fields
  27. $html = preg_replace_callback(
  28. '/{([^}]+)}/',
  29. function ($match) use ($company, $company_pro_document, $pro) {
  30. $token = $match[1];
  31. $replacement = '';
  32. if(strpos($token, '.') !== FALSE) {
  33. $token = explode('.', $token);
  34. // if prefixed with * - means pre-send fill
  35. $waitForCountersignToShow = false;
  36. if($token[0][0] === '*') {
  37. $token[0] = substr($token[0], 1);
  38. $waitForCountersignToShow = true;
  39. }
  40. $replacement = @${$token[0]}->{$token[1]};
  41. // special cases
  42. // if the fields are relating to PRO SIGNATURE, then don't display anything until signing
  43. if($token[0] === 'company_pro_document') {
  44. if (!$company_pro_document->has_pro_signed &&
  45. ($token[1] === 'pro_signatures' || $token[1] === 'pro_signed_at')) {
  46. $replacement = '';
  47. }
  48. }
  49. // if you want to $waitForCountersignToShow, then do that
  50. if($waitForCountersignToShow && !$company_pro_document->has_hrm_pro_counter_signed){
  51. $replacement = '';
  52. }
  53. if(strpos($token[1], "_at") === strlen($token[1]) - 3) {
  54. if(!!$replacement) {
  55. $replacement = date('m/d/Y', strtotime($replacement));
  56. }
  57. }
  58. }
  59. else {
  60. switch ($token) {
  61. case 'TODAY':
  62. $replacement = date('Y-m-d');
  63. break;
  64. default:
  65. $replacement = '<span style="color:red;">' . $token . '</span>';
  66. break;
  67. }
  68. }
  69. return $replacement;
  70. },
  71. $html
  72. );
  73. $pdf = PDF::loadView('layouts.document-pdf', compact('company_pro_document', 'html'));
  74. return $pdf->stream($uid.'.pdf');
  75. // return view('layouts.document-pdf', compact('company_pro_document', 'html'));
  76. }
  77. }