207 lines
9.7 KiB
PHP
207 lines
9.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Sends in the data from the comment form to MiniCRM
|
|
*
|
|
* @link https://github.com/Duskell
|
|
* @since 1.1.11
|
|
*
|
|
* @package Partnerexpo_Core
|
|
* @compackage Partnerexpo_Core/includes/mc-integrations
|
|
*/
|
|
|
|
if ( ! defined( 'WPINC' ) ) {
|
|
die;
|
|
}
|
|
|
|
class Partnerexpo_Core_Integration_Comment_Form {
|
|
private $client;
|
|
|
|
public function __construct($crm_client) {
|
|
$this->client = $crm_client;
|
|
}
|
|
|
|
public function queue_up($comment_id, $comment_approved, $commentdata) {
|
|
as_schedule_single_action( time() + 60, 'process_comment_data', [ $comment_id ] );
|
|
}
|
|
|
|
public function run_process($comment_id) {
|
|
# DATA #
|
|
$crm_user_id = "Kotlárik Diána, Ügyfélszolgálat";
|
|
$category_id = 98;
|
|
$deadline = date("Y-m-d");
|
|
$com_id_text = __("Komment azonosító", "partnerexpo-core");
|
|
$task_message = __("Új visszajelzés érkezett a(z) %s oldalra!\n\nVisszajelzés tartalma:\n%s\n\nEzen a linken tudod megtekinteni a visszajelzést a WordPress adminban: %s", "partnerexpo-core");
|
|
$error_text = __("hiba", "partnerexpo-core");
|
|
$gdpr_value = 1;
|
|
$newsletter_value = 1;
|
|
# # # # #
|
|
try {
|
|
if ( ! $this->client ) {
|
|
Logger::log(__("MiniCRM Kliens nem található, Szinkronizálás megszakítása!", 'partnerexpo-core'), [$com_id_text => $comment_id]);
|
|
return;
|
|
}
|
|
|
|
$com_data = get_comment($comment_id, ARRAY_A);
|
|
|
|
$data = [
|
|
'firstName' => get_comment_meta($comment_id, 'first_name', true) ?? '',
|
|
'lastName' => get_comment_meta($comment_id, 'last_name', true) ?? '',
|
|
'company' => get_comment_meta($comment_id, 'company', true) ?? '',
|
|
'email' => get_comment_meta($comment_id, 'mail', true) ?? '',
|
|
'phone' => get_comment_meta($comment_id, 'phone', true) ?? '',
|
|
'content' => get_comment_text($comment_id) ?? '',
|
|
'postTitle' => get_the_title($com_data['comment_post_ID']) ?? '',
|
|
'editLink' => get_edit_comment_link($comment_id) ?? '',
|
|
'gdpr' => get_comment_meta($comment_id, 'gdpr', true) ?? '',
|
|
'newsletter' => get_comment_meta($comment_id, 'newsletter', true) ?? '',
|
|
];
|
|
|
|
# Task messages #
|
|
|
|
$message = sprintf($task_message, $data['postTitle'] ?? __("Nincs cím megadva!", "partnerexpo-core"), $data['content'] ?: __("Nincs leírás megadva!", "partnerexpo-core"), $data['editLink'] ?? __("Szerkesztési link nem elérhető!", "partnerexpo-core"));
|
|
|
|
# # # # # # # # #
|
|
|
|
if (empty($data['email'])) {
|
|
Logger::log(__("Email nincs megadva, lekérdezés nem lehetséges!", "partnerexpo-core"), [$com_id_text => $comment_id, 'data' => $data]);
|
|
return;
|
|
}
|
|
|
|
$user_exists = $this->client->get('Contact', ['Email' => $data['email'], 'Deleted' => 0]);
|
|
if ( ! is_array($user_exists) ) {
|
|
Logger::log(__("Kontakt létezésének lekérése sikertelen!", "partnerexpo-core"), [$com_id_text => $comment_id, 'email' => $data['email'], $error_text => $this->client->getLastError()]);
|
|
return;
|
|
}
|
|
|
|
$user_id = 0;
|
|
$has_company = true;
|
|
$comp_id = 0;
|
|
|
|
if ($user_exists['Count'] == 0) {
|
|
$new_contact = $this->client->put('Contact', [
|
|
'FirstName' => $data['firstName'],
|
|
'LastName' => $data['lastName'],
|
|
'Email' => $data['email'],
|
|
'Phone' => $data['phone'],
|
|
'Type' => "Person"
|
|
]);
|
|
if ( ! is_array($new_contact) || ! isset($new_contact['Id']) ) {
|
|
Logger::log(__("Kontakt létrehozása sikertelen!", "partnerexpo-core"), [$com_id_text => $comment_id, 'email' => $data['email'], $error_text => $this->client->getLastError()]);
|
|
return;
|
|
}
|
|
$user_id = $new_contact['Id'];
|
|
$has_company = false;
|
|
} else {
|
|
$user_id = array_key_first($user_exists['Results']);
|
|
$existing_contact = $this->client->get('Contact/'.$user_id);
|
|
if ( ! is_array($existing_contact) || ! isset($existing_contact['Id']) ) {
|
|
Logger::log(__("Kontakt adatok lekérése sikertelen!", "partnerexpo-core"), [$com_id_text => $comment_id, 'contact_id' => $user_id, $error_text => $this->client->getLastError()]);
|
|
return;
|
|
}
|
|
|
|
if (!isset($existing_contact['BusinessId']) || empty($existing_contact['BusinessId'])) {
|
|
$has_company = false;
|
|
} else {
|
|
$comp_id = $existing_contact['BusinessId'];
|
|
}
|
|
}
|
|
|
|
if ( ! $user_id ) {
|
|
Logger::log(__("Kontakt azonosító hiányzik lekérés/létrehozás után is!", "partnerexpo-core"), [$com_id_text => $comment_id, 'email' => $data['email']]);
|
|
return;
|
|
}
|
|
|
|
if ( ! $has_company ) {
|
|
$comp_exists = $this->client->get('Contact', ['Name' => $data['company'], 'Type' => 'Business', 'Deleted' => 0]);
|
|
if ( ! is_array($comp_exists.,.) ) {
|
|
Logger::log(__("Kontakt létezésének lekérése sikertelen!", "partnerexpo-core"), [$com_id_text => $comment_id, 'email' => $data['email'], $error_text => $this->client->getLastError()]);
|
|
return;
|
|
}
|
|
|
|
if ($comp_exists['Count'] == 0) {
|
|
$new_comp = $this->client->put('Contact', [
|
|
'Name' => $data['company'],
|
|
'ContactId' => $user_id,
|
|
'Type' => "Business"
|
|
]);
|
|
if ( ! is_array($new_comp) || ! isset($new_comp['Id']) ) {
|
|
Logger::log(__("Cég létrehozása sikertelen!", "partnerexpo-core"), [$com_id_text => $comment_id, 'email' => $data['email'], $error_text => $this->client->getLastError()]);
|
|
return;
|
|
}
|
|
$comp_id = $new_comp['Id'];
|
|
} else {
|
|
$comp_id = array_key_first($comp_exists['Results']);
|
|
}
|
|
|
|
$updated_contact = $this->client->put('Contact/'.$user_id, [
|
|
'BusinessId' => $comp_id
|
|
]);
|
|
}
|
|
|
|
if ( ! $comp_id ) {
|
|
Logger::log(__("Cég azonosító hiányzik lekérés/létrehozás után is!", "partnerexpo-core"), [$com_id_text => $comment_id, 'email' => $data['email']]);
|
|
return;
|
|
}
|
|
|
|
|
|
$card_exists = $this->client->get('Project', ['CategoryId' => $category_id, 'ContactId' => $user_id, 'Deleted' => 0]);
|
|
if ( ! is_array($card_exists) ) {
|
|
Logger::log(__("Adatlap adatok lekérése sikertelen:", "partnerexpo-core"), [$com_id_text => $comment_id, 'contact_id' => $user_id, $error_text => $this->client->getLastError()]);
|
|
return;
|
|
}
|
|
|
|
$card_id = 0;
|
|
|
|
|
|
if ($card_exists['Count'] == 0) {
|
|
$payload = [
|
|
'CategoryId' => $category_id,
|
|
'ContactId' => $user_id,
|
|
'Name' => $data['lastName']. " " .$data['firstName'],
|
|
'UserId' => $crm_user_id,
|
|
'GdprNyilatkozat4' => ($data['gdpr'] === 'on') ? 1 : '',
|
|
'ModeratorRendszerepitoHirlevel2' => ($data['newsletter'] === 'on') ? 1 : ''
|
|
];
|
|
|
|
$new_card = $this->client->put('Project', $payload);
|
|
if ( ! is_array($new_card) || ! array_key_exists('Id', $new_card) ) {
|
|
Logger::log(__("Adatlap létrehozás sikertelen:", "partnerexpo-core"), [$com_id_text => $comment_id, 'contact_id' => $user_id, $error_text => $this->client->getLastError()]);
|
|
return;
|
|
}
|
|
$card_id = $new_card['Id'];
|
|
|
|
$result = $this->client->put('ToDo', [
|
|
'UserId' => $crm_user_id,
|
|
'ProjectId' => $card_id,
|
|
'Comment' => $message,
|
|
'Deadline' => $deadline,
|
|
]);
|
|
|
|
if ( ! is_array($result) ) {
|
|
Logger::log(__("Teendő létrehozása sikertelen létező adatlap esetén!", "partnerexpo-core"), [$com_id_text => $comment_id, 'project_id' => $card_id, $error_text => $this->client->getLastError()]);
|
|
}
|
|
} else {
|
|
$card_id = array_key_first($card_exists['Results']);
|
|
|
|
$result = $this->client->put('ToDo', [
|
|
'UserId' => $crm_user_id,
|
|
'ProjectId' => $card_id,
|
|
'Comment' => $message,
|
|
'Deadline' => $deadline,
|
|
]);
|
|
|
|
if ( ! is_array($result) ) {
|
|
Logger::log(__("Teendő létrehozása sikertelen létező adatlap esetén!", "partnerexpo-core"), [$com_id_text => $comment_id, 'project_id' => $card_id, $error_text => $this->client->getLastError()]);
|
|
}
|
|
}
|
|
|
|
} catch ( Exception $e ) {
|
|
Logger::log(__("Ismeretlen hiba történt a szinkronizálás során!", "partnerexpo-core"), [$com_id_text => $comment_id, 'exception' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|