PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / V3.0.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder vV3.0.2
3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 All 138 releases
bit-form / includes / Core / Integration / Telegram / RecordApiHelper.php

RecordApiHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder V3.0.2, at includes/Core/Integration/Telegram/RecordApiHelper.php

119 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Telegram Record Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\Telegram;
9
10 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
11 use BitCode\BitForm\Core\Util\FieldValueHandler;
12 use BitCode\BitForm\Core\Util\HttpHelper;
13
14 /**
15 * Provide functionality for Record insert,upsert
16 */
17 class RecordApiHelper
18 {
19 private $_defaultHeader;
20 private $_integrationID;
21 private $_logID;
22 private $_logResponse;
23 private $_entryID;
24 private $_apiEndPoint;
25
26 public function __construct($apiEndPoint, $integId, $logID, $entryID)
27 {
28 // wp_send_json_success($tokenDetails);
29 $this->_defaultHeader['Content-Type'] = 'multipart/form-data';
30 $this->_integrationID = $integId;
31 $this->_logID = $logID;
32 $this->_logResponse = new UtilApiResponse();
33 $this->_entryID = $entryID;
34 $this->_apiEndPoint = $apiEndPoint;
35 }
36
37 public function sendMessages($data)
38 {
39 $insertRecordEndpoint = $this->_apiEndPoint . '/sendMessage';
40 return HttpHelper::get($insertRecordEndpoint, $data, $this->_defaultHeader);
41 }
42
43 public function executeRecordApi($integrationDetails, $fieldValues, $formID, $entryID)
44 {
45 $msg = FieldValueHandler::replaceFieldWithValue($integrationDetails->body, $fieldValues);
46 $messagesBody = str_replace(['<p>', '</p>'], ' ', $msg);
47
48 if (!empty($integrationDetails->actions->attachments)) {
49 $attachment = $integrationDetails->actions->attachments;
50 $file = [];
51 foreach ($fieldValues as $fieldKey => $fieldValue) {
52 if (in_array($fieldKey, $attachment)) {
53 if (is_array($fieldValue)) {
54 $file = array_merge($file, $fieldValue);
55 } else {
56 $file[] = $fieldValue;
57 }
58 }
59 }
60
61 if (!empty($file)) {
62 if (is_array($file) && count($file) > 1) {
63 $data = [
64 'chat_id' => $integrationDetails->chat_id,
65 'caption' => $messagesBody,
66 'media' => $file
67 ];
68
69 $sendPhotoApiHelper = new FilesApiHelper($formID, $entryID);
70 $recordApiResponse = $sendPhotoApiHelper->uploadMultipleFiles($this->_apiEndPoint, $data);
71 } else {
72 $data = [
73 'chat_id' => $integrationDetails->chat_id,
74 'caption' => $messagesBody,
75 'parse_mode' => $integrationDetails->parse_mode,
76 'photo' => is_array($file) ? $file[0] : $file
77 ];
78
79 $sendPhotoApiHelper = new FilesApiHelper($formID, $entryID);
80 $recordApiResponse = $sendPhotoApiHelper->uploadFiles($this->_apiEndPoint, $data);
81 }
82 } else {
83 $data = [
84 'chat_id' => $integrationDetails->chat_id,
85 'text' => $messagesBody,
86 'parse_mode' => $integrationDetails->parse_mode
87 ];
88 $recordApiResponse = $this->sendMessages($data);
89 }
90
91 $type = 'insert';
92 } else {
93 $data = [
94 'chat_id' => $integrationDetails->chat_id,
95 'text' => $messagesBody,
96 'parse_mode' => $integrationDetails->parse_mode
97 ];
98 $recordApiResponse = $this->sendMessages($data);
99 $type = 'insert';
100 }
101 if (is_string($recordApiResponse)) {
102 $recordApiResponse = json_decode($recordApiResponse);
103 }
104
105 $entryDetails = [
106 'formId' => $formID,
107 'entryId' => $entryID,
108 'fieldValues' => $fieldValues
109 ];
110
111 if ($recordApiResponse && $recordApiResponse->ok) {
112 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'success', $recordApiResponse, $entryDetails);
113 } else {
114 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'error', $recordApiResponse, $entryDetails);
115 }
116 return $recordApiResponse;
117 }
118 }
119