Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  Plugin Name: Sync Job Applicants
  5.  Plugin URI: https://www.creatit.hu/en/
  6.  Description: Synchronization of all applicants to the Tempus database
  7.  Version: 1.0
  8.  Author: Creatit Solutions
  9.  Author URI: https://www.creatit.hu/en/
  10. */
  11.  
  12. add_action('wpcf7_before_send_mail', function() {
  13.   $submission = WPCF7_Submission::get_instance();
  14.   insertApplicantIntoTempus([
  15.     'first_name' => $submission->get_posted_data('first-name'),
  16.     'last_name' => $submission->get_posted_data('last-name'),
  17.     'mobile' => $submission->get_posted_data('mobile'),
  18.     'email' => $submission->get_posted_data('email'),
  19.     'linkedin_url' => $submission->get_posted_data('linkedin-url'),
  20.     'cv_english' => transformFilePathToURL($submission->uploaded_files()['cv-english']),
  21.     'cv_hungarian' => transformFilePathToURL($submission->uploaded_files()['cv-hungarian'])
  22.   ]);
  23. });
  24.  
  25. function insertApplicantIntoTempus(array $applicant) {
  26.   $connection = connectToTempusDatabase();
  27.   $result = $connection->insert(
  28.     'jelentkezesek',
  29.     [
  30.       'Vezeteknev' => $applicant['last_name'],
  31.       'Utonev' => $applicant['first_name'],
  32.       'Telefon' => $applicant['mobile'],
  33.       'Email' => $applicant['email'],
  34.       'LinkedIn_URL' => $applicant['linkedin_url'],
  35.       'Jelentkezes_datuma' => current_time('mysql'),
  36.       'Tempus_atvetel_datuma' => current_time('mysql'),
  37.       'CV_magyar_filenev' => $applicant['cv_hungarian'],
  38.       'CV_nem_magyar_filenev' => $applicant['cv_hungarian']
  39.     ]
  40.   );
  41.  
  42.   if (!$result) {
  43.     error_log('Can\'t create applicant entry in Tempus database: ' . json_encode($result));
  44.   }
  45. }
  46.  
  47. function transformFilePathToURL(string $filePath) : string {
  48.   $fileUrl = 'http://' . getenv('DOCKERAPP_WEB_DOMAIN');
  49.   return str_replace('/srv/dockerapp/web', $fileUrl, $filePath);
  50. }
  51.  
  52. function connectToTempusDatabase() {
  53.   try {
  54.     $connection = new wpdb(
  55.       'magdianyus', // getenv('TEMPUS_DATABASE_USER')
  56.       getenv('TEMPUS_DATABASE_PASSWORD'),
  57.       getenv('TEMPUS_DATABASE_SCHEME'),
  58.       getenv('TEMPUS_DATABASE_HOST')
  59.     );
  60.   } catch (\Exception $e) {
  61.     echo 'dded';exit;
  62.   }
  63.  
  64.   if ($connection->show_errors()) {
  65.     error_log('Can\'t connect to Tempus database. Check the credentials or try again later.');
  66.   } else {
  67.     return $connection;
  68.   }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement