| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\MailNotifier; |
| 6 |
use BitCode\BitForm\Core\Database\IntegrationModel; |
| 7 |
use BitCode\BitForm\Core\Database\FormEntryLogModel; |
| 8 |
|
| 9 |
|
| 10 |
final class IntegrationHandler |
| 11 |
{ |
| 12 |
private static $_formID; |
| 13 |
private static $_integrationModel; |
| 14 |
private $_user_details; |
| 15 |
|
| 16 |
/** |
| 17 |
* Constructor of Integration Handler |
| 18 |
* |
| 19 |
* @param Integer $formID If Integration is accessible globally then |
| 20 |
* $formID will be 0 and category app |
| 21 |
* @param Array $user_details Details of user accessing data |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function __construct($formID, $user_details = null) |
| 25 |
{ |
| 26 |
static::$_formID = $formID; |
| 27 |
static::$_integrationModel = new IntegrationModel(); |
| 28 |
$this->_user_details = $user_details; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
public function getAIntegration($integrationID, $integrationCategory = null, $integrationType = null) |
| 33 |
{ |
| 34 |
$conditions = array( |
| 35 |
'form_id' => static::$_formID, |
| 36 |
'id' => $integrationID, |
| 37 |
); |
| 38 |
if (!is_null($integrationType)) { |
| 39 |
$conditions = array_merge($conditions, ['integration_type' => $integrationType]); |
| 40 |
} |
| 41 |
if (!is_null($integrationCategory)) { |
| 42 |
$conditions = array_merge($conditions, ['category' => $integrationCategory]); |
| 43 |
} |
| 44 |
return static::$_integrationModel->get( |
| 45 |
array( |
| 46 |
'id', |
| 47 |
'integration_name', |
| 48 |
'integration_type', |
| 49 |
'integration_details', |
| 50 |
'form_id' |
| 51 |
), |
| 52 |
$conditions |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
public function getAllIntegration($integrationCategory = null, $integrationType = null, $status = null) |
| 57 |
{ |
| 58 |
$conditions = array( |
| 59 |
'form_id' => static::$_formID |
| 60 |
); |
| 61 |
if (!is_null($integrationType)) { |
| 62 |
$conditions = array_merge($conditions, ['integration_type' => $integrationType]); |
| 63 |
} |
| 64 |
|
| 65 |
if (!is_null($integrationCategory)) { |
| 66 |
$conditions = array_merge($conditions, ['category' => $integrationCategory]); |
| 67 |
} |
| 68 |
if (!is_null($status)) { |
| 69 |
$conditions = array_merge($conditions, ['status' => 1]); |
| 70 |
} |
| 71 |
return static::$_integrationModel->get( |
| 72 |
array( |
| 73 |
'id', |
| 74 |
'form_id', |
| 75 |
'integration_name', |
| 76 |
'integration_type', |
| 77 |
'integration_details', |
| 78 |
'status' |
| 79 |
), |
| 80 |
$conditions |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
public function singleIntegration($integrationCategory = null, $integrationType = null, $status = null){ |
| 85 |
|
| 86 |
$conditions=''; |
| 87 |
if (!is_null($integrationType)) { |
| 88 |
$conditions .= "WHERE `integration_type`='$integrationType'"; |
| 89 |
} |
| 90 |
|
| 91 |
if (!is_null($integrationCategory)) { |
| 92 |
$conditions .= "AND `category`='$integrationCategory'"; |
| 93 |
} |
| 94 |
if (!is_null($status)) { |
| 95 |
$conditions .= "AND status=1"; |
| 96 |
} |
| 97 |
return static::$_integrationModel->single_row( |
| 98 |
$conditions, |
| 99 |
array('id') |
| 100 |
); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
public function saveIntegration($integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) |
| 105 |
{ |
| 106 |
if ($status == null) { |
| 107 |
$status = 1; |
| 108 |
} |
| 109 |
return static::$_integrationModel->insert( |
| 110 |
array( |
| 111 |
"integration_name" => $integrationName, |
| 112 |
"integration_type" => $integrationType, |
| 113 |
"integration_details" => $integrationDetails, |
| 114 |
'category' => $integrationCategory, |
| 115 |
'form_id' => static::$_formID, |
| 116 |
"user_id" => $this->_user_details['id'], |
| 117 |
"user_ip" => $this->_user_details['ip'], |
| 118 |
"status" => $status, |
| 119 |
"created_at" => $this->_user_details['time'], |
| 120 |
"updated_at" => $this->_user_details['time'] |
| 121 |
) |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
public function updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) |
| 126 |
{ |
| 127 |
if ($status === null) { |
| 128 |
$status = 1; |
| 129 |
} |
| 130 |
return static::$_integrationModel->update( |
| 131 |
array( |
| 132 |
"integration_name" => $integrationName, |
| 133 |
"integration_type" => $integrationType, |
| 134 |
"integration_details" => $integrationDetails, |
| 135 |
'category' => $integrationCategory, |
| 136 |
'form_id' => static::$_formID, |
| 137 |
"user_id" => $this->_user_details['id'], |
| 138 |
"user_ip" => $this->_user_details['ip'], |
| 139 |
"status" => $status, |
| 140 |
"updated_at" => $this->_user_details['time'] |
| 141 |
), |
| 142 |
array( |
| 143 |
"id" => $integrationID |
| 144 |
) |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
public function duplicateAllInAForm($oldFormId) |
| 149 |
{ |
| 150 |
$integCols = ["integration_name", "integration_type", "integration_details", 'category', 'form_id', "user_id", "user_ip", "status", "created_at", "updated_at"]; |
| 151 |
$integDupData = array( |
| 152 |
"integration_name", |
| 153 |
"integration_type", |
| 154 |
"integration_details", |
| 155 |
'category', |
| 156 |
static::$_formID, |
| 157 |
$this->_user_details['id'], |
| 158 |
$this->_user_details['ip'], |
| 159 |
"status", |
| 160 |
$this->_user_details['time'], |
| 161 |
$this->_user_details['time'] |
| 162 |
); |
| 163 |
return static::$_integrationModel->duplicate($integCols, $integDupData, ['form_id' => $oldFormId]); |
| 164 |
} |
| 165 |
|
| 166 |
public function deleteIntegration($integrationID) |
| 167 |
{ |
| 168 |
return static::$_integrationModel->delete( |
| 169 |
array( |
| 170 |
'id' => $integrationID, |
| 171 |
'form_id' => static::$_formID, |
| 172 |
) |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
public static function replaceFieldWithValue($dataToReplaceField, $fieldValues) |
| 177 |
{ |
| 178 |
if (empty($dataToReplaceField)) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
if (is_string($dataToReplaceField)) { |
| 182 |
$dataToReplaceField = static::replaceFieldWithValueHelper($dataToReplaceField, $fieldValues); |
| 183 |
} elseif (is_array($dataToReplaceField)) { |
| 184 |
foreach ($dataToReplaceField as $field => $value) { |
| 185 |
if (is_array($value) && count($value) === 1) { |
| 186 |
$dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value[0], $fieldValues); |
| 187 |
} elseif (is_array($value)) { |
| 188 |
$dataToReplaceField[$field] = static::replaceFieldWithValue($value, $fieldValues); |
| 189 |
} else { |
| 190 |
$dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value, $fieldValues); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
return $dataToReplaceField; |
| 195 |
} |
| 196 |
|
| 197 |
private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues) |
| 198 |
{ |
| 199 |
if (empty($stringToReplaceField)) { |
| 200 |
return $stringToReplaceField; |
| 201 |
} |
| 202 |
$fieldPattern = '/\${\w[^ ${}]*}/'; |
| 203 |
preg_match_all($fieldPattern, $stringToReplaceField, $matchedField); |
| 204 |
$uniqueFieldsInStr = array_unique($matchedField[0]); |
| 205 |
foreach ($uniqueFieldsInStr as $key => $value) { |
| 206 |
$fieldName = substr($value, 2, strlen($value) - 3); |
| 207 |
if (!empty($fieldValues[$fieldName])) { |
| 208 |
$stringToReplaceField = is_string($fieldValues[$fieldName]) ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField) : |
| 209 |
wp_json_encode($fieldValues[$fieldName]); |
| 210 |
} |
| 211 |
} |
| 212 |
return $stringToReplaceField; |
| 213 |
} |
| 214 |
|
| 215 |
public static function maybeSetCronForIntegration($workFlowReturnedData, $opType) |
| 216 |
{ |
| 217 |
$responseData = []; |
| 218 |
if (isset($workFlowReturnedData['message'])) { |
| 219 |
$responseData['message'] = $workFlowReturnedData['message']; |
| 220 |
} |
| 221 |
if (isset($workFlowReturnedData['redirectPage'])) { |
| 222 |
$responseData['redirectPage'] = $workFlowReturnedData['redirectPage']; |
| 223 |
} |
| 224 |
if ($opType === 'update' && isset($workFlowReturnedData['updatedData'])) { |
| 225 |
$responseData['updatedData'] = $workFlowReturnedData['updatedData']; |
| 226 |
} |
| 227 |
if (!isset($workFlowReturnedData['triggerData'])) { |
| 228 |
return $responseData; |
| 229 |
} |
| 230 |
$triggerData = $workFlowReturnedData['triggerData']; |
| 231 |
if (function_exists('fastcgi_finish_request') || !wp_doing_ajax()) { |
| 232 |
if (!headers_sent()) { |
| 233 |
header("Connection: close"); |
| 234 |
$contentType = wp_doing_ajax() || defined('REST_REQUEST') ? "application/json" : "text/html"; |
| 235 |
header('Content-Type: '. $contentType .'; charset=' . get_option('blog_charset')); |
| 236 |
status_header(200); |
| 237 |
$response = array( |
| 238 |
'success' => true, |
| 239 |
'data' => $responseData, |
| 240 |
); |
| 241 |
} |
| 242 |
ob_start(); |
| 243 |
echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : $workFlowReturnedData['message']; |
| 244 |
ob_end_flush(); |
| 245 |
flush(); |
| 246 |
session_write_close(); |
| 247 |
if (function_exists('fastcgi_finish_request')) { |
| 248 |
fastcgi_finish_request(); |
| 249 |
} |
| 250 |
|
| 251 |
if (isset($triggerData['mail'])) { |
| 252 |
foreach ($triggerData['mail'] as $value) { |
| 253 |
MailNotifier::notify($value, $triggerData['formID'], $workFlowReturnedData['fields'], $triggerData['entryID']); |
| 254 |
} |
| 255 |
} |
| 256 |
do_action("bitforms_exec_integrations", $triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']); |
| 257 |
if (defined('REST_REQUEST') || wp_doing_ajax()) { |
| 258 |
die; |
| 259 |
} |
| 260 |
return $workFlowReturnedData; |
| 261 |
} |
| 262 |
|
| 263 |
$entryID = $triggerData['entryID']; |
| 264 |
if (isset($workFlowReturnedData['cron']) && $workFlowReturnedData['cron'] && !isset($triggerData['mail'])) { |
| 265 |
$eventScheuled = wp_schedule_single_event(time(), "bitforms_exec_integrations", array($triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID'])); |
| 266 |
$scheduleTime = wp_next_scheduled("bitforms_exec_integrations", array($triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID'])); |
| 267 |
$responseData['cron'] = get_site_url(null, "/wp-cron.php?doing_wp_cron&{$scheduleTime}"); |
| 268 |
} else if (!empty($triggerData['integrations']) || !empty($triggerData['mail'])) { |
| 269 |
$triggerData['fields'] = $workFlowReturnedData['fields']; |
| 270 |
set_transient("bitform_trigger_transient_{$entryID}", $triggerData, HOUR_IN_SECONDS); |
| 271 |
$entryLog = new FormEntryLogModel(); |
| 272 |
$queueuEntry = $entryLog->log_history_insert( |
| 273 |
array( |
| 274 |
"log_id" => $triggerData['logID'], |
| 275 |
"integration_id" => 0, |
| 276 |
"api_type" => wp_json_encode(["type" => "trigger", "type_name" => "Workflow", "on" => $opType]), |
| 277 |
"response_type" => "queued", |
| 278 |
"response_obj" => json_encode(['status' => 'queued']), |
| 279 |
"created_at" => current_time("mysql"), |
| 280 |
) |
| 281 |
); |
| 282 |
$responseData['cronNotOk'] = [ |
| 283 |
$triggerData['entryID'], |
| 284 |
$triggerData['logID'], |
| 285 |
$queueuEntry |
| 286 |
]; |
| 287 |
} |
| 288 |
|
| 289 |
return $responseData; |
| 290 |
} |
| 291 |
} |
| 292 |
|